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

合集下载

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

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

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章习题参考答案

数据库第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.Unique2.填空题(1)数据通常存储在表中,表存储在数据库文件中,任何有相应权限的用户都可以对之进行操作。

数据库概论第1-3章习题参考答案

数据库概论第1-3章习题参考答案

第1章绪论习题参考答案1、试述数据、数据库、数据库管理系统、数据库系统的概念。

(参见P3、4、5页)参考答案:描述事物的符号记录称为数据;数据库是长期储存在计算机内的、有组织的、可共享的数据集合;数据库管理系统是位于用户与操作系统之间的一层数据管理软件; 数据库系统是指在计算机系统中引入数据库后的系统,一般由数据库、数据库管理系统(及其开发工具)、应用系统、数据库管理员和用户构成。

2.使用数据库系统有什么好处?(参见P12页)参考答案:数据库系统使信息系统从以加工数据的程序为中心转向围绕共享的数据库为中心的阶段,这样既便于数据的集中管理,又有利于应用程序的研制和维护,提高了数据的利用率和相容性,提高了决策的可靠性。

3.试述文件系统与数据库系统的区别和联系。

(8、9、10页)参考答案:1)数据结构化是数据库与文件系统的根本区别。

在文件系统中,相互独立的文件的记录内部是有结构的,管其记录内部已有了某些结构,但记录之间没有联系。

数据库系统实现整体数据的结构化,是数据库的主要特征之一。

2)在文件系统中,数据的最小存取单位是记录,粒度不能细到数据项。

而在数据库系统中,存取数据的方式也很灵活,可以存取数据库中的某一个数据项、一组数据项一个记录或或一组记录。

3)文件系统中的文件是为某一特定应用服务的,文件的逻辑结构对该应用程序来说是优化的,因此要想对现有的数据再增加一些新的应用会很困难,系统不容易扩充。

而在数据库系统中数据不再针对某一应用,而是面向全组织,具有整体的结构化。

5.试述数据库系统的特点。

(9、10、11页)参考答案:数据结构化;数据的共享性高、冗余度低、易扩充;数据独立性高;数据由DBMS统一管理和控制。

6.数据库管理系统的主要功能有哪些? (4页)参考答案:数据定义功能、数据操纵功能、数据库的运行管理、数据库的建立和维护功能。

7.试述数据模型的概念(13页)、数据模型的作用、数据模型的三个要素。

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

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

