数据库系统概论基础知识(待更新)

1.查询指定列
select sname,sno
from student
(1)查询全部记录
select *
from student
(2)查询不相同的值
select distinct sno(不相同的)
from student
(3)查询经过计算的值
select sname,2014-sage
from student
(4)要求小写字母
select lower(sdept)
from student
(5)改变指定查询结果的列标题//用空格跟在后面
select sname NAME, 2014-sage BIRTHDAY
from student
2.选择表中若干元组
(1)消除重复的行
select distinct sno
from sc
//默认的是all
select all sno
from sc
(2)查询满足条件的元组
【1】比较大小
select sname,sage
from student
where sage<20
【2】确定范围
select sname,sdept,sage
from student
where sagebetween 20 and 23
【3】确定集合
select sname,sdept,sage
from student
where sdept in ('cs','ma','is')
【4】字符匹配(%表示任意长度,_表示单个字符//用like来进行字符串的匹配)
select sname,sno,ssex
from student
where sname like '刘%'

select sname,sno,ssex
from student
where sname like '欧阳_'

【5】如果要查询的字符串本身就含有通配符%或_,这时就需要使用  escape '换码字符' 对通配符进行转义
查询DB_Design课程的课程号和学分
select cno,ccreadit
from course
where cname like 'DB\_Design' escape '\'
【6】涉及空值的查询
查询所有有成绩的学生学号和课程号
select sno,cno
from sc
where grade is not null
【7】多重条件的查询
查询计算机系的年龄在20岁以下的学生姓名
select sname
from student
where sdept='is' and sage<20 
3.排序(order by)默认是升序的
升序(asc)降序(desc)
查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列
select *
from student
order by sdept asc,sage desc
4.聚集函数(做一些统计,对整体性质的描述)
count(*)                 统计元组个数(处理这个操作外,其他都是跳过空值,只处理非空值)
count(列名)           统计一列中值的个数
sum (列名)            计算一列值的总和
avg (列名)             计算一列值的平均值 
max (列名)            求一列值中的最大值
min (列名)            求一列值中的最小值
查询选修了课程的学生人数
select count(distinct sno)
from sc
计算选修了1 号课程的学生的最高值
select max(grade)
from sc
5.分组(group by) 分组后聚集函数将作用于每一个组,即每一个组都有一个函数值(having 是对组的限定)
求各个课程号即相应的选课人数
select cno,count(sno)
from sc
group by cno
查询选修了三门以上课程的学生学号
select sno
from sc
group by sno
having count(*)>3


链接查询
1.等值于非等值连接查询
查询选修2号课程并且成绩在90 分以上的所有学号和姓名
select student,sno,sname
from student,sc
where student.sno=sc.sno and sc.cno='2' and sc.grade>90
2.自身连接
查询每一门课的间接先行选修课
select first.cno, second.cpno
from course first,course second
where first.cpno=second.cno
3.外连接
列出每个学生的基本情况,及其选课情况(若某个学生没有选课,也需要把student 的悬浮元组保存在结果关系中)
select student.sno,sname,ssex,sdept,cno,grade
from student left outer join sc on (student.sno=sc.sno)
4.多表连接


嵌套查询
1.待有in谓词的子查询
查询与'刘晨'在同一系的学生
select sno,sname,sdept
from student
where sdept in (
    select sdept
    from student
    where sname ='刘晨')
查询选修了课程名为'信息系统'的学生学号和姓名
select sno,sname
from student
where sno in (
    select sno
    from sc
    where cno in (
        select cno
        from course
        where cname='信息系统'
        )
    )
2.带比较运算符的子查询//相关子查询
找出每个学生超过自己选修课程平均成绩的课程号
select cno
from sc x
where grade>=(
    select avg(grade)
    from sc y
    where x.sno=y.sno)
3.带any,all 谓词的子查询
查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄
select sname,sage
from student
where sage<any(
    select sage
    from student
    where sdept='cs')
and sdept!='cs'
4.带有exists(存在)谓词的子查询
带有exists谓词的子查询不返回任何数据,只产生逻辑真或逻辑假
查询所有选修了1号课程的学号是呢过姓名
select sname
from student
where exists(
    select *
    from sc
    where sno=student.sno and cno='1')
***查询选修了全部课程的学生姓名(没有一门课程,是他不选修的)
select sname
from student
where not exists(
    select *
    from course
    where not exists(
        select *
        from sc
        where sno=student.sno and cno=course.cno))

集合查询(注意: 参加集合操作的个查询结果的列数必须相同,对应项的数据类型有必须相同)
并集(union)
查询选修了课程1或者选修了课程2 的学生
select sno
from sc
where cno='1'
union
select sno
from sc
where cno='2'
交集(intersect)
查询计算机科学系的学生与年龄不大于19 岁的学生的交集
select *
from student
where sdept='cs'
intersect
select *
from student
where sage<=19
差集(except)
查询计算机科学系的学生与年龄不大于19岁的学生的差集
select *
from student
where sdept='cs'
except
select *
from student
where sage<=19

插入数据
1.插入元组
insert
into student(sno,sname,ssex,sdept,sage)
values('201215128','陈东','男','is','18')
2.插入子查询结果
求平均值,存入数据库里
insert
into dept(sdept,age)
select sdept,avg(sage)
from student
group by sdept

修改数据

1.修改某一元组的值

将学生201215121的年龄改为22岁

update student

set sage=22

where sno='201215121'

2.修改多个元组的值

update student

set sage=sage+1

3.带子查询的修改语句

update sc

set grade=0

where sno in (

    select sno

    from student
    where sdept='cs')

删除数据
1.删除某一个元组的值
删除学号为201215128的学生纪录
delete
from student 
where sno='201215128'
2.删除多个元组的值
删除所有学生的选课纪录
delete
from cs
3.带子查询的删除语句
删除计算机科学系所有学生的选课纪录
delete
from sc
where sno in(
    selete sno
    from student
    where sdept='cs')
where sno='201215128'

猜你喜欢

转载自blog.csdn.net/red_red_red/article/details/88828540