1. 数据库与表操作 (DDL) 1.1 数据库操作 -- 创建数据库 create database hm0707; -- 删除数据库 drop database hm0707; 1.2 表操作 -- 创建表 -- create table 表名(字段名 数据类型,字段名 数据类型,...字段名不限) -- int整数 decimal小数 char字符 -- varchar字符串 datetime时间日历 create table student( idint , namevarchar ( 50 ) , salarydecimal ( 5 , 2 ) , -- 代表数字可以写5位 最多有2位是小数 lasttimedatetime ) ; -- 删除表 drop table student; -- 修改表名 exec sp_rename'student' , 'stu' ; -- 修改表中的字段名 exec sp_rename'student.lasttime' , 'time' , 'column' ; -- 删除表中的字段 alter table studentdrop column time ; -- 向表中添加字段信息(只能添加到最后) alter table studentadd time datetime ; 2. 数据增删改查 (DML) 2.1 插入数据 -- 全表添加 insert into studentvalues ( 1 , 'abc' , 23.45 , '2026-07-07 15:20:34' ) ; -- 指定字段添加 insert into student( name, salary) values ( 'aa' , 56 ) ; 2.2 查询数据 -- 全表查询 select * from student; -- 查询指定字段 select name, salaryfrom student; -- WHERE条件查询 select * from studentwhere id= 1 ; 2.3 更新数据 -- 修改语句 update studentset salary= 60 where id= 1 ; 2.4 删除数据 -- 删除语句 delete from studentwhere name= 'aa' ; 3. 高级查询与聚合 3.1 逻辑运算符 -- AND同时满足 select * from empwhere deptno= 10 and sal> 1000 ; -- OR满足其中之一 select ename, deptno, salfrom empwhere deptno= 10 or sal> 2000 ; -- 区间查询 select ename, salfrom empwhere sal> 1300 and sal< 3000 ; -- BETWEEN AND区间查询 select ename, sal, deptnofrom empwhere salbetween 1300 and 3000 and deptno= 10 ; 3.2 NULL值处理 -- 查询没有提成的员工 select ename, commfrom empwhere commis not null ; 3.3 IN操作符 -- 查询编号为7369,7499,7521 select ename, empnofrom empwhere empnoin ( 7369 , 7499 , 7521 ) ; 3.4 模糊查询 -- LIKE模糊查询 -- % 代表0个或者多个字符 _下划线代表一个字符 -- 查询名字中包含a的员工 select empno, enamefrom empwhere enamelike '%a%' ; -- 查询名字中第二个字符是A的员工 select empno, enamefrom empwhere enamelike '_a%' ; 3.5 别名与去重 -- 别名 select empnoas '员工编号' from empwhere empno= 7900 ; -- 去重 select distinct ( job) from emp; 3.6 排序 -- 排序(默认升序,降序加DESC) select empno, salfrom emporder by saldesc ; -- 多字段排序 select empno, sal, deptnofrom emporder by deptno, saldesc ; -- 条件排序 select empno, sal, deptnofrom empwhere deptno= 20 and sal> 2000 order by saldesc ; 4. 子查询与视图 4.1 子查询 -- 1. 查询拿最高工资的员工姓名 select ename, salfrom empwhere sal= ( select max ( sal) from emp) ; -- 2. 查询工资高于平均工资的员工 select ename, salfrom empwhere sal> ( select avg ( sal) from emp) ; -- 3. 查询工资高于20号部门最高工资的所有员工 select * from empwhere sal> ( select max ( sal) from empwhere deptno= 20 ) ; -- 4. 查询和jones相同职位的员工信息 select * from empwhere job= ( select jobfrom empwhere ename= 'jones' ) ; -- 5. 复杂子查询:求emp表中除了名字为king的人中年薪最高的员工 select * from ( select * , 12 * ( sal+ ISNULL( comm, 0 ) ) as nxfrom empwhere ename!= 'king' ) s1where s1. nx= ( select max ( s. nx) from ( select * , 12 * ( sal+ ISNULL( comm, 0 ) ) as nxfrom empwhere ename!= 'king' ) s) ; 4.2 视图 -- 创建视图 create view tas ( select * , 12 * ( sal+ ISNULL( comm, 0 ) ) as nxfrom empwhere ename!= 'king' ) ; -- 使用视图查询 select max ( nx) from t; select * from twhere nx= ( select max ( nx) from t) ; 5. 连接查询 5.1 内连接(等值连接) -- 查询所有的员工信息和对应的部门地点 select e. * , d. * from emp e, dept dwhere e. job= 'salesman' and e. deptno= d. deptno; 5.2 外连接 -- 右外连接 select e. * , d. * from emp eright join dept don e. deptno= d. deptno; 5.3 自连接 -- 一张表的连接 select e. ename, e. empno, e. mgr, d. ename, d. empnofrom emp e, emp dwhere e. mgr= d. empno; 6. 聚合函数与分组 6.1 聚合函数 -- 聚合函数:max最大、min最小、avg平均值、sum求和、count统计 -- 查询emp表中工资最高的工资 select max ( sal) as '最高工资' from empwhere job= 'salesman' ; -- 查询emp表中日期最小的值 select min ( hiredate) as '最小年龄' from emp; -- 查询emp表中的平均工资 select avg ( sal) as '平均工资' from emp; -- 查询emp表中工资的总和 select sum ( sal) as '工资的总和' from emp; -- 查询员工的数量 select count ( ename) as '员工的数量' from emp; 6.2 分组查询 -- GROUP BY分组(一般和聚合函数一起使用) -- 求每个部门的平均工资 select deptno, avg ( sal) as afrom empgroup by deptno; -- 求每个工作岗位的人数 select job, count ( job) as cfrom empgroup by job; -- 求每个部门工资大于1500的平均工资 select deptno, avg ( sal) as afrom empwhere sal> 1500 group by deptno; -- 求每个部门工资大于1500的平均工资的升序 select deptno, avg ( sal) as afrom empwhere sal> 1500 group by deptnoorder by aasc ; -- 多字段分组排序 select deptno, avg ( sal) as a, count ( sal) as cfrom empwhere sal> 1500 group by deptnoorder by cdesc , aasc ; 6.3 HAVING子句 -- 查询每个部门的平均工资,只查询工资大于1800的员工并且查询平均工资大于2000的 select deptno, avg ( sal) as afrom empwhere sal> 1800 group by deptnohaving avg ( sal) > 2000 order by a; -- 查询每个部门名字里包含a的员工的平均工资,只显示平均工资高于2000的 select deptno, avg ( sal) as afrom empwhere enamelike '%a%' group by deptnohaving avg ( sal) > 2000 ; -- 复杂分组查询 select deptno, avg ( sal) as a, count ( * ) as cfrom empgroup by deptnohaving avg ( sal) > 2000 order by a; 6.4 复杂分组示例 -- 查询emp表,名字不是以k开头的信息 -- 每个部门的员工最低工资高于1000的部门编号, -- 工资总和,平均工资以及最低工资 根据平均工资排序 select deptno, avg ( sal) as a, sum ( sal) as s, min ( sal) as mfrom empwhere enamenot like 'k%' group by deptnohaving min ( sal) > 1000 order by a; -- 查询部门编号是10,30的部门员工中每个职业中 -- 最高工资低于5000的职业名称,人数, -- 平均工资,最高工资,根据人数排序 -- 如果人数一致根据最高工资降序排序 select job, count ( * ) as c, avg ( sal) as a, max ( sal) as mfrom empwhere deptno= 10 or deptno= 30 group by jobhaving max ( sal) < 5000 order by c, mdesc ; -- 查询emp表中,工资在1000-3000的员工信息, -- 每个部门的编号,工资总和,平均工资, -- 过滤掉平均工资低于2000的部门,平均工资升序排序 -- 如果一致则按照工资总和排序 select deptno, sum ( sal) as s, avg ( sal) as afrom empwhere salbetween 1000 and 3000 group by deptnohaving avg ( sal) >= 2000 order by a, sdesc ; 7. 约束与索引 7.1 约束类型 -- 主键约束(包含非空和唯一) 非空 唯一 检查 外键 -- 主键自增 IDENTITY(i,j) i初始值 j步长 -- not null非空 identity(1,1)主键自增 primary key主键 -- unique 唯一 check检查约束 create table hh( idint identity ( 1 , 1 ) primary key , namevarchar ( 20 ) unique , sexint check ( sex= 0 or sex= 1 ) ) ; drop table hh; insert into hhvalues ( null , 1 ) ; select * from hh; 7.2 外键约束 -- 外键约束 insert into emp( deptno) values ( 50 ) ; select * from emp; -- 对deptno设置外键约束:只能是有的部门你才能添加 -- 主表:主表里面一定有一个主键 create table zhu( idint primary key ) ; -- constraint 约束名 foreign key (本表的字段) references 主表(字段) create table fu( idint , kidint , constraint yu1foreign key ( kid) references zhu( id) ) ; insert into zhuvalues ( 1 ) ; delete from zhuwhere id= 1 ; insert into fuvalues ( 1 , 1 ) ; drop table fu; 7.3 索引与查询优化 -- 索引:提高查询效率,一般是给字段设置索引 -- 二分查找优点:查询效率快 -- 缺点:占数据库内存 -- 如何优化查询速度:使用WHERE条件,不要全表扫描 8. 存储过程与事务 8.1 存储过程 -- 存储过程:类似方法 CREATE PROCEDURE AddMillionRowsAS BEGIN SET NOCOUNTON ; DECLARE @i INT = 0 ; WHILE @i < 10000 BEGIN INSERT INTO zhuVALUES ( @i ) SET @i = @i + 1 ; END END -- 执行存储过程 EXEC AddMillionRowsselect count ( * ) from zhu8.2 事务控制 -- TCL事务控制语言 commit ; -- 提交事务 rollback ; -- 回滚事务 9. 分页与日期函数 9.1 分页查询 -- 分页查询 select * from emporder by saloffset 0 rows fetch next 5 rows only; -- TOP简单分页(只能查询前面的几条数据) select top 2 * from emporder by saldesc ; 9.2 日期函数 -- 日期函数:YEAR、MONTH、DAY select month ( hiredate) , hiredatefrom emp; 10. Visual Studio 连接实战 10.1 连接SQL Server
10.2 创建SQL中的表
10.3 插入数据
10.4 查询数据
10.5 修改数据
10.6 删除数据