Sql_DDL&DML

//------------------DDL----------------

--创建create

  create database mDB

  create table mTbl

--删除drop

  drop database mDB

  drop table mTbl      --(推荐逻辑删除)

--增加约束

  alter table mTbl

  add constraint Ck_name check(len(Name)>2)

//-----------------DML-----------------

--插入值

  insert into mTbl(Name,age) values('Joe',20)      --标示自动设置

  set identity_insert mTbl on

  insert into mTbl(id,Name,age) values(100,'Cheng',20)  --标示手动设置

  set identity_insert mTbl off

--修改

  update mTbl set Name='Joo',age=21 where(id=100)

--删除

  delete from mTbl where()

  truncate table mTbl        --清空表 执行效率比delete高 并且会将标示种子恢复

  1 --基本格式
  2 select * from TblClass
  3     
  4 --对于列限制
  5 
  6         --取指定列
  7         select cid,cname from Tblclass
  8         select cname from Tblclass
  9 
 10         --起别名 as
 11         select cid as 编号, cname 名称, 描述=cdescription from Tblclass
 12 
 13 --对于行限制
 14         insert into tblclass values('myClass001','jjoe')
 15         insert into tblclass values('myClass002','jjoe')
 16         insert into tblclass values('myClass003','jjoe')
 17         insert into tblclass values('myClass004','jooe')
 18     --top : 表示取前n行 与 前 n%
 19     select top 2 * from tblclass
 20     select top 50 percent * from tblclass
 21     
 22 select * from tblclass
 23 update tblclass set cdescription ='bi...sowshoddllll' where cid%2=0
 24 
 25     --distinct : 消除重复数据  不查主键列的时候 可以消除重复 比如男女
 26     select cdescription from tblclass
 27     select distinct cdescription from tblclass
 28     select distinct cdescription,cname from tblclass        --两列都重复才消除
 29 
 30 
 31 
 32 
 33 --where子句 也是对行进行筛选    做条件用的  设置大小范围
 34 
 35     select * from tblclass 
 36     where cid>=3        --比较运算符  >  <    >=    <=  != (能比较的类型)
 37 
 38     --查询班级编号在2-4之间的信息:
 39     select * from tblclass
 40     where cid>2 and cid<5        --逻辑运算符 and or not 
 41 
 42     --班级编号在1到4之间 并且 要求描述的字符串个数大于5
 43     select * from tblclass
 44     where cid>1 and cid <4 and len(cdescription)>5            --len()  用于访问字符个数
 45     
 46     --班级编号在1到4之间 或者 要求描述的字符串个数大于5
 47     select * from tblclass
 48     where (cid>1 and cid<4) or len(cdescription)>5                --运算符not的优先级最高
 49 
 50     --取范围,表示在一个连续的范围内 between ... and ... [5,8]  闭区间
 51     select * from tblclass
 52     where cid between 2 and 4    
 53     select * from tblclass 
 54     where (cid between 2 and 4)  and len(cdescription)>5
 55         
 56     --in关键字 表示一个不连续的范围
 57         --查询编号为1,3,5的班级
 58             select * from tblclass
 59             where cid=1 or cid=3 or cid=5
 60         select * from tblclass
 61         where cid in(1,3,5)
 62 
 63     select * from tblclass
 64 
 65     --模糊查询: like  _(任意一个字符)  %(任意多个字符)  [](连续区间) [^](表示非)
 66     select * from tblclass
 67     where cdescription like '%good%'    
 68 
 69         --查询描述包含dll的信息
 70         select * from tblclass
 71         where cdescription like '%dll%'
 72         --查询描述中以b开头并且是4个字符
 73         select * from tblclass
 74         where cdescription like 'j___'
 75 
 76         update tblclass set cdescription='Join us !' where cname='myclass002'
 77         --查询描述中包含!的班级名称            转义[!] [%]
 78         select cname from tblclass
 79         where cdescription like '%[!]%'
 80         
 81         select  * from tblclass
 82         insert into tblclass(cname,cdescription) values('myclass005','my description is : good!')
 83         
 84         
 85 set identity_insert tblclass on
 86 insert into tblclass(cid,cname) values(100,'myclass100')
 87 set identity_insert tblclass off
 88 
 89 
 90 
 91 insert into tblclass values('myclass102',null)
 92 --空值判断  is [not] null 
 93         select * from tblclass 
 94         where cdescription is null
 95 
 96 --函数判断 isnull() 判断值是否为空 如果为空 不显示null而给一个默认值
 97 select cid,cname,isnull(cdescription,'暂时没描述') from tblclass
 98 
 99 
