数据库原理与应用第2版(雷景生)课后答案(第三、第四章)

第三章

选择题

1、A。
π运算符对应的是投影操作,而投影操作是对一个关系进行垂直分割,消去某些列,并重新按排列的操作。则由定义可知,例如π2,4(S)表示关系S中的第二列和第四列组成了一个新的关系,新关系的第一列为S中的第二列,新关系的第二列为S中的第四列,由此可知,π运算最初的作用就是一个选择的作用,选择出被需要的列来组成一个新的关系,故答案A正确。FROM言下之意即为“从···来”,与π运算语义不符,故答案B不正确。WHERE代表的是条件,与选择无关,故答案C不正确。GROUPE BY代表将结果按一定规则进行分组,与π运算无任何关系,故答案D不正确。

2、C。
σ运算符对应的是选择操作,而选择操作是对一个关系进行水平切割,选取符合条件的元组的操作。则由定义可知,σ运算只选取符合条件的元组,即与WHERE代表的条件相符合,故答案C正确。

3、C
当我们使用SQL Server Management Studio时可知,当我们输入一个正确的SELECT语句时,输出出来的是一个我们需要的表格,所以答案C正确。

4、C。
在课本4.5.1这节中可知,RDBMS执行CREATE VIEW语句的结果只是把视图的定义存入数据字典,并不执行其中的SELECT语句。故答案C正确。

5、C。
定义视图:SQL语言用CREATE VIEW 命令建立视图
故答案C正确。

6、B。
由书4.3.4一节可知,集合操作主要包括并操作、交操作和差操作结果表的列数必须相同,对应项的数据类型也必须相同,所以当两个子查询的结果结构完全一致时,才可以执行并、交、差操作,故答案B正确。

7、C。
HAVING必须和GROUP BY连用,一般结构为:
SELECT <目标列> FROM <表名或视图名>
GROUP BY<列名> HAVING<条件表达式>
当存在HAVING语句时,GROUP BY语句必须存在,故答案C正确。

8、B。
由4.3.1中第2项第(4)条字符匹配的查询可知,“%”代表任意长度(长度可以为0)的字符串,“-”代表任意单个字符。故答案B正确。

9、D。
已知BETWEEN···AND是闭区间,所以数据是60~100的闭区间,故答案D正确。

10、A。
删除数据的一般格式:
DELETE FROM <表名>
[WHERE <条件>]
由4.4.3可知,DELETE语句的功能是从指定表中删除满足WHERE子句条件的所有元祖。如果省略WHERE子句,表示删除表中全部元组,但表的定义仍在数据字典中,即DELETE语句删除的是表中的数据,而不是关于表的定义。故答案A正确。

综合题

1、(1)、创建客户表Customers
Create table Customers(
Cid char(6) primary key,
Cname varchar(20) not null,
City varchar(20) not null
)

(2)、创建代理人表Agents
Create table Agents(
Aid char(4) primary key,
Aname varchar(20) not null,
City varchar(20) not null
)

(3)、创建产品表Products
Create table Products(
Pid char(4) primary key,
Pname varchar(20) not null,
Quantity int,
Price float(2)
)

(4)、创建订单表Orders
Create table Orders(
Ord_no char(4) primary key,
Months smallint not null,
Cid char(5) foreign key references Customers(Cid),
Aid char(4) foreign key references Agents(Aid),
Pid char(4) foreign key references Products(Pid),
Qty int,
Amount float(2)
)

2、(1)、select c#,cname from C
where teacher=’LIU’

(2)、select s#,sname from S
where sex=’男’ and age>23

(3)、select cname,teacher from C
where c# in(
select c# from SC
where s#=’S3’
)

(4)、select sname from S
where sex=’女’ and s# not in(
select distinct s# from SC
where c# not in(
select c# from C
where teacher=’LIU’
)
}

(5)、select distinct c# from SC
where s# not in(
select s# from S
where sname=’WANG’
)

(6)、select distinct a.s# from SC as a,SC as b
where a.s#=b.s# and a.c#!=b.c#

