数据库之数据的增删改查语句大全

--添加数据
    --添加单条
insert into 表名(字段名1,字段名2)  values (值1,值2)
    --添加多条
insert into 表名(字段名1,字段名2)  values (值1,值2),
                                            (值1,值2),
                                            (值1,值2)
                                            

--使用update更新语句,修改数据
update 表名 set 字段名=新值,字段名2=新值2

update student set ccredit=2 where cno='co1'


--删除数据
--删除表中所有数据
delete from 表名

delete from 表名 where 条件
--删除前20%的数据
delete top(20) present from sc


--数据查询
    --查询全部数据
select * from 表名

select 字段1,字段2 from 表名

--给列取别名
select sname as 姓名 from student 

--限制结果集的行数,指定取出多少行
select top 5 sname,ssex from student  --取出前5行数据

--消除重复的列  ,多个相同的课程号列只取一个
select distinct sno from sc

--条件查询
    --  >  <  =   !=  not
    --查找年龄小于20学生的学号
    select sno from student where sage<20   
    
    --范围查询  between and 和 not between and 
    --查找年龄在20到50之间学生
    select * from student where sage between 20 and 50
    
    --in 确定集合,属于集合的元组
    select * from student where sdept in ('信息系','计算机系','数学系')
    
--模糊查询  like
    --  _ 匹配任意一个字符,
    --    %匹配0个多个字符,
    --    []匹配【】中的任意一个字符,
    --    [^]不匹配他中的任意一个字符
        --查询姓张的学生
    select * from student where sname like '张%'
        --查询第二个字为大或小的人
    select * from student where sname like '_[大小]%'
        --查询最后一位不是 1 ,2的学生
    select * from student where sno like '%[^12]'
    

--数据排序 order by 默认是升序(上面小,下面打)
select *from student order by gkfs
--降序排列 
select *from student order by gkfs desc
    


--聚合函数,进行计算
--1.    查询各个系男女学生人数,高考分数平均分,高考分数最高分,高考分数最低分,
--        显示系,性别,人数,高考分数平均分,高考分数最高分,高考分数最低分。
select sdept,ssex,count(ssex),AVG(gkfs)as 平均分,MAX(gkfs)as 最高分,MIN(gkfs)as 最低分 from 录取表 


--分组计算 group by
    --统计每门课的选课人数,显示出课程号和人数
    select cno as 课程号,COUNT(sno) as 选课人数 from sc group by cno

--2.    查询每个学生的所选课程的课程数,所选课程的平均分,显示学号,所选课程的课程数,所选课程的平均分。
select * from sc
select sno,COUNT(cno) as 课程数,AVG(grade) as 平均分 from sc GROUP by sno

--3.    查询每个学生的所选课程各科都及格的课程数,所选课程的平均分,
--            显示学号,所选课程的课程数,所选课程的平均分。
--having是对分组进行筛选
select * from sc
select sno,COUNT(cno) as 课程数,AVG(grade)as 平均分 from sc 
group by sno having MIN(grade)>=60

--4.    查询每个学生的所选课程中分数不低于80分的课程平均分,显示学号,所选课程的平均分。
select  sno,AVG(grade) from sc where grade>=80 group by sno

--5.    查询每门课程的选修人数 ,所选课程的平均分
select cno,COUNT(*),AVG(grade) from sc group by cno

--查询选修了三门以上课程的学生--Query students who have taken more than three courses
select sno from sc group by sno having COUNT(*)>3


--LEFT对字符串进行操作,从左边进行截取
select LEFT('170508010430',4)    --1705

--right对字符串进行操作,从右边进行截取
select right('170508010430',2)    --30

--substaring 从指定位置取出  从第几位开始,取出几个数
select substring('1700508010430',2,5)  --70050

--转化为大写
select UPPER('YAng123')

--转化为小写
select lower('YAng123')


--子查询
    --单值嵌套查询
delect sno,grade from sc
where cno=(select cno from course where cname='数据库基础')

    --多值嵌套查询 in
    --查询和刘晨在同一个系的学生
select sno,sname,sdept from student 
where sdept in (select sdept from student where sname='刘晨')
and sname != '刘晨'

    --any 其中之一,有一个满足就为true
select * from course
where ccredit >any(select ccredit from course)

    --all 所有的,全部满足才为true
select * from course
where ccredit > all(select ccredit from course)
你 
    --exists 存在性检测
    --查询了选秀了c01课程的学生姓名
select sname from student
where exists(select * from sc where sno=student.sno and cno='c01')


--多表连接查询 join 
    --自连接              先join on 再where 最后group by
    --查询和刘晨在同一个系的学生的姓名和所在系
select s2,sname,s2.sdept from student s1 join student s2
on s1.sdept=s2.sdept
where s1.sname ='刘晨'
and s2.sname !='刘晨'

    --并运算 union 会自动剔除重复的数据行
    --列出课程编号为c01 c02的课程名和学分
select cname,ccredit from xourse where cno='c01'
union
select cname,ccredit from course where cno='c03'

    --交运算
    --既修了01又修了02
select cname,ccredit from xourse where cno='c01'
intersect
select cname,ccredit from course where cno='c03'

    --差运算 except同not in 。在一个集合有另一个集合没有
    
    

--case函数
    --  将一个测试表达式和一组简单的表达式进行比较,返回相应的结果
    --查询c07的课程,根据分数返回成绩结果并显示
select sno,
case
    when grade>=90 then '优秀'
    when grade between 80 and 89 then '良好'
    when grade between 70 and 79 then '中等'
    when grade between 60 and 69 then '及格'
    when grade <60 then '不及格'
end as 成绩
from scwhere cno='c07'

--1.在teacher中使用case语句为
--教授的老师基本工资设定为5000元,
--副教授老师基本工资设定为4000元,
--讲师老师基本工资设定为3000元,
--助教老师基本工资设定为2000元。
update teacher set 基本工资=
    case 职称
        when '教授' then 5000
        when '副教授' then 4000
        when '讲师' then 3000
        when '助教' then 2000
    end
    
--3.在teacher中使用case语句为
--教授的老师基本工资上浮50%
--副教授老师基本工资上浮40%,
--讲师的老师基本工资上浮30%
--助教老师基本工资上浮20%,
--其他人员基本工资上浮10%。
update teacher set 基本工资=基本工资*
     case 职称
        when '教授' then 1.5    --只能用小数不能使用百分数
        when '副教授' then 1.4
        when '讲师' then 1.3
        when '助教' then 1.2
        else 1.1
    end

猜你喜欢

转载自www.cnblogs.com/wuya132/p/11965962.html