数据库作业第三章

合集下载

数据库习题第三章 习题

数据库习题第三章 习题

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语言支持建立聚簇索引,这样可以提高查询效率,但是,并非所有属性列都适宜建立聚簇索引,下面()属性列不适宜建立聚簇索引。

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

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

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

数据库第三章习题参考

数据库第三章习题参考

5.求至少用了供应商S1所供应的全部零件的工程号JNO。 即查找:不存在这样的零件y,供应商S1供应了y,而工程x为选用y。 Select distinct jno From spj z Where not exists (select * from spj x where sno=‘S1’ and not exists (select * from spj y where y.pno=x.pno and y.jno=z.jno));
习题三 第5题
1. 找出所有供应商的姓名及其所在城市。 Select sname, city from s; 2. 找出所有零件的名称、颜色、重量。 Select pname, color, weight from p; 3.找出使用供应商S1所供应零件的工程项目代码。 Select jno from spj where sno=‘S1’;
7. 找出没有使用天津产的零件的工程项目代码。 Select jno from j where not exists (Select * from spj where spj.jno=j.jno and sno in (Select sno from s where city=‘天津’) );
3.求供应工程J1零件为红色的供应商号码。 Select sno from spj, p Where spj.pno=p.pno and jno=‘J1’ and color=‘红’; 或: Select sno from spj Where jno =‘J1’ and pno in (Select pno from p Biblioteka where color=‘红’ );
6. 找出使用上海产的零件的工程项目名。 Select jname from j,spj,s where j.jno=spj.jno and spj.sno=s.sno and s.city=‘上海’; 或: Select jname from j where jno in (Select jno from spj, s where spj.sno=s.sno and s.city=‘上海’);

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

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章习题解答PPT教学课件

