数据库第三章作业

合集下载

数据库第三章所有例题参考答案

数据库第三章所有例题参考答案

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)(完)。

数据库第三章习题

数据库第三章习题

第3章 SQL语言习题一、单项选择题1.SQL语言是()的语言,易学习。

A.过程化 B.非过程化 C.格式化 D.导航式2.SQL语言是()语言。

A.层次数据库B.网络数据库C.关系数据库D.非数据库3.SQL语言具有()的功能。

A.关系规范化、数据操纵、数据控制B数据定义、数据操纵、数据控制C.数据定义、关系规范化、数据控制D.数据定义、关系规范化、数据操纵4.关于SQL语言,下列说法正确的是()。

A 数据控制功能不是SQL语言的功能之一B SQL采用的是面向记录的操作方式,以记录为单位进行操作C SQL 是非过程化的语言,用户无须指定存取路径D SQL作为嵌入式语言语法与独立的语言有较大差别5.对表中数据进行删除的操作是()。

D.DELETE A.DROP B.ALTERC.UPDATE6.SQL语言的数据操纵语句包括SELECT,INSERT,UPDATE和DELETE等。

其中最重要的,也是使用最频繁的语句是()。

A.SELECTB.INSERTC.UPDATED.DELETE7.SQL语言具有两种使用方式,分别称为交互式SQL和()。

解释式SQL A.提示式SQL B.用户式SQL C.嵌入式SQLD.8.SQL语言中,实现数据检索的语句是()。

C.UPDATEB.INSERTD.DELETE A.SELECT9.下列SQL语句中,修改表结构的是()。

B.CREATEC.UPDATE D .DELETE A.ALTER10.在SQL中,用户可以直接操作的是()。

B 视图 D 基本表和视图C 存储文件 A 基本表11.在SQL的查询语句中,对应关系代数中“投影”运算的语句是()。

B FROM A WHEREC SELECTD HAVING12.在SELECT语句中,需对分组情况满足的条件进行判断时,应使用()。

B GROUP BYC ORDER BY A WHERED HAVING13.SQL中,与“NOT IN”等价的操作符是()。

3数据库基本操作习题与答案

3数据库基本操作习题与答案

第三章数据库基本操作一、选择题1. 如果需要给当前表增加一个字段,应使用的命令是________。

A) APPEND B) INSERTC) EDIT D) MODIFY STRU2. 设表文件及其索引已打开,为了确保指针定位在物理记录号为1的记录上,应该使用命令________。

A) SKIP 1 B) SKIP -1C) GO 1 D) GO TOP3. 要显示数据库中当前一条记录的内容,可使用命令________。

A) LIST B) BROWSEC) TYPE D) DISPLAY4. 在当前表中,查找第2个女同学的记录,应使用命令________。

A) LOCATE FOR 性别="女"B) LOCATE FOR 性别="女" NEXT 2C) LIST FOR 性别="女"CONTINUED) LOCATE FOR 性别="女"CONTINUE5. Visual FoxPro的数据库表之间可建立两种联系,它们是________。

A) 永久联系和临时联系B) 长期联系和短期联系C) 永久联系和短期联系D) 长期联系和临时联系6. 数据库表的索引中,字段值不能有重复的索引有________种。

A) 1 B) 2C) 3 D) 47. 建立表间临时关联的命令是________。

A) LET RELATION TO命令B) JOIN命令C) SET RELATION TO命令D) 以上都不是8. 通过关键字建立表间的临时关联的前提是________。

A) 父表必须索引并打开B) 子表必须索引并打开C) 两表必须索引并打开D) 两表都不必索引9. 查询设计器的“筛选”选项卡上,“插入”按钮的作用是________。

A) 用于增加查询输出字段B) 用于增加查询的表C) 用于增加查询去向D) 用于插入查询输出条件10. 在多工作区的操作中,如果选择了4,7,8号工作区并打开了相应的数据库,在命令窗口执行命令SELECT 0,其功能是________。

数据库第3章习题

数据库第3章习题

(一)选择题1.关系数据库管理系统应能实现的专门关系运算包括____。

A.排序、索引、统计B.选择、投影、连接C.关联、更新、排序D.显示、打印、制表2.在一个关系中如果有这样一个属性或属性组存在,它的值能唯一地标识关系中的每一个元组,称这个属性或属性组为____。

A.关键字B.数据项C.主属性D.主属性值3.同一个关系模型的任两个元组值____。

A.不能全同B.可全同C.必须全同D.以上都不是4.一个关系数据库文件中的各条记录____。

A.前后顺序不能任意颠倒,一定要按照输入的顺序排列B.前后顺序可以任意颠倒,不影响库中的数据关系C.前后顺序可以任意颠倒,但排列顺序不同,统计处理的结果就可能不同D.前后顺序不能任意颠倒,一定要按照关键字段值的顺序排列5.在关系代数的传统集合运算中,假定有关系R和S,运算结果为W。

如果W中的元组属于R,或者属于S,则W为____运算的结果。

如果W中的元组属于R而不属于S,则为____运算的结果。

如果W中的元组既属于R又属于S,则W为____运算的结果。

A.笛卡尔积B.并C.差D.交6.在关系代数的专门关系运算中,从表中取出满足条件的属性的操作称为____;从表中选取满足某种条件的元组的操作称为____;将两个关系中具有共同属性值的元组连接到一起构成新表的操作称为____。

A.选择B.投影C.连接D.扫描7.自然连接是构成新关系的有效方法。

一般情况下,当对关系R和S使用自然连接时,要求R和S含有一个或多个共有的____。

A.元组B.行C.记录D.属性8.等值连接与自然连接是____。

A.相同的B.不同的9.如图所示的关系R,经操作ΠA,B(σB=b(R))(Π为“投影”运算符,σ“选择”运算符)的运算结果是____。

D10.设有属性A,B,C,D,以下表示中不是关系的是____。

A.R(A)B.R(A,B,C,D)C.R(A×B×C×D)D.R(A,B)11.关系运算中花费时间可能最长的运算是____。

数据库 第三章习题参考答案

数据库 第三章习题参考答案

三、设计题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)建立存书表和销售表。

【精选】数据库第三章课后习题

【精选】数据库第三章课后习题
order by grade
• 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 张勇

数据库第三章部分习题答案

数据库第三章部分习题答案

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语言的关系代数特点和元组演算特点。

数据库作业第三章习题答案

数据库作业第三章习题答案

数据库作业第三章习题答案数据库作业第三章习题答案数据库作业是数据库课程中非常重要的一部分,通过完成作业可以帮助学生巩固和加深对数据库知识的理解和应用。

第三章习题主要涉及数据库设计和查询语言的使用。

在本篇文章中,我们将回答第三章习题,并探讨一些相关的概念和技巧。

1. 设计一个关系模式,用于存储学生的基本信息,包括学生编号、姓名、性别、年龄和专业。

请给出该关系模式的定义。

答案:学生(学生编号,姓名,性别,年龄,专业)2. 设计一个关系模式,用于存储课程的信息,包括课程编号、课程名称和学分。

请给出该关系模式的定义。

答案:课程(课程编号,课程名称,学分)3. 设计一个关系模式,用于存储学生选课的信息,包括学生编号、课程编号和成绩。

请给出该关系模式的定义。

答案:选课(学生编号,课程编号,成绩)4. 编写一个SQL查询语句,查询学生的姓名和年龄。

答案:SELECT 姓名, 年龄 FROM 学生;5. 编写一个SQL查询语句,查询选修了某门课程的学生的姓名和成绩。

答案:SELECT 学生.姓名, 选课.成绩FROM 学生, 选课WHERE 学生.学生编号 = 选课.学生编号AND 选课.课程编号 = '某门课程编号';6. 编写一个SQL查询语句,查询某个学生的选课情况,包括课程名称和成绩。

答案:SELECT 课程.课程名称, 选课.成绩FROM 课程, 选课WHERE 课程.课程编号 = 选课.课程编号AND 选课.学生编号 = '某个学生编号';通过以上习题的回答,我们可以看到数据库设计和查询语言的基本应用。

