SqlServer数据库基础sql

一.数据查询

  基础查询

select column1,column2 from table

  条件查询:

select column1,column2 from table where id = 1
select column1,column2 from table where id = 1 and name = '王五'
select column1,column2 from table where id = 1 or name = '王五'

  条件查询NOT的使用:

    注意事项: 

    1.NOT 的优先级通常低于 AND 和 OR,必要时使用括号明确逻辑关系

SELECT * FROM products
WHERE NOT (price > 100 AND stock < 10);

     2.在某些数据库中,可以使用 != 或 <> 代替 NOT =

SELECT * FROM users WHERE status != 'inactive';
-- 等价于
SELECT * FROM users WHERE NOT status = 'inactive';

NOT使用举例:

与where一起使用
select column1,column2 from table where not id = 1 //选择id不是1的数据

与in一起使用
select column1,column2 from table where id not in (1, 2, 3) // 选择id不是1,2,3的数据

与like一起使用
select column1,column2 from table where name not like '张%' //选择姓名不以张开头的数据

与between一起使用
select column1,column2 from table where dateTime not detween '2024-01-01' and '2024-12-30' //选择时间不在2024年的数据

与is null一起使用
select column1,cloumn2 from table where phone not is null //选择电话号码不为空的数据

与exists一起使用
  exists的作用: 
    存在性检查:EXISTS用于检查子查询是否返回至少一行记录
    布尔返回值:如果子查询返回至少一行,EXISTS返回TRUE;否则返回FALSE
    相关性:通常与主查询相关联(相关子查询)

//选择有订单的数据
select column1,column2 from table where exists(
         select 1 from order where oder.id = table.id
)

//选择没有订单的数据
select column1, column from table where not exists (
         select 1 from order where oder.id = table.id
)

   排序

select column1,column2 from table order by id asc //更加id排序,默认为 升序 asc
select column1,column2 from table order by id desc //desc 为降序,跟id降序

  聚合函数

1. count() 计数

-- 计算表中的总行数
SELECT COUNT(*) FROM employees;

-- 计算特定列的非NULL值数量
SELECT COUNT(department) FROM employees;

-- 计算不同值的数量 distinct关键字是去重
SELECT COUNT(DISTINCT department) FROM employees;

2.sum() 求和

-- 计算所有员工的工资总和
SELECT SUM(salary) FROM employees;

-- 计算每个部门的工资总和然后根据部门进行分组  group by 关键字是分组
SELECT department, SUM(salary) 
FROM employees 
GROUP BY department;

 3.avg()平均值

-- 计算所有员工的平均工资
SELECT AVG(salary) FROM employees;

-- 计算每个部门的平均工资然后根据部门进行分组
SELECT department, AVG(salary) 
FROM employees 
GROUP BY department;

4.max()最大值

-- 找出最高工资
SELECT MAX(salary) FROM employees;

-- 找出每个部门的最高工资然后根据部门进行分组
SELECT department, MAX(salary) 
FROM employees 
GROUP BY department;

5.min()最小值

-- 找出最低工资
SELECT MIN(salary) FROM employees;

-- 找出每个部门的最低工资然后根据部门进行分组
SELECT department, MIN(salary) 
FROM employees 
GROUP BY department;

  分组

select column1, avg(column2) from table group by column1

  连表查询

内链接 

  • 只返回两个表中匹配的行

select column1,column2 from table1 inner join table2 on table1.id = table2.id

左外连接

  • 返回左表所有行,右表无匹配则显示NULL

select column1,column2 from table1 left join table2 on table1.id = table2.id

右外连接

  • 返回右表所有行,左表无匹配则显示NULL

select column1,column2 from table1 right join table2 on table1.id = table2.id

全外连接

  • 返回两表所有行,无匹配则显示NULL

select column1,column2 from table1 full join table2 on table1.id = table2.id

交叉连接

  • 返回两表的笛卡尔积(所有可能的组合)

select column1, column from table1 cross join table2

 创建表

create table 表名 (
    列1 数据类型 约束,
    列2 数据类型 约束
)
create table 学生表(
    学生编号 int primary key identity(1,1),
    学生姓名 nvarchar(50) not null,
    学生年级 nchar(50),
    学生年龄 int check(学生年龄 > 0),
    入籍日期 date default getdate()
)

修改表结构

-- 添加列
ALTER TABLE 表名 ADD 列名 数据类型;

-- 修改列
ALTER TABLE 表名 ALTER COLUMN 列名 新数据类型;

-- 删除列
ALTER TABLE 表名 DROP COLUMN 列名;

-- 添加约束
ALTER TABLE 表名 ADD CONSTRAINT 约束名 约束定义;

-- 删除约束
ALTER TABLE 表名 DROP CONSTRAINT 约束名;

 

posted @ 2025-04-11 11:02  龙卷风吹毁停车场  阅读(25)  评论(0)    收藏  举报