别名,选择,查询,排序,去重,筛选
select * from UserInfo as ui --起别名 select UserName,UserPwd --指定选择的列 from UserInfo as ui select UserName as name --给列起别名 from UserInfo ui select top 2 * --查询前两行 from UserInfo select top 10 percent * --查询前百分之10 from UserInfo insert into UserInfo values(‘zsb1‘,‘1‘),(‘zsb2‘,‘3‘),(‘zsb1‘,‘4‘),(‘zsb2‘,‘7‘) select * from UserInfo order by UserName desc,UserPwd asc --指定多维排序:先按UserInfo降序排列,再按UserPwd升序排列 select UserName from UserInfo select distinct UserName from UserInfo--输出结果里消除重复行 select * from UserInfo where UserName=‘zsb1‘ --筛选出符合where后条件的元组 select * from UserInfo where UserName between ‘zsb1‘ and ‘zsb2‘ --筛选出UserName在‘zsb1‘到‘zsb2‘之间的元组
模糊查询,连接,多重连接,聚合函数,开窗函数
use master --drop database Class create database Class on primary( name=‘Class‘, filename=‘D:SQLTestClass.mdf‘, size=10mb, filegrowth=10mb ) log on( name=‘Class_log‘, filename=‘D:SQLTestClass_log.ldf‘, size=5mb, filegrowth=10% ) use Class--使用数据库 truncate table ClassInfo --创建班级信息表格 create table Classinfo( classId int identity(1,1) primary key not null,--后面三个参数分别是 自增(start=1,step=1),主键,非空 className nvarchar(10) not null --nvarchar 使用Unicode编码,长度可变字符串 ) --插入数据 insert into Classinfo(className) values(‘计科1‘),(‘计科2‘),(‘计科3‘),(‘计科4‘) select * from Classinfo --创建学生信息表格 create table StuInfo( stuId int identity(1,1) primary key not null, stuName nvarchar(10) not null, stuSexy bit default(0) not null,--默认值为0 stuBirthday date not null, stuPhone char(11), stuEmail varchar(20), classId int not null, foreign key (classId) references ClassInfo(ClassId) --classId作为外键,是从ClassInfo的ClassId引用过来的 ) --添加学生信息 insert into StuInfo values(‘zsben‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,1), (‘zsben2‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,2) select * from StuInfo insert into StuInfo values(‘zsben3‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,1), (‘zsben4‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,2) insert into StuInfo values(‘zsben4‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,1), (‘zsben5‘,1,‘1999-1-26‘,‘17150014294‘,‘[email protected]‘,2) --创建科目表 create table CourseInfo( cId int identity(1,1) primary key not null, cName nvarchar(20) not null ) --插入科目信息 insert into CourseInfo values(‘数据库‘),(‘算法设计‘),(‘计算机网络‘) select * from CourseInfo --drop table ScoreInfo --创建成绩信息表 create table ScoreInfo( scoreId int identity(1,1) primary key not null, score int not null, classId int not null, foreign key (classId) references Classinfo(ClassId), StuId int not null, foreign key (StuId) references StuInfo(StuId) ) select * from Classinfo select * from StuInfo select * from CourseInfo select * from ScoreInfo update Classinfo set className=‘数媒1‘ where className=‘计科3‘ delete Classinfo where className=‘计科4‘ insert into ScoreInfo values(100,1,2) select * from StuInfo where classId between 1 and 2 select * from StuInfo where classId in (1,2) --in 后面包含满足要筛选的值 select * from StuInfo where stuId between 3 and 6 and classId=1 --逻辑and关系 select * from StuInfo where stuName like ‘%3%‘ --模糊查询,名字中包含3 select * from StuInfo where stuName like ‘z%‘ --模糊查询,名字以z开头的 select * from StuInfo where stuName like ‘zsben_‘--模糊查询,名字以zsben开头且长度为6的所有元组 select * from StuInfo where stuPhone like ‘1[1-8]%‘ --查询电话号第一位是1,第二位在[1,8]的元组 select * from StuInfo where stuPhone like ‘1[^1-8]%‘--和上面相反 select * from StuInfo where stuEmail like ‘%@qq%‘ --查询使用qq邮箱的同学 update StuInfo set stuPhone=null where stuId=6 --修改电话信息 select * from StuInfo where stuPhone is not null --判断变量是不是null用这个方式 --内连接inner join:表两边完全匹配的元组进行连接 --查询学生姓名及所在班级 --先尝试打出整个元组 select * from StuInfo inner join Classinfo on StuInfo.classId=Classinfo.classId --用别名代替简化 select * from StuInfo as si inner join Classinfo as ci on si.classId=ci.classId --查询学生姓名及所在班级 select si.stuName,ci.className from StuInfo as si inner join Classinfo as ci on si.classId=ci.classId --求出计科1班的姓名与班级 select si.stuName,ci.className from StuInfo as si inner join Classinfo as ci on si.classId=ci.classId where className=‘计科1‘ select * from Classinfo select * from StuInfo select * from CourseInfo select * from ScoreInfo --左外连接left join:两表中完全匹配的数据 左边表匹配不上的数据 --打印出所有班级里的所有学生(不管这个班级有没有学生,没有输出null即可) select * from Classinfo as ci left join StuInfo as si on ci.classId=si.classId --右外连接同理 select * from Classinfo as ci right join StuInfo as si on ci.classId=si.classId --完全连接full join 左右两边不匹配的都要选择 select * from Classinfo as ci full join StuInfo as si on ci.classId=si.classId --连接多表 --选择分数, --ScoreInfo连接了StuInfo和ClassInfo,通过这个来连接即可 -- StuInfo.stuid=ScoreInfo.stuid and CourseInfo.cId=ScoreInfo.cid select si.stuName,sco.score,ci.cName from ScoreInfo sco inner join StuInfo as si on si.stuId=sco.StuId inner join CourseInfo as ci on ci.cId=sco.cId --再连接上每个学生的班级 select si.stuName,sco.score,ci.cName,class.className from ScoreInfo sco inner join StuInfo as si on si.stuId=sco.StuId inner join CourseInfo as ci on ci.cId=sco.cId inner join Classinfo as class on si.classId=class.classId --聚合函数 select COUNT(*) from StuInfo --COUNT()函数计数有多少元组 select COUNT(*) as cnt1 from StuInfo --给函数值起个别名 where classId=1 --只计数那些班级为1的元组 select COUNT(stuPhone) from StuInfo --只计数值不为null的元组 select * from ScoreInfo select MAX(score) from ScoreInfo --科目一最高分 where scoreId=1 select Min(score) from ScoreInfo --2号同学最低分 where StuId=2 --求‘数据库‘的平均分 select AVG(si.score) from CourseInfo as ci inner join ScoreInfo as si on si.cId=ci.cId --先连接起来 where ci.cName=‘数据库‘ --筛选出‘数据库‘元组 --开窗函数over():将统计信息分布到行中 select *,avg(score) over() --注意:聚合函数avg()把所有元组的score聚合成一行,over()这一行再分到每个元组里去 from ScoreInfo