数据库第3章习题解答PPT教学课件
UPDATE SPJ
SET SNO=‘S3’
WHERE SNO=‘S5’ AND JNO=‘J4’ AND PNO=‘P6’ 10)从供应商关系中删除S2的记录,并从供应情况关系中删除
SELECT SNO FROM SC WHERE CNO=‘k1’ AND
SNO IN (SELECT SNO FROM SC WHERE CNO=‘k5’);
2020/12/10
6
3.用SQL语句建立第二章习题5中的四个表:
供应商关系:S(SNO,SNAME,STATUS,CITY) 零件关系:P(PNO,PNAME,COLOR, WEIGHT) 工程项目关系:J(JNO,JNAME,CITY) 供应情况关系:SPJ(SNO,PNO,JNO,QTY)
18
5)找出上海厂商供应的所有零件号码 SELECT DISTINCT PNO FROM S, SPJ WHERE S.SNO=SPJ.SNO AND S.CITY=‘上海’;
SELECT DISTINCT PNO
FROM SPJ
WHERE SNO IN
(SELECT SNO
FROM S
WHERE S.CITY=‘上海’);
6)找出使用上海产的零件的工程名称
SELECT JNAME
FROM S, SPJ, J
WHERE S.SNO=SPJ.SNO AND J.JNO=SPJ.JNO AND
2020/12/10 S.CITY=‘上海’ ;
19
7)找出没有使用天津产的零件的工程号码
SELECT JNO FROM J WHERE JNO NOT IN
CREATE TABLE SPJ (SNO CHAR(4) NOT NULL,

数据库第三章习题

数据库第三章习题

第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.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;--将每个单词的第一个字母大写其它字母小写返回。

数据库第三章习题及答案

数据库第三章习题及答案

第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为成绩。

数据库第三章习题参考答案范文大全

数据库第三章习题参考答案范文大全

数据库第三章习题参考答案范文大全第一篇:数据库第三章习题参考答案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 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 (存在量词方式)SEX=’F’ AND 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.针对上题中建立的4 个表试用sQL 语言完成第二章习题5 中的查询。

( l )求供应工程Jl 零件的供应商号码SNO ;SELECT DIST SNO FROM SPJ WHERE JNO=’J1’( 2 )求供应工程Jl 零件Pl 的供应商号码SNO ;SELECT DIST SNO FROM SPJ WHERE JNO='J1' AND PNO='P1'( 3 )求供应工程Jl 零件为红色的供应商号码SNO ;SELECT SNO FROM SPJ,P WHERE JNO='J1' AND SPJ.PNO=P.PNO AND COLOR='红' ( 4 )求没有使用天津供应商生产的红色零件的工程号JNO ;SELECT DIST JNO FROM SPJ WHERE JNO NOT IN (SELE JNO FROM SPJ,P,S WHERE S.CITY='天津' AND COLOR='红' AND S.SNO=SPJ.SNO AND P.PNO=SPJ.PNO)。

( 5 )求至少用了供应商Sl 所供应的全部零件的工程号JNO ;由于VFP不允许子查询嵌套太深,将查询分为两步A、查询S1供应商供应的零件号SELECT DIST PNO FROM SPJ WHERE SNO='S1'结果是(P1,P2)B、查询哪一个工程既使用P1零件又使用P2零件。

SELECT JNO FROM SPJ WHERE PNO='P1'AND JNO IN (SELECT JNO FROM SPJ WHERE PNO='P2')5.针对习题3中的四个表试用SQL语言完成以下各项操作:(1)找出所有供应商的姓名和所在城市。

SELECT SNAME,CITY FROM S(2)找出所有零件的名称、颜色、重量。

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

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

数据库第三章部分习题答案3.2对于教学数据库的三个基本表s(s#,sname,age,sex)sc(s#,c#,grade)c(c#,cname,教师)试用sql的查询语句表达下列查询:3.2.1搜索17岁以下女生的学号和姓名,选择#,snamefromswhereage<17andsex=f;3.2.2检索男孩学习的课程编号和课程名称,从C中选择C#,CNAMEwherec#in(selectdistinctc#fromscwheres#in(selects#fromswheresex=m))3.2.3检索男生学习课程的教师的职务编号和姓名selectt#,tnamefromt其中#in(从C中选择distinctt#wherec#in(selectdistinctc#fromsc#in(选择)在哪里#froms其中性别=1);3.2.4检索至少选修两门课程的学生的学号挑选#fromscgroupbys#拥有计数(c#)>=2;3.2.5检索至少有学号为s2和s4所学的课程和课程名selectc#,cnamefromc其中c#in((选择c#fromscwheres#='s2')intersect(选择C#fromsc#='s4')3.2.6检索‘wang’同学不学的课程号从CEXcept中选择C#(selectdistinctc#fromscwheres#=(selects#fromswheresname='wang'));3.2.7检索所有学生的课程号和课程名称selectc#,cname弗洛姆wherenotexists(selects#弗洛姆wherec.c#notin(selectc#fromscwheresc.s#=s.s#));3.2.8检索选修课程包括“刘”老师教授的所有课程的学生的学号和姓名。

选择#,snamefromswherenotexists((selectc#弗洛姆wheret#=(selectt#fromtwhere name='liu'))除了(选择C#fromsc wheresc.s#=s.s#));3.4有两个基本表R(a、B、c)和S(a、B、c)。

数据库作业第三章

数据库作业第三章

第三章关系数据库系统RDBS作业一. 简答题1.对于表中几个特殊的列,如主键、候选键和外键,分别用什么限制来保证它们的完整性?对表中其它一般性的列,用什么限制来保证它们的完整性?主键约束:主要是针对主键,以保证主键值的完整性。

要求主键值必须满足值唯一、不能为空值。

唯一约束:主要是针对候选键,以保证主键值的完整性。

要求候选键必须满足值唯一、可有一个且仅有一个空值。

外键约束:是维护表与表之间外键所对应属性(组)数据的一致性。

主表到从表,表示主表中的主键值在修改和删除时,从表中与该主键值相同的外键值可级联(CASCADE)修改和删除,或改为空值(SET NULL)或默认值(SET DEFAULT),或禁止(NO ACTION)主表主键值的修改和删除;从表到主表,表示从表中的外键值在插入和修改时,其值应参照(REFERENCE)主表中的主键值。

对于其他一般性的列,还有检查约束和断言。

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

3.SQL SERVER中在定义某些限制时,分列级与表级,其分类的原则是什么?列级检查约束针对表中一列,表级检查约束则针对同一表中多列。

4.外键限制定义的条件?在含外键的表上定义;定义外键限制的列必须是另一个表中的主键。

5.请说明在维护表间数据完整时外键限制与触发器的异同。

1.错误信息的管理上:约束与触发器在遇到问题时都可以返回给用户一定的错误信息。

但是,触发器可以返回数据库管理员自定义的错误信息,而且还可以实现较为复杂的逻辑控制,而约束只能够通过标准化的系统错误信息来传递错误消息;2.性能上的差异分析:从性能上来说,约束的执行性能都要高一点。

虽然约束的执行性能比较高,但是其向用户提供的错误信息确实非常有限的。

3.管理维护的工作量:由于约束基本上都是数据库现成的解决方案。

无论是索引约束还是外键约束,又或者是check约束。

往往在数据库系统中已经有了现成的解决方案。

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

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

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

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

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

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)在“图书”表中以“作者”建立一个索引。

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

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

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

第三章课后习题3-7(1) delete from sWhere placeofb=’上海’;(2)delete from scWhere s# in (select s#from swhere sname=’李建平’);(3)delete form sWhere s# in(select s#from scwhere grade is null);3-8(1)update scSet grade=61Where grade<60 and c# in(select c#from cwhere cname=’计算机网络’);(2)update scSet grade=grade*1.05Where grade<(select avg(grade)From scWhere c# in(select c#From cWhere cname=’数据结构’)) andC# in(select c#From cWhere cname=’数据结构’);3-10(1)create view grade_tAsSelect s.s#,sname,c.c#,cname,classh,grade,t.t#,tnameFrom s,c,t,sc,teachWhere s.s#=sc.s# and c.c#=sc.c# and sc.c#=teach.c# and teach.t#=t.t#;(2)create view teach_lAsSelect t.t#,tname,c#,cname,classh,avg(grade) as avg_gradeFrom t,c,sc,teachWhere t.t#=teach.t# and teach.c#=sc.c# and sc.c#=c.c#Group by t.t#;3-11 select s#,snameFrom sWhere ssex=’男’;3-12 select s#,snameFrom sWhere sbirthin>’1981-1-1’ and sex=’女’;3-13 select s#,ssex,scode#From sWhere s# in (select s#From scWhere c# in (select c#From cWhere cname=’操作系统’));3-14 select s#,sname,scode#From sWhere s# in(select s#From scWhere c# in(select c#From teachWhere t# in(select t#From tWhere tname=’刘少华’)));3-15 select s#,snameFrom sWhere not exists(select *From cWhere not exists(select *From scWhere sc.s#=s.s# andc.c#=sc.c#));3-16 select c#,classh,tnameFrom c,teach,t,s,scWhere c.c#=sc.c# and sc.c#=teach.c# and teach.t#=t.t# and sc.s#=s.s# and s.sname=’王丽丽’;3-17 select c#,classhFrom cWhere c# in(select c#From teachWhere t# in(select t#From tWhere tname=’刘少华’));3-18 select tnameFrom tWhere t# in(select t#From teach);3-19 select s#,sname,sbirthinFrom sWhere scode# in(select scode#From ssWhere ssname=’计算机应用技术’) order by sbirthin;3-20 select s#,snameFrom sWhere s# in(select s#From scWhere c# in(select c#From cWhere cname=’计算机网络’)); 3-21 select s#,snameFrom sWhere s# in(select s#From scWhere c# in(select c#From cWhere cname=’计算机网络’));Intersectselect s#,snameFrom sWhere s# in(select s#From scWhere c# in(select c#From cWhere cname=’信息安全技术’)); 3-22 select s#,sname,ssnameFrom s,ssWhere s.scode#=ss.scode# and s# not in(select s#From scWhere c# in(select c#From cWhere cname=’计算机网络’));3-23 select c#,cnameFrom cWhere c# in(select c#From scGroup by c#Having count(*)>=5);。

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

第三章关系数据库系统RDBS作业一. 简答题1.对于表中几个特殊的列,如主键、候选键和外键,分别用什么限制来保证它们的完整性?对表中其它一般性的列,用什么限制来保证它们的完整性?主键约束:主要是针对主键,以保证主键值的完整性。

要求主键值必须满足值唯一、不能为空值。

唯一约束:主要是针对候选键,以保证主键值的完整性。

要求候选键必须满足值唯一、可有一个且仅有一个空值。

外键约束:是维护表与表之间外键所对应属性(组)数据的一致性。

主表到从表,表示主表中的主键值在修改和删除时,从表中与该主键值相同的外键值可级联(CASCADE)修改和删除,或改为空值(SET NULL)或默认值(SET DEFAULT),或禁止(NO ACTION)主表主键值的修改和删除;从表到主表,表示从表中的外键值在插入和修改时,其值应参照(REFERENCE)主表中的主键值。

对于其他一般性的列,还有检查约束和断言。

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

3.SQL SERVER中在定义某些限制时,分列级与表级,其分类的原则是什么?列级检查约束针对表中一列,表级检查约束则针对同一表中多列。

4.外键限制定义的条件?在含外键的表上定义;定义外键限制的列必须是另一个表中的主键。

5.请说明在维护表间数据完整时外键限制与触发器的异同。

1.错误信息的管理上:约束与触发器在遇到问题时都可以返回给用户一定的错误信息。

但是,触发器可以返回数据库管理员自定义的错误信息,而且还可以实现较为复杂的逻辑控制,而约束只能够通过标准化的系统错误信息来传递错误消息;2.性能上的差异分析:从性能上来说,约束的执行性能都要高一点。

虽然约束的执行性能比较高,但是其向用户提供的错误信息确实非常有限的。

3.管理维护的工作量:由于约束基本上都是数据库现成的解决方案。

无论是索引约束还是外键约束,又或者是check约束。

往往在数据库系统中已经有了现成的解决方案。

数据库管理员通过直接引用这些解决方案即可以实现特定的功能,而不用再费力的编写触发器来实现。

而触发器中系统没有现成的可以引用,而都需要数据库管理员通过实际清理来进行编写。

6.关系代数的基本操作符?笛卡尔乘积最大的作用是什么?关系代数的基本操作符:SELECTION(选择)、PROJECTION(投影)、UNION(并或称联合)、INTERSECTION(交)、DIFFERENCE(差)、CROSS-PRODUCT(积)。

笛卡尔操作的最大作用是把任意两个不相关的表联接起来。

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

同时,在结果模式中重复的字段只有一个。

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

9.连接的分类?条件连接(Condition Join ):加入连接条件,对两个关系实施连接; 等连接(Equi Join ):是条件连接的特例。

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

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

二. 单项选择题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)σA = a1(Y ×Z )X 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 c4{<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)Y ZC A Bc1 a1 b2c1 a1 b1c2 a1 b1c3 a2 b1c4 a3 null(3)X Y ZA B Ca1 b1 c1a1 b1 c2a1 b2 c1a2 b1 c3五.一个电影资料库有四个实体“电影”,“演员”,“导演”,“电影公司”。

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

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

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

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

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

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

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