三、设计题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章习题参考答案3.1select readername,workunit,identitycardfrom readerwhere substring(identitycard,7,4)=‘1991’///字符串截取substr(字段名,起始点,个数) 或者select readername,workunit,identitycardfrom readerwhere identitycard like ‘______1991%’六个_3.2select readerno,readername,sexfrom readerwhere workunit=’信息管理学院’3.3select readerno,readername, workunit,bookno,bookname,borrowdatafrom reader,borrow,bookwhere reader.readerno=borrow.reader and borrow.bookno=book.booknoand year(returndata) between 2005 and 2008 and ifreturn =03.4select a.classno,max(price) 最高价格,avg(price) 平均价格from book a,bookclass bwhere a.classno=b.classnogroup by a.classnoorder by 最高价格desc3.5 select * from book where bookname like ‘%数据库%’3.6 select bookno,publishingdata,shopdata,booknameFrom bookWhere year(shopdata) between 2005 and 20083.7 select readerno,readernameForm readerWhere readerno not in (select distinct readerno from borrow where bookno like ‘001%’)3.8 select readerno,bookno,borrowdataForm borrowWhere bookno=’001-000029’3.9 select readernameForm readerWhere readerno not in (select distinct readerno from borrow)3.10 select classname,count(distinct book.classno),sum(shopnum)From book,bookclassWhere book.classno=’001’ and book.classno=bookclass.classnoGroup by book.classno3.11 select classname,sum(shopnum)From book,bookclassWhere book.classno=bookclass.classnoGroup by book.classno3.12 select a.readerno,readername,borrowdata,booknameFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoAnd b.readerno in (select readerno from borrowwhere bookno in(select bookno from bookwhere bookname=’离散数学’)) And b.readerno in (select readerno from borrowwhere bookno in(select bookno from bookwhere bookname=’数据库’))3.13 select a.readerno,readername,borrowdata,booknameFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoAnd not exists (select * from book where bookno=’002’And not exi s ts (select * from borrowwhere book.bookno=borrow.bookno))3.14 select b.bookno,bookname.borrowdata,returndataFrom reader a,borrow b,book cWhere a.readerno=b.readerno and b.bookno=c.bookno and a.readername=’马永强’3.15 select a.readerno,readername,borrowdata,bookname,returndataFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoand b.workunit=’会计学院‘and c.ifreturn=03.16 select a.readerno,readername,borrowdata,bookname,returndataFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoand a.publishingname=’清华大学出版社‘3.17 select readerno,readername,workunitFrom readerWhere not exist(select * from borrow where reader.readerno=borrow.readerno)3.18 select a.readerno,readername,a.bookno,booknameFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoAnd b.readerno in (select readerno from borrowgroup by readernohaving count(*)>=3)order by a.readerno3.19 select a.readerno,readername,a.bookno,booknameFrom borrow a,reader b , book cWhere a.readerno=b.readerno and a.bookno=c.booknoAnd year(borrowdate) between 2007 and 20083.20 select readerno,readername,workunitFrom readerWhere not exist s(select * from readerwhere readername=’马永强’and not exist s(select * from borrow where reader.readerno=borrow.readerno))3.21 select a.readerno,readername,sum(price)From borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknoAnd b.readerno in (select readerno from borrowgroup by readernohaving sum(price)>150)group by a.readerno,readername3.22 select readerno,readername, substring(identitycard,7,4)From readerWhere readerno not in(select readerno from borrow,book,bookclass where book.bookno=borrow.bookno and bookclass.classno=book.classno and bookclass.classname=’经济管理’)3.23 select a.readerno,readername, substring(identitycard,7,4)From borrow a,reader b , book cWhere a.readerno=b.readerno and a.boono=c.booknogroup by a.readernohaving sum(price)=(select max(sumprice) from (select sum(price) sumpricefrom borrowgroup by readerno) d)3.24 update bookSet price=price+price*0.1 (set price=price*1.1)From book,bookclassWhere book.classno=bookclass.classno and classname=’经济管理’3.28 create view view1AsSelect book.* from book,bookclassWhere book.classno=bookclass.classno and publishingname=’清华大学出版社’and year(publishingdate) between 2008 and 2009 and classname=’计算机类’补充内容--【字符串函数】--字符串截取substr(字段名,起始点,个数)select Name,substr(Name,2,4),substr(Name,0,3),substr(Name,-2,3),substr(Name,-2,1) from t1;--字符串从前面取三个(0开始)select Name,substr(Name,0,3) from t1;--字符串从后面取三个select Name,substr(Name,-3,3),length(Name) 串长度 from t1;SELECT ASCII('A'),ASCII('B') from dual;select CHR(100),CHR(80) from dual;select CONCAT(CHR(65),CONCAT(CHR(67),CHR(98))) from dual;select CHR(65)||CHR(66)||CHR(76) from dual;--将每个单词的第一个字母大写其它字母小写返回。

数据库1-3章习题参考答案

数据库1-3章习题参考答案

第二章 习题
二、多项选择题 5、下列关系代数运算中,要求是相容关系的是( ) A. 投影 B. 并 C.交 D.差 6、 关系模型的完整性规则包括( )。 A. 实体完整性规则 B.参照完整性规则 C.安全性规则 D.用户定义的完整性规 则 7.扩充关系代数 包括 ( )。 A. 外联接 B. 除 C.外部并 D.联接 8、自然联接运算是由( )操作组合而成 A. 投影 B.选择 C.笛卡儿积 D.并 9、关系模型是由( )组成 A. 数据结构 B.数据描述语言 C.数据操作 D.完 整性规则
6、试述过程性DML与非过程性DML的区别 。 用户使用过程性DML编程时,不仅需要指出 “做什么”,而且还需指出“怎么做”。用 户使用非过程性DML编程时,则需指出“做 什么”,不需指出“怎么做” 。
三、应用题
1、为某百货公司设计一个E-R模型。 某百货公司管辖若干个连锁商店,每家商 店经营若干种商品,每家商店有若干职工, 但每个职工只能服务于一家商店。 试画出反映商店、商品、职工之间联系的 E-R模型,并将其转换成关系模式集。
1、在关系中能唯一标识元组的属性集为( D )。 A.外部键 B.候选键 C.主键 D.超键
2、在实体中有属性可作为键而选定其中一个时,称
为该实体的 ( C )。 A.外部键 B.候选键 C.主键 D.主属性
3、若某属性虽非该实体的主键,却是另一实体的主
键,称该属性为( A )。 A.外部键 B.候选键 C.主键 D.主属性
第一章 习题
6、数据独立性与数据联系这两个概念有什 么区别? 7、试述DBMS在用户访问数据库过程中所 起的作用。
8、试述过程性DML与非过程性DML的区别 。
三、应用题
1、为某百货公司设计一个E-R模型。 某百货公司管辖若干个连锁商店,每家商 店经营若干种商品,每家商店有若干职工, 但每个职工只能服务于一家商店。 试画出反映商店、商品、职工之间联系的 E-R模型,并将其转换成关系模式集。

