--新建数据库 create database Studentdb go --使用数据库 use Studentdb go --新建表 create table Username ( StudentNo int identity(1,1) not null,--学号并设置标识列 LoginPwd nvarchar(20) not null,--密码 StudentName nvarchar(20) not null,--姓名 Sex char(2) not null,--性别 GradeId int not null,--年级 Phone nvarchar(20)not null,--电话 Address nvarchar(40) not null,--地址 BornDate datetime,--生日 Email nvarchar(20) not null,--邮件 ) --创建一个主键约束 alter table Username add constraint Keyname--主键的别名 primary key(StudentNo) --通过select into语句将现有表中的数据添加到新表中 select identity(int,1,1) as StudentNo into NewTable from Username --通过UNION关键字合并数据进行插入 insert Username(LoginPwd,StudentName,Sex,GradeId,Phone,Address,BornDate,Email) select '123','秦浩天','男','S2','13518159261','北京抚琴南路',1992-05-12,'[email protected]' UNION select '51842','王冰','女','Y2','15883958283','上海交大西门',1990-10-05,'[email protected]' --使用UPDATE更新数据 update Username set StudentName='秦浩然' where StudentName='秦浩天' update Score set Score=Score+5 where Score <= 95 --使用delete删除数据 delete from Username where StudentName='秦浩然' --使用truncate table删除数据 truncate table Username --查询部分行或列 select LoginPwd,StudentName,Address from Username where StudentName='王冰'--反之:where StudentName<>'王冰' --查询中使用别名 select StudentName as 学生姓名,Address as 地址,Phone as 电话号码 from Username where StudentName <> '王冰' --查询结果合并 select StudentName+'.'+LoginPwd as 姓名.密码 from Username select 姓名.密码 = StudentName+'.'+LoginPwd from Username --查询空值 select StudnetName from Username where LoginPwd is null --用常量指定学校 select 姓名=StudentName,地址=Address,'北京大学' as 学校名称 from Username --显示前3条数据 select top 3 * from Username --按数据量的百分比显示此处显示20% select top 20 percent StudnetName,Address from Username --使用order by排序,默认为asc select * from Score order by Score,desc --年龄如果为空姓名按降序排序 select * from Student where Age is null order by StudentName desc --用来寻找一个指定字符串在另一个字符串中的起始位置 select charindex('name','My name is hqs',1)--返回int值:4 --返回字符串长度 select len('不是穿上情侣装就可以装情侣')--返回int值:26 --字母转换为大写 select upper('my name is tom')--返回大写值:MY NAME IS TOM --清除字符左边的空格 select ltrim(' I love you ')--返回:I lovey you --清除字符右边的空格 select rtrim(' me too ')--返回: me too --从字符串右边返回指定数目的字符 select right('北京.上海',2)--返回:上海 --替换一个字符串 select replace('爱上你等于爱上了错''上','下')--返回:爱下你等于爱下了错 --删除指定长度,并插入新的字符串 select stuff('123456',2,4,'中华人民')--返回:1中华人民6 --获取系统当前时间 select getdate()--返回系统当前时间:2009-05-11 12:00:00.000 --将指定的数值添加到指定的日期部分后的日期 select dateadd(mm,4,'01/05/1992')--返回:05/05/1992 注意这里的:4和日期中的天数 --两个日期之间的间隔 select datediff(mm,'01/05/1992','05/05/1992')--返回:4 --指定字符串形式 select datename(dw,'01/01/2000')--返回:当前是星期几 --年:yy 季度:qq 月份:mm 一年中第几天:dy 天:dd 周:wk 一周星期几:dw 小时:hh 分钟:mi 秒钟:ss 毫秒:ms --指定日期中的整数 select datepart(day,'01/15/2000')--返回15 --返回0到1之间的随机float值 select rand()--返回:0.7952618415626 --取数值表达式的绝对值 select abs(-43)--返回:43 --向上取整数 select ceiling(43.5)--返回:44 --向下取整数 select floor(43.5)--返回:43 --取数值表达式的幂值 select power(5,2)--返回:25 --四舍五入为指定精度 select round(43.541,1)--返回:43.500 --对于正数返回+1,对于负数返回-1,对于0返回0 select sign(-12)--返回:-1 --取浮点表达式的平方根 select sqrt(9)--返回:3 --转变数据类型 select convert(varchar(5),'12345')--返回int型:12345 --返回当前用户的名字 select current_user--返回:你登录的用户名 --返回用于用于指定表达式的字节数 select datalength('我的世界我做主')--返回:7 --返回当前用户所登录的计算机名字 select host_name()--返回:你所登录的计算机的名字 --返回当前所登录的用户名称 select system_user--返回:你当前所登录的用户名 --给定用户的ID返回用户名 select user_name(1)--返回:从任意数据库中返回"dbo" --使用like进行模糊查询 select * from Username where StudentName like '王%' --between在某个范围内进行查询 select * from Username where BornDate not between '2010-01-01' and '2010-12-12' --使用in在列举值内进行查询 select StudentName as 学生姓名 from Username where Address in('北京','广州','上海') order by Address --sum()求和 select sum(Score) as 学号为23的学生总分 from Score where StudentNo=23 --avg()求平均 select avg(Score) as 平均成绩 from Score where Score>=60 --max()最大值 min()最小值 select avg(Score) as 平均成绩,max(Score) as 最高分,min(Score) as 最低分 from Score where Score >=60 --count() 集中计数 select count(*) as 及格人数 from Score where Score>=60 --group by分组查询 select CourseId,avg(Score) as 课程平均成绩 from Score group by CourseId --查询男女学生的人数各是多少 select count(*) as 人数,SSex from Username group by SSex --查询每个年级的总人数 select count(*) as 年级人数,SGrade from Students group by SGrade --查询每个科目的平均分,并按照由低到高的顺序排列 select CourseId, avg(Score) as 课程平均成绩 from Score group by CourseId order by avg(Score) desc --多列分组查询 select count(*) as 人数,SGrade as 年级,SSex as 性别 from Students group by SGrade,SSex order by SGrade --查看一个班人数超过15个的年级 select count(*) as 人数,SGrade as 年级 from Students group by SGrade having count(*)>15 --查询平均分及格的课程信息 select CourseId as 课程编号,avg(Score) as 课程平均成绩 from Score group by CourseId having avg(Score)>=60 --查询每门课程及格总人数和及格学生的平均分 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score where Score>=60 group by ScourseId --查询每门课程及格总人数和及格平均分在80分以上的记录 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score where Score>=60 group by CourseId having avg(Score)>=80 --查询有多个员工工资不低于2000的部门编号 select 部门编号,count(*) from 员工信息表 where 工资>=2000 group by 部门编号 having count(*)>1 --在where子句中指定连接条件 select StudentName,CourseId,Score from Students,Score where Scode = StudentId --在from 子句中使用inner join..on select S.StudentName,C.CourseId,C.Score from Students as S inner join Score as C on (S.Scode = C.StudentId) --左外连接查询 select S.SName,C.CourseId,C.Score from Students as S left outer join Score as C on S.Scode = C.StudentId --右外连接查询 select Titles.Title_id,Titles.Title, Publishers.Pub_name from titles right outer join Publishers on Titles.Pub_id=Publishers.Pub_id