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

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数据库基本操作习题与答案

第三章数据库基本操作一、选择题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章习题

(一)选择题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)建立存书表和销售表。
【精选】数据库第三章课后习题

• 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、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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(注:文档可能无法思考全面,请浏览后下载,供参考。