数据库原理第三章练习
数据库第三章所有例题参考答案

11级信管,保密,图档上机考试题目与参考答案3.3Simple Select Statements1.EXAMPLE 3.3.1find the aid values and names of agents that are based in New York. select aid, aname from agents where city=’New York’;2.EXAMPLE3.3.3Retrieve all pid values of parts for which orders are placed.select distinct pid from orders;3.EXAMPLE 3.3.4retrieve all customer-agent name pairs, (cname, aname), where the customer places an order through the agent.select distinct ame,agents.anamefrom customers,orders,agentswhere customers.cid=orders.cid and orders.aid=agents.aid;4.EXAMPLE 3.3.6all pairs of customers based in the same city.select c1.cid, c2.cidfrom customers c1, customers c2where c1.city = c2.city and c1.cid < c2.cid;5.EXAMPLE 3.3.7find pid values of products that have been ordered by at least twocustomers.select distinct x1.pidfrom orders x1, orders x2where x1.pid = x2.pid and x1.cid < x2.cid;6.EXAMPLE 3.3.8Get cid values of customers who order a product for which an order is also placed by agent a06.select distinct y.cidfrom orders x, orders ywhere y.pid = x,pid and x.aid = ‘a06’;3.4Subqueries7.EXAMPLE 3.4.1Get cid values of customers who place orders with agents in Duluth or Dallas.select distinct cid from orderswhere aid in (select aid from agentswhere city= ‘Duluth’ or city = ‘Dallas’)8.EXAMPLE 3.4.2to retrieve all information concerning agents based in Duluth or Dallas (very close to the Subquery in the previous example).select * from agentswhere city in (‘Duluth’, ‘Dallas’ );or select *from agentswhere city = ‘Duluth’ or city = ‘Dallas’;9.EXAMPLE 3.4.3to determine the names and discounts of all customers who place orders through agents in Duluth or Dallas.select distinct cname, discnt from customerswhere cid in (select cid from orders where aid in(select aid from agents where city in (‘Duluth’, ‘Dallas’ ))); 10.EXAMPLE 3.4.4to find the names of customers who order product p05.select distinct cname from customers, orderswhere customers.cid = orders.cid and orders.pid = ‘p05’or select disti nct cname from customers where ‘p05’ in(select pid from orders where cid = customers.cid);11.EXAMPLE 3.4.5Get the names of customers who order product p07 from agent a03. select distinct cname from customerswhere cid in (select cid from orders where pid = ‘p07’ and aid = ‘a03’) 12.EXAMPLE 3.4.6to retrieve ordno values for all orders placed by customers in Duluth through agents in New York.select ordno from orders x where exists(select * from customers c, agents awhere c.cid = x.cid and a.aid = x.aid and c.city = ‘Duluth’ anda.city=‘New York’);13.EXAMPLE 3.4.7find aid values of agents with a minimum percent commission.select aid from agents where percent = (select min(percent) from agents);14.EXAMPLE 3.4.8find all customers who have the same discount as that of any of the customers in Dallas or Boston.select cid, cname from customerswhere discnt = some (select discnt from customerswhere city = ‘Dallas’ or city = ‘Boston’);15.EXAMPLE 3.4.9Get cid values of customers with discnt smaller than those of any customers who live in Duluth.select cid from customerswhere discnt <all (select discnt from customerswhere city = ‘Duluth’);16.EXAMPLE 3.4.10Retrieve all customer names where the customer places an order through agent a05.select distinct ame from customers cwhere exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);or select distinct ame from customers c, orders xwhere c.cid = x.cid and x.ai d = ‘a05’ ;17.EXAMPLE 3.4.11Get cid values of customers who order both products p01 and p07. select distinct cid from orders xwhere pid = ‘p01’ and exsits (select * from orderswhere cid = x.cid and pid = ‘p07’);orselect distinct x.cid from orders x, orders ywhere x.pid = ‘p01’ and x.cid = y.cid and y.pid = ‘p07’;18.EXAMPLE 3.4.12Retrieve all customer names where the customer does not place an order through agent a05.select distinct ame from customers cwhere not exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);19.EXAMPLE 3.4.13retrieving all customer names where the customer does not place an order through agent a05, but using the two equivalent NOT IN and <>ALLpredicates in place of NOT EXISTS.select distinct ame from customers cwhere c.cid not in (select cid from orders where aid = ‘a05’);or select ame from customers cwhere c.cid <>all (select cid from orders where aid = ‘a05’);20.EXAMPLE 3.4.14Find cid values of customers who do not place any order through agent a03.select distinct cid from orders xwhere not exists (select * from orderswhere cid = x.cid and aid = ‘a03’);orselect cid from customers cwhere not exists (select * from orderswhere cid = c.cid and aid = ‘a03’);21.EXAMPLE 3.4.15Retrieve the city names containing customers who order product p01. select distinct city from customers where cid in(select cid from orders where pid = ‘p01’);or select distinct city from customers where cid =some(select cid from orders where pid = ‘p01’);or select distinct city from customers c where exsits(select * from orders where cid = c.cid and pid = ‘p01’);or select distinct city from customers c, orders xwhere x.cid = c.cid and x.pid = ‘p01’;or select distinct city from customers c where ‘p01’ in(select pid from orders where cid = c.cid);3.5UNION Operators and FOR ALL Conditions 22.EXAMPLE 3.5.1to create a list of cities where either a customer or an agent, or both, is based.select city from customersunion select city from agents;23.EXAMPLE 3.5.2Get the cid values of customers who place orders with all agents based in New York.select c.cid from customers cwhere not exsits(select * from agents awhere a.city = ‘New York’ and not exsits(select * from orders xwhere x.cid = c.cid and x.aid = a.aid));24.EXAMPLE 3.5.3Get the aid values of agents in New York or Duluth who place orders forall products costing more than a dollar.select aid from agents awhere (a.city = ‘New York’ or a.city = ‘Duluth’)and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = a.aid));25.EXAMPLE 3.5.4Find aid values of agents who place orders for product p01 as well as for all products costing more than a dollar.select a.aid from agents a where a.aid in(select aid from orders where pid = ‘p01’)and not exsits (select p.pid from products pwhere p.price > 1.00 and not exsits (select * from orders xwhere x.pid = p.pid and x.aid = a.aid));or select distinct y.aid from orders ywhere y.pid = ‘p01’ and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = y.aid));26.EXAMPLE 3.5.6Find pid values of products supplied to all customers in Duluth.select pid from products pwhere not exsits(select c.cid from customers cwhere c.city = ‘Duluth’and not exists(select * from orders xwhere x.pid = p.pid and x.cid = c.cid));3.7 Set Functions in SQL27.EXAMPLE 3.7.1determine the total dollar amount of all orders.select sum(dollars) as totaldollars from orders28.EXAMPLE 3.7.2To determine the total quantity of product p03 that has been ordered. select sum(qty) as TOTAL from orders where pid=’p03’29.EXAMPLE 3.7.4Get the number of cities where customers are based.select count(distinct city) from customers30.EXAMPLE 3.7.5List the cid values of alt customers who have a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)31.EXAMPLE 3.7.6Find products ordered by at least two customers.select p.pid from products pwhere 2 <=(select count(distinct cid) from orders where pid=p.pid)图档的学生的上机考查的考题到此为止___________________________________________________________ ___________________________________________________________ 信管,保密的学生上机考查还包括下面的题目32.EXAMPLE 3.7.7Add a row with specified values for columns cid, cname, and city (c007, Windix, Dallas, null)to the customers table.insert into customers(cid, cname, city)values (‘c007’, ‘Windix’, ‘Dallas’)33.EXAMPLE 3.7.9After inserting the row (c007, Windix, Dallas, null) to the customers table in Example 3.7.7, assume that we wish to find the average discount of all customers.select avg(discnt) from customers3.8 Groups of Rows in SQL34.EXAMPLE 3.8.1to calculate the total product quantity ordered of each individual product by each individual agent.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aid35.EXAMPLE 3.8.2Print out the agent name and agent identification number, and the product name and product identification number, together with the total quantity each agent supplies of that product to customers c002 and c003.select aname, a.aid, pname, p.pid, sum(qty)from orders x, products p, agents awhere x.pid = p.pid and x.aid = a.aid and x.cid in (‘c002’, ‘c003’)group by a.aid, a.aname, p.pid, p.pname36.EXAMPLE 3.8.3Print out all product and agent IDs and the total quantity ordered of the product by the agent, when this quantity exceeds 1000.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aidhaving sum(qty) > 100037.EXAMPLE 3.8.4Provide pid values of all products purchased by at least two customers. select distinct pid from ordersgroup by pidhaving count(distinct cid) >= 23.9 A Complete Description of SQL Select38.EXAMPLE 3.9.1List all customers, agents, and the dollar sales for pairs of customers and agents, and order the result from largest to smallest sales totals. Retain only those pairs for which the dollar amount is at least equal to 900.00. select ame, c.cid, a.aname, a.aid, sum(dollars) as casalesfrom customers c, orders o, agents awhere c.cid = o.cid, and a.aid = o.aidgroup by ame, c.cid, a.aname, a.aidhaving sum(o.dollars) >= 900.00order by casales desc39.EXAMPLE 3.9.2listed the cid values of all customers with a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)40.EXAMPLE 3.9.3Retrieve the maximum discount of all customers.select max(discnt) from customers;select distinct discnt from customers cwhere discnt >= all (select discnt from customers dwhere d.cid<>c.cid)41.EXAMPLE 3.9.4Retrieve all data about customers whose cname begins with the letter “A”.select * from customers where cname like ‘A%’42.EXAMPLE 3.9.5Retrieve cid values of customers whose cname does not have a third letter equal to “%”.select cid from customers where cname not like ‘__[%]’43.EXAMPLE 3.9.6Retrieve cid values of customers whose cname begins “Tip_” and has an arbitrary number of characters following.select cid from customers where cname like ‘TIP\[_]%’44.EXAMPLE 3.9.7Retrieve cid values of customers whose cname starts with the sequence “ab\”.select cid from customers where cname like ‘ab\%’3.10 Insert, Update, and Delete Statements 45.EXAMPLE 3.10.1Add a row with specified values to the orders table, setting the qty and dollars columns null.insert into orders (ordno, month, cid, aid, pid)values (1107, ‘aug’, ‘c006’, ‘a04’, ‘p01’)46.EXAMPLE 3.10.2Create a new table called swcusts of Southwestern customers, and insert into it all customers from Dallas and Austin.create table swcusts (cid char(4) not null,cname varchar(13),city varchar(20),discnt real);insert into swcustsselect * from customerswhere city in (‘Dallas’, ‘Austin’)47.EXAMPLE 3.10.3Give all agents in New York a 10% raise in the percent commission they earn on an order.update agents set percent = 1.1 * percent where city = ‘New York’48.EXAMPLE 3.10.4Give all customers who have total orders of more than $1000 a 10% increase in the discnt.update agents set percent = 1.1 * discntwhere cid in(select cid from orders group by cid having sum(dollars) > 1000) 49.EXAMPLE 3.10.6Delete all agents in New York.delete from agents where city = ‘New York’50.EXAMPLE 3.10.7Delete all agents who have total orders of less than $600.Delete from agents where aid in(select aid from ordersGroup by aidHaving sum(dollars)<600)51.EXAMPLE 3.11.2Retrieve the names of customers who order products costing $0.50. delete from agents where aid in(select aid from orders group by aid having sum(dollars)<600)(完)。
数据库原理第三章第四章课后习题答案建库建表