关系模式的定义是数据库设计的基础,它描述了数据表的结构和属性。

在查询语言的使用中,我们可以通过SELECT语句来检索和过滤数据,通过WHERE子句来指定查询条件。

除了上述习题的答案,我们还可以进一步探讨数据库设计的一些原则和技巧。

例如,为了提高数据库的性能和可扩展性,我们可以使用索引来加快数据的检索速度。

数据库原理及应用教程第三章作业

数据库原理及应用教程第三章作业

三、设计题1、设有以下两个数据表,各表的结果及字段名如下:图书(Book)包括书名(BNo)、类型(BType)、书名(BName)、作者(BAuth)、单价(BPrice)、出版社(PNo)出版社(Publish)包括出版社号(PNo)、出版社名称(PName)、所在城市(PCity)、电话(PTel)。

用SQL实现下述功能:(1)在“”高等教育出版社出版、书名为“操作系统”的图书的作者名;答:select BAuthfrom Book,Publishwhere Book.PNo =Publish.PNoand BName='操作系统'and PName='高等教育出版社出版'(2)查找为作者“张欣”出版全部“小说”类图书的出版社的电话;答:select PTelfrom Book,Publishwhere Book.PNo=Publish.PNoand BAuth='张欣'and BType='小说'(3)查询“电子工业出版社”出版的“计算机”类的图书的价格,同时输出版社名称及图书类别;答:select BPrice,PName,BTypefrom Book,Publishwhere Book.PNo=Publish.PNoand PName='电子工业出版社'and BType='BType'(4)查找比“人民邮电出版社”出版的“高等数学”价格低的同名书的有关信息;答:select *from Bookwhere BName='高等数学'and BPrice< ANY (select BPricefrom Book,Publishwhere PName='人民邮电出版社'and BName='高等数学'and Publish.PNo=Book.PNo )(5)查找书名中有“”计算机一词的图书的书名及作者;答:select BName,BAuthfrom Bookwhere BName like '%计算机%'(6)在“图书”表中正增加“出版时间”(BDate)项,其数据类型为日期型;答:alter table BookaddBDate datetime(7)在“图书”表中以“作者”建立一个索引。

数据库第三章习题及答案

数据库第三章习题及答案
学生关系 S(S#,SNAME,AGE,SEX) 学习关系 SC(S#,C#,GRADE) 课程关系 C(C#,CNAME) 其中 S#、C#、SNAME、AGE、SEX、GRADE、CNAME 分别表示学号、课程号、姓名、年龄、性别、成绩和课程名。 用 SQL 语句表达下列操作。 (1)检索选修课程名称为 ’MATHS’ 的学生的学号与姓名。 (2)检索至少学习了课程号为 ’C1’ 和 ’C2’ 的学生的学号。 (3)检索年龄在 18 到 20 之间(含 18 和 20)的女生的学号、姓名和年龄。 (4)检索平均成绩超过 80 分的学生学号和平均成绩。
,不存放视图的
③ 。 答案:①一个或几个基本表 。
②插入’95031’班学号为 30、姓名为’郑和’的学生记录;

③将学号为 10 的学生姓名改为’王华’;

④将所有’95101’班号改为’95091’;

⑤删除学号为 20 的学生记录;

⑥删除姓’王’的学生记录;

答案:
①INSERT INTO R VALUES(25,’李明’,’男’,21,’95031’) ②INSERT INTO R(NO,NAME,CLASS) VALUES(30,’郑和’,’95031’) ③UPDATE R SET NAME=‘王华’ WHERE NO=10 ④UPDATE R SET CLASS=’95091’WHERE CLASS=’95101’ ⑤DELETE FROM R WHERE NO=20 ⑥DELETE FROM R WHERE NAME LIKE’王%’
(4) SELECT S# ,AVG(GRADE) FROM SC GROUP BY S# HAVING AVG(GRADE)>80

数据库系统概论第三章课后作业

数据库系统概论第三章课后作业

第三章作业参考答案3.用SQL 语句建立第二章习题5中的4个表。

CREATE TABLE S(SNO CHAR(3)primary key,SNAME CHAR(10)not null,STATUS CHAR(2),CITY CHAR(10));CREATE TABLE P(PNO CHAR(3)primary key,PNAME CHAR(10),COLOR CHAR(4),WEIGHT INT);CREATE TABLE J(JNO CHAR(3)primary key,JNAME CHAR(10),CITY CHAR(10));CREATE TABLE SPJ(SNO CHAR(3),PNO CHAR(3),JNO CHAR(3),QTY INTPrimary key(sno,pno,jno));4.针对上题中建立的4个表试用SQL 语言完成第二章习题5中的查询。

(1)求供应工程J1零件的供应商号码SNO;关系代数:SELECT SNOFROM SPJWHERE JNO =‘J1’;(2)求供应工程J1零件P1的供应商号码SNO;关系代数:SELECT SNOFROM SPJWHERE JNO =‘J1’AND PNO =‘P1’;(3)求供应工程J1零件为红色的供应商号码SNO;关系代数:FROM SPJWHERE JNO =‘J1’AND PNO IN(SELECT PNOFROM PWHERE COLOR =‘红’);或者是SELECT SNOFROM SPJ,PWHERE JNO =‘J1’AND SPJ.PNO = P.PNOAND COLOR =‘红’;(4)求没有使用天津供应商生产的红色零件的工程号JNO;注意:从J表入手,以包含那些尚未使用任何零件的工程号。

关系代数:SELECT JNOFROM JWHERE NOT EXISTSFROM SPJWHERE SPJ.JNO = J.JNOAND SNO IN(SELECT SNOFROM SWHERE CITY =’天津’)AND PNO IN(SELECT PNOFROM PWHERE COLOR =’红’));或者SELECT JNOFROM JWHERE NOT EXISTS(SELECT *FROM SPJ,S,PWHERE SPJ.JNO = J.JNOAND SPJ.SNO = S.SNOAND SPJ.PNO = P.PNOAND S.CITY =‘天津’AND P.COLOR =‘红’);(5)求至少用了供应商S1所供应的全部零件的工程号JNO(类似于《概论》P113例44)。

数据库第3章习题参考答案

数据库第3章习题参考答案

第3章习题解答1.选择题(1)表设计器的“允许空”单元格用于设置该字段是否可输入空值,实际上就是创建该字段的(D)约束。

A.主键B.外键C.NULL D.CHECK (2)下列关于表的叙述正确的是(C)。

A.只要用户表没有人使用,则可将其删除B.用户表可以隐藏C.系统表可以隐藏D.系统表可以删除(3)下列关于主关键字叙述正确的是(A )。

A.一个表可以没有主关键字B.只能将一个字段定义为主关键字C.如果一个表只有一个记录,则主关键字字段可以为空值D.都正确(4)下列关于关联叙述正确的是( C )。

A.可在两个表的不同数据类型的字段间创建关联B.可在两个表的不同数据类型的同名字段间创建关联C.可在两个表的相同数据类型的不同名称的字段间创建关联D.在创建关联时选择了级联更新相关的字段,则外键表中的字段值变化时,可自动修改主键表中的关联字段(5)CREATE TABLE语句(C )。

A.必须在数据表名称中指定表所属的数据库B.必须指明数据表的所有者C.指定的所有者和表名称组合起来在数据库中必须唯一D.省略数据表名称时,则自动创建一个本地临时表(6)删除表的语句是(A)。

A.Drop B.Alter C.Update D.Delete(7)数据完整性不包括(B )。

A.实体完整性B.列完整性C.域完整性D.用户自定义完整(8)下面关于Insert语句的说法正确的是(A )。

A.Insert一次只能插入一行的元组B.Insert只能插入不能修改C.Insert可以指定要插入到哪行D.Insert可以加Where条件(9)表数据的删除语句是( A )。

A.Delete B.Inser C.Update D.Alter(10)SQL数据定义语言中,表示外键约束的关键字是(B )。

