数据库作业六

例题练习

例题均在下表中查询:

Create Table student(
Sno char(12) Primary Key, 
Sname char(12),
Ssex char(2),
Sdept char(40),
Sage SmallInt
);
Insert Into Student Values ('20191102062','刘哲轩','男','Computer Science',19)
Insert Into Student Values ('20191102063','叶晓晓','男','information security',20)
Insert Into Student Values ('20191102064','刘小轩','女','Computer Science',21)
Insert Into Student Values ('20191102065','叶大大','男','information security',20)
Insert Into Student Values ('20191102066','刘大轩','男','Computer Science',22)
Insert Into Student Values ('20191102067','刘轩','女','information security',20)
Insert Into Student Values ('20191102068','刘车干','男','Computer Science',19)
Insert Into Student Values ('20191102069','刘刘','女','information security',20)
Insert Into Student Values ('20191102070','刘刘流','男','Computer Science',22)
Insert Into Student Values ('20191102071','叶小刘','女','information security',20)
Create Table Course(
   Cno Char(12) Primary Key,--课序号
   Cname Char(12),
   Cpno Char(12),--先行课的课序号
   Ccredit SmallInt,
   Foreign Key(Cpno) References Course(Cno)
);
Insert Into Course Values('1','数据库',null,4)
Insert Into Course Values('2','数学',null,2)
Insert Into Course Values('3','信息系统',null,4)
Insert Into Course Values('4','操作系统',null,3)
Insert Into Course Values('5','数据结构',null,4)
Insert Into Course Values('6','数据处理',null,2)
Insert Into Course Values('7','PASCAL语言',null,4)
update course set cpno='5' where cno='1'
update course set cpno='1' where cno='3'
update course set cpno='6' where cno='4'
update course set cpno='7' where cno='5'
update course set cpno='6'where cno='7'
Create Table SC(--学生选课表
  Sno Char(12),
  Cno Char(12),
  Grade SmallInt,
  Primary Key(Sno, Cno),
  Foreign Key(Sno) References Student(Sno),
  Foreign Key(Cno) References Course(Cno)
);
Insert Into SC Values('20191102062', '1', 100)
Insert Into SC Values('20191102062', '2', 51)
Insert Into SC Values('20191102063', '1', 65)
Insert Into SC Values('20191102064', '2', 46)
Insert Into SC Values('20191102065', '3', 85)
Insert Into SC Values('20191102066', '3', 95)
Insert Into SC Values('20191102067', '7', 99)
Insert Into SC Values('20191102068', '6', 93)

连接查询的 WHERE子句中用来连接两个表的条件称为连接条件或连接谓词,其一般格式为

[<表名1>.]<列名1><比较运算符>[<表名2>.]<列名2>

其中比较运算符主要有=、>、<、>=、=、!=(或<>)等。
此外连接谓词还可以使用下面形式:

[<表名1>.]<列名1> BETWEEN [<表名2>.]<列名2>AND[<表名2>.]<列名3>

当连接运算符为=时,称为等值连接。使用其他运算符称为非等值连接。
连接谓词中的列名称为连接字段。连接条件中的各连接字段类型必须是可比的,但名字不必相同。

例3.49 查询每个学生及其选修课程情况。

学生情况存放在Student表中,学生选课情况存放在SC表中,所以本查询实际上涉及Student与SC两个表。这两个表之间的联系是通过公共属性 Sno实现的。本例中,SELECT子句与WHERE子句中的属性名前都加上了表名前缀,这是为了避免混淆。如果属性名在参加连接的各表中是唯一的,则可以省略表名前缀。

Select Student.*, SC.* From Student, SC Where Student.Sno = SC.sno;

在这里插入图片描述

Select * From Student, SC Where Student.Sno = SC.sno;

这句语句和上句效果一致
在这里插入图片描述

Select * From Student, SC;

当没有条件表达式时, 多表查询求出的是两表的笛卡尔积

例3.50 用自然连接完成

Select Student.Sno, Sname, Ssex, Sage, Sdept, Cno,Grade From Student, SC 
Where Student.Sno = SC.Sno;

本例中,由于Sname,Ssex,Sage,Sdept,Cno和 Grade属性列在Student 表与SC表中是唯一的,因此引用时可以去掉表名前缀;而Sno在两个表都出现了,因此引用时必须加上表名前缀。
一条SOL语句可以同时完成选择和连接查询,这时WHERE子句是由连接谓词和选择谓词组成的复合条件。
在这里插入图片描述

3.51 查询选修1号课程且成绩在90分以上的所有的同学的学号和姓名。

该查询的一种优化(高效)的执行过程是,先从 SC 中挑选出 Cno='2’并且 Grade>90的元组形成一个中间关系,再和 Student中满足连接条件的元组进行连接得到最终的结果关系。

Select Student.Sno, Sname From Student, SC
Where Student.Sno = SC.Sno And SC.Cno = '1' And SC.Grade > 90;

在这里插入图片描述

3.52 查询每一门课的间接先修课(即先修课的先修课)

在Course表中只有每门课的直接先修课信息,而没有先修课的先修课。要得到这个信息,必须先对一门课找到其先修课,再按此先修课的课程号查找它的先修课程。这就要将Course表与其自身连接。

Select First.Cno, Second.Cpno From Course First, Course Second
Where First.Cpno = Second.Cno;

在这里插入图片描述

3.53

左外连接列出左边关系(如本例Student)中所有的元组,右外连接列出右边关系中所有的元组。

Select Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade
From Student Left Outer Join SC ON(Student.Sno = SC.Sno);

在这里插入图片描述


连接操作除了可以是两表连接、一个表与其自身连接外,还可以是两个以上的表进行连接,后者通常称为多表连接。

3.54 查询每个学生的学号、姓名、选修的课程名及成绩。

Select Student.Sno, Sname, Cname, Grade
From Student, SC, Course
Where Student.Sno = SC.Sno And SC.Cno = Course.Cno;

在这里插入图片描述
关系数据库管理系统在执行多表连接时,通常是先进行两个表的连接操作,再将其连接结果与第三个表进行连接。本例的一种可能的执行方式是,先将Student 表与SC 表进行连接,得到每个学生的学号、姓名、所选课程号和相应的成绩,然后再将其与Course表进行连接,得到最终结果。

猜你喜欢

转载自blog.csdn.net/qq_45834992/article/details/115438750