数据库原理第三章第四章课后习题答案建库建表张敬怡12032316习题3建表和数据录入CREAT TABLE StudentSno Char(7) NOT NULL UNIQE,Sname VarChar(20) NOT NULL,Ssex Char(2) NOT NULL,Sage Smallint NULL,Clno Char(5) NOT NULL;INSERT INTO StudentV ALUES(‘2000101’, ‘李勇’, ‘男’, ‘20’, ‘00311’), (‘2000102’, ‘刘诗晨’, ‘女’, ‘19’, ‘00311’), (‘2000103’, ‘王一鸣’, ‘男’, ‘20’, ‘00312’), (‘2000104’, ‘张婷婷’, ‘女’, ‘21’, ‘00312’), (‘2001101’, ‘李勇敏’, ‘女’, ‘19’, ‘01311’), (‘2001102’, ‘贾向东’, ‘男’,‘22’, ‘01311’), (‘2001103’, ‘陈宝玉’, ‘男’, ‘20’, ‘01311’), (‘2001104’, ‘张逸凡’, ‘男’, ‘21’, ‘01311’); CREAT TABLE CourseCno Char(1) NOT NULL UNIQE,Cname VarChar(20) NOT NULL,Credit Smallint NULL;INSERT INTO CourseV ALUES(‘1’, ‘数据库’, ‘4’, ),(‘2’, ‘离散数学’, ‘3’, ),(‘3’, ‘管理信息系统’, ‘2’, ),(‘4’, ‘操作结构’, ‘4’, ),(‘6’, ‘数据处理’, ‘2’, ),(‘7’, ‘C语言’, ‘4’, );CREAT TABLE ClassClno Char(5) NOT NULL UNIQE,Speciality VarChar(20) NOT NULL,Inyear Char(4) NOT NULL,Number Integer NULL,Monitor Char(7) NULL;INSERT INTO ClassV ALUES(‘00311’, ‘计算机软件’, ‘2000’, ‘120’, ‘2000101’),(‘00312’, ‘计算机应用’, ‘2000’, ‘140’, ‘2000103’),CREAT TABLE GradeSno Char(7) NOT NULL,Cno Char(1) NOT NULL,Gmark Numberic(4,1) NULL;INSERT INTO GradeV ALUES(‘2000101’, ‘1’, ‘92’, ),(‘2000101’, ‘3’, ‘88’, ),(‘2000101’, ‘5’, ‘86’, ),(‘2000102’, ‘1’, ‘78’, ),(‘2000102’, ‘6’, ‘55’, ),(‘2000103’, ‘3’, ‘65’, ),(‘2000103’, ‘6’, ‘78’, ),(‘2000103’, ‘5’, ‘66’, ),(‘2000104’, ‘1’, ‘54’, ),(‘2000104’, ‘6’, ‘83’, ),(‘2001101’, ‘2’, ‘70’, ),(‘2001102’, ‘2’, ‘80’, ),(‘2001102’, ‘4’, ‘90’, ),(‘2000103’, ‘1’, ‘83’, ),(‘2000103’, ‘2’, ‘76’, ),(‘2000103’, ‘4’, ‘56’, ),(‘2000103’, ‘7’, ‘88’, );习题311题1.ALTER TABLE StudentADD Nation Varchar(20) NULL;2.ALTER TABLE StudentDROP COLUMN Nation;3.INSET INTO CourseV ALUES(‘2001110’, ‘3’, ‘80’);4.UPDATA Course SETCredit=70 WHERE Cno=‘2001110’;5.DELATE FROM CourseWHERE Cno=‘2001110’;6.CREATE INDEX IX_ClassON Student(Clno ASC);7.DROP INDEX Student IX_Class;1.SELECT DISTINCT CnoFROM Grade;2.SELECT*FROM StudentWHERE Ssex=女and Clno=’01311’;3.SELECT Sname,Ssex,(2014-Sage)as birthday FROM StudentWHERE Clno=’01311’OR Clno=’01312’;4.SELECT*WHERE Sname LIKE ‘李%’;5.SELECT NUMBERFROM ClassWHERE Clno=(SELECT ClnoFROM StudentWHERE Sname=李勇);6.SELECT A VG(Gmark)AS平均成绩MAX(Gmark) AS 最高分MIN(Gmark)AS 最低分FROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统);7.SELECT Count(DISTINCT Sno)FROM Grade;8.SELECT Count(DISTINCT Sno)FROM GradeWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统));9.SELET SnameFROM StudentWHERE Sno=(SELECT SnoWHERE Sno NOT IN (SELECT DISTINCT SnoFROM Grade))AND Clno=(SELECT Clno1.SELECT*FROM StudentWHERE Sname<>李勇AND Clno=(SELECT ClnoWHERE Sname=李勇);2.SELECT*FROM StudentWHERE Sname<>李勇AND Sno IN=(SELECT DISTINCT Sno FROM GradeWHERE Cno=ANY(SELECT Cno FROM GradeWHERE Sno=(SELECT SnoFROM StudentWHERE Sname=李勇)));3.SELECT*FROM StudentWHERE Sage BETWEEN(SELECT Sage FROM StudentWHERE Sname=李勇)AND 25;4.SELECT Sno,SnameFROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统));5.SELECT SnameFROM StudentWHERE Sno<>(SELECT SnoFROM GradeWHERE Cno=1);6.SELECT SnameWHERE NOT EXISTS(SELECT*FROM CourseWHERE NOT EXISTS (SELECT*FROM GradeWHERE Student Sno=Grade.SnoAND/doc/b21760812.html,o=/doc/ b21760812.html,o));14题1.SELECT Sno,GmarkFROM GradeWHERE Cno=3ORDER BY Gmark DESC;降序2.SELECT*FROM StudentORDER BY Clno,Sage DESC;3.SELECT Cno,COUNT(Sno)AS 选课人数FROM GradeGROUP BY Cno;4.SELECT SnoFROM GradeGROUP BY SnoHA VING COUNT(Sno)>3; 找出选修了三门以上课程的学生学号15题1.UPDATA GradeSET Gmark=0WHERE Sno IN(SELECT SnoFROM StudentWHERE Clno=‘01311’);2. DELETE*FROM GradeWHERE Sno IN(SELECT SnoFROM StudentWHERE Clno=(SELECT ClnoFROM ClassWHERE Speciality=计算机软件AND Inyear=2001));3.DELETE*FROMGradeWHERE Sno IN(SELECT SnoFROM StudentWHERE Sname='李勇')UPDATE ClassSET Number=Number-1WHERE Clno=(SELECT ClnoFROM StudentWHERE Sname='李勇')UPDATE ClassSET Monitor=NULLWHEN Monitor=(SELECT SnoFROM StudentWHERE Sname='李勇')DELETE FROM StudentWHERE Sname='李勇' ;4.ALTER TABLE ClassADD Cavg_age Smallint NULLUPDATE ClassUPDATE Clno='00311' THEN (SELECT A VG(Sage) FROM Student WHERE Clno='00311'),UPDATEClno='00312' THEN(SELECT A VG(Sage) FROM Student WHERE Clno='00312'),WHEN Clno='01311' THEN (SELECT A VG(Sage) FROM Student WHERE Clno='01311');16题1.CREAT VIEW Stu_01311_1AS SELECT*FROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=1);2.CREAT VIEW Stu_01311_2AS SELECT FROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=1AND Gmark<60);3.CREAT VIEW Stu_yearAS SELECT Sno,Sname,(2014-Sage)AS Birth_yearFROM Student;4.SELECT SnameFROM VIEW Stu_yearWHERE Birth_year>1990;5.SELECT Sno,Sname,Birth_yearFROM VIEW Stu_yearWHERE Sno=(SELECT SnoFROM VIEW Stu_01311_2);习题4建表和完整性约束CREAT TABLE Student( Sno Char(7) NOT NULL UNIQE PRIMARY KEY,Sname VarChar(20) NOT NULL,Ssex Char(2) NOT NULL CHECK(Sex IN (‘男’,‘女’))Sage Smallint NULL CHECK(Age>=14 AND Age<=65),Clno Char(5) NOT NULL REFERENCES Class(Clno)ON UPDATE CASCADE );CREAT TABLE Course( Cno Char(1) NOT NULL UNIQE PRIMARY KEY,Cname VarChar(20) NOT NULL,Credit Smallint NULL CHECK(Redit IN(‘1’,‘2’,‘3’,‘4’,‘5’,‘6’)) );CREAT TABLE Class( Clno Char(5) NOT NULL UNIQE PRIMARY KEY,Speciality VarChar(20) NOT NULL,Inyear Char(4) NOT NULL,Number Integer NULL CHECK(Number>=1AND Number<=100),Monitor Char(7) NULL REFERENCES Student(Sno) );CREAT TABLE Grade( Sno Char(7) NOT NULL,Cno Char(1) NOT NULL,Gmark Numberic(4,1) NULL CHECK(Mark>=0 AND Mark<=100)PRIMARY KEY(Sno,Cno)FOREIGN KEY Sno REFERENCES Student(Sno)ON DELETE CASCADEON UPDATE CASCADEFOREIGN KEY Cno REFERENCES Course(Cno)ON DELETE CASCADEON UPDATE CASCADE );PS:王老师我笔记本里装不了SQL的软件所以只是把代码写出来了没运行过......。
数据库系统原理第三章同步练习

