数据库学习总结.docx
《数据库学习总结.docx》由会员分享,可在线阅读,更多相关《数据库学习总结.docx(12页珍藏版)》请在冰点文库上搜索。
数据库学习总结
学习总结
聚集函数:
程序一:
selectcno,count(Sno)/*count(*)或者count(cno),count(grade)等都是一样的结果,可以思考下就明白了*/
fromsc
groupbycno
连接:
程序一:
select*
/*selectstudent.*,sc.**/
fromstudent,sc
wherestudent.sno=sc.sno
/*wheresc.sno=student.sno结果是一样的*/
如果是fromsc,student则sc表的东西会在前面
如果把上面的作为自然连接,只要把列名依次列出来,不列举同名的而已,笑尿了。
。
麻烦死了,为了省去少列举一列
程序一:
selectStudent.sno,sname,grade
fromstudent,sc
wherestudent.sno=sc.snoando=2andgrade>80
程序二:
selectstudent.sno,sname,grade
fromstudent,
(
select*
fromsc
wherecno='2'andgrade>80
)
cou/*把这个查询结果命名为cou表,然后进行连接查询*/
wherestudent.sno=cou.sno
程序三:
selectsno,sname
fromstudent
wheresnoin
(
selectsc.sno
fromsc
wherecno='2'andgrade>80
)
这个怎么改也达不到上面的效果
Like的用法问题:
程序一:
/*
selectSdept
fromstudent
whereSnamelike'刘晨';
*/
selectSdept
fromstudent
whereSname='刘晨';
Like可能还是与字符集有关,若改成snamelike’刘%’可以,若改成snamelike‘刘_’不行
自身连接中的一个问题:
对于这样一个表,用自连接解决:
查询与“刘晨”在同一个系学习的学生
正确的做法是
为何这样写就会是这样的结果
以下是论证过程,看完,体会后就懂得两张表连接的过程是第一张表的第一行与第二张第一行比较,接着第一张第二行与第二张第一行进行比较,依次,直到第一张表比较完,才转到第二张第二行比较
查询所有选修了2号课程的学生姓名
程序一:
selectSname
fromStudent
whereexists
(
select*
fromSc
whereSc.Sno=Student.SnoandCno='2'
)
程序二:
selectSname
fromstudent
whereSnoin
(
selectSno
fromSC
whereCno='2'
)
程序三:
selectSname
fromStudent,Sc
whereStudent.Sno=Sc.SnoandSc.Cno='2'
程序四:
selectdistinctSname
/*可以把distinct去掉,想想为何每个重复的是5个*/
fromStudent,Sc
whereStudent.Snoin
(
selectSno
fromSc
whereCno='2'
)
查询没有选修2号课程的学生姓名
程序一:
selectSname
fromstudent
whereSnoin
(
selectSno
fromsc
whereCno!
='2'
)
/*运行结果与上一题相同,经思考明白,问题来了,然后怎么办呢?
*/
程序二:
selectSname
fromstudent
whereSnonotin
(
selectSno
fromsc
whereCno='2'
)
程序三:
selectSname
fromStudent
wherenotexists
(
select*
fromSc
whereSc.Sno=Student.SnoandCno='2'
)
程序四:
selectdistinctSname
/*可以把distinct去掉,想想为何每个重复的是个*/
fromStudent,Sc
whereStudent.Snonotin
(
selectSno
fromSc
whereCno='2'
)
程序五:
另一个连接没法写,如果仿照上题的程序三。
因为SC表的原因
select*
fromStudent,Sc
wherenot(Student.Sno=Sc.SnoandSc.Cno='2')
这样写也不对,可以想想
例3.62查询选修了全部课程的学生姓名
程序一:
selectSname
fromStudent
wherenotexists
(
select*
fromSc
wherenotexists
(
select*
fromsc
whereStudent.Sno=Sc.Sno
)
)
结果是写成了查询选课的学生。
。
而不是全部课程
正确程序:
selectSname
fromStudent
wherenotexists
(
select*
fromCourse
wherenotexists
(
select*
fromsc
whereCourse.Cno=Sc.CnoandStudent.Sno=Sc.Sno
)
)
例3.63查询至少选修了学生95002选修的全部课程的学生号码
程序一:
最初的程序是这样的
selectx3.Sno
fromScx3
wherenotexists
(
select*
fromScx1
wherenotexists
(
select*
fromScx2
wherex2.Sno='95002'andx1.Cno=X2.Cno
)
)/*查询一个学生,没有没选95002选的课,*/
而实际结果是,该程序段:
select*
fromScx1
whereexists
(
select*
fromScx2
wherex2.Sno='95002'andx1.Cno=X2.Cno
)
查询结果是:
95001285
95001388
95002290
95002380
95004376(95004为增加的测试数据)
翻译过来,其实是查询一个选了95002选的课(不是全部)所以
程序段:
select*
fromScx1
wherenotexists
(
select*
fromScx2
wherex2.Sno='95002'andx1.Cno=X2.Cno
)
结果不对:
95001192
程序二:
/*书上例程*/
selectdistinctSno
fromScx
wherenotexists
(
select*
fromScy
wherey.Sno='95002'and
notexists
(
select*
fromScz
wherez.Sno=x.Snoand
z.Cno=y.Cno
)
)
程序三:
/*借助于例3.62的方法*/
selectdistinctx1.Sno
fromScx1
wherenotexists
(
select*
from
(
selectCno
fromSc
whereSno='95002'
)temp
wherenotexists
(
select*
fromScx2
wherex2.Sno=X1.Snoandx2.Cno=temp.Cno
)
)
这题真的很头疼。
。
。
需要记啊
——ByYueTengFei
13计科卓越二班