A.Check B.Foreign Key C.Primary Key D.Unique 2.填空题(1)数据通常存储在表中,表存储在数据库文件中,任何有相应权限的用户都可以对之进行操作。

数据库运维第三章自测答案

数据库运维第三章自测答案

数据库运维第三章自测答案1. ()是 SQL Server里保存所有的临时表和临时存储过程。

[单选题] *A master数据库B tempdb数据库(正确答案)C model数据库D msdb数据库2. 关于JDBC PreparedStatement,下面说法错误的是 [单选题] *A 数据库B 高级语言C OSD 数据库应用系统和开发工具(正确答案)3. 关于JDBC PreparedStatement,下面说法错误的是 [单选题] *A 可以用来进行动态查询B 通过预编译和缓存机制提升了执行的效率C 不能直接用它来执行in条件语句,但可以动态生成PreparedStatement,一样能享受PreparedStatement缓冲带来的好处(正确答案)D 有助于防止SQL注入,因为它会自动对特殊字符转义4. 在通常情况下,下面的关系中不可以作为关系数据库的关系是 [单选题] *A R1(学生号,学生名,性别)B R2(学生号,学生名,班级号)C R3(学生号,学生名,宿舍号)D R4(学生号,学生名,简历)(正确答案)5. 为数据表创建索引的目的是? [单选题] *A 提高查询的检索性能(正确答案)B 创建唯一索引C 创建主键D 归类6. 下述SQL语句中,起修改表中数据作用的命令动词是 [单选题] *A ALTERB CREATEC UPDATED INSERT(正确答案)7. SQL的数据更新不包括下列哪个命令 [单选题] *A INSERTB UPDATEC DELETED CREATE(正确答案)8. 以下不属于事务的特性的是 [单选题] *A 隔离性B 原子性C 可用性(正确答案)D 一致性E 持久性9. MODIFY STRUCTURE 命令的功能是: [单选题] *A 修改记录值A 修改记录值B 修改表结构C 修改数据库结D 修改数据库或表结构(正确答案)C 修改数据库结D 修改数据库或表结构10. ()使用户可以看见和使用的局部数据的逻辑结构和特征的描述。

数据库习题第三章 习题

数据库习题第三章 习题

CH3关系数据库标准语言SQL一、选择题1、SQL属于()数据库语言A、关系型B、网状型C、层次型D、面向对象型2、SQL中创建基本表应使用()语句A、CREATE INDEXB、CREATE TABLEC、CREATE VIEWD、CREATE DATEBASE3、SQL中创建视图应使用()语句A、CREATE SHCEMAB、CREATE TABLEC、CREATE VIEWD、CREATE DATEBASE4、关系代数中的Π运算对应SELECT语句中的()子句A、SELECTB、FROMC、WHERED、GROUP BY5、关系代数中的σ运算对应SELECT语句中的()子句A、SELECTB、FROMC、WHERED、GROUP BY6、WHERE子句的条件表达式中,可以匹配0个到多个字符的通配是()A、*B、%C、_D、?7、WHERE子句的条件表达式中,可以匹配单个字符的通配是()A、*B、%C、_D、?8、SELECT语句中与HA VING子句同时使用的是()子句A、ORDER BYB、WHEREC、GROUP BYD、无需配合9、与WHERE G BETWEEN 60 AND 100 语句等价的子句是()A、WHERE G>60 AND G<100B、WHERE G>=60 AND G<100C、WHERE G>60 AND G<=100D、WHERE G>=60 AND G<=10010、若用如下的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)11、SQL语言支持建立聚簇索引,这样可以提高查询效率,但是,并非所有属性列都适宜建立聚簇索引,下面()属性列不适宜建立聚簇索引。

数据库第3章习题解答PPT教学课件

数据库第3章习题解答PPT教学课件
不能取空值。由于已规定供应商号为主码,所以对属性 SNO的定义中的“NOT NULL”可以省略不写。
CREATE TABLE S (SNO CHAR(4) NOT NULL ,
SNAME CHAR(20) NOT NULL,
STATUS CHAR(10),
CITY CHAR(20),
PRIMARY KEY (SNO));
CREATE TABLE SPJ (SNO CHAR(4) NOT NULL,
PNO CHAR(4) NOT NULL,
JNO CHAR(4) NOT NULL,
QTY SMALLINT,
PRIMARY KEY (SNO,PNO,JNO),
FOREIGN KEY (SNO) REFERENCES S(SNO),
2)求供应工程J1零件P1的供应商号码SNO; SELECT SNO FROM SPJ WHERE JNO=‘J1’ AND PNO=‘P1’;
3)求供应工程J1零件为红色的供应商号SNO; SELECT DISTINCT SNO FROM SPJ WHERE JNO=‘J1’ AND PNO IN (SELECT PNO FROM P WHERE COLOR=‘红’);
FOREIGN KEY (PNO) REFERENCES P(PNO),
2020/12/10
FOREIGN KEY (JNO) REFERENCES J(JNO)); 9
4.针对上题中建立的四个表试用SQL语言完成第二 章习题5中的查询
1)求供应工程J1零件的供应商号码SNO;
2)求供应工程J1零件P1的供应商号码SNO;
2020/12/10
3
(1) 检索“程军”老师所授课程的课程号CNO和课程名CNAME。

(完整word版)数据库系统基础教程第三章答案

(完整word版)数据库系统基础教程第三章答案