性。
8. 消除了非主属性对候选键局部依赖的关系模式, 9. 两个函数依赖集F和G等价的充分必要条件是
10. 消除了每一属性对候选键传递依赖的关系模
式称为 BCNF 模式
11. 一个关系模式属于 4NF ,它必定属于BCNF。
A. 互不相关的
B. 不可分解的
C. 长度可变的
D. 互相关联的
6. 假设关系模式R(A,B)属于3NF,下列说法( B )
是正确的
A. 它一定消除了插入和删除异常
B. 仍存在一定的插入和删除异常
C. 一定属于BCNF
D. A和C
7. 设有关系W(工号, 姓名, 工种, 定额), 将其规范
化到第三范式正确的答案是( C )
1NF变成了3NF
A. 局部函数依赖和传递函数依赖
B. 完全函数依赖和传递函数依赖
C. 完全函数依赖
D. 局部函数依赖
13. 下述说法正确的是( D )
A. 属于BCNF的关系模式不存在存储异常
B. 函数依赖可由属性值决定,不由语义决定
C. 超键就是候选键
D. 键是唯一能决定一个元组的属性或属性组
一、单项选择题
1. 当B属性函数依赖于A属性时,属性A与B的联
系是(B )
A. 一对多
C. 多对多
B. 多对一
C. 以上都不是
2. 关系模式R中的属性全部是主属性,则R的最高
范式必定是(B )
A. 2NF
B. 3NF
C. BCNF
D. 4NF
3. 在关系模式R(A,B,C,D)中,有函数依赖集F={
Z=U-X-Y,则 X →→Z
5. 若关系模式R已属于第一范式,且其中的每一
数据库 第三章习题参考答案

三、设计题1.(1)SELECT BAuth FROM Book, PublishWHERE Book.PNo= Publish.PNo AND BName=’操作系统’ AND PName=’高等教育出版社’(2)查找为作者“张欣”出版全部“小说”类图书的出版社的电话。
SELECT PTel FROM Book, PublishWHERE Book.PNo= Publish.PNo AND BType =’小说’ AND BAuth=’张欣’(3)查询“电子工业出版社”出版的“计算机”类图书的价格,同时输出出版社名称及图书类别。
SELECT BPrice, PName, BType FROM Book, PublishWHERE Book.PNo= Publish.PNo AND PName =’电子工业出版社’ AND BType =’计算机’(4)查找比“人民邮电出版社”出版的“高等数学”价格低的同名书的有关信息。
SELECT * FROM BookWHERE BName =’高等数学’AND BPrice<ANY(SELECT BPrice FROM Book,PublishWHERE Book.PNo= Publish.PNo AND PName =’人民邮电出版社’ AND BName =’高等数学’)AND PName <>’人民邮电出版社’(5)查找书名中有“计算机”一词的图书的书名及作者。
SELECT BName, BAuth FROM BookWHERE BName LIKE’%计算机%’(6)在“图书”表中增加“出版时间”(BDate)项,其数据类型为日期型。
ALTER TABLE BookADD BDate datetime(7)在“图书”表中以“作者”建立一个索引。
CREATE INDEX Name ON Book(BAuth) desc2.(1)建立存书表和销售表。
【精选】数据库第三章课后习题

• 14、 • (1)GRANT SELECT ON 职工,部门TO 王明 • (2) GRANT INSERT,DELETE ON 职工,部门TO
李勇
• (3) GRANT SELECT ON 职工WHEN USER() = NAME TO ALL
• (4) GRANT SELECT,UPDATE(工资) ON 职工 TO 刘星
• 7、视图的优点 • 视图能够简化用户的操作 • 视图使用户能以多种角度看待同一数据 • 视图对重构数据库提供了一定程度的逻辑
独立性; • 视图能够对机密数据提供安全保护。
• 8、所有的视图是否都可以更新?
• 不是。视图是不实际存储数据的虚表,因 此对视图的更新,最终要转换为对基本表 的更新。因为有些视图的更新不能惟一有 意义地转换成对相应基本表的更新,所以 ,并不是所有的视图都是可更新的。
SPJ TO 李天明;
• 13、 • (1)INSERT INTO SC(Sno,Cno,Grade)
VALUES("2000012", "1128", NULL); • (2)SELECT Sno,Cno
FROM SC
WHERE Grade IS NULL;
• (3)SELECT cname,grade FROM course,Sc WHERE o=o AND cname="英语"
• (1) SELECT DIST PNO,QTY FROM SPQ
• (2) SELECT DIST * FROM SPQ WHERE SNO="S1‘
• 12、 • (1)GRANT INSERT
ON TABLE S TO 张勇
数据库原理第三章练习

第三章SQL语言一、选择题:1、SQL语言是的语言,易学习。
A.过程化 B.非过程化C.格式化 D.导航式2、SQL语言是语言。
A.层次数据库 B.网络数据库C.关系数据库 D.非数据库3、SQL语言具有的功能。
2、关系规范化,数据操纵,数据控制B.数据定义,数据操纵,数据控制C.数据定义,关系规范化,数据控制D.数据定义,关系规范化,数据操纵4、SQL语言具有两种使用方式,分别称为交互式SQL和。
A.提示式SQL B.多用户SQLC.嵌入式SQL D.解释式SQL5、SQL语言中,实现数据检索的语句是。
A.SELECT B.INSERTC.UPDATE D.DELETE6、下列SQL语句中,修改表结构的是。
A.ALTER B.CREATEC.UPDATE D.DELETE7、SQL中,与“NOT IN”等价的操作符是。
A.=SOME B.<>SOMEC.=ALL D.<>ALL8、假设有三个基本表:学生表S、课程表C、学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)检索所有比“王华”年龄大的学生姓名、年龄和性别。
正确的SQL语句是。
A.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGE FROM SWHERE SN=”王华”)B.SELECT SN,AGE,SEXFROM SWHERE SN=”王华”C.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGEWHERE SN=”王华”)D.SELECT SN,AGE,SEXFROM SWHERE AGE>王华.AGE9、检索选修课程”C2”的学生中成绩最高的学生的学号。
正确的SELECT语句是。
A.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=(SELECT GRADE FROM SCWHERE C#= “C2”)B.SELECT S#FROM SCWHERE C#=”C2” AND GRADE IN(SELECT GRADE FROM SCWHERE C#= “C2”)C.SELECT S#FROM SCWHERE C#=”C2” AND GRADE NOT IN(SELECT GRADE FROM SCWHERE C#= “C2”)D.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=ALL(SELECT GRADE FROM SCWHERE C#= “C2”)10、检索学生姓名及其所选修课程的课程号和成绩。
数据库第三章部分习题答案

3.2 对于教学数据库的三个基本表S(S#,SNAME,AGE,SEX)SC(S#,C#,GRADE)C(C#,CNAME,TEACHER)试用SQL的查询语句表达下列查询:3.2.1检索年龄小于17岁的女学生的学号和姓名select s#,sname from Swhere age<17 and sex=F;3.2.2检索男生所学课程的课程号和课程名select c#,cname from Cwhere c# in (select distinct c#from SCwhere s# in (select s# from S where sex=M)) 3.2.3检索男生所学课程的任课老师的工号和姓名实用文档select t#,tname from Twhere t# in(select distinct t#from C实用文档where c# in(select distinct c#from SCwhere s# in(select s#from Swhere sex=1)));3.2.4检索至少选修两门课程的学生的学号select s#from SCgroup by s#having count(c#)>=2;3.2.5检索至少有学号为S2和S4所学的课程和课程名select c#,cnamefrom C实用文档where c# in((select c#from sc where s#='S2')intersect实用文档(select c# from sc where s#='S4') );3.2.6检索‘WANG’同学不学的课程号select c# from cexcept(select distinct c#from scwhere s# =(select s# from s where sname='WANG'));3.2.7检索全部学生都选修的课程号和课程名select c#,cnamefrom cwhere not exists(select s#from swhere c.c# not in (select c# from sc where sc.s#=s.s# ));实用文档3.2.8检索选修课程包含'LIU'老师所授课程的全部课程的学生的学号和姓名select s#,snamefrom s实用文档where not exists((select c#from cwhere t#=(select t#from twhere tname='LIU')) except(select c# from sc wheresc.s#=s.s#) );3.4 设有两个基本表R(A,B,C)和S(A,B,C),试用SQL查询语句表达下列关系代数表达式:① R∪S ② R∩S ③ R-S ④R×S ⑤πA,BπB,C(S)⑥π1,6(σ3=4(R×S)⑦π1,2,3(R S)⑧R÷πC(S)解:①(SELECT * FROM R)UNION(SELECT * FROM S);②(SELECT * FROM R)3=3实用文档INTERSECT(SELECT * FROM S);③(SELECT * FROM R)MINUS(SELECT * FROM S);④SELECT *实用文档FROM R, S;⑤SELECT R.A, R.B, S.CFROM R, SWHERE R.B=S.B;⑥SELECT R.A, S.CFROM R, SWHERE R.C=S.A;⑦SELECT R.* (R.*表示R中全部属性)FROM R, SWHERE R.C=S.C;⑧R÷πC(S)的元组表达式如下:{ t |(∃u)(∀v)(∃w)(R(u)∧S(v)∧R(w)∧w[1]=u[1] ∧w[2]=u[2] ∧w[3]=v[3] ∧t[1]=u[1] ∧t[2]=u[2])}据此,可写出SELECT语句:SELECT A, BFROM R RXWHERE NOT EXISTS实用文档( SELECT *FROM SWHERE NOT EXISTS( SELECT *FROM R RY实用文档WHERE RY.A=RX.A AND RY.B=RX.B ANDRY.C=S.C));3.6 试叙述SQL语言的关系代数特点和元组演算特点。
数据库原理与应用77页第三章习题3

完成第三章课后第2、3、4题,写相关SQL程序,提交数据库备份文件及SQL程序代码,(如不提交附件)可直接粘贴代码提交1、设有一图书馆数据库,包括三个表:图书表、读者表、借阅表。
三个表的结构如表3-3、表3-4和表3-5所示。
完成以下习题。
表3-3 图书表结构列名说明数据类型约束图书图书唯一的图书号定长字符串,长度主键号为20书名图书的书名变长字符串,长度为50空值作者图书的编著者名变长字符串,长度为30空值出版社图书的出版社变长字符串,长度为30空值单价出版社确定的图书的单价浮点型,FLOAT空值表3- 4 读者表结构列名说明数据类型约束说明读者号读者唯一编号定长字符串,长度为10主键姓名读者姓名定长字符串,长度为8非空值性别读者性别定长字符串,长度为2非空值办公电话读者办公电话定长字符串,长度为8空值部门读者所在部门变长字符串,长度为30空值表3- 5 借阅表结构列名说明数据类型约束说明读者号读者的唯一编号定长字符串,长度为10外码,引用读者表的主键图书号图书的唯一编号定长字符串,长度为20外码,引用图书表的主键借图书借定长字符非空值出日期出的日期串,长度为8,为’yymmdd’归还日期图书归还的日期定长字符串,长度为8,为’yymmdd’空值主键为:(读者号, 图书号)(1)用SQL语句创建图书馆数据库。
C读者表te database 图书馆数据库;(2)用SQL语句创建上述三个表。
use图书馆数据库Goc读者表te table图书表(图书号char (20)primary key,书名varchar (50),作者varchar (30),出版社varchar (30),单价float);use图书馆数据库goc读者表te table读者表(读者号char (10)primary key,姓名char (8)not null,性别char (2)not null,办公电话char (8),部门char(30));use图书馆数据库gocreate table借阅表(读者号char (10)not null,图书号char (20)not null,借出日期char (8)not null,归还日期char (8),foreign key (读者号)references 读者表(读者号),foreign key (图书号)references 图书表(图书号));(3)基于图书馆数据库的三个表,用SQL语言完成以下各项操作:1)给图书表增加一列“ISBN”,数据类型为CHAR(10)alter table图书表add ISBN CHAR(10);2)为刚添加的ISBN列增加缺省值约束,约束名为ISBNDEF,缺省值为’7111085949’;ALTER TABLE图书表ADD CONSTRAINT ISBNDEF DEFAULT'7111085949'FOR ISBN; 3)为读者表的’办公电话’列,添加一个CHECK约束,要求前五位’88320’,约束名为CHECKDEF。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第三章SQL语言一、选择题:1、SQL语言是的语言,易学习。
A.过程化B.非过程化C.格式化D.导航式2、SQL语言是语言。
A.层次数据库B.网络数据库C.关系数据库D.非数据库3、SQL语言具有的功能。
2、关系规范化,数据操纵,数据控制B.数据定义,数据操纵,数据控制C.数据定义,关系规范化,数据控制D.数据定义,关系规范化,数据操纵4、SQL语言具有两种使用方式,分别称为交互式SQL和。
A.提示式SQL B.多用户SQLC.嵌入式SQL D.解释式SQL5、SQL语言中,实现数据检索的语句是。
A.SELECT B.INSERTC.UPDATE D.DELETE6、下列SQL语句中,修改表结构的是。
A.ALTER B.CREATEC.UPDATE D.DELETE7、SQL中,与“NOT IN”等价的操作符是。
A.=SOME B.<>SOMEC.=ALL D.<>ALL8、假设有三个基本表:学生表S、课程表C、学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)检索所有比“王华”年龄大的学生姓名、年龄和性别。
正确的SQL语句是。
A.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGE FROM SWHERE SN=”王华”)B.SELECT SN,AGE,SEXFROM SWHERE SN=”王华”C.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGEWHERE SN=”王华”)D.SELECT SN,AGE,SEXFROM SWHERE AGE>王华.AGE9、检索选修课程”C2”的学生中成绩最高的学生的学号。
正确的SELECT语句是。
A.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=(SELECT GRADE FROM SCWHERE C#= “C2”)B.SELECT S#FROM SCWHERE C#=”C2” AND GRADE IN(SELECT GRADE FROM SCWHERE C#= “C2”)C.SELECT S#FROM SCWHERE C#=”C2” AND GRADE NOT IN(SELECT GRADE FROM SCWHERE C#= “C2”)D.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=ALL(SELECT GRADE FROM SCWHERE C#= “C2”)10、检索学生姓名及其所选修课程的课程号和成绩。
正确的SELECT语句是:。
A.SELECT S.SN,SC.C#,SC.GRADEFROM SWHERE S.S#=SC.S#B.SELECT S.SN,SC.C#,SC.GRADEFROM SCWHERE S.S#=SC.S#C.SELECT S.SN,SC.C#,SC.GRADEFROM S,SCWHERE S.S#=SC.S#D.SELECT S.SN,SC.C#,SC.GRADEFROM S,SC11、要查找选修“COMPUTER“课程的女学生姓名,将涉及到关系。
A.S B.SC,CC.S,SC D.S,C,SC12、若用如下SQL语句创建一个表student:CREATE TABLE student (NO CHAR(4)NOT NULL,NAME CHAR(8)NOT NULL,SEX CHAR(2),AGE INT)可以插入到student 表中的是。
A.(’1031’,’曾华’,男,23)B.(’1031’,’曾华’,NULL,NULL)C.(NULL,’曾华’,’男’,’23’)D.(’1031’,NULL,’男’,23)13、SQL是的缩写。
A Standard Query LanguageB Select Query LanguageC Structured Query LanguageD 以上都不是14、视图是。
A 基本表B 外视图C 概念视图D 虚拟表15、SQL语言的操作对象。
A 只能是一个集合B 可以是一个或多个集合C 不能是集合D 可以是集合或非集合16、索引的作用之一是。
A 节省存储空间B 便于管理C 加快查询速度D 建立各数据表之间的关系17、以下有关索引的叙述中正确的是。
A 索引越多,更新速度越快B 索引需要用户维护C 并置索引中列的个数不受限制D 索引可以用来提供多种存取路径18、以下有关视图查询的叙述中正确的是。
A 首先查询出视图中所包含的数据,再对进行查询B 直接对数据库存储的视图数据进行查询C 将对视图的查询转换为对相关基本表的查询D 不能对基本表和视图进行连表操作19、以下有关UNIQUE约束的叙述中不正确的是。
A UNIQUE约束中不允许出现空值B UNIQUE用于保证列或列组合的值唯一性C 可以定义多个UNIQUE约束D 系统为定义了UNIQUE约束的那些列建立唯一索引20、以下关于空值的叙述中不正确的是。
A 用=NULL查询指定列为空值的记录B 包含空值的表达式其计算结果为空值C 聚集函数通常忽略空值D 对允许空值的列排序时,包含空值的记录总是排在最前面21、部分匹配查询中有关通配符“%”的叙述中正确的是。
A“%”代表一个字符B“%”代表多个字符C“%”可以代表零个或多个字符D“%”不能与“_”同使用22、在分组检索中,要去掉不满足条件的分组,应当。
A 使用WHERE子句B 使用HA VING子句C 先使用WHERE子句,再使用HA VING子句D 先使用HA VING子句,再使用WHERE子句23、以下有关子查询的叙述中不正确的是。
A 子查询可以向其外部查询提供检索条件的条件值B 子查询可以嵌套多层C 子查询的结果是一个集合D 子查询总是先于其外部查询24、以下有关ORDER BY子句的叙述中不正确的是。
A ORDER BY 子句可以对多个列进行排序B 在SELECT语句中,ORDER BY 只能在所有其他子句之后,作为最后一个子句出现C 子查询也可以使用ORDER BY子句D 在视图中不能使用ORDER BY子句25、以下有关WHERE子句的叙述中不正确的是。
A WHERE子句中可以包含子查询B 连接条件和选择条件之间应当使用OR逻辑运算C 不包含WHERE子句的SELECT语句进行的是单纯的投影操作D 如果FROM子句中引用了N个表,则FROM子句中至少应当包括N-1个连接条件26、以下有关SELECT子句的叙述中不正确的是。
A SELECT子句中只能包含表中的列及其构成的表达式B SELECT子句规定了结果集中的列顺序C SELECT子句中可以使用别名D 如果FROM子句中引用的两个表中有同名的列,则在SELECT子句中引用它们时必须使用表名前缀加以限制。
27、以下有关聚集的叙述中不正确的是。
A 聚集将具有一个或多个公共列,并经常一起使用的相关表物理地聚集存储在一起B 聚集中相同的值只存储一次C 聚集加快了多个表连接操作速度,但对于单个表则没有什么意义D 必须首先删除所有聚集表,才能删除聚集二、填空题:1、SQL是。
2、SQL语言的数据定义功能包括、、和。
3、SELECT 命令中, 子句用于选择满足给定条件的元组,使用 子句可按指定列的值分组,同时使用 子句可提取满足条件的组。
4、在SELECT 命令中进行查询,若希望查询的结果不出现重复元组,应在SELECT 语句中使用 保留字。
5、子查询的条件依赖于父查询,这类查询称为 。
6、视图是一个虚表,它是从 导出的表。
在数据库中,只存放视图的 ,不存放视图对应的 。
7、设有如下关系表R ,S ,T :R (BH ,XM ,XB ,DWH )S (DWH ,DWM )T (BH ,XM ,XB ,DWH )① 实现R ⋃T 的SQL 语句是 。
② 实现)('100'R DWH =σ的SQL 语句是 。
③ 实现)(,R XB XM π的SQL 语句是 。
④ 实现))(('',R XB XB XM 女=σπ的SQL 语句是 。
⑤ 实现R*S 的SQL 语句是 。
⑥ 实现))*(('',,S R XB DWM XB XM 男=σπ的SQL 语句是 。
8、设有如下关系表R :R (NO ,NAME ,SEX ,AGE ,CLASS )主码是NO (学号)。
① 插入一条记录(25,‘李明’,‘男’,21,‘95031’)② 插入95031班学号为30号、姓名为‘郑和’的学生记录③ 将学号为10的学生姓名改为‘王华’④ 将所有‘95101’班号改为‘95091’⑤ 删除学号为20的学生记录⑥ 删除姓‘王’的学生记录9、宿主语言向SQL 语言提供参数是通过 ,在SQL 语句中应用时,必须在宿主变量前加 。
10、用OPEN 语句打开游标时,游标指针指向查询结果的 。
11、SQL 语言具有 和 两种使用方式。
12、SELECT 由 、 、 和 四个部分组成。
13、查询包含空值的记录时,使用比较运算符 ;查询不包含空值的记录时,使用比较运算符 。
14、建立索引的两个主要目的是 和 。
15、SQL 的数据更新功能主要包括 、 和 三个语句。
16、视图是一个虚拟表,它由 导出,数据库系统只存储视图的 ,而不存储视图的 。
17、在部分匹配查询中,通配符“%”代表,“_”代表。
18、在SQL的数据库空值功能中,授予权限使用语句,收回权限使用语句。
三、应用题:1、有两个关系:C(CNO,CN,PCNO)SC(SNO,CNO,G)其中,C为课程表,对应的属性分别是课程号、课程名、选修课号;SC为学生选课表,对应的属性分别是学号、课号和成绩。
用SQL语言写出:(1)对关系SC中课号等于C1的选择运算(2)对关系C的课号、课程名的投影运算(3)两个关系的自然连接运算2、设有如下4个关系模式:书店(书店编号,书店名,地址)图书(书号,书名,定价)图书馆(馆号,馆名,城市,电话)图书发行(馆号,书号,书店号,数量)请回答下列问题:(1)用SQL语句检索已经发行的图书中最贵和最便宜的书名和定价(2)写出下列SQL语句所表达的中文含义:SELECT 馆名FROM 图书馆WHERE 馆名IN(SELECT 馆号FROM 图书发行WHERE 书号IN(SELECT 书号FROM 图书WHERE 书名=‘数据库系统基础’)3、设有职工关系模式如下:people(pno,pname,sex,job,wage,dptno)其中,pno为职工号,pname为职工姓名,sex为性别,job为职业,wage为工资,dptno为所在部门号。