100 --==============================================
101 --order by 子句排序子句
102 select * from tblclass
103 --order by cid asc  --按cid升序排序 asc
104 --order by cid desc --按cid降序排序    desc
105 
106 
107 --可以按照多列排序 order by cid desc,cname asc
108 order by cid desc,cname asc
109 
110 --==============================================
111 --分组子句group by ... having ...
112 --聚合函数
113 --聚合: 把多行合并成一行
114 use itcastcn
115 select  * from tblscore
116 --找英语的最高分
117 select max(tenglish) from tblscore
118 --找数学的最低分
119 select min(tmath) from tblscore
120 --找英语的平均分
121 select avg(tenglish) from tblscore
122 --求数学成绩的总和
123 select sum(tmath) from tblscore
124 --求参加考试的人数
125 select count(*) from tblstudent
126 select * from tblscore

//2015-12-07    分组   聚合 常用函数  格式化 字符串函数 日期函数  联合查询

  1 --分组  group by  统计各班人数  出现在分组选项的列 可以出现在查询结果中 其他的列不可以
  2 --和聚合函数一起出现在结果中
  3 select tsclassid,count(*) as 人数 from tblstudent
  4 group by tsclassid
  5 
  6 
  7 --做选择 having  和where的区别: having是完成统计之后  在统计结果之中进行筛选 where 在之前
  8     --查找出班级人数大于5的班级信息
  9 select tsclassid,count(*) as 人数 from tblstudent
 10 group by tsclassid having count(*)>5
 11 
 12 --select ... from  ... where ... group by ... having...
 13 
 14 -->>完整的select语句  select distinct top n[percent] * 
 15                     --from 表 where (条件) group by ... having ... order by ...
 16 
 17 
 18 select  * from tblstudent
 19 
 20 select tsclassid,count(*) as renshu 
 21 from tblstudent
 22 where tsGender=''
 23 group by tsclassid having tsclassid=5
 24 
 25 
 26 select distinct top 1 tsage,count(*) as 总人数
 27 from tblstudent 
 28 where tsgender=''
 29 group by tsage having tsage>15
 30 order by tsage asc
 31 
 32 --练习:查询所有女生信息
 33 select * from tblstudent where tsgender = ''
 34 --练习: 查询班级为3的男生信息
 35 select * from tblstudent where tsgender='' and tsclassid=3
 36 --练习:查询姓张的男学生
 37 select * from tblstudent where tsname like '张%' and tsgender=''
 38 --练习:找出各个城市的人数
 39 select tsaddress,count(*) as 人数
 40 from tblstudent
 41 group by tsaddress 
 42 order by 人数 desc
 43 --练习:找每个班中人数最多的城市名称
 44 
 45 
 46 
 47 
 48 
 49 
 50 --20151207
 51 --常用函数:
 52     select * from tblclass
 53     --cast(值 as 类型)
 54     update tblclass set cdescription = cdescription +cast(cid as char(2))
 55     --convert(目标类型,值[,format])    
 56     select convert(float,'12.3434')
 57 
 58     --格式化
 59     select convert(float,'12.3434',0)
 60     select convert(float,'123.23232123123',6)
 61     select convert(decimal(10,2),'123.23123123')
 62     select getdate()
 63     select convert(datetime,'2015-12-07',103)
 64     
 65     select convert(varchar(10),getdate())
 66     --2015-12-07 16:12:38.730
 67 
 68 
 69 --字符串函数
 70 select upper('asdf')
 71 select * from tblclass
 72 select len(cdescription) from tblclass
 73 
 74 select ltrim('   asd')
 75 select rtrim('asd    ')
 76 
 77 --字符串截取 left right substring
 78 --下标从1开始
 79 
 80 
 81 select substring('asdfasllsl',3,5)--从1开始数 第三个 5个字符
 82 
 83 
 84 --日期函数
 85 --增加日期
 86 select dateadd(year,5,getdate())
 87 select dateadd(month,5,getdate())
 88 --两个时间做差
 89 select datediff(day,getdate(),'2015-12-20')
 90 
 91 select * from tblemployee
 92 select datepart(dayofyear,empjoindate) from tblemployee
 93 
 94 select year(empjoindate) from tblemployee
 95 select datepart(hour,getdate())
 96 
 97 
 98 select getdate()
 99 
