Sql学习笔记一
create database School --drop database School use School create table TblStudent ( --tsid int identity(1,1) not null primary key --identity 自动增长 primary key主键 tsid int identity(1,1) primary key, tsname nvarchar(10) not null, tsgender bit not null, tsaddress nvarchar(300) null, tsphone varchar(100), tsage int, tsbirthday datetime, tscardId varchar(18), tscalssId int not null ) create table TblCalss ( tsclassId int identity(1,1) primary key, tsclassName varchar(100) not null, tscalssDesc varchar(500) null ) select * from TblCalss insert into TblCalss(tsclassName,tscalssDesc) values('黑马8期','牛B中的战斗机') --向自动编号列插入数据 set identity_insert tblcalss on insert into TblCalss(tsclassId,tsclassName,tscalssDesc) values(101,'黑马9期','牛B中的战斗机') set identity_insert tblcalss off insert into TblCalss values('黑马5期','战斗机') --通过一条语句向表中插入多条SQL语句 insert into TblCalss(tsclassName,tscalssDesc) select '黑马2期','第2个黑马' union select '黑马3期','第3个黑马' union select '黑马4期','第4个黑马' create table TblCalssBak ( clsId int identity(1,1) primary key, clsName nvarchar(50) not null, clsDesc nvarchar(50) null ) select * from TblCalssBak --将TblClass表中的数据导入到TblCalssBak中 insert into TblCalssBak(clsName,clsDesc) select tsclassName,tscalssDesc from TblCalss --更新语句 -- update 表名 set 列名=值 ,列名2=值2 whre 列名 =>< 值 or and.... --<>不等于 select * from TblCalss update TblCalss set tsclassName='一班' where tsclassId=2 --删除语句 -- delete from 表名 where ..... delete from TblCalss where tsclassId=107 -- delete from TblCalss 表示把表中所有数据全部删除,不加where方式来删除相对于truncate来说速度慢很多。删除表中的数据后,自动编号依然保留当前已增长到的位置。 -- truncate table TblCalss 把表中数据清空,不能根据where,删除数据时速度很快,以最小的方式来记录日志。删除后,重新添加数据时自动编号会恢复到一开始的位置。 -- drop table TblCalss 表示删除表 --1.主键约束 --2.外键约束 --3.唯一约束(索引键) --4.默认约束(性别:男) --5.检查约束 --6.非空约束 select * from TblCalss select tsclassname from tblcalss select getdate(); select top 3 * from TblCalss --desc 降序 106 105 104 asc升序104 105 106 select top 3 * from TblCalss order by tsclassid desc select top 3 tsclassId,tsclassName from TblCalss order by tsclassid desc --针对查询出来的结果去除重复 distinct 列名 select distinct tsclassName from TblCalss select * from TblStudent insert into TblStudent values('张三',1,'上海市宝山区',13912345678,20,null,'0401',1) insert into TblStudent values('张三',1,'合肥市高新区',18912345678,23,null,'0401',1) insert into TblStudent values('张三',1,'上海市徐汇区',13012345678,24,null,'0402',102) --所有年龄加1 update TblStudent set tsage=tsage+1 --聚合函数 select avg(tsage*1.0) as 平均年龄 from TblStudent select avg(tsage*1.0) 平均年龄 from TblStudent select 平均年龄=avg(tsage*1.0) from TblStudent select sum(tsage) as 总年龄 from TblStudent -- max min count(*) --select count(列名) as 总条数 from TblStudent select count(*) as 总条数 from TblStudent -- 执行结果一行显示 子查询 select 总条数=(select count(*) as 总条数 from TblStudent), 总年龄=(select sum(tsage) as 总年龄 from TblStudent), 平均年龄=(select avg(tsage) as 总年龄 from TblStudent) select * from TblCalss select * from TblStudent -- between and in(列名中的值) or -------------类型转换------------------ create table MyTest ( autoId int identity(1,1) primary key, userName varchar(1000), number varchar(100) ) insert into mytest values('A','1') insert into mytest select 'B','2' union select 'C','3' union select 'D','4' union select 'E','5' union select 'F','6' union select 'G','7' union select 'H','8' union select 'I','9' union select 'J','10' union select 'K','11' union select 'L','12' union select 'M','13' select * from mytest --cast() convert() select * from mytest order by cast(number as int) desc select * from mytest order by convert(int,number) asc ------合并行叫做联合 union----------- select 'Joke',18,'joke@qq.com' union all --可以把多个结果集联合到一起,把行联在一起。如果列的数目不一样,会报错 select '许正龙',19,'xzl@qq.com' --union 与 union all的区别 --union 在联合的时候会自动去除重复数据,反之 union all create table TblTeacher ( ttId int identity(1,1) primary key, ttname varchar(50), ttgender varchar(2), ttage int ) insert into TblTeacher values('张老师','男',20) insert into TblTeacher values('陈老师','女',30) insert into TblTeacher values('黄老师','男',20) insert into TblTeacher values('夏老师','男',40) insert into TblTeacher values('何老师','男',32) insert into TblTeacher values('洪老师','女',26) select tsname,tsgender,tsage from TblStudent union all select ttname,ttgender,ttage from TblTeacher --select * into 表2 from 表1 whre .... --上面这种写法表示要将表1的数据插入到表2中,在运行完毕这条sql语句后,表2才会被创建,如果在执行SQL语句前表2已经存在了,则报错。即该条语句不能重复执行。 select * into newTeacher from TblTeacher select * from newTeacher select len('haha') declare @str varchar(100) set @str=' 我的左侧有空格!' select @str as 初始字符, len(@str) as 初始长度,ltrim(@str) as 现有字符,len(ltrim(@str)) as 现有长度 --------分组查询-且分组先执行group by--然后再执行select--- select * from TblStudent --按班级编号分组 select tscalssId as '班级编号','人数'=count(*) from TblStudent group by tscalssId --在查询中使用group by分组,在select中出现的列必须是 group by中包含的列或者该列必须聚合函数中 select '人数'=count(tscalssId) from TblStudent group by tscalssId --2请从学生表中查询出每个班级ID和班级中男同学的人数 --请统计学生表中每个班级的男同学人数 select 班级Id=tscalssId,男同学人数=count(*) from TblStudent where tsgender=1 group by tscalssId --这里的count()会对每一组数据时行聚合,统记条数 select tscalssId,count(tsname),tsage from TblStudent group by tscalssId --会报以下错误,因为tsage没有出现在聚合函数中 --消息 8120,级别 16,状态 1,第 1 行 --选择列表中的列 'TblStudent.tsage' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 select * from TblStudent select tscalssId,count(tsname) from TblStudent group by tscalssId --count统计的是条数,不是年龄总合 --sql语句执行的顺序 --1>..from表 --2>..where条件 --3>..group by 列 --4>..having 筛选条件 --5>..select 5-2>distinct,7>top(应用top选项最后计算)5-1>选择列 --6>..order by 列 --完整的顺序 --from --on --join --where --group by --width cube || width rollup --having --select --distinct --order by --top --请统计销售总价超过3000元的商品名称和销售总价,并按销售总价降序排序 --select --商品名称=商品名称, --销售总价=sum(销售数量*销售价格) --from myOrders --group by 商品称名 --havaing sum(销售数量*销售价格)>3000 --order by 销售总价 desc create table test9009 ( autoId int identity(1,1) primary key, score int ) insert into test9009 values(200) insert into test9009 values(500) insert into test9009 values(700) insert into test9009 values(1200) insert into test9009 values(2000) select * from test9009 select autoId, case when score<500 then '新兵蛋子' when score>=500 and score<1000 then '上等兵' when score>=1000 and score<1500 then '下士' else '上士' end as 称号 from test9009 -- 下面这种写法可以用区间判断,也可以用在等值判断。 select *, case when score=1000 then '上等兵' when score=2000 then '下士' when score=3000 then '中士' end from test9009 select *, case when score=1000 then '上等兵' when score=2000 then '下士' when score=3000 then '1' end from test9009 create table T8 ( A int, B int, C int ) insert into T8 values(10,20,30) insert into T8 values(20,12,6) insert into T8 values(100,20,300) insert into T8 values(11,120,3) select * from T8 select *, 列1= case when A>B then A else B end, 列2= case when B>C then B else C end from T8 ---子查询 select * from TblStudent select * from TblCalss select * from TblStudent where tscalssId in ---in可以返回多个值 =只能返回一个值 ( select tsclassId from TblCalss where tsclassName='黑马2期' or tsclassName='黑马9期' ) use Itcast2013 create procedure usp_add2 @num1 int, @num2 int as begin select @num1+@num2 end exec usp_add2 @num1=100,@num2=200 create proc usp_selectStuByClassId @classId int, @recordCount int output as begin select * from TblStudent where tSClassId=@classId select @recordCount=count(*) from TblStudent where tSClassId=@classId end declare @rc int exec usp_selectStuByClassId @classId=5,@recordCount=@rc output print @rc