1 --新建数据库
2 create database Studentdb
3 go
4
5 --使用数据库
6 use Studentdb
7 go
8
9 --新建表
10 create table Username
11 (
12 StudentNo int identity(1,1) not null,--学号并设置标识列
13 LoginPwd nvarchar(20) not null,--密码
14 StudentName nvarchar(20) not null,--姓名
15 Sex char(2) not null,--性别
16 GradeId int not null,--年级
17 Phone nvarchar(20)not null,--电话
18 Address nvarchar(40) not null,--地址
19 BornDate datetime,--生日
20 Email nvarchar(20) not null,--邮件
21 )
22
23 --创建一个主键约束
24 alter table Username
25 add constraint Keyname--主键的别名
26 primary key(StudentNo)
27
28 --通过select into语句将现有表中的数据添加到新表中
29 select identity(int,1,1) as StudentNo
30 into NewTable
31 from Username
32
33 --通过UNION关键字合并数据进行插入
34 insert Username(LoginPwd,StudentName,Sex,GradeId,Phone,Address,BornDate,Email)
35 select '123','秦浩天','男','S2','13518159261','北京抚琴南路',1992-05-12,'qht@163.com' UNION
36 select '51842','王冰','女','Y2','15883958283','上海交大西门',1990-10-05,'wangbin@xina.com'
37
38 --使用UPDATE更新数据
39 update Username
40 set StudentName='秦浩然'
41 where StudentName='秦浩天'
42
43 update Score
44 set Score=Score+5
45 where Score <= 95
46
47 --使用delete删除数据
48 delete from Username
49 where StudentName='秦浩然'
50
51 --使用truncate table删除数据
52 truncate table Username
53
54 --查询部分行或列
55 select LoginPwd,StudentName,Address
56 from Username
57 where StudentName='王冰'--反之:where StudentName<>'王冰'
58
59 --查询中使用别名
60 select StudentName as 学生姓名,Address as 地址,Phone as 电话号码
61 from Username
62 where StudentName <> '王冰'
63 --查询结果合并
64 select StudentName+'.'+LoginPwd as 姓名.密码
65 from Username
66
67 select 姓名.密码 = StudentName+'.'+LoginPwd
68 from Username
69
70 --查询空值
71 select StudnetName
72 from Username
73 where LoginPwd is null
74
75 --用常量指定学校
76 select 姓名=StudentName,地址=Address,'北京大学' as 学校名称
77 from Username
78
79 --显示前3条数据
80 select top 3 *
81 from Username
82
83 --按数据量的百分比显示此处显示20%
84 select top 20 percent StudnetName,Address
85 from Username
86
87 --使用order by排序,默认为asc
88 select *
89 from Score
90 order by Score,desc
91 --年龄如果为空姓名按降序排序
92 select *
93 from Student
94 where Age is null
95 order by StudentName desc
96
97 --用来寻找一个指定字符串在另一个字符串中的起始位置
98 select charindex('name','My name is hqs',1)--返回int值:4
99
100 --返回字符串长度
101 select len('不是穿上情侣装就可以装情侣')--返回int值:26
102
103 --字母转换为大写
104 select upper('my name is tom')--返回大写值:MY NAME IS TOM
105
106 --清除字符左边的空格
107 select ltrim(' I love you ')--返回:I lovey you
108
109 --清除字符右边的空格
110 select rtrim(' me too ')--返回: me too
111
112 --从字符串右边返回指定数目的字符
113 select right('北京.上海',2)--返回:上海
114
115 --替换一个字符串
116 select replace('爱上你等于爱上了错''上','下')--返回:爱下你等于爱下了错
117
118 --删除指定长度,并插入新的字符串
119 select stuff('123456',2,4,'中华人民')--返回:1中华人民6
120
121 --获取系统当前时间
122 select getdate()--返回系统当前时间:2009-05-11 12:00:00.000
123
124 --将指定的数值添加到指定的日期部分后的日期
125 select dateadd(mm,4,'01/05/1992')--返回:05/05/1992 注意这里的:4和日期中的天数
126
127 --两个日期之间的间隔
128 select datediff(mm,'01/05/1992','05/05/1992')--返回:4
129
130 --指定字符串形式
131 select datename(dw,'01/01/2000')--返回:当前是星期几
132 --年:yy 季度:qq 月份:mm 一年中第几天:dy 天:dd 周:wk 一周星期几:dw 小时:hh 分钟:mi 秒钟:ss 毫秒:ms
133
134 --指定日期中的整数
135 select datepart(day,'01/15/2000')--返回15
136
137 --返回0到1之间的随机float值
138 select rand()--返回:0.7952618415626
139
140 --取数值表达式的绝对值
141 select abs(-43)--返回:43
142
143 --向上取整数
144 select ceiling(43.5)--返回:44
145
146 --向下取整数
147 select floor(43.5)--返回:43
148
149 --取数值表达式的幂值
150 select power(5,2)--返回:25
151
152 --四舍五入为指定精度
153 select round(43.541,1)--返回:43.500
154
155 --对于正数返回+1,对于负数返回-1,对于0返回0
156 select sign(-12)--返回:-1
157
158 --取浮点表达式的平方根
159 select sqrt(9)--返回:3
160
161
162 --转变数据类型
163 select convert(varchar(5),'12345')--返回int型:12345
164
165 --返回当前用户的名字
166 select current_user--返回:你登录的用户名
167
168 --返回用于用于指定表达式的字节数
169 select datalength('我的世界我做主')--返回:7
170
171 --返回当前用户所登录的计算机名字
172 select host_name()--返回:你所登录的计算机的名字
173
174 --返回当前所登录的用户名称
175 select system_user--返回:你当前所登录的用户名
176
177 --给定用户的ID返回用户名
178 select user_name(1)--返回:从任意数据库中返回"dbo"
179
180 --使用like进行模糊查询
181 select * from Username where StudentName like '王%'
182
183 --between在某个范围内进行查询
184 select * from Username where BornDate not between '2010-01-01' and '2010-12-12'
185
186 --使用in在列举值内进行查询
187 select StudentName as 学生姓名
188 from Username
189 where Address in('北京','广州','上海')
190 order by Address
191
192 --sum()求和
193 select sum(Score) as 学号为23的学生总分
194 from Score
195 where StudentNo=23
196
197 --avg()求平均
198 select avg(Score) as 平均成绩
199 from Score
200 where Score>=60
201
202 --max()最大值 min()最小值
203 select avg(Score) as 平均成绩,max(Score) as 最高分,min(Score) as 最低分
204 from Score where Score >=60
205
206 --count() 集中计数
207 select count(*) as 及格人数
208 from Score
209 where Score>=60
210
211 --group by分组查询
212 select CourseId,avg(Score) as 课程平均成绩
213 from Score
214 group by CourseId
215
216 --查询男女学生的人数各是多少
217 select count(*) as 人数,SSex from Username
218 group by SSex
219
220 --查询每个年级的总人数
221 select count(*) as 年级人数,SGrade from Students
222 group by SGrade
223
224 --查询每个科目的平均分,并按照由低到高的顺序排列
225 select CourseId, avg(Score) as 课程平均成绩 from Score
226 group by CourseId
227 order by avg(Score) desc
228
229 --多列分组查询
230 select count(*) as 人数,SGrade as 年级,SSex as 性别 from Students
231 group by SGrade,SSex
232 order by SGrade
233
234 --查看一个班人数超过15个的年级
235 select count(*) as 人数,SGrade as 年级 from Students
236 group by SGrade
237 having count(*)>15
238
239 --查询平均分及格的课程信息
240 select CourseId as 课程编号,avg(Score) as 课程平均成绩
241 from Score
242 group by CourseId
243 having avg(Score)>=60
244
245 --查询每门课程及格总人数和及格学生的平均分
246 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score
247 where Score>=60
248 group by ScourseId
249
250 --查询每门课程及格总人数和及格平均分在80分以上的记录
251 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score
252 where Score>=60
253 group by CourseId
254 having avg(Score)>=80
255
256 --查询有多个员工工资不低于2000的部门编号
257 select 部门编号,count(*) from 员工信息表
258 where 工资>=2000
259 group by 部门编号
260 having count(*)>1
261
262 --在where子句中指定连接条件
263 select StudentName,CourseId,Score
264 from Students,Score
265 where Scode = StudentId
266
267 --在from 子句中使用inner join..on
268 select S.StudentName,C.CourseId,C.Score
269 from Students as S
270 inner join Score as C on (S.Scode = C.StudentId)
271
272 --左外连接查询
273 select S.SName,C.CourseId,C.Score
274 from Students as S
275 left outer join Score as C on S.Scode = C.StudentId
276
277 --右外连接查询
278 select Titles.Title_id,Titles.Title, Publishers.Pub_name
279 from titles
280 right outer join Publishers on Titles.Pub_id=Publishers.Pub_id
转载出至:http://blog.csdn.net/heqingsong1/article/details/7495496