Exercise 3.1.1Answers for this exercise may vary because of different interpretations.Some possible FDs:Social Security number → nameArea code → stateStreet address, city, state → zipcodePossible keys:{Social Security number, street address, city, state, area code, phone number}Need street address, city, state to uniquely determine location. A person couldhave multiple addresses. The same is true for phones. These days, a person couldhave a landline and a cellular phoneExercise 3.1.2Answers for this exercise may vary because of different interpretationsSome possible FDs:ID → x-position, y-position, z-positionID → x-velocity, y-velocity, z-velocityx-position, y-position, z-position → IDPossible keys:{ID}{x-position, y-position, z-position}The reason why the positions would be a key is no two molecules can occupy the same point.Exercise 3.1.3aThe superkeys are any subset that contains A1. Thus, there are 2(n-1) such subsets, since each of the n-1 attributes A2 through A n may independently be chosen in or out.Exercise 3.1.3bThe superkeys are any subset that contains A1 or A2. There are 2(n-1) such subsets when considering A1 and the n-1 attributes A2 through A n. There are 2(n-2) such subsets when considering A2 and the n-2 attributes A3 through A n. We do not count A1 in these subsets because they are already counted in the first group of subsets. The total number of subsets is 2(n-1) + 2(n-2).Exercise 3.1.3cThe superkeys are any subset that contains {A1,A2} or {A3,A4}. There are 2(n-2) such subsets when considering {A1,A2} and the n-2 attributes A3 through A n. There are 2(n-2) – 2(n-4) such subsets when considering {A3,A4} and attributes A5 through A n along with the individual attributes A1 and A2. We get the 2(n-4) term because we have to discard the subsets that contain the key {A1,A2} to avoid double counting. The total number of subsets is 2(n-2) +2(n-2) – 2(n-4).Exercise 3.1.3dThe superkeys are any subset that contains {A1,A2} or {A1,A3}. There are 2(n-2) such subsets when considering {A1,A2} and the n-2 attributes A3 through A n. There are 2(n-3) such subsets when considering {A1,A3} and the n-3 attributes A4 through A n We do not count A2 in these subsets because they are already counted in the first group of subsets. The total number of subsets is 2(n-2) + 2(n-3).Exercise 3.2.1aWe could try inference rules to deduce new dependencies until we are satisfied we have them all. A more systematic way is to consider the closures of all 15 nonempty sets of attributes.For the single attributes we have {A}+ = A, {B}+ = B, {C}+ = ACD, and {D}+ = AD. Thus, the only new dependency we get with a single attribute on the left is C→A.Now consider pairs of attributes:{AB}+ = ABCD, so we get new dependency AB→D. {AC}+ = ACD, and AC→D is nontrivial. {AD}+= AD, so nothing new. {BC}+ = ABCD, so we get BC→A, and BC→D. {BD}+ = ABCD, giving us BD→A and BD→C. {CD}+ = ACD, giving CD→A.For the triples of attributes, {ACD}+ = ACD, but the closures of the other sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, and BCD→A.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 11 new dependencies mentioned above are:C→A, AB→D, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A, ABC→D, ABD→C, and BCD→A.Exercise 3.2.1bFrom the analysis of closures above, we find that AB, BC, and BD are keys. All other sets either do not have ABCD as the closure or contain one of these three sets.Exercise 3.2.1cThe superkeys are all those that contain one of those three keys. That is, a superkeythat is not a key must contain B and more than one of A, C, and D. Thus, the (proper) superkeys are ABC, ABD, BCD, and ABCD.Exercise 3.2.2ai) For the single attributes we have {A}+ = ABCD, {B}+ = BCD, {C}+ = C, and {D}+ = D. Thus, the new dependencies are A→C and A→D.Now consider pairs of attributes:{AB}+ = ABCD, {AC}+ = ABCD, {AD}+ = ABCD, {BC}+ = BCD, {BD}+ = BCD, {CD}+ = CD. Thus the new dependencies are AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→D and BD→C.For the triples of attributes, {BCD}+ = BCD, but the closures of the other sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, and ACD→B.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 13 new dependencies mentioned above are:A→C, A→D, AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→D, BD→C, ABC→D, ABD→C and ACD→B.ii) For the single attributes we have {A}+ = A, {B}+ = B, {C}+ = C, and {D}+ = D. Thus, there are no new dependencies.Now consider pairs of attributes:{AB}+ = ABCD, {AC}+ = AC, {AD}+ = ABCD, {BC}+ = ABCD, {BD}+ = BD, {CD}+ = ABCD. Thus the new dependencies are AB→D, AD→C, BC→A and CD→B.For the triples of attributes, all the closures of the sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, ACD→B and BCD→A.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 8 new dependencies mentioned above are:AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.iii) For the single attributes we have {A}+ = ABCD, {B}+ = ABCD, {C}+ = ABCD, and {D}+ = ABCD. Thus, the new dependencies are A→C, A→D, B→D, B→A, C→A, C→B, D→B and D→C.Since all the single attributes’ closures are ABCD, any superset of the singleattributes will also lead to a closure of ABCD. Knowing this, we can enumerate the restof the new dependencies.The collection of 24 new dependencies mentioned above are:A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.Exercise 3.2.2bi) From the analysis of closures in 3.2.2a(i), we find that the only key is A. All other sets either do not have ABCD as the closure or contain A.ii) From the analysis of closures 3.2.2a(ii), we find that AB, AD, BC, and CD are keys.All other sets either do not have ABCD as the closure or contain one of these four sets.iii) From the analysis of closures 3.2.2a(iii), we find that A, B, C and D are keys. All other sets either do not have ABCD as the closure or contain one of these four sets.Exercise 3.2.2ci) The superkeys are all those sets that contain one of the keys in 3.2.2b(i). The superkeys are AB, AC, AD, ABC, ABD, ACD, BCD and ABCD.ii) The superkeys are all those sets that contain one of the keys in 3.2.2b(ii). The superkeys are ABC, ABD, ACD, BCD and ABCD.iii) The superkeys are all those sets that contain one of the keys in 3.2.2b(iii). The superkeys are AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD and ABCD.Exercise 3.2.3aSince A1A2…A n C contains A1A2…A n, then the closure of A1A2…A n C contains B. Thus it follows that A1A2…A n C→B.Exercise 3.2.3bFrom 3.2.3a, we know that A1A2…A n C→B. Using the concept of trivial dependencies, we can show that A1A2…A n C→C. Thus A1A2…A n C→BC.Exercise 3.2.3cFrom A1A2…A n E1E2…E j, we know that the closure contains B1B2…B m because of the FD A1A2…A n→B1B2…B m. The B1B2…B m and the E1E2…E j combine to form the C1C2…C k. Thus the closure ofA1A2…A n E1E2…E j contains D as well. Thus, A1A2…A n E1E2…E j→D.Exercise 3.2.3dFrom A1A2…A n C1C2…C k, we know that the closure contains B1B2…B m because of the FD A1A2…A n→B1B2…B m. The C1C2…C k also tell us that the closure of A1A2…A n C1C2…C k contains D1D2…D j. Thus, A1A2…A n C1C2…C k→B1B2…B k D1D2…D j.Exercise 3.2.4aIf attribute A represented Social Security Number and B represented a person’s name,then we would assume A→B but B→A would not be valid because there may be many peoplewith the same name and different Social Security Numbers.Exercise 3.2.4bLet attribute A represent Social Security Number, B represent gender and C represent name. Surely Social Security Number and gender can uniquely identify a person’s name (i.e.AB→C). A Social Security Number can also uniquely identify a person’s name (i.e. A→C). However, gender does not uniquely determine a name (i.e. B→C is not valid).Exercise 3.2.4cLet attribute A represent latitude and B represent longitude. Together, both attributes can uniquely determine C, a point on the world map (i.e. AB→C). However, neither A nor B can uniquely identify a point (i.e. A→C and B→C are not valid).Exercise 3.2.5Given a relation with attributes A1A2…A n, we are told that there are no functional dependencies of the form B1B2…B n-1→C where B1B2…B n-1 is n-1 of the attributes from A1A2…A n and C is the remaining attribute from A1A2…A n. In this case, the set B1B2…B n-1 and any subset do not functionally determine C. Thus the only functional dependencies that we can make are ones where C is on both the left and right hand sides. All of these functional dependencies would be trivial and thus the relation has no nontrivial FD’s.Exercise 3.2.6Let’s prove this by using the contrapositive. We wish to show that if X+ is not a subset of Y+, then it must be that X is not a subset of Y.If X+ is not a subset of Y+, there must be attributes A1A2…A n in X+ that are not in Y+. If any of these attributes were originally in X, then we are done because Y does not contain any of the A1A2…A n. However, if the A1A2…A n were added by the closure, then we must examine the case further. Assume that there was some FD C1C2…C m→A1A2…A j where A1A2…A j is some subset of A1A2…A n. It must be then that C1C2…C m or some subset of C1C2…C m is in X. However, the attributes C1C2…C m cannot be in Y because we assumed that attributes A1A2…A n are only in X+ and are not in Y+. Thus, X is not a subset of Y.By proving the contrapositive, we have also proved if X ⊆ Y, then X+⊆ Y+.Exercise 3.2.7The algorithm to find X+ is outlined on pg. 76. Using that algorithm, we can prove that (X+)+ = X+. We will do this by using a proof by contradiction.Suppose that (X+)+≠ X+. Then for (X+)+, it must be that some FD allowed additional attributes to be added to the original set X+. For example, X+→ A where A is some attribute not in X+. However, if this were the case, then X+ would not be the closure of X. The closure of X would have to include A as well. This contradicts the fact that we weregiven the closure of X, X+. Therefore, it must be that (X+)+ = X+ or else X+ is not the closure of X.Exercise 3.2.8aIf all sets of attributes are closed, then there cannot be any nontrivial functional dependencies. Suppose A1A2...A n→B is a nontrivial dependency. Then {A1A2...A n}+ contains B and thus A1A2...A n is not closed.Exercise 3.2.8bIf the only closed sets are ø and {A,B,C,D}, then the following FDs hold:A→B A→C A→DB→A B→C B→DC→A C→B C→DD→A D→B D→CAB→C AB→DAC→B AC→DAD→B AD→CBC→A BC→DBD→A BD→CCD→A CD→BABC→DABD→CACD→BBCD→AExercise 3.2.8cIf the only closed sets are ø, {A,B} and {A,B,C,D}, then the following FDs hold:A→BB→AC→A C→B C→DD→A D→B D→CAC→B AC→DAD→B AD→CBC→A BC→DBD→A BD→CCD→A CD→BABC→DABD→CACD→BBCD→AExercise 3.2.9We can think of this problem as a situation where the attributes A,B,C represent cities and the functional dependencies represent one way paths between the cities. The minimal bases are the minimal number of pathways that are needed to connect the cities. We do not want to create another roadway if the two cities are already connected.The systematic way to do this would be to check all possible sets of the pathways. However, we can simplify the situation by noting that it takes more than two pathways to visit the two other cities and come back. Also, if we find a set of pathways that is minimal, adding additional pathways will not create another minimal set.The two sets of minimal bases that were given in example 3.11 are:{A→B, B→C, C→A}{A→B, B→A, B→C, C→B}The additional sets of minimal bases are:{C→B, B→A, A→C}{A→B, A→C, B→A, C→A}{A→C, B→C, C→A, C→B}Exercise 3.2.10aWe need to compute the closures of all subsets of {ABC}, although there is no need to think about the empty set or the set of all three attributes. Here are the calculations for the remaining six sets:{A}+=A{B}+=B{C}+=ACE{AB}+=ABCDE{AC}+=ACE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: C→A and AB→C. Note that BC->A is true, but follows logically from C->A, and therefore may be omitted from our list.Exercise 3.2.10bWe need to compute the closures of all subsets of {ABC}, although there is no need to think about the empty set or the set of all three attributes. Here are the calculations for the remaining six sets:{A}+=AD{B}+=B{C}+=C{AB}+=ABDE{AC}+=ABCDE{BC}+=BCWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: AC→B.Exercise 3.2.10cWe need to compute the closures of all subsets of {ABC}, although there is no need tothink about the empty set or the set of all three attributes. Here are the calculationsfor the remaining six sets:{A}+=A{B}+=B{C}+=C{AB}+=ABD{AC}+=ABCDE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: AC→B and BC→A.Exercise 3.2.10dWe need to compute the closures of all subsets of {ABC}, although there is no need tothink about the empty set or the set of all three attributes. Here are the calculationsfor the remaining six sets:{A}+=ABCDE{B}+=ABCDE{C}+=ABCDE{AB}+=ABCDE{AC}+=ABCDE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: A→B, B→C and C→A.Exercise 3.2.11For step one of Algorithm 3.7, suppose we have the FD ABC→DE. We want to use Armstrong’s Axioms to show that ABC→D and ABC→E follow. Surely the functional dependencies DE→D and DE→E hold because they are trivial and follow the reflexivity property. Using the transitivity rule, we can derive the FD ABC→D from the FDs ABC→DE and DE→D. Likewise, we can do the same for ABC→DE and DE→E and derive the FD ABC→E.For steps two through four of Algorithm 3.7, suppose we have the initial set ofattributes of the closure as ABC. Suppose also that we have FDs C→D and D→E. Accordingto Algorithm 3.7, the closure should become ABCDE. Taking the FD C→D and augmenting both sides with attributes AB we get the FD ABC→ABD. We can use the splitting method in stepone to get the FD ABC→D. Since D is not in the closure, we can add attribute D. Taking the FD D→E and augmenting both sides with attributes ABC we get the FD ABCD→ABCDE.Using again the splitting method in step one we get the FD ABCD→E. Since E is not in the closure, we can add attribute E.Given a set of FDs, we can prove that a FD F follows by taking the closure of the left side of FD F. The steps to compute the closure in Algorithm 3.7 can be mimicked by Armstrong’s axioms and thus we can prove F from the given set of FDs using Armstrong’s axioms.Exercise 3.3.1aIn the solution to Exercise 3.2.1 we found that there are 14 nontrivial dependencies, including the three given ones and eleven derived dependencies. They are: C→A, C→D,D→A, AB→D, AB→ C, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A, ABC→D, ABD→C, and BCD→A.We also learned that the three keys were AB, BC, and BD. Thus, any dependency above that does not have one of these pairs on the left is a BCNF violation. These are: C→A, C→D,D→A, AC→D, and CD→A.One choice is to decompose using the violation C→D. Using the above FDs, we get ACD and BC as decomposed relations. BC is surely in BCNF, since any two-attribute relation is. Using Algorithm 3.12 to discover the projection of FDs on relation ACD, we discover that ACD is not in BCNF since C is its only key. However, D→A is a dependency that holds in ABCD and therefore holds in ACD. We must further decompose ACD into AD and CD. Thus, the three relations of the decomposition are BC, AD, and CD.Exercise 3.3.1bBy computing the closures of all 15 nonempty subsets of ABCD, we can find all thenontrivial FDs. They are B→C, B→D, AB→C, AB→D, BC→D, BD→C, ABC→D and ABD→C. From the closures we can also deduce that the only key is AB. Thus, any dependency above that does not contain AB on the left is a BCNF violation. These are: B→C, B→D, BC→D andBD→C.One choice is to decompose using the violation B→C. Using the above FDs, we get BCD and AB as decomposed relations. AB is surely in BCNF, since any two-attribute relation is. Using Algorithm 3.12 to discover the projection of FDs on relation BCD, we discover that BCD is in BCNF since B is its only key and the projected FDs all have B on the left side. Thus the two relations of the decomposition are AB and BCD.Exercise 3.3.1cIn the solution to Exercise 3.2.2(ii), we found that there are 12 nontrivial dependencies, including the four given ones and the eight derived ones. They are AB→C, BC→D, CD→A, AD→B, AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are AB, AD, BC, and CD. Thus, any dependency above that does not have one of these pairs on the left is a BCNF violation. However, all of the FDs contain a key on the left so there are no BCNF violations.No decomposition is necessary since all the FDs do not violate BCNF.Exercise 3.3.1dIn the solution to Exercise 3.2.2(iii), we found that there are 28 nontrivial dependencies, including the four given ones and the 24 derived ones. They are A→B, B→C, C→D, D→A, A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C, AB→D, AC→B, AC→D,AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are A,B,C,D. Thus, any dependency above that does nothave one of these attributes on the left is a BCNF violation. However, all of the FDs contain a key on the left so there are no BCNF violations.No decomposition is necessary since all the FDs do not violate BCNF.Exercise 3.3.1eBy computing the closures of all 31 nonempty subsets of ABCDE, we can find all the nontrivial FDs. They are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ABE→C, ABE→D, ADE→C, BCE→D, BDE→C, ABCE→D, and ABDE→C. From the closures we canalso deduce that the only key is ABE. Thus, any dependency above that does not contain ABE on the left is a BCNF violation. These are: AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ADE→C, BCE→D and BDE→C.One choice is to decompose using the violation AB→C. Using the above FDs, we get ABCDand ABE as decomposed relations. Using Algorithm 3.12 to discover the projection of FDs on relation ABCD, we discover that ABCD is not in BCNF since AB is its only key and the FD B→D follows for ABCD. Using violation B→D to further decompose, we get BD and ABC as decomposed relations. BD is in BCNF because it is a two-attribute relation. Using Algorithm 3.12 again, we discover that ABC is in BCNF since AB is the only key and AB→Cis the only nontrivial FD. Going back to relation ABE, following Algorithm 3.12 tells us that ABE is in BCNF because there are no keys and no nontrivial FDs. Thus the three relations of the decomposition are ABC, BD and ABE.Exercise 3.3.1fBy computing the closures of all 31 nonempty subsets of ABCDE, we can find all the nontrivial FDs. They are: C→B, C→D, C→E, D→B, D→E, AB→C, AB→D, AB→E, AC→B, AC→D, AC→E, AD→B, AD→C, AD→E, BC→D, BC→E, BD→E, CD→B, CD→E, CE→B, CE→D, DE→B,ABC→D, ABC→E, ABD→C, ABD→E, ABE→C, ABE→D, ACD→B, ACD→E, ACE→B, ACE→D, ADE→B, ADE→C, BCD→E, BCE→D, CDE→B, ABCD→E, ABCE→D, ABDE→C and ACDE→B. From the closures we can also deduce that the keys are AB, AC and AD. Thus, any dependency above that does not contain one of the above pairs on the left is a BCNF violation. These are: C→B, C→D,C →E,D →B, D →E, BC →D, BC →E, BD →E, CD →B, CD →E, CE →B, CE →D, DE →B, BCD →E, BCE →D and CDE →B.One choice is to decompose using the violation D →B. Using the above FDs, we get BDE and ABC as decomposed relations. Using Algorithm 3.12 to discover the projection of FDs on relation BDE, we discover that BDE is in BCNF since D, BD, DE are the only keys and all the projected FDs contain D, BD, or DE in the left side. Going back to relation ABC,following Algorithm 3.12 tells us that ABC is not in BCNF because since AB and AC are its only keys and the FD C →B follows for ABC. Using violation C →B to further decompose, we get BC and AC as decomposed relations. Both BC and AC are in BCNF because they are two-attribute relations. Thus the three relations of the decomposition are BDE, BC and AC.Exercise 3.3.2Yes, we will get the same result. Both A →B and A →BC have A on the left side and part ofthe process of decomposition involves finding {A}+to form one decomposed relation and Aplus the rest of the attributes not in {A}+as the second relation. Both cases yield the same decomposed relations.Exercise 3.3.3Yes, we will still get the same result. Both A →B and A →BC have A on the left side andpart of the process of decomposition involves finding {A}+to form one decomposedrelation and A plus the rest of the attributes not in {A}+as the second relation. Both cases yield the same decomposed relations.Exercise 3.3.4This is taken from Example 3.21 pg. 95.Suppose that an instance of relation R only contains two tuples.The projections of R onto the relations with schemas {A,B} and {B,C} are:If we do a natural join on the two projections, we will get:The result of the natural join is not equal to the original relation R.Exercise 3.4.1aThis is the initial tableau:→A.Since there is not an unsubscripted row, the decomposition for R is not lossless for this set of FDs.We can use the final tableau as an instance of R as an example for why the join is not lossless. The projected relations are:The joined relation is:The joined relation has three more tuples than the original tableau.Exercise 3.4.1bThis is the initial tableau:This is the final tableau after applying FDs AC→E and BC→DSince there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.1cThis is the initial tableau:This is the final tableau after applying FDs A→D, D→E and B→D.Since there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.1dThis is the initial tableau:This is the final tableau after applying FDs A→D, CD→E and E→DSince there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.2When we decompose a relation into BCNF, we will project the FDs onto the decomposed relations to get new sets of FDs. These dependencies are preserved if the union of these new sets is equivalent to the original set of FDs.For the FDs of 3.4.1a, the dependencies are not preserved. The union of the new sets of FDs is CE→A. However, the FD B→E is not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.For the FDs of 3.4.1b, the dependencies are preserved. The union of the new sets of FDs is AC→E and BC→D. This is precisely the same as the original set of FDs and thus the two sets of FDs are equivalent.For the FDs of 3.4.1c, the dependencies are not preserved. The union of the new sets of FDs is B→D and A→E. The FDs A→D and D→E are not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.For the FDs of 3.4.1d, the dependencies are not preserved. The union of the new sets of FDs is AC→E. However, the FDs A→D, CD→E and E→D are not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.Exercise 3.5.1aIn the solution to Exercise 3.3.1a we found that there are 14 nontrivial dependencies. They are: C→A, C→D, D→A, AB→D, AB→ C, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A,ABC→D, ABD→C, and BCD→A.We also learned that the three keys were AB, BC, and BD. Since all the attributes on the right sides of the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1bIn the solution to Exercise 3.3.1b we found that there are 8 nontrivial dependencies. They are B→C, B→D, AB→C, AB→D, BC→D, BD→C, ABC→D and ABD→C.We also found out that the only key is AB. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The 3NF violations are B→C, B→D, BC→D and BD→C.Using algorithm 3.26, we can decompose into relations using the minimal basis B→C andB→D. The resulting decomposed relations would be BC and BD. However, none of these two sets of attributes is a superkey. Thus we add relation AB to the result. The final set of decomposed relations is BC, BD and AB.Exercise 3.5.1cIn the solution to Exercise 3.3.1c we found that there are 12 nontrivial dependencies. They are AB→C, BC→D, CD→A, AD→B, AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are AB, AD, BC, and CD. Since all the attributes on the right sides of the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1dIn the solution to Exercise 3.3.1d we found that there are 28 nontrivial dependencies. They are A→B, B→C, C→D, D→A, A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C,AB→D, AC→B, AC→D, AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D,ABD→C, ACD→B and BCD→A.We also found out that the keys are A,B,C,D. Since all the attributes on the right sidesof the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1eIn the solution to Exercise 3.3.1e we found that there are 16 nontrivial dependencies. They are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ABE→C, ABE→D, ADE→C, BCE→D, BDE→C, ABCE→D, and ABDE→C.We also found out that the only key is ABE. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The 3NFviolations are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ADE→C, BCE→D and BDE→C.Using algorithm 3.26, we can decompose into relations using the minimal basis AB→C,DE→C and B→D. The resulting decomposed relations would be ABC, CDE and BD. However,none of these three sets of attributes is a superkey. Thus we add relation ABE to the result. The final set of decomposed relations is ABC, CDE, BD and ABE.Exercise 3.5.1fIn the solution to Exercise 3.3.1f we found that there are 41 nontrivial dependencies. They are: C→B, C→D, C→E, D→B, D→E, AB→C, AB→D, AB→E, AC→B, AC→D, AC→E, AD→B, AD→C, AD→E, BC→D, BC→E, BD→E, CD→B, CD→E, CE→B, CE→D, DE→B, ABC→D, ABC→E,ABD→C, ABD→E, ABE→C, ABE→D, ACD→B, ACD→E, ACE→B, ACE→D, ADE→B, ADE→C, BCD→E, BCE→D, CDE→B, ABCD→E, ABCE→D, ABDE→C and ACDE→B.We also found out that the keys are AB, AC and AD. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The3NF violations are C→E, D→E, BC→E, BD→E, CD→E and BCD→E.Using algorithm 3.26, we can decompose into relations using the minimal basis AB→C, C→D, D→B and D→E. The resulting decomposed relations would be ABC, CD, BD and DE. Since relation ABC contains a key, we can stop with the decomposition. The final set of decomposed relations is ABC, CD, BD and DE.Exercise 3.5.2aThe usual procedure to find the keys would be to take the closure of all 63 nonempty subsets. However, if we notice that none of the right sides of the FDs contains。

