学习数据库概论第四天

文章目录


2.连接查询
若一个查询同时涉及两个以上的表,则称之为连接查询。分为:等值连接查询、自然连接查询、非等值连接查询、自身连接查询、外连接查询和复合条件连接查询等。

等值连接和非等值连接
例3.49 查询每个学生及其选修课程的情况。
Select Student.,SC.
From Student,SC
Where Student.Sno=SC.Sno;
若在等值连接中把目标列中重复的属性列去掉则为自然连接。
例3.50 对例3.49用自然连接完成。
Select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
From Student,SC
Where Student.Sno=SC.Sno;
例3.51 查询选修2号课程且成绩在90分以上的所有学生的学号和姓名。
Select Student.Sno,Sname
From Student,SC
Where Student.Sno=SC.Sno and Cno=’2’ and Grade>90;

自身连接
一个表与其自己进行连接,称为表的自身连接。
例3.52查询每一门课的间接先修课。
Select first.Cno,second.Cpno
From Course first,Course second
Where first.Cpno=second.Cno;

外连接
左外连接列出左边关系中的所有元组,右外连接列出右边关系中所有的元组。

多表连接
例3.54 查询每个学生的学号、姓名、选修的课程名及成绩。
Select Student.Sno,Sname,Cname,Grade
From Student,SC,Course
Where Student.Sno=SC.Sno and SC.Cno=Course.Cno;

3.嵌套连接
一个Select-from-where语句称为一个查询块。将一个查询块嵌套在另一个查询块的where子句或having短语的条件中的查询称为嵌套查询。上层的查询块叫外层查询或父查询,下层查询块叫内层查询或子查询。
例3.55 查询与“刘晨”在同一个系学习的学生。
Select *
From Student
Where Sdept in
(select Sdept
From Student
Where Sname=’刘晨’);
子查询的查询条件不依赖于父查询,称为不相关子查询。
例3.56 查询选修了课程名为“信息系统”的学生学号和姓名。
Select Sno,Sname
From Student,SC
Where Sno in
(select Sno
From SC
Where Cno in
(select Cno
From Course
Where Cname=’信息系统’));
如果子查询的查询条件依赖于父查询,这类子查询称为相关子查询。
例3.57 找出每个学生超过他自己选修课程平均成绩的课程号。
Select Sno,Cno
From SC x
Where Grade >=(select avg(Grade)
From SC y
Where y.Sno=x.Sno);
例3.58 查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄。
Select Sname,Sage
From Student
Where Sage<any(select Sage
From Student
Where Sdept=’CS’) and Sdept<>’CS’;
例3.59 查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄。
Select Sname,Sage
From Student
Where Sage<all(select Sage
From Student
Where Sdept=’CS’) and Sdept<>’CS’;

带有exists谓词的子查询不返回任何数据,只产生逻辑真值‘true’或逻辑假值‘false’.
例3.60 查询所有选修了1号课程的学生姓名。
Select Sname
From Student
Where exists(select *
From SC
Where Student.Sno=Sno and Cno=’1’);
若内层查询结果非空,则外层的where子句返回真值,否则返回假值。
例3.61 查询没有选修1号课程的学生姓名。
Select Sname
From Student
Where no exists(select *
From SC
Where Student.Sno=Sno and Cno=’1’);
例3.62 查询选修了全部课程的学生姓名。
Select Sname
From Student
Where not exists(select *
From Course
Where not exists(select *
From SC
Where Sno=Student.Sno
And Cno=Course.Cno));
例3.63查询至少选修了学生201215122选修的全部课程的学生号码。
Select distinct Sno
From SC SCX
Where not exists(select *
From SC SCY
Where SCY.Sno=’201215122’ and not exists(select *
From SC SCZ
Where SCZ.Sno=SCX.Sno and SCZ.Cno=SCY.Cno));

集合查询
并操作union、交操作intersect和差操作except
例3.64查询计算机科学系的学生及年龄不大于19岁的学生。
Select *
From Student
Where Sdept=’CS’
Union
Select *
From Student
Where Sage<=19;
例3.65查询选修课程1或者选修了课程2的学生集合的并集
Select *
From Student
Where Cno=’1’
Union
Select *
From Student
Where Cno=’2’
例3.66 查询计算机科学系的学生与年龄不大于19岁的学生的交集。
Select *
From Student
Where Sdept=’CS’
Intersect
Select *
From Student
Where Sage<=19;
例3.67 查询既选修了课程1又选修了课程2的学生。
Select *
From Student
Where Cno=’1’
Intersect
Select *
From Student
Where Cno=’2’;
例3.68 查询计算机科学系的学生与年龄不大于19岁的学生的差集。
Select *
From Student
Where Sdept=’CS’
Except
Select *
From Student
Where Sage<=19;

基于派生表的查询
子查询不仅可以出现在where子句中,还可以出现在from子句中,这时子查询生成的临时派生表成为主查询的查询对象。
通过from子句生成派生表时,必须为派生表关系指定一个别名,而对于基本表,别名是可选项。

3.5数据更新

插入数据

1.插入元组
Insert into 表名 [属性列,属性列,…] values (常量,常量,…);
Into 子句中没有出现的属性列,新元组在这些列上将取空值。在表定义时说明了not null的属性列不能取空值,否则将会出错。
如果into子句中没有指明任何属性列名,则新插入的元组必须在每个属性列上均有值。
例3.69 将一个新学生元组(学号:201215128,姓名:陈冬,性别:男,所在系:IS,年龄:18岁)插入到Student表中。
Insert into Student values(‘201215128’,’陈冬’,’男’,’IS’,18);
字符串常数通常用单引号括起来。
例3.71 插入一条选课记录(‘201215128’,‘1’)。
Insert into SC(Sno,Cno) values(‘201215128’,’1’);

2.插入子查询结果
Insert into 表名(属性列,…)
子查询;

修改数据
Update 表名
Set 列名=表达式
[where 条件]

1.修改某一个元组的值
例3.73 将学生201215121的年龄改为22岁。
Update Student
Set Sage=22
Where Sno=’201215121’;
2.修改多个元组的值
例3.74 将所有学生的年龄增加1岁。
Update Student
Set Sage=Sage+1;
例3.75 将计算机科学系全体学生的成绩置零。
Update SC
Set Grade=0
Where Sno in(select Sno
From Student
Where Sdept=’CS’);

删除数据
Delete
From 表名
[where 条件]
Delete语句删除的是表中的数据,而不是关于表的定义。
例3.76 删除学号为201215128的学生记录
Delete
From Student
Where Sno=’201215128’;
例3.77删除所有学生选课记录
Delete
From SC;
例3.78 删除计算机科学系所有学生的选课记录。
Delete
From SC
Where Sno in(select Sno
From Student
Where Sdept=’CS’);

发布了38 篇原创文章 · 获赞 38 · 访问量 2736

猜你喜欢

转载自blog.csdn.net/l13kddd/article/details/104564920