1)2)演员(演员工作证号,姓名,出生年,性别)演员工作证号为主健; 导演(导演工作证号,姓名,出生年,性别)导演工作证号为主健; 电影公司(公司名称,所在国家)电影公司为主健;电影(电影编号,电影名,电影类型,对白语言,导演工作证号),电影编号为主键,导演工作证号为外键,电影名为候选键;出演(演员工作证号,电影编号,角色)(演员工作证号,电影编号)为主健,演员工作证号和电影编号各为外键;出品(电影编号,公司名称,出品年份)(电影编号,公司名称)为主健,电影编号和公司名称各为外键;属于a (公司名称,演员工作证号)(公司名称,演员工作证号)为主健,公司名称和演员工作证号各为外键;属于b (公司名称,导演工作证号)(公司名称,导演工作证号)为主健,公司名称,导演工作证号各为外键。

3)①σ出生年 < 1957(演员)∩σ性别 =男(演员)SELECT *FROM 演员WHERE 出生年<1957 INTERSECTSELECT*FROM WHERE 性别=男n演员 电影电影公司导演出演执导属于b属于a出品出品年份演员工作证号姓名 性别 出生年性别导演工作证号姓名 出生年公司名称所在国家n 1nm nmmnm角色电影类型 电影名电影编号 对白语言②ρ(Temp,σ出品年份 =2000(出品)∩σ公司名称=环球公司(出品))π电影名,姓名(Temp 电影导演)SELECT 电影名FROM 出品,电影WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号INTERSECTSELECT姓名FROM 出品,电影,导演WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号AND导演.导演工作证号=电影.导演工作证号③π姓名(σ姓名=张一(导演)电影出演演员)SELECCT 姓名FROM 导演,电影,出演,演员WHERE 导演.姓名=‘张一’AND电影.导演工作证号=导演.导演工作证号AND出演.电影编号=演员.工作证号六.某出版社管理系统有四个实体,即出版社(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作者(Author )书籍(Book )出版社(Publisher )编辑(Editor )主编ZX 校对JD出版CB地址(Paddr ) 作者编码(Aid ) 姓名(Aname ) 性别(Agender )电话(Atel )性别(Egender )电话(Ptel )姓名(Ename ) 出生日期出版社编码(Pid)出版社名称(Pname )11n1nn编辑工号(Eid )国际图书分类号(Isbn ) 书名(Bname ) 单价(Bprice )出版日期(Pdate )( 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。

相关文档
最新文档