(7)、select c#,cname from c
where not exists(select * from s where not exists(
select * from sc where s#=s.s# and c#=c.c#)

(8)、select distinct s# from sc as a
where not exists(select * from c where teacher=’LIU’
and not exists(select * from sc as b
where b.s#=a.s# and b.c#=a.c#))

3、(1)、select * from Orders
where Cid=’C006’

(2)、select distinct Cname from Customers c,Orders o
where c. Cid=o. Cid and Pid=’P01’

(3)、select Cname from Customers c,Orders o,Products p
where c. Cid=o. Cid and p. Cid=o. Cid and Price=’0.50’
Group by Cname
having SUM(Qty) >500

(4)、select distinct Cname from Customers c,Orders o
where c. Cid=o.cid and c. Cid not in(
select Cid from Orders
where Pid=’P01’
)

(5)、select distinct c. Cid, Cname,a.Aid,Aname,c.City
from Customers c,Agent a,Orders o
where c.Cid=o.Cid and a.Aid=o.Aid and c.City=a.City

(6)、select distinct Pid from Orders o,Customers c, Agents a
where o.Aid=a.Aid and c.Cid=o.Cid and c.City=’南京’ and a.City=’北京’

(7)、select distinct c.Cid from Orders o,Customers c,Agents a
where p.Pid=o.Pid and c.Cid=o.Cid and p.Pid in(
select Pid from Products
where Price=’1.00’
)

4、(1)、select * from Orders
where Qty between 500 and 800

(2)、select Pname 产品名称,Price 单价 from Products
where Pname like ‘%水%’

(3)、select * from Orders
where Cid=(
select Cid from Customers
where Cname=’王_’) and Months=’1’
Order by Qty desc

(4)、select Months 月份,count(*) 订单数,sum(Qty) 总订货数量,sum(Amount)总金额
from Orders
Group by Months
Order by Months desc

(5)、select Months from Orders
where Cid in(
select Cid from Customers
where City=’上海’
)
Group by Months
having sum(Qty)>200

(6)、select sum(Qty) 总订货数量,sum(Amount) 总金额
from Orders
where Pid=(
select Pid from Products
where Pname=’橡皮’
)

(7)、select distinct o.Cid,Cname from Orders o,Customers c
where o.Cid=c.Cid and Aid not in(
select Aid from Agents
where City=’北京’) and Pid not in (
select Pid not in (
select Pid from Products
where Pname=’笔袋’)

(8)、select Ord_no from Orders
where Qty>All(select Qty from Orders
where Months=’3’)

(9)、insert into Products
values(‘P20’,’粉笔’,’25000’,’1.50’)

(10)、update Products
set Price=Price+0.50
where Price=1.00

(11)、update Orders
set Qty=200
where Aid=(
select Aid from Agents
where City=’上海’) and Pid=(
select Pid from Products where Pname=’笔袋’)

(12)、update Orders
set Aid=’A05’
where Aid=’A06’ and Pid=’P01’ and Cid=’C006’

(13)、delete from Customers
where Cid=’C006’

(14)、delete from Orders
where Pid=(
select Pid from Products
where Pname=’尺子’) and Months=’3’

(15)、create view Agent
as
select Aid,Pid,Price
from Agents,Products

(16)、create view product
as
select Pname 产品名称,sum(Qty) 总订货数量,sun (Amount) 总金额
from Products p,Orders o
where p.Pid=o.Pid and Price>1.00
group by Pname

第四章

简答题
1、什么是存储过程?为什么要使用存储过程?

答:(1)、存储过程的定义:存储过程是存储在数据库服务器中的一组编译成单个执行计划的SQL语句。
原因:存储过程可以包含程序控制流、查询子句、操作字句,还可以接受参数、输出参数、返回单个值或多个结果集,使用存储过程有如下优点:

(2)、由于存储过程不像解释执行的SQL语句那样在提出操作请求时才进行语法分析和优化,因而运行效率高,它提供了在服务器端快速执行SQL语句的有效途径;

(3)、存储过程降低了客户机和服务器之间的通信量。客户机上的应用程序只要通过网络向服务器发出存储过程的名字和参数,就可以让RDBMS执行多条SQL语句,并执行数据处理,只将最终处理结果返回客户端;

(4)、方便企业实施规则。可以吧企业规则的运算程序写成存储过程放入数据库服务器,由RDBMS管理,既有利于集中控制,又方便维护。当用户规则发生变化时只要修改存储过程,无需修改其他应用程序。

2、试述触发器的概念和作用
答:概念:触发器是用户定义在关系表上的一类由事件驱动的特殊过程,也是一种保证数据完整性的方法。触发器也可以看做是一类特殊的存储过程,一旦定义,无须用户调用,任何对表的修改操作均由服务器自动激活相应的触发器。
作用:能够实现主键和外键所不能保证的复杂的参照完整性和数据的一致性。

3、什么是INSERTED表和DELETED表?试说明这两张表的结构。

INSERTED表:用于存储INSERT和UPDATE语句所影响的行的复本,执行INSERT和UPDATE语句时,新的数据行被添加到基本表中,同时这些数据行的备份被复制到INSERTED临时表中。
DELETED表:用于存储DELETE和UPDATE语句所影响的行的复本,执行DELETE或UPDATE语句时,行从触发器表中删除,并传输到DELETED表中,DELETED表和元数据表通常没有相同的行。
两张表的结构:(1)、这两个表都是逻辑表,并且是由系统管理的,存储在内存中,不是存储在数据库中。因此,不允许用户直接对其操作。

(2)、这两个表的结构与被该触发器作用的表有相同的表结构。它们是动态驻留在内存中,当触发器工作完成,它们也被删除。

(3)、这两个表主要保存因用户操作而被影响到的原数据值或新数据值,且是只读的,可以引用表中的数据,但不能向其写入内容。

4、什么是默认对象和默认值?他们有什么区别?

默认对象: 需要用create default语句进行定义,作为一种单独存储的数据库对象,它是独立于表的,删除表并不能删除默认对象,需要使用drop default语句删除默认对象。
默认值:是一种数据库对象,可以绑定到表的一列或多列上,也可以绑定到用户自定义的数据类型上,其作用类似于DEFAULT约束,当向表中插入数据,且没有为列输入值时,系统自动给列附一个默认值。与DEFAULT不同的是它的使用规则,通过一次定义,可以多次使用。在create table或alter table语句中定义后,被嵌入到定义的表结构中。也就是说,在删除表的时候默认约束也将随之被删除。
区别:默认值是用create table语句创建表时,使用default子句为表中的列提供默认值;
默认值对象是用create default语句来创建时,使用时须将它绑定到列上。

5、什么是规则?规则和CHECK约束有什么区别?

答:(1)、规则是数据库对存储在表中的列或用户自定义数据类型中的值的规定和限制,是单独存储的独立的数据库对象。

(2)、区别:①、CHECK约束是在使用CREATE TABLE 语句建表时指定的,而规则是作为独立于表的数据库对象,通过与指定表或数据类型绑定来实现完整性约束。

②、在一列上只能使用一个规则,但可以使用多个CHECK约束

③、规则可以应用于多个列,还可以应用于用户自定义的数据类型,而CHECK约束只能应用于它定义的列。

综合题
1、(1)、create procedure addPrice
@Pid char(4)
as
update Products
set Price=Price+0.5
where Pid=@Pid
exec addPrice @Pid=’P01’(执行过程自定义)

(2)、create procedure newRecord
@Pid char(4),@Pname varchar(20),@Quantity int,@Price real
as
insert into Products
values(@Pid,@Pname,@Quantity,@Price)
exec newRecord
@Pid=’P09’,@Pname=’笔记本’,@Quantity=’20’,@Price=’4.50’

(3)、create procedure totalQty
@Cid char(5),@Aid char(5)
as
select sum(Qty) from Orders
where Cid=@Cid and Aid=@Aid
exec totalQty @Cid=’C001’,@Aid=’A01’

2、(1)、create trigger insertNew
on Products
instead of insert
as
declare @Pid char(4),@Pname varchar(20),@Quantity int,@Price real
select @Pid=Pid,@Pname=Pname,@Quantity=Quantity,@Price=Price
from inserted
if @Price<0.50
begin
set @Price=0.50 ——0.50不用加上单引号,因为@Price为real类型
print ‘产品单价不得低于0.50元!’
end
insert into Products
values (@Pid,@Pname,@Quantity,@Price)

(2)、当订货数量发生变化时,需要用inserted表找到相应的唯一的Pid,因为修改了一个数量,所以inserted表中只有一个Pid,通过这个Pid就能找到相应的Price
create trigger changeQty
on Orders
after update
as
declare @Qty int,@Pid char(4)
select @Qty=Qty,@Pid=Pid
from inserted
begin transaction
if update(Qty)
begin
update Orders
set Amount=@Qty*(select Price from Products
where Pid=@pid)
end
commit transaction

猜你喜欢

转载自blog.csdn.net/w15977858408/article/details/89642863
今日推荐