VF数据库第三章

VF数据库第三章

A)LEFT("VisuAl FoxPro",6)B)YEAR(DATE())C)V D)A){^2002-05-01 10B){^2002-05-01}-DAC){^2002-05-01}+DA D){^2002-05-01}+[1000]A)AT(‟B)CTOD(‟01C)BOF() D)SUBSTR(DTOC(DATE())A.DATE()B.TIME()C.YEAR()D.DATETIME()A.DISPLAY FIELDB.DISPLAY OFFC. DISPLAY MEMORYD.DISPLAYA.CB.LC.DD.NA.STORE 8 TO XB.STORE 8C.X=8D.X=Y=8A. B.C. D.A.NB.CC.DD.UA.2005B.05C.2006D.A."125"-"90"B."ABC"+"Def"="ABeDef"C.CTOD("99D.DTOC(DATE())>"96A.99B.99C.10D.1A."123456789"B."123456 789"C."123 456789"D."1234 56789"A.[HelloB.…HelloC."HelloD.{HelloA..NOT.B. .NOT.C. D.A.?SUBSTR(XY,B.?SUBSTR(XY10)D.?SUBSTR(XYC.?SUBSTR(XY10)A.?B.?C.?D.?A.3.141B.3.142C.3.140D.3.0A.246B.-246C.0D.A.1000B.1000.5C.1001D.1000.50A.06B.99C.20D.6A. LEFT("FoxPro"B.YEAR(DATE())D.C.TYPE("36-5*4")A-BA. .T. .T.B. .F. .F.C. .T. .F.D. .F. .T.A.1000.54B.1000.55C.1000.545D.1000.5454A."ABCD"+space(5)+"efgh"B."ABCD"+"efgh"C."ABCD"+"efgh"+space(5)D."ABCD"+"efgh"+space(1)A.1998B.98C.20D.12A.STORE 99B.STOREDTOC("99C. STORE{99D.STORE"99A.CTOD("2000B.{99C.CTOD(D.DATE()A. B.C. D.A.YEAR(DATE())B.DATE()-{12C.DATE()-100D.DTOC(DATE())-"12A.99B.05C.20D.A.3020B.50C.20D.A.1505B.20C.M05D.A. B.C. D.A.3B.1C.1D.1 A.…this…$…this B.…this…$…THISC.…thisD.…this…>…this [A] 教师批改:A BC DA BC DA BC DA BC DA) b+A B) b+RIGHT(a,1)C) b+ LEFT(a,3,4) D) b+RIGHT(a,2)A BC DA)B)C)D)A)NOT(X==y)AND(X$y) B)NOT(X$Y)OR(X C)NOT()(>=Y) D)NOT(X$Y)A. {^2003-03-01 10:10:10 AM}-100B. {^2003-03-01}-DAC. {^2003-03-01}+DAD. {^2003-03-01} +100A. N^3B. C-"A"C. N=100 AND LD. C>10A. 503. 00B. 5. 00C. 5+03D.A. 123456B. 123500C. 123456.700D. -123456.79A. FoxB. ProC. Fox ProD. FoxProA. -3B. -2C. 3D. 2A. RELEASE ALL B*B. RELEASE B*C. RELEASE ALL EXCEPT B*D. RELEASE ALL LIKE B*A. B.C. D. EMPTY(. NULL. )A. ?YEAR(DATE())B. ?LEFT(DTOC(DATE( )),4)C. ?LEFT(DTOC(DATE( ),1),4)D. ?SUBSTR(DTOC(DATE( ),1),1,4)A. B. Visual FoxProC. D.A. STORE 10 TO X,YB. STORE 10,1 TO X,YC. X=10,Y=1D. X,Y=10 [B] 教师批改:A. NOTB. NOTC. NOTD. NOTA. B.C.YBD.Y&AA.FoxB.ProC.Fox ProD.FoxProA.1810B.4C.5D.. F.。

数据库第三章课后习题解答

数据库第三章课后习题解答

3-3 习题33.4 在SQL Server中,创建一个名为students且包含有下列几个属性的表。

SNO char(10);NAME varchar(10);SEX char(1);BDATE datetime;DEPT varchar(10);DORMITORY varchar(10).要求:1.采用两种形式创建表,即用SQL语句和用图形界面的形式来创建。

2.定义必要的约束,包括主键SNO,NAME值不允许为空,且SEX取值为0或1。

【解答】·进入SQL查询分析器建立查询,创建students表的SQL语句如下,操作如图3.17所示。

use mydb /* 假设在mydb库中建表*/create table students(SNO char(10) not NULL primary key,NAME varchar(10) not NULL,SEX char(1) not NULL check(sex='0' or sex='1'),BDATE datetime,DEPT varchar(10),DORMITORY varchar(10))- 1-图3.17 用SQL语句创建students表·进入企业管理器用基本操作创建students表。

用右键单击“mydb”数据库,从弹出的菜单中选择“新建”,再从其下一级菜单中选择“表”。

或者,用右键单击“mydb”数据库下一级的“表”,从弹出的菜单中选择“新建表”。

然后,在弹出的窗体中,把students表所包含的字段逐一输入,每个字段都要指明列名、数据类型、长度和是否允许空值、是否主键等内容,如图3.18所示。

图3.18用基本操作创建students表其中,SEX字段取值为0或1,需要建立约束。

操作是用右键单击SEX字段,从弹出的菜单中选择“CHECK约束”,再从弹出的“属性”窗体中,选择“CHECK约束”卡,在约束表达式框中输入约束表达式,如图3.19所示。

数据库原理第三章课后习题答案

数据库原理第三章课后习题答案

第三章作业一、试述SQL特点SQL集数据查询、数据操纵、数据定义和数据控制功能于一体,其主要特点包括以下几部分。

1.综合统一2.高度非过程化3.面向集合的操作方式4.以同一种语法结构提供多种使用方式5.语言简洁,易学易用二、设有两个关系S(A,B,C,D)和T(C,D,E,F),写出与下列查询等价的SQL表达式(1)select A,B,S.C, S.D,E,Ffrom S,Twhere S.C=T.C(2)select * from S,Twhere S.C=T.C三、设关系RA B C10 NULL 2020 30 NULL写出查询语句SELECT * FROM R WHERE X的查询结果,其中X分别为1.1 A IS NULL;1.2 A>8 AND B<20;1.3 A>8 OR B<20;1.4 C+10>25;1.5 EXISTS (SELECT B FROM R WHERE A=10);use Rcreate table R(A tinyint primary key,B tinyint,C tinyint)1.11.21.31.41.5四、基于教材中的学生-课程数据库,用SQL完成如下查询:2.1 创建一张新表,记录每个学生的学号、选课门数和总学分数。

格式如下SCC(sno, totalCourse, totalCredit)并插入每个学生相应的数据。

create table SCC( sno char(10),totalcourse tinyint,totalcredit int)insertinto SCC(sno,totalcourse,totalcredit)select sc.sno,count(distinct o)as totalcourse,sum(ccredit)as totalcredit from sc,student,coursegroup by sc.snoselect*from SCC2.2、查询缺考和不及格课程多于3门的学生的学号和姓名select sc.sno,snamefrom student,scwhere exists(select snofrom scwhere grade<60 or grade=nullgroup by snohaving count(grade)>3)2.3 查询每个学生超过他自己选修课程平均成绩的课程号(写出3种以上类型的方法)(1)select cnofrom sc,(select sno,avg(grade)from sc group by sno)as avg_sc(avg_sno,avg_grade)where sc.sno=avg_sc.avg_sno and sc.grade>=avg_sc.avg_grade(2)select sno,cnofrom sc xwhere grade>=(select avg(grade)from sc ywhere y.sno=x.sno);2.4 查询同时选修了“数据库”和“数据结构”的学生的学号和姓名(写出5种以上类型方法)(1)select sno,snamefrom student,coursewhere cname='数据库'and sno in(select snofrom scwhere cname='数据结构')(2)select sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据库'intersectselect sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据结构';五、在上机实践过程中遇到过什么问题?解决方案是什么?。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第3章关系数据库系统RDBS一. 简答题1.对于表中几个特殊的列,如主键、候选键和外键,分别用什么限制来保证它们的完整性?对表中其它一般性的列,用什么限制来保证它们的完整性?答:主键:其值必须是唯一,不为空值;候选键:其值必须唯一,可有也只能有一个空值;外键:主键值在修改和删除时,从表中与该主键值相同的外键值可级联(CASCADE)修改和删除,或改为空值、默认值、禁止主表主键值的修改和删除;一般的列:检查约束和断言。

2.SQL SERVER中规则的目的?答:规则的目的针对表中的某一列,指明该列的取值范围。

3.SQL SERVER中在定义某些限制时,分列级与表级,其分类的原则是什么?答:列级检查约束针对表中一列;表级检查约束则针对同一表中多列4.外键限制定义的条件?答:外键限制的列必须是另一个表中的主键。

5.关系代数的基本操作符?笛卡尔乘积最大的作用是什么?答:基本操作符:投影,选择,交,积,差;作用:把任意两个不相关的表相连。

6.为什么说在实际查询中自然连接是用得比较多的?答:可以从两个关系实例的笛卡尔乘积中选出同时满足一个或多个条件等式的行,每个条件等式中的列名相同。

7.关系代数中对结果有重复元组时,如何处理?答:将去掉重复元组。

8.连接的分类?答:条件连接:加入连接条件,对两个关系实施连接;等连接:是条件连接的特例。

要求连接条件由等式组成;自然连接:是等连接的特例。

要求等式中涉及的字段名必须相等;外连接:是涉及有空值的自然连接。

9.外连接又分3种,其依据是什么?答:左外联结,右外联结,全外联结。

二. 单项选择题1. ( ③ )不是关系代数的基本操作。

①Selection ②Projection ③Join ④Intersection 2. ( ③ )用唯一限制来约束。

①主键 ②外键 ③候选键 ④简单键 3. ( ② )与“列”不同义。

①字段 ②元组 ③成员 ④属性三. 判断题(正确打√,错误打×)1. ( √ )关系代数中的改名操作既可用于改名也可用于存放临时关系模式结果。

2. ( × )对主表,插入操作可能会违背参照完整性限制,但删除和更新不会。

3. ( × )等连接是自然连接的特例.4. ( √ )关系代数是与关系模型有关的查询语言。

5. ( √ )外连接可能涉及有空值。

六. 设有如下图所示三个关系实例X 、Y 和Z ,请分别求出下列各表达式的值。

(1)σA = a1(Y ×Z )(2)Y Z (3)X Y Z答:(1){<b1,c2,a1,c1>,<b1,c2,a1,c2>,<b2,c1,a1,c1>,<b2,c1,a1,c2>,<b1,c1,a1,c1>,<b1,c1,a1,c2>,<b1,c3,a1,c1>,<b1,c3,a1,c2>} (2)C A B c1 a1 b2X A B Y B C Z A Ca1 b1 b1 c2 a1 c1 a1 b2 b2 c1 a1 c2 a2 b1 b1 c1 a2 c3 a3 b1 b1 c3 a3 c4c1 a1 b1c2 a1 b1c3 a2 b1c4 a3 null(3)A B Ca1 b1 c1a1 b1 c2a1 b2 c1a2 b1 c31.一个电影资料库有四个实体“电影”,“演员”,“导演”,“电影公司”。

“电影”的属性有电影编号,电影名,电影类型,对白语言;“演员”的属性有演员工作证号,姓名,出生年,性别;“导演”的属性有导演工作证号,姓名,出生年,性别;“电影公司”的属性有公司名称,所在国家。

这些实体间的联系及它们的属性有:演员出演电影,为多对多联系,该联系含角色属性;导演执导电影,每部电影只由一个导演执导;演员和导演属于电影公司;电影公司出品电影,有出品年份属性。

1)请画出ER图,要求标出实体的主键、联系的约束类型和键约束。

2)将此ER图转换为关系模型,要求标出各关系的主键,如果存在的话还应指明其候选键和外键。

3)请用关系代数表达式和SQL分别表达下列查询①查询1957年之前出生的男演员的姓名。

②查询2000年环球公司出品的电影的名字和导演姓名。

③查询张一导演所导演的影片中的主角演员姓名。

答:(1)电影类型(2)演员:演员工作证号为主健;导演:导演工作证号为主健;电影公司:电影公司为主健;电影:电影编号为主键,导演工作证号为外键,电影名为候选键;出演(演员工作证号,电影编号,角色)(演员工作证号,电影编号)为主健,演员工作证号和电影编号各为外键;出品:为主健,电影编号和公司名称各为外键;属于a:为主健,公司名称和演员工作证号各为外键;属于b:为主健,公司名称,导演工作证号各为外键(3)①σ出生年 < 1957(演员)∩σ性别 =男(演员)SELECT *FROM 演员WHERE出生年<1957 INTERSECTSELECT*FROM WHERE性别=男②ρ(Temp,σ出品年份 =2000(出品)∩σ公司名称=环球公司(出品))π电影名,姓名(Temp 电影导演)SELECT 电影名FROM 出品,电影WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号INTERSECTSELECT姓名FROM 出品,电影,导演WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号AND导演.导演工作证号=电影.导演工作证号③π姓名(σ姓名=张一(导演)电影出演演员)SELECCT 姓名FROM 导演,电影,出演,演员WHERE 导演.姓名=‘张一’AND电影.导演工作证号=导演.导演工作证号AND出演.电影编号=演员.工作证号2.某出版社管理系统有四个实体,即出版社(Publisher)、编辑(Editor)、作者(Author)和书籍(Book)。

“出版社”的属性有出版社编码(Pid)、出版社名称(Pname)、地址(Paddr)和电话(Ptel);“编辑”的属性有编辑工号(Eid)、姓名(Ename)、性别(Egender)、出生日期;“作者”的属性有作者编码(Aid)、姓名(Aname)、性别(Agender)、电话(Atel);“书籍”的属性有国际图书分类号(Isbn)、书名(Bname)、单价(Bprice)。

这些实体间的联系及它们的属性有:作者“主编”(ZX)书籍,为1:n联系;编辑“校对”(JD)书籍,为1:n联系;出版社“出版”(CB)书籍,为1:n联系;“出版”的属性有出版日期(Pdate)。

(1)请画出概念数据模型的E-R图,要求标注联系的约束类型和键约束。

(2)将此E-R图表示的数据模型转换为关系模型,要求标出各关系的主键。

(3)给出创建“出版”关系(表)的SQL语句(需要创建相应的主键约束和外键约束)。

(4)创建一个由地址中含有“成都市”的出版社出版的书籍的视图。

(5)请分别用关系代数表达式和SQL查询语句表达下列查询:①由出版社“XNJDP”出版的、由编辑名为“MTQ”校对的书籍的ISBN号和书名。

②由“男”性作者主编的、且由出版社“XNJDP”在2008.1.1至2008.12.31之间出版的书籍的ISBN号和书名。

③由“女”性编辑校对的、且单价在20至40元之间的书籍的ISBN号和书名。

答:(1)2)出版社Publisher (出版社编码Pid,出版社名称Pname,地址Paddr,电话Ptel, 国际图书分类号Isbn, 出版日期Pdate)编辑Editor(编辑工号Eid,姓名Ename,性别Egender,,出生日期, 国际图书分类号Isbn)作者Author(作者编码Aid,姓名Aname,性别Agender,电话Atel, 国际图书分类号Isbn)书籍Book(国际图书分类号Isbn,书名Bname,单价Bprice)。

3)CREATE TABLE Publisher( Pid char NOT NULL,Pname char NOT NULL,Paddr char NOT NULL,Ptel char NOT NULL,Isbn char NOT NULL,Pdete datetime NOT NULL,CONSTREINT Pid_Key PRIMARY KEY (Pid),CONSTRAINT Isbn_constREFERENCES Book( Isbn)ON DELETE CASCADEON UPDATE CASCADE)4) CREATE VIEW Book(Isbn, Bname,Bprice)ASSELECT Isbn, Bname,BpriceFROM Publisher,BookWHERE Publisher .Paddr=‘成都市’AND Publisher.Isbn= Book .Isbn 5)①πIsbn, Bname(σPname=XNJDP(Publisher)σEname=MTQ(Editor) Book)SELECT Isbn, BnameFROM Publisher,Book, EditorWHERE Publisher.Pname=’XNJDP’AND Editor.Ename=’MTQ’ANG Publisher.Isbn= Book .IsbnAND Editor.Isbn= Book .Isbn②πIsbn, Bname(σPname=XNJDP(Publisher)∩σPdate<2008.12.31(Publisher)∩σPdate>2008.1.1(Publisher))σAgender=‘男‘(Author) Book)SELECT Isbn, BnameFROM Publisher,Book, AuthorWHERE Publisher.Pname=’XNJDP’AND Publisher. Pdate<2008.12.31 AND Publisher.Pdate>2008.1.1 AND Author. Agender=‘男’ ANG Publisher.Isbn= Book .Isbn AND Author.Isbn= Book .Isbn③πIsbn, Bname(σEgender=‘女‘(Editor) (σBprice>20(Book)∩σBprice<40(Book)) SELECT Isbn, BnameFROM Book, EditorWHERE Book.Bprice>20 AND Book.Bprice<40 AND Editor. Egender‘女’ANG Editor.Isbn==Book .Isbn(注:文档可能无法思考全面,请浏览后下载,供参考。

相关文档
最新文档