100 
101 
102 
103 --联合查询  union
104 select cid,cname from tblclass
105 union
106 select * from tbldepartment
107 
108 use itcastcn
109 
110 select 'Max:',max(tenglish) from tblscore
111 union
112 select 'Min:',min(tenglish) from tblscore
113 union
114 select 'Avg:',avg(tenglish) from tblscore
115 
116 
117 --合并成一行
118 select max(tenglish),min(tenglish),avg(tenglish) from tblscore
119 
120 
121 --练习:
122 select * from tblteacher
123 select ttname,ttsalary from tblteacher
124 union all
125 select '平均工资:',avg(ttsalary) from tblteacher
126 union    all
127 select '最高工资:',max(ttsalary) from tblteacher

//2015-12-08  连接查询  判断

  1 --连接查询   join on
  2 select * from tblclass
  3 select * from tblstudent
  4 --查询显示学生姓名以及对应班级名称     多表查询 
  5 
  6 select tsname as '姓名',cname as '班级名'
  7 from tblclass
  8 inner join
  9 tblstudent on  tblstudent.tsclassid=tblclass.cid    
 10 
 11 --查询学生姓名以及英语数学分数
 12 
 13 select tblstudent.tsname,tblscore.tenglish,tblscore.tmath 
 14 from tblstudent
 15 join 
 16 tblscore on tblstudent.tsid=tblscore.tsid
 17 
 18 --查询所有学生的姓名、年龄、所在班级
 19 
 20 select tsname,tsage,cname 
 21 from tblstudent
 22 join
 23 tblclass on tblstudent.tsclassid=tblclass.cid
 24 order by tsage asc
 25 
 26 --查询所有年龄超过20 的学生姓名、年龄、班级
 27 select tsname,tsage,cname 
 28 from tblstudent
 29 join
 30 tblclass on tblstudent.tsclassid=tblclass.cid
 31 where tblstudent.tsage>20
 32 order by tsage desc
 33 
 34 --查询学生姓名、年龄、班级及成绩
 35 select tblstudent.tsname,tblstudent.tsage,tblclass.cname,tblscore.tenglish,tblscore.tmath 
 36 from tblstudent
 37 join 
 38 tblclass on tblstudent.tsclassid=tblclass.cid
 39 join 
 40 tblscore on tblstudent.tsid=tblscore.tsid
 41 
 42 
 43 ----查询所有没有参加考试(在成绩表中不存在的学生)学生的姓名
 44 --select tsclassid,max(tsaddress) from tblstudent
 45 --group by tsclassid
 46 
 47 
 48 --练习:
 49 --1.查询所有英语及格的学生姓名、年龄以及成绩
 50 select tsname,tsage,tenglish 
 51 from tblstudent
 52 join
 53 tblscore on tblstudent.tsid=tblscore.tsid
 54 where tblscore.tenglish>60
 55 order by tenglish asc
 56 
 57 
 58 --2.查询所有没有参加考试的学生的姓名
 59 --学生编号不在tblscore表中出现not in()
 60 select tsname from tblstudent
 61 where tsid not in (select tsid from tblscore)
 62 
 63 
 64 --3.分组使用多个属性
 65 --查询每个班级的各个地区有多少人   先对班级分组  在对地区分组
 66 select tsclassid,tsaddress,count(*)
 67 from tblstudent
 68 group by tsclassid,tsaddress
 69 order by tsclassid
 70 
 71 --查询每个班级男女人数
 72 select tsclassid,tsgender,count(*) as 人数
 73 from tblstudent
 74 group by tsclassid ,tsgender    
 75 order by tsclassid
 76 
 77 --查询每个班级每个年龄人数    显示的只能是聚合的列的信息
 78 select tsclassid,tsage,count(*) as 人数
 79 from tblstudent
 80 group by tsclassid,tsage
 81 order by tsage
 82 
 83 
 84 --3.计算各员工的工龄
 85 select datediff(day,'2014-11-12',getdate())
 86 
 87 
 88 select * 
 89 from tblemployee
 90 where datediff(day,empjoindate,getdate())+1>365
 91 
 92 
 93 -------------------------------外连接---------------------------
 94 select * from  TblDepartment
 95 select * from Tblemployee
 96 
 97 select * from tbldepartment left join tblemployee on empdeptid=deptid
 98 select * from tblemployee left join tbldepartment on empdeptid=deptid
 99 
