create database lianxi0720
gouse lianxi0720gocreate table student( code int not null,--学号,不可为空 name varchar(10),--学生姓名 sex varchar(10),--性别 banji varchar(10),--班级 yufen decimal(18,2),--语文分数 shufen decimal(18,2),--数学分数 yingfen decimal(18,2),--英语分数)go--添加学生信息insert into student values(1001,'张三','男','一班',80,90,95)insert into student values(1002,'李四','女','一班',34,55,77)insert into student values(1003,'王五','男','一班',23,77,87)insert into student values(1004,'赵六','女','一班',36,64,87)insert into student values(1005,'冯七','男','二班',22,34,55)insert into student values(1006,'王八','女','二班',76,35,24)insert into student values(1007,'邓超','男','二班',87,42,76)insert into student values(1008,'王祖蓝','女','二班',23,54,98)insert into student values(1009,'王宝强','男','三班',53,42,54)insert into student values(1010,'陈赫','女','三班',66,77,88)insert into student values(1011,'郑凯','男','三班',12,43,76)insert into student values(1012,'郭德纲','女','三班',78,79,72)go--修改1011学生的语分为66update student set yufen =66 where code=1011 --查看所有信息select * from student--查看前三条信息select top 3 * from student--查看一班的所有同学信息select * from student where banji='一班'--查看一班所有女生的姓名select name as 性别 from student where sex='女' and banji='一班'--删除一班的所有女生
delete from student where banji='一班' and sex='女'--删除所有信息delete from student--修改1003学号的同学的数学分数为88(现在是77)
update student set shufen = 88 where code=1003--模糊查询
--查看所有姓李的人的信息select * from student where name like'李%'--查看所有姓名中含有"王"这个字的学生的所有信息select * from student where name like '%王%'--排序查询 order by
--查看语文分数升序排列 后缀是asc可以不写,默认是升序select yufen from student order by yufen--查看语文分数的降序排列 后缀是descselect yufen from student order by yufen desc--语分按照降序排列之后选择前三名的所有信息select top 3 * from student order by yufen desc--查看一班语分最高的人的所有信息select top 1 * from student where banji='一班' order by yufen desc --聚合函数--AVG 平均值--查看所有人的语分平均分select AVG(yufen) from student--查看一班所有人的语文平均分select AVG(yufen) from student where banji='一班'--计数 COUNT
--查看所有数据的总条数select COUNT(*) from student--查看姓王的总共有及格select COUNT(*) from student where name like '王%'--查看总共有几个班级select COUNT(distinct banji) from student--最大MAX
--查看一班的语文最高分select MAX(yufen) from student where banji ='一班'--最小 MIN
--查看一班的语文最小分数select MIN(yufen) from student where banji='一班'--求和 SUM
--查看一班的语文总分select SUM(yufen) from student where banji='一班' --分组 group by select banji from student group by banji--查看每个班级的语文平均分select banji as 班级 , AVG(yufen) as 语文平均分 from student group by banji--查看每个班有多少人select banji, COUNT(*) from student group by banji--having 增加什么限制 后面只允许跟聚合函数,前面必须是group by
--查看分数超过30并且人数超过2个人的班级以及班级及格人数select banji ,COUNT(*) from student where yufen>30 group by banji having COUNT(*)>2--查看分数超过30并且人数超过2个人的班级以及班级及格人数,根据超过30分的人数来进行升序排序select banji ,COUNT(*) from student where yufen>30 group by banji having COUNT(*)>2 order by COUNT(*)--数学函数
--绝对值select ABS(-7)--取上线select CEILING(3.14)--取下线select FLOOR(3.14)--次方select POWER(2,4)--四舍五入select ROUND(2.5,0)--平方根select SQRT(4)--平方select SQUARE(4) --字符串函数--将字符转换为ascii码select ASCII('i')--将ascii码转换为字符select CHAR(110)--查看第一次出现的索引号select CHARINDEX('eabc','abcdefghjklmn')--截取,从左往右select LEFT('abcdefghjklmn',4)--截取,从右往左select RIGHT('abcdefghjklmn',4)--字符串长度,若前面有空格,那就算上,后面的不算select LEN(' abcde ')--转大写select UPPER('abcdefg')--转小写select LOWER('ABCDEFG')--去除前面的空格select LTRIM(' abcd')--去除后面的空格select RTRIM(' abcd ')--复制select REPLICATE('abc',2)--将小数转换为字符串select STR(2332.32121,3,1) --时间日期函数SET DATEFIRST 1SELECT @@DATEFIRST AS '1st Day', DATEPART(dw, GETDATE()) AS 'Today'select GETDATE()--增加月份select DATEADD(MONTH,5,'2016-8-31')--算时间差select DATEDIFF(DAY,'2008-8-8','2016-8-31')--精确获取时间select SYSDATETIME()