3.1 sql概述
sql(structured query language)
结构化查询语言,是关系数据库的标准语言
sql 是一个通用的、功能极强的关系数据库语言
基本表
本身独立存在的表
sql中一个关系就对应一个基本表
一个(或多个)基本表对应一个存储文件
一个表可以带若干索引
存储文件
逻辑结构组成了关系数据库的内模式
物理结构是任意的,对用户透明
视图
从一个或几个基本表导出的表
数据库中只存放视图的定义而不存放视图对应的数据
视图是一个虚表
用户可以在视图上再定义视图
sql的特点
1.综合统一
2.面向过程
3.集合操作方式(操作对象、查找结果可以是元组的集合
一次插入、删除、更新操作的对象可以是元组的集合
4.能够嵌入到高级语言
3.2 学生-课程数据库
学生-课程模式 s-t :
学生表:student(sno,sname,ssex,sage,sdept)
课程表:course(cno,cname,cpno,ccredit)
学生选课表:sc(sno,cno,grade)
3.3 数据定义
3.3.1 模式的定义与删除
一、创建模式
[例1]定义一个学生-课程模式s-t
create schema “s-t” authorization wang;
为用户wang定义了一个模式s-t
[例2]create schema authorization wang;
<模式名>隐含为用户名wang
如果没有指定<模式名>,那么<模式名>隐含为<用户名>
定义模式实际上定义了一个命名空间
在这个空间中可以定义该模式包含的数据库对象,例如基本表、视图、索引等。
在create schema中可以接受create table,create view和grant子句。
create schema <模式名> authorization <用户名>[<表定义子句>|<视图定义子句>|<授权定义子句>]
[例3]
create schema test authorization zhang
create table tab1(col1 smallint,
col2 int,
col3 char(20),
col4 numeric(10,3),
col5 decimal(5,2)
);
为用户zhang创建了一个模式test,并在其中定义了一个表tab1。
二、删除模式
[例4] drop schema zhang cascade;
删除模式zhang
同时该模式中定义的表tab1也被删除
3.3.2 基本表的定义、删除与修改
一、定义基本表
create table <表名>
(<列名> <数据类型>[ <列级完整性约束条件> ]
[,<列名> <数据类型>[ <列级完整性约束条件>] ] …
[,<表级完整性约束条件> ] );
<表名>:所要定义的基本表的名字
<列名>:组成该表的各个属性(列)
<列级完整性约束条件>:涉及相应属性列的完整性约束条件
<表级完整性约束条件>:涉及一个或多个属性列的完整性约束条件
如果完整性约束条件涉及到该表的多个属性列,则必须定义在表级上,否则既可以定义在列级也可以定义在表级。
[例5] 建立“学生”表student,学号是主码,姓名取值唯一。
create table student
(sno char(9) primary key, /* 列级完整性约束条件*/
sname char(20) unique, /* sname取唯一值*/
ssex char(2),
sage smallint,
sdept char(20)
);
[例6] 建立一个“课程”表course
create table course
( cno char(4) primary key,
cname char(40),
cpno char(4),
ccredit smallint,
foreign key (cpno) references course(cno)
);
[例7] 建立一个“学生选课”表sc
create table sc
(sno char(9),
cno char(4),
grade smallint,
primary key (sno,cno),
/* 主码由两个属性构成,必须作为表级完整性进行定义*/
foreign key (sno) references student(sno),
/* 表级完整性约束条件,sno是外码,被参照表是student */
foreign key (cno) references course(cno)
/* 表级完整性约束条件, cno是外码,被参照表是course*/
);
二、数据类型
sql中域的概念用数据类型来实现
定义表的属性时 需要指明其数据类型及长度
选用哪种数据类型
取值范围
要做哪些运算
三、模式与表
每一个基本表都属于某一个模式
一个模式包含多个基本表
定义基本表所属模式
方法一:在表名中明显地给出模式名
create table “s-t”.student(......); /*模式名为 s-t*/
create table “s-t”.cource(......);
create table “s-t”.sc(......);
方法二:在创建模式语句中同时创建表
方法三:设置所属的模式
四、修改基本表
alter table <表名>
[ add <新列名> <数据类型> [ 完整性约束 ] ]
[ drop <完整性约束名> ]
[ alter column<列名> <数据类型> ];
[例8]向student表增加“入学时间”列,其数据类型为日期型。
alter table student add s_entrance date;
不论基本表中原来是否已有数据,新增加的列一律为空值。
[例9]将年龄的数据类型由字符型(假设原来的数据类型是字符型)改为整数。
alter table student alter column sage int;
[例10]增加课程名称必须取唯一值的约束条件。
alter table course add unique(cname);
五、删除基本表
drop table <表名>[restrict| cascade];
restrict:删除表是有限制的。
欲删除的基本表不能被其他表的约束所引用(check ,外码)
如果存在依赖该表的对象,则此表不能被删除(例如视图)
cascade:删除该表没有限制。
在删除基本表的同时,相关的依赖对象一起删除
[例11] 删除student表
drop table student cascade ;
基本表定义被删除,数据被删除
表上建立的索引、视图、触发器等一般也将被删除
[例12] 若表上建有视图,选择restrict时表不能删除
create view is_student
as
select sno,sname,sage
from student
where sdept='is';
drop table student restrict;
--error: cannot drop table student because other
objects depend on it
[例12]如果选择cascade时可以删除表,视图也自动被删除
drop table student cascade;
--notice: drop cascades to view is_student
select * from is_student;
--error: relation " is_student " does not exist
3.3.3 索引的建立与删除
建立索引的目的:加快查询速度
谁可以建立索引
dba 或 表的属主(即建立表的人)
dbms一般会自动建立以下列上的索引
primary key
unique
谁 维护索引
dbms自动完成
使用索引
dbms自动选择是否使用索引以及使用哪些索引
语句格式
create [unique] [cluster] index <索引名>
on <表名>(<列名>[<次序>][,<列名>[<次序>] ]…);
[例13] create cluster index stusname
on student(sname);
在student表的sname(姓名)列上建立一个聚簇索引
在最经常查询的列上建立聚簇索引以提高查询效率
一个基本表上最多只能建立一个聚簇索引
经常更新的列不宜建立聚簇索引
[例14]为学生-课程数据库中的student,course,sc三个表建立索引。
create unique index stusno on student(sno);
create unique index coucno on course(cno);
create unique index scno on sc(sno asc,cno desc);
student表按学号升序建唯一索引
course表按课程号升序建唯一索引
sc表按学号升序和课程号降序建唯一索引
删除索引
drop index <索引名>;
[例15] 删除student表的stusname索引
drop index stusname;
3.4 数据查询
语句格式
select [all|distinct] <目标列表达式>
[,<目标列表达式>] …
from <表名或视图名>[, <表名或视图名> ] …
[ where <条件表达式> ]
[ group by <列名1> [ having <条件表达式> ] ]
[ order by <列名2> [ asc|desc ] ];
select子句的<目标列表达式>可以为:算术表达式、字符串常量、函数、列别名
3.4.1 单表查询
查询仅涉及一个表:
一、选择表中的若干列
查询指定列
[例1] 查询全体学生的学号与姓名。
select sno,sname
from student;
[例2] 查询全体学生的姓名、学号、所在系。
select sname,sno,sdept
from student;
选出所有属性列
在select关键字后面列出所有列名
将<目标列表达式>指定为 *
[例3] 查询全体学生的详细记录。
select sno,sname,ssex,sage,sdept
from student;
或
select *
from student;
[例4] 查全体学生的姓名及其出生年份。
select sname,2004-sage /*假定当年的年份为2004年*/
from student
输出结果:
sname 2004-sage
李勇 1984
刘晨 1985
王敏 1986
张立 1985
[例5] 查询全体学生的姓名、出生年份和所有系,要求用小写字母表示所有系名
select sname,‘year of birth: ',2004-sage,
islower(sdept)
from student;
输出结果:
sname 'year of birth:' 2004-sage islower(sdept)
李勇 year of birth: 1984 cs
刘晨 year of birth: 1985 is
王敏 year of birth: 1986 ma
张立 year of birth: 1985 is
使用列别名改变查询结果的列标题:
select sname name,'year of birth: ’ birth,
2000-sage birthday,lower(sdept) department
from student;
输出结果:
name birth birthday department
------- ---------------- ------------- ------------------
李勇 year of birth: 1984 cs
刘晨 year of birth: 1985 is
王敏 year of birth: 1986 ma
张立 year of birth: 1985 is
二、选择表中的若干元组
1. 消除取值重复的行
如果没有指定distinct关键词,则缺省为all
[例6] 查询选修了课程的学生学号。
select sno from sc;
等价于:
select all sno from sc;
执行上面的select语句后,结果为:
sno
200215121
200215121
200215121
200215122
200215122
指定distinct关键词,去掉表中重复的行
select distinct sno
from sc;
执行结果:
sno
200215121
200215122
2.查询满足条件的元组
(1) 比较大小
[例7] 查询计算机科学系全体学生的名单。
select sname
from student
where sdept=‘cs’;
[例8] 查询所有年龄在20岁以下的学生姓名及其年龄。
select sname,sage
from student
where sage < 20;
[例9] 查询考试成绩有不及格的学生的学号。
select distinct sno
from sc
where grade<60;
(2)确定范围
谓词: between … and …
not between … and …
[例10] 查询年龄在20~23岁(包括20岁和23岁)之间的学生的
姓名、系别和年龄
select sname,sdept,sage
from student
where sage between 20 and 23;
[例11] 查询年龄不在20~23岁之间的学生姓名、系别和年龄
select sname,sdept,sage
from student
where sage not between 20 and 23;
(3) 确定集合
谓词:in <值表>, not in <值表>
[例12]查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。
select sname,ssex
from student
where sdept in ( 'is','ma','cs' );
[例13]查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
select sname,ssex
from student
where sdept not in ( 'is','ma','cs' );
(4)字符匹配
谓词: [not] like ‘<匹配串>’ [escape ‘ <换码字符>’]
1)匹配串为固定字符
[例14] 查询学号为200215121的学生的详细情况。
select *
from student
where sno like ‘200215121';
等价于:
select *
from student
where sno = ' 200215121 ';
2) 匹配串为含通配符的字符串
[例15] 查询所有姓刘学生的姓名、学号和性别。
select sname,sno,ssex
from student
where sname like ‘刘%’;
%,任意长度,例如a%b,表示a开头b结尾的任意长度的字符串
/,代表单个字符
[例16] 查询姓"欧阳"且全名为三个汉字的学生的姓名。
select sname
from student
where sname like '欧阳__';
字符集为ascii时,汉字需要两个_,字符集为gbk时,只需要一个_
[例17] 查询名字中第2个字为"阳"字的学生的姓名和学号。
select sname,sno
from student
where sname like ‘__阳%’;
[例18] 查询所有不姓刘的学生姓名。
select sname,sno,ssex
from student
where sname not like '刘%';
3) 使用换码字符将通配符转义为普通字符
[例19] 查询db_design课程的课程号和学分。
select cno,ccredit
from course
where cname like 'db\_design' escape '\‘;
[例20] 查询以"db_"开头,且倒数第3个字符为 i的课程的详细情况。
select *
from course
where cname like 'db\_%i_ _' escape ' \ ‘;
escape '\' 表示“ \” 为换码字符
(5) 涉及空值的查询
谓词: is null 或 is not null
“is” 不能用 “=” 代替
[例21] 某些学生选修课程后没有参加考试,所以有选课记录,但没
有考试成绩。查询缺少成绩的学生的学号和相应的课程号。
select sno,cno
from sc
where grade is null
[例22] 查所有有成绩的学生学号和课程号。
select sno,cno
from sc
where grade is not null;
(6)多重条件查询
逻辑运算符:and和 or来联结多个查询条件
and的优先级高于or
可以用括号改变优先级
可用来实现多种其他谓词
[not] in
[not] between … and …
[例23] 查询计算机系年龄在20岁以下的学生姓名。
select sname
from student
where sdept= 'cs' and sage<20;
改写[例12]
[例12] 查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。
select sname,ssex
from student
where sdept in ( 'is','ma','cs' )
可改写为:
select sname,ssex
from student
where sdept= ' is ' or sdept= ' ma' or sdept= ' cs ';
三、order by子句
order by子句
可以按一个或多个属性列排序
升序:asc;降序:desc;缺省值为升序
当排序列含空值时
asc:排序列为空值的元组最后显示
desc:排序列为空值的元组最先显示
[例24] 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
select sno,grade
from sc
where cno= ' 3 '
order by grade desc;
[例25] 查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
select *
from student
order by sdept,sage desc;
四、聚集函数
聚集函数:
计数
count([distinct|all] *)
count([distinct|all] <列名>)
计算总和
sum([distinct|all] <列名>)
计算平均值
avg([distinct|all] <列名>)
最大最小值
max([distinct|all] <列名>)
min([distinct|all] <列名>)
[例26] 查询学生总人数。
select count(*)
from student;
[例27] 查询选修了课程的学生人数。
select count(distinct sno)
from sc;
[例28] 计算1号课程的学生平均成绩。
select avg(grade)
from sc
where cno= ' 1 ';
五、 group by子句
group by子句分组:
细化聚集函数的作用对象
未对查询结果分组,聚集函数将作用于整个查询结果
对查询结果分组后,聚集函数将分别作用于每个组
作用对象是查询的中间结果表
按指定的一列或多列值分组,值相等的为一组
[例31] 求各个课程号及相应的选课人数。
select cno,count(sno)
from sc
group by cno;
查询结果:
cno count(sno)
1 22
2 34
3 44
4 33
5 48
[例32] 查询选修了3门以上课程的学生学号。
select sno
from sc
group by sno
having count(*) >3;
having短语与where子句的区别:
作用对象不同
where子句作用于基表或视图,从中选择满足条件的元组
having短语作用于组,从中选择满足条件的组。
3.4.2 连接查询
连接查询:同时涉及多个表的查询
连接条件或连接谓词:用来连接两个表的条件
一般格式:
[<表名1>.]<列名1> <比较运算符> [<表名2>.]<列名2>
[<表名1>.]<列名1> between [<表名2>.]<列名2> and [<表名2>.]<列名3>
连接字段:连接谓词中的列名称
连接条件中的各连接字段类型必须是可比的,但名字不必是相同的
嵌套循环法(nested-loop)
首先在表1中找到第一个元组,然后从头开始扫描表2,逐一查找满足连接件的元组,找到后就将表1中的第一个元组与该元组拼接起来,形成结果表中一个元组。
表2全部查找完后,再找表1中第二个元组,然后再从头开始扫描表2,逐一查找满足连接条件的元组,找到后就将表1中的第二个元组与该元组拼接起来,形成结果表中一个元组。
重复上述操作,直到表1中的全部元组都处理完毕。
排序合并法(sort-merge)
常用于=连接
首先按连接属性对表1和表2排序。
对表1的第一个元组,从头开始扫描表2,顺序查找满足连接条件的元组,找到后就将表1中的第一个元组与该元组拼接起来,形成结果表中一个元组。当遇到表2中第一条大于表1连接字段值的元组时,对表2的查询不再继续。
找到表1的第二条元组,然后从刚才的中断点处继续顺序扫描表2,查找满足连接条件的元组,找到后就将表1中的第一个元组与该元组拼接起来,形成结果表中一个元组。直接遇到表2中大于表1连接字段值的元组时,对表2的查询不再继续。
重复上述操作,直到表1或表2中的全部元组都处理完毕为止。
索引连接(index-join)
对表2按连接字段建立索引。
对表1中的每个元组,依次根据其连接字段值查询表2的索引,从中找到满足条件的元组,找到后就将表1中的第一个元组与该元组拼接起来,形成结果表中一个元组。
一、等值与非等值连接查询
等值连接:连接运算符为=
[例33] 查询每个学生及其选修课程的情况
select student.*,sc.*
from student,sc
where student.sno = sc.sno;
自然连接:
[例34] 对[例33]用自然连接完成。
select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno = sc.sno;
二、自身连接
自身连接:一个表与其自己进行连接
需要给表起别名以示区别
由于所有属性名都是同名属性,因此必须使用别名前缀
[例35]查询每一门课的间接先修课(即先修课的先修课)
select first.cno,second.cpno
from course first,course second
where first.cpno = second.cno;
三、外连接
外连接与普通连接的区别
普通连接操作只输出满足连接条件的元组
外连接操作以指定表为连接主体,将主体表中不满足连接条件的元组一并输出
[例 36] 改写[例33]
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left out join sc on (student.sno=sc.sno);
左外连接
列出左边关系(如本例student)中所有的元组
右外连接
列出右边关系中所有的元组
四、复合条件连接
复合条件连接:where子句中含多个连接条件
[例37]查询选修2号课程且成绩在90分以上的所有学生
select student.sno, sname
from student, sc
where student.sno = sc.sno and
/* 连接谓词*/
sc.cno= ‘2’ and sc.grade > 90;
/* 其他限定条件 */
[例38]查询每个学生的学号、姓名、选修的课程名及成绩
select student.sno,sname,cname,grade
from student,sc,course /*多表连接*/
where student.sno = sc.sno
and sc.cno = course.cno;
3.4.3 嵌套查询
嵌套查询概述
一个select-from-where语句称为一个查询块
将一个查询块嵌套在另一个查询块的where子句或having短语的条件中的查询称为嵌套查询
select sname /*外层查询/父查询*/
from student
where sno in
(select sno /*内层查询/子查询*/
from sc
where cno= ' 2 ');
子查询的限制
不能使用order by子句, order by子句只能对最终查询结果排序
层层嵌套方式反映了 sql语言的结构化(多个简单查询构成复杂查询,从而增强sql的查询能力)
有些嵌套查询可以用连接运算替代
不相关子查询:
子查询的查询条件不依赖于父查询
由里向外逐层处理。即每个子查询在上一级查询处理之前求解,子查询的结果用于建立其父查询的查找条件。
相关子查询:子查询的查询条件依赖于父查询
首先取外层查询中表的第一个元组,根据它与内层查询相关的属性值处理内层查询,若where子句返回值为真,则取此元组放入结果表。
然后再取外层表的下一个元组。
重复这一过程,直至外层表全部检查完为止。
一、带有in谓词的子查询
[例39] 查询与“刘晨”在同一个系学习的学生。
此查询要求可以分步来完成
① 确定“刘晨”所在系名
select sdept
from student
where sname= ' 刘晨 ';
结果为: cs
② 查找所有在is系学习的学生。
select sno,sname,sdept
from student
where sdept= ' cs ';
结果为:
将第一步查询嵌入到第二步查询的条件中
select sno,sname,sdept
from student
where sdept in
(select sdept
from student
where sname= ‘ 刘晨 ’);
此查询为不相关子查询。
用自身连接完成[例39]查询要求
select s1.sno,s1.sname,s1.sdept
from student s1,student s2
where s1.sdept = s2.sdept and
s2.sname = '刘晨';
[例40]查询选修了课程名为“信息系统”的学生学号和姓名
select sno,sname ③ 最后在student关系中
from student 取出sno和sname
where sno in
(select sno ② 然后在sc关系中找出选
from sc 修了3号课程的学生学号
where cno in
(select cno ① 首先在course关系中找出
from course “信息系统”的课程号,为3号
where cname= ‘信息系统’
)
);
用连接查询实现[例40]
select sno,sname
from student,sc,course
where student.sno = sc.sno and
sc.cno = course.cno and
course.cname=‘信息系统’;
二、带有比较运算符的子查询
当能确切知道内层查询返回单值时,可用比较运算符(>,<,=,>=,<=,!=或< >)。
与any或all谓词配合使用
例:假设一个学生只可能在一个系学习,并且必须属于一个系,则在[例39]可以用 = 代替in :
select sno,sname,sdept
from student
where sdept =
(select sdept
from student
where sname= ‘刘晨’);
子查询一定要跟在比较符之后
错误的例子:
select sno,sname,sdept
from student
where ( select sdept
from student
where sname= ‘ 刘晨 ’ )
= sdept;
子查询一定要跟在比较符之后
错误的例子:
select sno,sname,sdept
from student
where ( select sdept
from student
where sname= ‘ 刘晨 ’ )
= sdept;
[例41]找出每个学生超过他选修课程平均成绩的课程号。
select sno, cno
from sc x
where grade >=(select avg(grade)
from sc y
where y.sno=x.sno);
x是表sc的别名,又称为元组变量,可以用来表示sc的一个元组。内层查询是求一个学生所有选修课程平均成绩的,至于是哪儿个学生的平均成绩要看参数x.sno的值,而该值是与父查询相关的,因此这类查询称为成为相关子查询。
可能的执行过程:
1. 从外层查询中取出sc的一个元组x,将元组x的sno值(200215121)传送给内层查询。
select avg(grade)
from sc y
where y.sno='200215121';
2. 执行内层查询,得到值88(近似值),用该值代替内层查询,得到外层查询:
select sno, cno
from sc x
where grade >=88;
3. 执行这个查询,得到
(200215121,1)
(200215121,3)
4.外层查询取出下一个元组重复做上述1至3步骤,直到外层的sc元组全部处理完毕。结果为:
(200215121,1)
(200215121,3)
(200215122,2)
三、带有any(some)或all谓词的子查询
谓词语义
any:任意一个值
all:所有值
需要配合使用比较运算符
> any 大于子查询结果中的某个值
> all 大于子查询结果中的所有值
< any 小于子查询结果中的某个值
< all 小于子查询结果中的所有值
>= any 大于等于子查询结果中的某个值
>= all 大于等于子查询结果中的所有值
<= any 小于等于子查询结果中的某个值
<= all 小于等于子查询结果中的所有值
= any 等于子查询结果中的某个值
=all 等于子查询结果中的所有值(通常没有实际意义)
!=(或<>)any 不等于子查询结果中的某个值
!=(或<>)all 不等于子查询结果中的任何一个值
[例42] 查询其他系中比计算机科学某一学生年龄小的学生姓名和年龄
select sname,sage
from student
where sage < any (select sage
from student
where sdept= ' cs ')
and sdept <> ‘cs ' ; /*父查询块中的条件 */
执行过程:
1.rdbms执行此查询时,首先处理子查询,找出cs系中所有学生的年龄,构成一个集合(20,19)。
2. 处理父查询,找所有不是cs系且年龄小于20 或 19的学生。
[例43] 查询其他系中比计算机科学系所有学生年龄都小的学生姓名及年龄。
方法一:用all谓词
select sname,sage
from student
where sage < all
(select sage
from student
where sdept= ' cs ')
and sdept <> ' cs ’;
方法二:用聚集函数
select sname,sage
from student
where sage <
(select min(sage)
from student
where sdept= ' cs ')
and sdept <>' cs ’;
四、 带有exists谓词的子查询
1. exists谓词
存在量词
带有exists谓词的子查询不返回任何数据,只产生逻辑真值“true”或逻辑假值“false”。
若内层查询结果非空,则外层的where子句返回真值
若内层查询结果为空,则外层的where子句返回假值
由exists引出的子查询,其目标列表达式通常都用* ,因为带exists的子查询只返回真值或假值,给出列名无实际意义
2. not exists谓词
若内层查询结果非空,则外层的where子句返回假值
若内层查询结果为空,则外层的where子句返回真值
[例44]查询所有选修了1号课程的学生姓名。
思路分析:
本查询涉及student和sc关系
在student中依次取每个元组的sno值,用此值去检查sc关系
若sc中存在这样的元组,其sno值等于此student.sno值,并且其cno= '1',则取此student.sname送入结果关系
用嵌套查询
select sname
from student
where exists
(select *
from sc
where sno=student.sno and cno= ' 1 ');
本例中子查的查询条件赖于外层父的某个属性值(在本例中是 stdent 的 sno值),因此也是相关子查询。这个相关子查询的处理过程是:首先取外层查询中(student)表的第一个元组,根据它与内层查询相关的属性值(smo 值)处理内层询 where 子返回值为真,则取外层查询中该元组的 sname 放入结果表;然后再取(studet)表的下一个元组;重复这一过程,直至外层(student)表全部检查完为止。
用连接运算
select sname
from student, sc
where student.sno=sc.sno and sc.cno= '1';
[例45] 查询没有选修1号课程的学生姓名。
select sname
from student
where not exists
(select *
from sc
where sno = student.sno and cno='1');
不同形式的查询间的替换
一些带exists或not exists谓词的子查询不能被其他形式的子查询等价替换
所有带in谓词、比较运算符、any和all谓词的子查询都能用带exists谓词的子查询 等价替换
用exists/not exists实现全称量词(难点)
sql语言中没有全称量词" (for all)
可以把带有全称量词的谓词转换为等价的带有存在量词的谓词:
例:[例39]查询与“刘晨”在同一个系学习的学生。
可以用带exists谓词的子查询替换:
select sno,sname,sdept
from student s1
where exists
(select *
from student s2
where s2.sdept = s1.sdept and
s2.sname = ‘刘晨’);
[例46] 查询选修了全部课程的学生姓名。
select sname
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno= student.sno
and cno= course.cno
)
);
用exists/not exists实现逻辑蕴函(难点)
sql语言中没有蕴函(implication)逻辑运算
可以利用谓词演算将逻辑蕴函谓词等价转换为:
[例47]查询至少选修了学生200215122选修的全部课程的学生号码。
解题思路:
用逻辑蕴函表达:查询学号为x的学生,对所有的课程y,只要200215122学生选修了课程y,则x也选修了y。
形式化表示:
用p表示谓词 “学生200215122选修了课程y”
用q表示谓词 “学生x选修了课程y”
则上述查询为:
等价变换:
变换后语义:不存在这样的课程y,学生200215122选修了y,而学生x没有选。
用not exists谓词表示:
select distinct sno
from sc scx
where not exists
(select *
from sc scy
where scy.sno = ' 200215122 ' and
not exists
(select *
from sc scz
where scz.sno=scx.sno and
scz.cno=scy.cno));
[例48] 查询计算机科学系的学生及年龄不大于19岁的学生。
方法一:
select *
from student
where sdept= 'cs'
union
select *
from student
where sage<=19;
union:将多个查询结果合并起来时,系统自动去掉重复元组。
union all:将多个查询结果合并起来时,保留重复元组
方法二:
select distinct *
from student
where sdept= 'cs' or sage<=19;
[例49] 查询选修了课程1或者选修了课程2的学生。
select sno
from sc
where cno=' 1 '
union
select sno
from sc
where cno= ' 2 ';
[例50] 查询计算机科学系的学生与年龄不大于19岁的学生的交集
select *
from student
where sdept='cs'
intersect
select *
from student
where sage<=19
[例50] 实际上就是查询计算机科学系中年龄不大于19岁的学生
select *
from student
where sdept= 'cs' and sage<=19;
[例51] 查询选修课程1的学生集合与选修课程2的学生集合的交集
select sno
from sc
where cno=' 1 '
intersect
select sno
from sc
where cno='2 ';
[例51] 实际上是查询既选修了课程1又选修了课程2 的学生
select sno
from sc
where cno=' 1 ' and sno in
(select sno
from sc
where cno=' 2 ');
[例52] 查询计算机科学系的学生与年龄不大于19岁的学生的差集。
select *
from student
where sdept='cs'
except
select *
from student
where sage <=19;
[例52]实际上是查询计算机科学系中年龄大于19岁的学生
select *
from student
where sdept= 'cs' and sage>19;
select语句的一般格式
select [all|distinct]
<目标列表达式> [别名] [ ,<目标列表达式> [别名]] …
from <表名或视图名> [别名]
[ ,<表名或视图名> [别名]] …
[where <条件表达式>]
[group by <列名1>
[having <条件表达式>]]
[order by <列名2> [asc|desc]
3.4.4 集合查询
集合操作的种类
并操作union
交操作intersect
差操作except
参加集合操作的各查询结果的列数必须相同;对应项的数据类型也必须相同
3.4.5 select语句的一般形式
select [all|distinct]
<目标列表达式> [别名] [ ,<目标列表达式> [别名]] …
from <表名或视图名> [别名]
[ ,<表名或视图名> [别名]] …
[where <条件表达式>]
[group by <列名1>
[having <条件表达式>]]
[order by <列名2> [asc|desc]
3.5 数据更新
3.5.1 插入数据
两种插入数据方式
1. 插入元组
2. 插入子查询结果
可以一次插入多个元组
语句格式
insert
into <表名> [(<属性列1>[,<属性列2 >…)]
values (<常量1> [,<常量2>] … )
功能
将新元组插入指定表中
into子句
属性列的顺序可与表定义中的顺序不一致
没有指定属性列
指定部分属性列
values子句
提供的值必须与into子句匹配
值的个数
值的类型
[例1] 将一个新学生元组(学号:200215128;姓名:陈冬;性别:男;所在系:is;年龄:18岁)插入到student表中。
insert
into student (sno,sname,ssex,sdept,sage)
values ('200215128','陈冬','男','is',18);
[例2] 将学生张成民的信息插入到student表中。
insert
into student
values (‘200215126’, ‘张成民’, ‘男’,18,'cs');
[例3] 插入一条选课记录( '200215128','1 ')。
insert
into sc(sno,cno)
values (‘ 200215128 ’,‘ 1 ’);
rdbms将在新插入记录的grade列上自动地赋空值。
或者:
insert
into sc
values (' 200215128 ',' 1 ',null);
into子句(与插入元组类似)
子查询
select子句目标列必须与into子句匹配
值的个数
值的类型
[例4] 对每一个系,求学生的平均年龄,并把结果存入数据库。
第一步:建表
create table dept_age
(sdept char(15) /* 系名*/
avg_age smallint); /*学生平均年龄*/
第二步:插入数据
insert
into dept_age(sdept,avg_age)
select sdept,avg(sage)
from student
group by sdept;
rdbms在执行插入语句时会检查所插元组是否破坏表上已定义的完整性规则
实体完整性
参照完整性
用户定义的完整性
not null约束
unique约束
值域约束
3.5.2 修改数据
语句格式
update <表名>
set <列名>=<表达式>[,<列名>=<表达式>]…
[where <条件>];
功能
修改指定表中满足where子句条件的元组
set子句
指定修改方式
要修改的列
修改后取值
where子句
指定要修改的元组
缺省表示要修改表中的所有元组
三种修改方式
1. 修改某一个元组的值
2. 修改多个元组的值
3. 带子查询的修改语句
[例5] 将学生200215121的年龄改为22岁
update student
set sage=22
where sno=' 200215121 ';
[例6] 将所有学生的年龄增加1岁
update student
set sage= sage+1;
[例7] 将计算机科学系全体学生的成绩置零。
update sc
set grade=0
where 'cs'=
(selete sdept
from student
where student.sno = sc.sno);
rdbms在执行修改语句时会检查修改操作是否破坏表上已定义的完整性规则
实体完整性
主码不允许修改
用户定义的完整性
not null约束
unique约束
值域约束
3.5.3 删除数据
语句格式
delete
from <表名>
[where <条件>];
功能
删除指定表中满足where子句条件的元组
where子句
指定要删除的元组
缺省表示要删除表中的全部元组,表的定义仍在字典中
三种删除方式
1. 删除某一个元组的值
2. 删除多个元组的值
3. 带子查询的删除语句
[例8] 删除学号为200215128的学生记录。
delete
from student
where sno= 200215128 ';
[例9] 删除所有的学生选课记录。
delete
from sc;
[例10] 删除计算机科学系所有学生的选课记录。
delete
from sc
where 'cs'=
(selete sdept
from student
where student.sno=sc.sno);
3.6 视图
视图的特点
虚表,是从一个或几个基本表(或视图)导出的表
只存放视图的定义,不存放视图对应的数据
基表中的数据发生变化,从视图中查询出的数据也随之改变
基于视图的操作
查询
删除
受限更新
定义基于该视图的新视图
3.6.1 定义视图
一、建立视图
语句格式
create view
<视图名> [(<列名> [,<列名>]…)]
as <子查询>
[with check option];
组成视图的属性列名:全部省略或全部指定
子查询不允许含有order by子句和distinct短语
with check option 表示对视图进行 update,insert 和 delete 操作时要保证更新、插人或删除的行满足视图定义中的谓词条件(即子询中的条件表达式)。
组成视图的属性列名或者全部省略或者全部指定,没有第三种选择。如果省略了视图的各个属性列名,则隐含该视图由子查询中 select 子句中的诸字段组成。但在下列三种情况下必须明确指定组成视图的所有列名:
(1)某个目标列不是单纯的属性名而是聚集函数或列表达式(2)多表连接时选出了几个同名列作为视图的字段;
(3)要在视图中为某个启用新的更合适的名字。
rdbms执行create view语句时只是把视图定义存入数据字典,并不执行其中的select语句。
在对视图查询时,按视图的定义从基本表中将数据查出。
[例1] 建立信息系学生的视图。
create view is_student
as
select sno,sname,sage
from student
where sdept= 'is';
[例2]建立信息系学生的视图,并要求进行修改和插入操作时仍需保证该视图只有信息系的学生 。
create view is_student
as
select sno,sname,sage
from student
where sdept= 'is'
with check option;
with check option子句的存在,对is_student视图的更新操作:
修改操作:自动加上sdept= 'is'的条件
删除操作:自动加上sdept= 'is'的条件
插入操作:自动检查sdept属性值是否为'is'
如果不是,则拒绝该插入操作
如果没有提供sdept属性值,则自动定义sdept为'is'
基于多个基表的视图
[例3] 建立信息系选修了1号课程的学生视图。
create view is_s1(sno,sname,grade)
as
select student.sno,sname,grade
from student,sc
where sdept= 'is' and
student.sno=sc.sno and
sc.cno= '1';
基于视图的视图
[例4] 建立信息系选修了1号课程且成绩在90分以上的学生的视图。
create view is_s2
as
select sno,sname,grade
from is_s1
where grade>=90;
带表达式的视图
[例5] 定义一个反映学生出生年份的视图。
create view bt_s(sno,sname,sbirth)
as
select sno,sname,2000-sage
from student;
分组视图
[例6] 将学生的学号及他的平均成绩定义为一个视图
假设sc表中“成绩”列grade为数字型
creat view s_g(sno,gavg)
as
select sno,avg(grade)
from sc
group by sno;
不指定属性列
[例7]将student表中所有女生记录定义为一个视图
create view f_student(f_sno,name,sex,age,dept)
as
select *
from student
where ssex=‘女’;
缺点:
修改基表student的结构后,student表与f_student视图的映象关系被破坏,导致该视图不能正确工作。
二、删除视图
语句的格式:
drop view <视图名>;
该语句从数据字典中删除指定的视图定义
如果该视图上还导出了其他视图,使用cascade级联删除语句,把该视图和由它导出的所有视图一起删除
删除基表时,由该基表导出的所有视图定义都必须显式地使用drop view语句删除
[例8] 删除视图bt_s: drop view bt_s;
删除视图is_s1:drop view is_s1;
拒绝执行
级联删除:
drop view is_s1 cascade;
3.6.2 查询视图
用户角度:查询视图与查询基本表相同
rdbms实现视图查询的方法
视图消解法(view resolution)
进行有效性检查
转换成等价的对基本表的查询
执行修正后的查询
[例9] 在信息系学生的视图中找出年龄小于20岁的学生。
select sno,sage
from is_student
where sage<20;
is_student视图的定义 (参见视图定义例1)
视图消解转换后的查询语句为:
select sno,sage
from student
where sdept= 'is' and sage<20;
[例10] 查询选修了1号课程的信息系学生
select is_student.sno,sname
from is_student,sc
where is_student.sno =sc.sno and sc.cno= '1';
视图消解法的局限
有些情况下,视图消解法不能生成正确查询,如例11。
[例11]在s_g视图中查询平均成绩在90分以上的学生学号和平均成绩
select *
from s_g
where gavg>=90;
s_g视图的子查询定义:
create view s_g (sno,gavg)
as
select sno,avg(grade)
from sc
group by sno;
错误:
select sno,avg(grade)
from sc
where avg(grade)>=90
group by sno;
正确:
select sno,avg(grade)
from sc
group by sno
having avg(grade)>=90;
3.6.3 更新视图
[例12] 将信息系学生视图is_student中学号200215122的学生姓名改为“刘辰”。
update is_student
set sname= '刘辰'
where sno= ' 200215122 ';
转换后的语句:
update student
set sname= '刘辰'
where sno= ' 200215122 ' and sdept= 'is';
[例13] 向信息系学生视图is_s中插入一个新的学生记录:200215129,赵新,20岁
insert
into is_student
values(‘200215129’,‘赵新’,20);
转换为对基本表的更新:
insert
into student(sno,sname,sage,sdept)
values(‘200215129 ','赵新',20,'is' );
[例14]删除信息系学生视图is_student中学号为200215129的记录
delete
from is_student
where sno= ' 200215129 ';
转换为对基本表的更新:
delete
from student
where sno= ' 200215129 ' and sdept= 'is';
更新视图的限制:一些视图是不可更新的,因为对这些视图的更新不能唯一地有意义地转换成对相应基本表的更新
例:视图s_g为不可更新视图。
update s_g
set gavg=90
where sno= ‘200215121’;
这个对视图的更新无法转换成对基本表sc的更新
允许对行列子集视图进行更新
对其他类型视图的更新不同系统有不同限制
3.6.4 视图的作用
1. 视图能够简化用户的操作
2. 视图使用户能以多种角度看待同一数据
3. 视图对重构数据库提供了一定程度的逻辑独立性
4. 视图能够对机密数据提供安全保护
5. 适当的利用视图可以更清晰的表达查询
发表评论