100 select * from tblemployee right join tbldepartment on empdeptid=deptid
101 select * from tbldepartment right join tblemployee on empdeptid=deptid
102 
103 
104 select * from tblemployee full join tbldepartment on empdeptid=deptid
105 
106 
107 --低于60分的提示不及格
108 select * from tblscore
109 select * from tblstudent
110 
111 
112 --判断不想等的情况
113 select tsname,
114 isnull(
115 case 
116 when tenglish<60
117 then '不及格'
118 else cast(tenglish as varchar(10))
119 end ,'quekao'
120 )
121 from tblstudent 
122 left join tblscore on tblscore.tsid=tblstudent.tsid
123 
124 
125 
126 --判断相等的情况
127 select * from tblstudent
128 
129 use mytestdatabase
130 create table TblStudent
131 (
132     Autoid  int identity(1,1) ,
133     StId int not null,
134     StName nvarchar(100),
135     StGender int 
136 )
137 
138 select * from TblStudent
139 
140 insert into TblStudent(stid,stname,stgender) values('00003','LJ',0)
141 
142 update tblstudent set stgender=1 where stid='00002'
143 
144 
145 --将性别0 1 显示为男女
146 select *,
147 case stgender when 0 then N'' when 1 then N'' end as 性别
148 from tblstudent
149 
150 
151 --显示成绩为 优良中差
152 use ItCastCn
153 select * from tblScore
154 
155 select *,
156 case 
157 when tenglish<=100 and tenglish >=90 then '' 
158 when tenglish<90 and tenglish>=70 then ''
159 when tenglish<70 and tenglish>=60 then ''
160 when tenglish<60 then ''
161 end 
162 from tblscore
163 
164 -- 财务流水表
165     --TblMoneyFlow
166 create Table TblMoneyFlow
167 (
168         fid int identity(1,1) primary key not null,
169         ftitle nvarchar(10),
170         fmoney money
171 )
172 
173 select * from tblmoneyflow
174 
175 insert into TblMoneyFlow values('发工资',5000)
176 insert into TblMoneyFlow values('奖金',1000)
177 insert into TblMoneyFlow values('兼职',400)
178 insert into TblMoneyFlow values('请客',200)
179 insert into TblMoneyFlow values('大保健',500)
180 
181 
182 select * ,case when fmoney>0 then fmoney end as '收入',
183 case when fmoney<0 then abs(fmoney) end as '支出'
184 from tblmoneyflow 
原文地址:https://www.cnblogs.com/siyi/p/5019099.html