资源描述
单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,An Introduction to Database System,单击此处编辑母版标题样式,专题,关系代数表达式书写,主讲人:刘永磊,An Introduction to Database System,一些例子,1.,查询选修了,2,号课程的学生的学号。,Sno,(,Cno,=2,(,SC,),2.,查询至少选修了一门其直接先行课为,5,号课程的的学生姓名,Sname,(,Sno,(,Cpno,=5,(Course)SC),Sno,,,Sname,(Student),3.,查询选修了全部课程的学生号码和姓名。,Sno,,,Cno,(,SC,),Cno,(,Course,),Sno,,,Sname,(,Student,),An Introduction to Database System,不识庐山真面目,只缘身在此山中。,自然语言,关系代数表达式,从系统外考虑,An Introduction to Database System,示例数据库,(a),Student,学生,-,课程数据库,:,学生关系,Student,、,课程关系,Course,和选修关系,SC,学号,Sno,姓名,Sname,性别,Ssex,年龄,Sage,所在系,Sdept,200215121,李勇,男,20,CS,200215122,刘晨,女,19,IS,200215123,王敏,女,18,MA,200215125,张立,男,19,IS,An Introduction to Database System,课程号,Cno,课程名,Cname,先行课,Cpno,学分,Ccredit,1,数据库,5,4,2,数学,2,3,信息系统,1,4,4,操作系统,6,3,5,数据结构,7,4,6,数据处理,2,7,PASCAL,语言,6,4,Course,(b),An Introduction to Database System,(c),SC,学号,Sno,课程号,Cno,成绩,Grade,200215121,1,92,200215121,2,85,200215121,3,88,200215122,2,90,200215122,3,80,An Introduction to Database System,书写关系代数表达式的步骤,1.,“,超脱于世外”,把自己当做没学过关系代数的“白丁”,人工完成题目给出的查询,记录人眼的移动过程。,查询选修了,2,号课程的学生的学号,An Introduction to Database System,(c),SC,学号,Sno,课程号,Cno,成绩,Grade,200215121,1,92,200215121,2,85,200215121,3,88,200215122,2,90,200215122,3,80,An Introduction to Database System,2.,“,出得去,回得来”,将人眼的移动过程翻译成关系代数表达式。(利用对于学过的关系代数理论的理解),人眼纵向扫描满足条件的元组,选择,人眼定位到某个元组后选择某些属性列的值,投影,An Introduction to Database System,查询选修了,2,号课程的学生的学号。,Sno,(,Cno,=2,(,SC,),查询至少选修,1,号课程和,3,号课程的学生号码,An Introduction to Database System,(c),SC,学号,Sno,课程号,Cno,成绩,Grade,200215121,1,92,200215121,2,85,200215121,3,88,200215122,2,90,200215122,3,80,是否包含,1,3,是否包含,1,3,An Introduction to Database System,首先建立一个临时关系,K,:,Cno,1,3,An Introduction to Database System,Sno,Cno,(SC,),200215121,象集,1,,,2,,,3,200215122,象集,2,,,3,K=1,,,3,于是:,Sno,Cno,(SC),K,=,200215121,Sno,Cno,200215121,1,200215121,2,200215121,3,200215122,2,200215122,3,An Introduction to Database System,首先建立一个临时关系,K,:,Cno,1,3,An Introduction to Database System,具有上述类似的人眼移动过程,除法,在整个过程中人眼未关注的列可以用投影,去除,查询至少选修了一门其直接先行课为,5,号课程的学生姓名,An Introduction to Database System,课程号,Cno,课程名,Cname,先行课,Cpno,学分,Ccredit,1,数据库,5,4,2,数学,2,3,信息系统,1,4,4,操作系统,6,3,5,数据结构,7,4,6,数据处理,2,7,PASCAL,语言,6,4,Course,(b),An Introduction to Database System,(c),SC,学号,Sno,课程号,Cno,成绩,Grade,200215121,1,92,200215121,2,85,200215121,3,88,200215122,2,90,200215122,3,80,1,An Introduction to Database System,(a),Student,学号,Sno,姓名,Sname,性别,Ssex,年龄,Sage,所在系,Sdept,200215121,李勇,男,20,CS,200215122,刘晨,女,19,IS,200215123,王敏,女,18,MA,200215125,张立,男,19,IS,200215121,An Introduction to Database System,上述,”,拿着”一些属性值到另外一张表,/,同一张表(自身连接)里去找具有相同值的项配对,自然连接,只能外码可以配对,An Introduction to Database System,查询至少选修了一门其直接先行课为,5,号课程的学生姓名,Sname,(,Sno,(,Cno,(,Cpno,=5,(,Course,),),Sno,Cno,(,SC,),),Sno,,,Sname,(,Student,),),查询选修了全部课程的学生号码和姓名。,An Introduction to Database System,课程号,Cno,课程名,Cname,先行课,Cpno,学分,Ccredit,1,数据库,5,4,2,数学,2,3,信息系统,1,4,4,操作系统,6,3,5,数据结构,7,4,6,数据处理,2,7,PASCAL,语言,6,4,Course,(b),An Introduction to Database System,(c),SC,学号,Sno,课程号,Cno,成绩,Grade,200215121,1,92,200215121,2,85,200215121,3,88,200215122,2,90,200215122,3,80,是否包含,17,是否包含,17,An Introduction to Database System,(a),Student,学号,Sno,姓名,Sname,性别,Ssex,年龄,Sage,所在系,Sdept,200215121,李勇,男,20,CS,200215122,刘晨,女,19,IS,200215123,王敏,女,18,MA,200215125,张立,男,19,IS,200215121,An Introduction to Database System,查询选修了全部课程的学生号码和姓名。,Sno,,,Cno,(,SC,),Cno,(,Course,),Sno,,,Sname,(,Student,),An Introduction to Database System,练习,有一学校教学数据库,包括学生、课程、教师、学生成绩,4,个关系。,学生关系,S(SNO,SN,AGE,SEX),有属性:学号、姓名、年龄、性别,课程关系,C(CNO,CN,PCNO),包括属性:课程号、课程名、先修课程号,教师关系,T(ENO,EN,DEPT),包括属性:职工号、姓名、系别,学生成绩关系,SC(SNO,CNO,ENO,G),包括属性:学生号、课程号、任课教师职工号、学生学习成绩,1.,选修所有课程并且成绩为,A,的学生名,2.,选修了王平老师讲授的所有课程的学生名,3.,求不选修信息系老师开设的所有的课程的学生名,An Introduction to Database System,分析,学生,教师,课程,m,n,p,An Introduction to Database System,分析(续),S(SNO,SN,AGE,SEX),;,C(CNO,CN,PCNO),;,T(ENO,EN,DEPT),SC(SNO,CNO,ENO,G),An Introduction to Database System,休息一会儿。,追求,An Introduction to Database System,
展开阅读全文