数据库第三章习题及答案

数据库第三章习题及答案

第3章关系数据库标准语言SQL一、选择题1、SQL语言是的语言,易学习。

A.过程化 B.非过程化 C.格式化 D.导航式答案:B2、SQL语言是语言。

A.层次数据库 B.网络数据库 C.关系数据库 D.非数据库答案:C3、SQL语言具有的功能。

A.关系规范化、数据操纵、数据控制 B.数据定义、数据操纵、数据控制C.数据定义、关系规范化、数据控制 D.数据定义、关系规范化、数据操纵答案:B4、SQL语言具有两种使用方式,分别称为交互式SQL和。

A.提示式SQL B.多用户SQL C.嵌入式SQL D.解释式SQL 答案:C5、假定学生关系是S(S#,SNAME,SEX,AGE),课程关系是C(C#,CNAME,TEACHER),学生选课关系是SC(S#,C#,GRADE)。

要查找选修“COMPUTER”课程的“女”学生姓名,将涉及到关系。

A.S B.SC,C C.S,SC D.S,C,SC 答案:D6、若用如下的SQL语句创建一个student表:CREATE TABLE student(NO C(4) NOT NULL,NAME C(8) NOT NULL,SEX C(2),AGE N(2))可以插入到student表中的是。

A.(‘1031’,‘曾华’,男,23) B.(‘1031’,‘曾华’,NULL,NULL)C.(NULL,‘曾华’,‘男’,‘23’) D.(‘1031’,NULL,‘男’,23) 答案:B7、当两个子查询的结果时,可以执行并,交,差操作.A.结构完全不一致 B.结构完全一致C.结构部分一致D.主键一致答案:B第8到第10题基于这样的三个表即学生表S、课程表C和学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)其中:S#为学号,SN为姓名,SEX为性别,AGE为年龄,DEPT为系别,C#为课程号,CN为课程名,GRADE为成绩。

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

3-2 对于教务管理数据库的三个基本表
S(SNO,SNAME, SEX, AGE,SDEPT)
SC(SNO,CNO,GRADE)
C(CNO,CNAME,CDEPT,TNAME)
试用SQL的查询语句表达下列查询:
⑴检索LIU老师所授课程的课程号和课程名。

⑵检索年龄大于23岁的男学生的学号和姓名。

⑶检索学号为200915146的学生所学课程的课程名和任课教师名。

⑷检索至少选修LIU老师所授课程中一门课程的女学生姓名。

⑸检索WANG同学不学的课程的课程号。

⑹检索至少选修两门课程的学生学号。

⑺检索全部学生都选修的课程的课程号与课程名。

⑻检索选修课程包含LIU老师所授课程的学生学号。

