文档介绍:该【数据库系统概论SQL语句 】是由【guoxiachuanyue004】上传分享,文档一共【26】页,该文档可以免费在线阅读,需要了解更多关于【数据库系统概论SQL语句 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
第三章关系数据库标准语言SQL
(续1)
河南财经政法大学
AnIntroductiontoDatabaseSvstem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
•:・连接查询:同吋涉及多个表的查询
•连接条件或连接谓词:用來连接两个表的条件
一般格式:
[V表名1>.]v列名1>V比较运算符〉[V表名2>.]v列名2>
[v表名1>.]v列名1>BETWEEN[v表名2>.]v列名2>AND[v表名2>.]v列名3>
•连接字段:连接谓词中的列名称
连接条件中的各连接字段类型必须是可比的,但名字不必是相同的
连接操作的执行过程
❖嵌套循环法(NESTED-LOOP)
■首先在表1中找到第一个元组,然后从头开始扫描表2,逐一查找满足连接件的元组,找到后就将表1中的第一个元组与该元组拼接起來,形成结果表中一个元组。
・表2全部查找完后,再找表1中第二个元组,然后再从头开始扫描表2,逐一查找满足连接条件的元组,找到后就将表1中的第二个元组与该元组拼接起来,形成结果表中一个元组。
■重复上述操作,直到表1中的全部元组都处理完毕
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
排序合并法(SORT-MERGE)
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
常用于二连接
■首先按连接属性对表1和表2排序
■对表1的第一个元组,从头开始扫描表2,顺序查找满足连接条件的元组,找到后就将表1中的第一个元组与该元组拼接起来,形成结果表中一个元组。当遇到表2中第一条大于表1连接字段值的元组时,对表2的查询不再继续
排序合并法
■找到表1的第二条元组,然后从刚才的中断点处继续顺序扫描表2,查找满足连接条件的元组,找到后就将表1中的第一个元组与该元组拼接起來,形成结果表中一个元组。直接遇到表2中大于表1连接字段值的元组时,对表2的查询不再继续
■重复上述操作,直到表1或表2中的全部元组都处理完毕为止
索引连接(INDEX-JOIN)
■对表2按连接字段建立索引
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
■对表1中的每个元组,依次根据其连接字段值查询
表2的索引,从中找到满足条件的元组,找到后就
将表1屮的第一个元组与该元组拼接起来,形成结果表中一个元组
一、等值与非等值连接查询
二、自身连接
三、外连接
四、复合条件连接
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
•:•等值连接:连接运算符为=
[例33]查询每个学生及其选修课程的情况
SELECTStudent.*,SC.*
FROMStudent,SC
=;
等值与非等值连藝备询(续)
查询结果:
Sname
Ssex
Sage
Sdept
Cno
Grade
200215121
李勇
男
20
CS
200215121
1
92
200215121
李勇
男
20
cs
200215121
2
85
200215121
李勇
男
20
CS
200215121
3
88
200215122
刘晨
女
19
cs
200215122
2
90
200215122
刘晨
女
19
cs
200215122
3
80
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
等T值连接査E
•:•自然连接:[例34]对[例33]用自然连接完成。
,Sname,Ssex,Sage,Sdept,Cno,Grade
FROMStudent,SC
=;
一、等值与非等值连接查询
二、自身连接
三、外连接
四、复合条件连接
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
•:•自身连接:一个表与其自己进行连接
•:•需要给表起别名以示区别
❖由于所有属性名都是同名属性,因此必须使用别名前缀[例35]查询每一门课的间接先修课(即先修课的先修课)
,
FROMCourse「S,CourseSECOND
=;
Cno
Cname
Cpno
Ccredit
1
数据库
5
4
2
数学
2
3
信息系统
1
4
4
操作系统
6
3
5
数据结构
7
4
6
数据处理
2
7
PASCAL语言
6
4
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
自身连接(续)
SECOND表(Course表)
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
Cno
Cname
Cpno
Ccredit
1
数据库
5
4
2
数学
2
3
信息系统
1
4
4
操作系统
6
3
5
数据结构
7
4
6
数据处理
2
7
PASCAL语言
6
4
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
一、等值与非等值连接查询
二、自身连接
三、外连接
四、复合条件连接
■普通连接操作只输出满足连接条件的元组
■外连接操作以指定表为连接主体,将主体表中不满足连接条件的元组一并输出
[例36]改写[例33]
,Sname,Ssex,Sage,Sdept,Cno,Grade
FROMStudentLEFTOUTJOINSCON(=);
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
Sname
Ssex
Sage
Sdept
Cno
Grade
200215121
李勇
男
20
CS
I
92
200215121
李勇
男
20
CS
2
85
200215121
李勇
男
20
CS
3
88
200215122
刘晨
女
19
CS
2
90
200215122
刘晨
女
\9
CS
3
80
200215123
王敏
女
18
MA
NULL
NULL
200215125
张立
男
19
IS
NULL
NULL
外连接(续)
•左外连接
■列出左边关系(如本例Student)中所有的元组
•右外连接
■列出右边关系中所有的元组
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem
AnIntroductiontoDatabaseSystem