解:
⑴SELECT C#,CNAME
FROM C
WHERE TEACHER=’LIU’;
⑵SELECT S#,SNAME
FROM S
WHERE AGE>23 AND SEX=’M’;
⑶SELECT CNAME,TEACHER
FROM SC,C
WHERE SC.C#=C.C# AND S#=’200915146’
⑷SELECT SNAME (连接查询方式)
FROM S,SC,C
WHERE S.S#=SC.S# AND SC.C#=C.C# AND SEX=’F’AND TEACHER=’LIU’;
或:
SELECT SNAME (嵌套查询方式)
FROM S
WHERE SEX=’F’AND S# IN
(SELECT S#
FROM SC
WHERE C# IN (SELECT C#
FROM C
WHERE TEACHER=’LIU’))
或:
SELECT SNAME (存在量词方式)
FROM S
WHERE SEX=’F’ AND EXISTS(SELECT*
FROM SC
WHERE SC.S#=S.S#
AND EXISTS(SELECT *
FROM C
WHERE C.C#=SC.C# AND TEACHER=’LIU’))
⑸SELECT C#
FROM C
WHERE NOT EXISTS
(SELECT *
FROM S,SC
WHERE S.S#=SC.S# AND SC.C#=C.C# AND SNAME=’WANG));
⑹SELECT DISTINCT X.S#
FROM SC AS X,SC AS Y
WHERE X.S#=Y.S# AND X.C#!=Y.C#;
⑺SELECT C#.CNAME
FROM C
WHERE NOT EXISTS (SELECT *
FROM S
WHERE NOT EXISTS
(SELECT *
FROM SC
WHERE S#=S.S# AND C#=C.C#));
⑻SELECT DISTINCT S#
FROM SC AS X
WHERE NOT EXISTIS
(SELECT *
FROM C
WHERE TEACHER=’LIU’ AND NOT EXISTS
(SELECT *
FROM SC AS Y
WHERE Y.S#=X.S# AND Y.C#=C.C#));
3-3 试用SQL查询语句表达下列对3.2题中教务管理数据库的三个基本表S、SC、C查询:
⑴统计有学生选修的课程门数。

⑵求选修4号课程的学生的平均年龄。

⑶求LIU老师所授课程的每门课程的学生平均成绩。

⑷统计每门课程的学生选修人数(超过10人的课程才统计)。

要求输出课程号
和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。

⑸检索学号比WANG同学大,而年龄比他小的学生姓名。

⑹检索姓名以WANG打头的所有学生的姓名和年龄。

⑺在SC中检索成绩为空值的学生学号和课程号。

⑻求年龄大于女同学平均年龄的男学生姓名和年龄。

⑼求年龄大于所有女同学年龄的男学生姓名和年龄。

解:
⑴SELECT COUNT(DISTINCT C#)
FROM SC;
⑵SELECT AVG(AGE)
FROM S,SC
WHERE S.S#=SC.S# AND C#=’4’AND SEX=’F’;
⑶SELECT C.C#,AVG(GRADE)
FROM SC,C
WHE RE SC.C#=C.C# AND TEACHER=’LIU’;
⑷SELECT C#,COUNT(S#)
FROM SC
GROUP BY C#
HAVING COUNT(*)>10
ORDER BY 2 DESC,1;
⑸SELECT SNAME
FROM S
WHERE S#>ALL(SELECT S#
FROM S
WHERE SNAME=’WANG’
AND AGE<ALL(SELECT AGE
FROM S
WHERE SNAME=’WANG’);
⑹SELECT SNAME,AGE
FROM S
WHERE SNAME LIKE ‘WANG%’
⑺SELECT S#,C#
FROM SC
WHERE GRADE IS NULL;
⑻SELECT SNAME,AGE
FROM S
WHERE SEX=’M’AND AGE>(SELECT AVG(AGE)
FROM S
WHERE SEX=’F’);
⑼SELECT SNAME,AGE
FROM S
WHERE SEX=’M’AND AGE>ALL(SELECT AGE
FROM S
WHERE SEX=’F’);
3-4 试用SQL更新语句表达对3.2给出的教务管理数据库中三个基本表S、SC、C 进行如下更新操作:
⑴往基本表S中插入一个学生元组(‘200912143’,‘张晶’,21)。

⑵在基本表S中检索每一门课程成绩都大于等于80分的学生学号、姓名和性别,并把检索到的值送往另一个已存在的基本表STUDENT(SNO,SNAME,SEX)。

⑶在基本表SC中删除尚无成绩的选课元组。

⑷把张成民同学在SC中的选课记录全部删去。

⑸把选修高等数学课程中不及格的成绩全部改为空值。

⑹把低于总平均成绩的女同学成绩提高5%。

⑺在基本表SC中修改4号课程的成绩,若成绩小于等于75分时提高5%,若成绩大于75分时提高4%(用两个UPDATE语句实现)。

解:
⑴INSERT INTO S(S#,SNAME,AGE)
VALUES(‘200912143’,’张晶’,21);
⑵INSERT INTO STUDENT (SNO,SNAME,SEX)
SELECT S#,SNAME,SEX
FROM S
WHERE S# IN (SELECT S#
FROM SC
WHERE 80<=ALL(SELECT GRADE
FROM SC
GROUP BY S#));
⑶DELETE FROM SC
WHERE GRADE IS NULL;
⑷DELETE
FROM SC
WHERE S# IN(SELECT S#
FROM S
WHERE SNAME=’张成民’)
⑸UPDATE SC
SET GRADE=NULL
WHERE GRADE<60 AND C# IN(SELECT C# FROM C
WHERE CNAME=’高等数学’);
⑹UPDATE SC
SET GRADE=GRADE*1.05
WHERE S# IN(SELECT S#
FROM S
WHERE SEX=’F’)
AND GRADE<(SELECT AVG(GRADE)
FROM SC);
⑺用两个UPDATE语句实现:
UPDATE SC
SET GRADE=GRADE*1.04
WHERE C#=’4’AND GRADE>75;
UPDATE SC
SET GRADE=GRADE*1.05
WHERE C#=’4’AND G RADE<=75;
注意:这两个UPDATE语句的顺序不能颠倒。

相关文档
最新文档