数据库原理第三章作业
数据库第三章所有例题参考答案
![数据库第三章所有例题参考答案](https://img.taocdn.com/s3/m/068dfd6927d3240c8547ef01.png)
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)(完)。
数据库原理第三章第四章课后习题答案建库建表
![数据库原理第三章第四章课后习题答案建库建表](https://img.taocdn.com/s3/m/d9132b29a7c30c22590102020740be1e650ecc68.png)
数据库原理第三章第四章课后习题答案建库建表张敬怡12032316习题3建表和数据录入CREAT TABLE StudentSno Char(7) NOT NULL UNIQE,Sname VarChar(20) NOT NULL,Ssex Char(2) NOT NULL,Sage Smallint NULL,Clno Char(5) NOT NULL;INSERT INTO StudentV ALUES(‘2000101’, ‘李勇’, ‘男’, ‘20’, ‘00311’), (‘2000102’, ‘刘诗晨’, ‘女’, ‘19’, ‘00311’), (‘2000103’, ‘王一鸣’, ‘男’, ‘20’, ‘00312’), (‘2000104’, ‘张婷婷’, ‘女’, ‘21’, ‘00312’), (‘2001101’, ‘李勇敏’, ‘女’, ‘19’, ‘01311’), (‘2001102’, ‘贾向东’, ‘男’,‘22’, ‘01311’), (‘2001103’, ‘陈宝玉’, ‘男’, ‘20’, ‘01311’), (‘2001104’, ‘张逸凡’, ‘男’, ‘21’, ‘01311’); CREAT TABLE CourseCno Char(1) NOT NULL UNIQE,Cname VarChar(20) NOT NULL,Credit Smallint NULL;INSERT INTO CourseV ALUES(‘1’, ‘数据库’, ‘4’, ),(‘2’, ‘离散数学’, ‘3’, ),(‘3’, ‘管理信息系统’, ‘2’, ),(‘4’, ‘操作结构’, ‘4’, ),(‘6’, ‘数据处理’, ‘2’, ),(‘7’, ‘C语言’, ‘4’, );CREAT TABLE ClassClno Char(5) NOT NULL UNIQE,Speciality VarChar(20) NOT NULL,Inyear Char(4) NOT NULL,Number Integer NULL,Monitor Char(7) NULL;INSERT INTO ClassV ALUES(‘00311’, ‘计算机软件’, ‘2000’, ‘120’, ‘2000101’),(‘00312’, ‘计算机应用’, ‘2000’, ‘140’, ‘2000103’),CREAT TABLE GradeSno Char(7) NOT NULL,Cno Char(1) NOT NULL,Gmark Numberic(4,1) NULL;INSERT INTO GradeV ALUES(‘2000101’, ‘1’, ‘92’, ),(‘2000101’, ‘3’, ‘88’, ),(‘2000101’, ‘5’, ‘86’, ),(‘2000102’, ‘1’, ‘78’, ),(‘2000102’, ‘6’, ‘55’, ),(‘2000103’, ‘3’, ‘65’, ),(‘2000103’, ‘6’, ‘78’, ),(‘2000103’, ‘5’, ‘66’, ),(‘2000104’, ‘1’, ‘54’, ),(‘2000104’, ‘6’, ‘83’, ),(‘2001101’, ‘2’, ‘70’, ),(‘2001102’, ‘2’, ‘80’, ),(‘2001102’, ‘4’, ‘90’, ),(‘2000103’, ‘1’, ‘83’, ),(‘2000103’, ‘2’, ‘76’, ),(‘2000103’, ‘4’, ‘56’, ),(‘2000103’, ‘7’, ‘88’, );习题311题1.ALTER TABLE StudentADD Nation Varchar(20) NULL;2.ALTER TABLE StudentDROP COLUMN Nation;3.INSET INTO CourseV ALUES(‘2001110’, ‘3’, ‘80’);4.UPDATA Course SETCredit=70 WHERE Cno=‘2001110’;5.DELATE FROM CourseWHERE Cno=‘2001110’;6.CREATE INDEX IX_ClassON Student(Clno ASC);7.DROP INDEX Student IX_Class;1.SELECT DISTINCT CnoFROM Grade;2.SELECT*FROM StudentWHERE Ssex=女and Clno=’01311’;3.SELECT Sname,Ssex,(2014-Sage)as birthday FROM StudentWHERE Clno=’01311’OR Clno=’01312’;4.SELECT*WHERE Sname LIKE ‘李%’;5.SELECT NUMBERFROM ClassWHERE Clno=(SELECT ClnoFROM StudentWHERE Sname=李勇);6.SELECT A VG(Gmark)AS平均成绩MAX(Gmark) AS 最高分MIN(Gmark)AS 最低分FROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统);7.SELECT Count(DISTINCT Sno)FROM Grade;8.SELECT Count(DISTINCT Sno)FROM GradeWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统));9.SELET SnameFROM StudentWHERE Sno=(SELECT SnoWHERE Sno NOT IN (SELECT DISTINCT SnoFROM Grade))AND Clno=(SELECT Clno1.SELECT*FROM StudentWHERE Sname<>李勇AND Clno=(SELECT ClnoWHERE Sname=李勇);2.SELECT*FROM StudentWHERE Sname<>李勇AND Sno IN=(SELECT DISTINCT Sno FROM GradeWHERE Cno=ANY(SELECT Cno FROM GradeWHERE Sno=(SELECT SnoFROM StudentWHERE Sname=李勇)));3.SELECT*FROM StudentWHERE Sage BETWEEN(SELECT Sage FROM StudentWHERE Sname=李勇)AND 25;4.SELECT Sno,SnameFROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=(SELECT CnoFROM CourseWHERE Cname=操作系统));5.SELECT SnameFROM StudentWHERE Sno<>(SELECT SnoFROM GradeWHERE Cno=1);6.SELECT SnameWHERE NOT EXISTS(SELECT*FROM CourseWHERE NOT EXISTS (SELECT*FROM GradeWHERE Student Sno=Grade.SnoAND/doc/b21760812.html,o=/doc/ b21760812.html,o));14题1.SELECT Sno,GmarkFROM GradeWHERE Cno=3ORDER BY Gmark DESC;降序2.SELECT*FROM StudentORDER BY Clno,Sage DESC;3.SELECT Cno,COUNT(Sno)AS 选课人数FROM GradeGROUP BY Cno;4.SELECT SnoFROM GradeGROUP BY SnoHA VING COUNT(Sno)>3; 找出选修了三门以上课程的学生学号15题1.UPDATA GradeSET Gmark=0WHERE Sno IN(SELECT SnoFROM StudentWHERE Clno=‘01311’);2. DELETE*FROM GradeWHERE Sno IN(SELECT SnoFROM StudentWHERE Clno=(SELECT ClnoFROM ClassWHERE Speciality=计算机软件AND Inyear=2001));3.DELETE*FROMGradeWHERE Sno IN(SELECT SnoFROM StudentWHERE Sname='李勇')UPDATE ClassSET Number=Number-1WHERE Clno=(SELECT ClnoFROM StudentWHERE Sname='李勇')UPDATE ClassSET Monitor=NULLWHEN Monitor=(SELECT SnoFROM StudentWHERE Sname='李勇')DELETE FROM StudentWHERE Sname='李勇' ;4.ALTER TABLE ClassADD Cavg_age Smallint NULLUPDATE ClassUPDATE Clno='00311' THEN (SELECT A VG(Sage) FROM Student WHERE Clno='00311'),UPDATEClno='00312' THEN(SELECT A VG(Sage) FROM Student WHERE Clno='00312'),WHEN Clno='01311' THEN (SELECT A VG(Sage) FROM Student WHERE Clno='01311');16题1.CREAT VIEW Stu_01311_1AS SELECT*FROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=1);2.CREAT VIEW Stu_01311_2AS SELECT FROM StudentWHERE Sno=(SELECT SnoFROM GradeWHERE Cno=1AND Gmark<60);3.CREAT VIEW Stu_yearAS SELECT Sno,Sname,(2014-Sage)AS Birth_yearFROM Student;4.SELECT SnameFROM VIEW Stu_yearWHERE Birth_year>1990;5.SELECT Sno,Sname,Birth_yearFROM VIEW Stu_yearWHERE Sno=(SELECT SnoFROM VIEW Stu_01311_2);习题4建表和完整性约束CREAT TABLE Student( Sno Char(7) NOT NULL UNIQE PRIMARY KEY,Sname VarChar(20) NOT NULL,Ssex Char(2) NOT NULL CHECK(Sex IN (‘男’,‘女’))Sage Smallint NULL CHECK(Age>=14 AND Age<=65),Clno Char(5) NOT NULL REFERENCES Class(Clno)ON UPDATE CASCADE );CREAT TABLE Course( Cno Char(1) NOT NULL UNIQE PRIMARY KEY,Cname VarChar(20) NOT NULL,Credit Smallint NULL CHECK(Redit IN(‘1’,‘2’,‘3’,‘4’,‘5’,‘6’)) );CREAT TABLE Class( Clno Char(5) NOT NULL UNIQE PRIMARY KEY,Speciality VarChar(20) NOT NULL,Inyear Char(4) NOT NULL,Number Integer NULL CHECK(Number>=1AND Number<=100),Monitor Char(7) NULL REFERENCES Student(Sno) );CREAT TABLE Grade( Sno Char(7) NOT NULL,Cno Char(1) NOT NULL,Gmark Numberic(4,1) NULL CHECK(Mark>=0 AND Mark<=100)PRIMARY KEY(Sno,Cno)FOREIGN KEY Sno REFERENCES Student(Sno)ON DELETE CASCADEON UPDATE CASCADEFOREIGN KEY Cno REFERENCES Course(Cno)ON DELETE CASCADEON UPDATE CASCADE );PS:王老师我笔记本里装不了SQL的软件所以只是把代码写出来了没运行过......。
数据库系统原理第三章同步练习
![数据库系统原理第三章同步练习](https://img.taocdn.com/s3/m/d6634fc85fbfc77da269b198.png)
性。
8. 消除了非主属性对候选键局部依赖的关系模式, 9. 两个函数依赖集F和G等价的充分必要条件是
10. 消除了每一属性对候选键传递依赖的关系模
式称为 BCNF 模式
11. 一个关系模式属于 4NF ,它必定属于BCNF。
A. 互不相关的
B. 不可分解的
C. 长度可变的
D. 互相关联的
6. 假设关系模式R(A,B)属于3NF,下列说法( B )
是正确的
A. 它一定消除了插入和删除异常
B. 仍存在一定的插入和删除异常
C. 一定属于BCNF
D. A和C
7. 设有关系W(工号, 姓名, 工种, 定额), 将其规范
化到第三范式正确的答案是( C )
1NF变成了3NF
A. 局部函数依赖和传递函数依赖
B. 完全函数依赖和传递函数依赖
C. 完全函数依赖
D. 局部函数依赖
13. 下述说法正确的是( D )
A. 属于BCNF的关系模式不存在存储异常
B. 函数依赖可由属性值决定,不由语义决定
C. 超键就是候选键
D. 键是唯一能决定一个元组的属性或属性组
一、单项选择题
1. 当B属性函数依赖于A属性时,属性A与B的联
系是(B )
A. 一对多
C. 多对多
B. 多对一
C. 以上都不是
2. 关系模式R中的属性全部是主属性,则R的最高
范式必定是(B )
A. 2NF
B. 3NF
C. BCNF
D. 4NF
3. 在关系模式R(A,B,C,D)中,有函数依赖集F={
Z=U-X-Y,则 X →→Z
5. 若关系模式R已属于第一范式,且其中的每一
数据库 第三章习题参考答案
![数据库 第三章习题参考答案](https://img.taocdn.com/s3/m/bd92d43ff12d2af90242e69b.png)
三、设计题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)建立存书表和销售表。
【精选】数据库第三章课后习题
![【精选】数据库第三章课后习题](https://img.taocdn.com/s3/m/2a08165515791711cc7931b765ce05087632750c.png)
• 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 张勇
数据库原理第三章练习
![数据库原理第三章练习](https://img.taocdn.com/s3/m/b725d0f0ad02de80d5d8400a.png)
第三章SQL语言一、选择题:1、SQL语言是的语言,易学习。
A.过程化 B.非过程化C.格式化 D.导航式2、SQL语言是语言。
A.层次数据库 B.网络数据库C.关系数据库 D.非数据库3、SQL语言具有的功能。
2、关系规范化,数据操纵,数据控制B.数据定义,数据操纵,数据控制C.数据定义,关系规范化,数据控制D.数据定义,关系规范化,数据操纵4、SQL语言具有两种使用方式,分别称为交互式SQL和。
A.提示式SQL B.多用户SQLC.嵌入式SQL D.解释式SQL5、SQL语言中,实现数据检索的语句是。
A.SELECT B.INSERTC.UPDATE D.DELETE6、下列SQL语句中,修改表结构的是。
A.ALTER B.CREATEC.UPDATE D.DELETE7、SQL中,与“NOT IN”等价的操作符是。
A.=SOME B.<>SOMEC.=ALL D.<>ALL8、假设有三个基本表:学生表S、课程表C、学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)检索所有比“王华”年龄大的学生姓名、年龄和性别。
正确的SQL语句是。
A.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGE FROM SWHERE SN=”王华”)B.SELECT SN,AGE,SEXFROM SWHERE SN=”王华”C.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGEWHERE SN=”王华”)D.SELECT SN,AGE,SEXFROM SWHERE AGE>王华.AGE9、检索选修课程”C2”的学生中成绩最高的学生的学号。
正确的SELECT语句是。
A.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=(SELECT GRADE FROM SCWHERE C#= “C2”)B.SELECT S#FROM SCWHERE C#=”C2” AND GRADE IN(SELECT GRADE FROM SCWHERE C#= “C2”)C.SELECT S#FROM SCWHERE C#=”C2” AND GRADE NOT IN(SELECT GRADE FROM SCWHERE C#= “C2”)D.SELECT S#FROM SCWHERE C#=”C2” AND GRADE>=ALL(SELECT GRADE FROM SCWHERE C#= “C2”)10、检索学生姓名及其所选修课程的课程号和成绩。
数据库第三章习题参考答案范文大全
![数据库第三章习题参考答案范文大全](https://img.taocdn.com/s3/m/520f0b2c78563c1ec5da50e2524de518964bd38a.png)
数据库第三章习题参考答案范文大全第一篇:数据库第三章习题参考答案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查询:⑴ 统计有学生选修的课程门数。
数据库原理与应用77页第三章习题3
![数据库原理与应用77页第三章习题3](https://img.taocdn.com/s3/m/32e723cc5fbfc77da269b110.png)
完成第三章课后第2、3、4题,写相关SQL程序,提交数据库备份文件及SQL程序代码,(如不提交附件)可直接粘贴代码提交1、设有一图书馆数据库,包括三个表:图书表、读者表、借阅表。
三个表的结构如表3-3、表3-4和表3-5所示。
完成以下习题。
表3-3 图书表结构列名说明数据类型约束图书图书唯一的图书号定长字符串,长度主键号为20书名图书的书名变长字符串,长度为50空值作者图书的编著者名变长字符串,长度为30空值出版社图书的出版社变长字符串,长度为30空值单价出版社确定的图书的单价浮点型,FLOAT空值表3- 4 读者表结构列名说明数据类型约束说明读者号读者唯一编号定长字符串,长度为10主键姓名读者姓名定长字符串,长度为8非空值性别读者性别定长字符串,长度为2非空值办公电话读者办公电话定长字符串,长度为8空值部门读者所在部门变长字符串,长度为30空值表3- 5 借阅表结构列名说明数据类型约束说明读者号读者的唯一编号定长字符串,长度为10外码,引用读者表的主键图书号图书的唯一编号定长字符串,长度为20外码,引用图书表的主键借图书借定长字符非空值出日期出的日期串,长度为8,为’yymmdd’归还日期图书归还的日期定长字符串,长度为8,为’yymmdd’空值主键为:(读者号, 图书号)(1)用SQL语句创建图书馆数据库。
C读者表te database 图书馆数据库;(2)用SQL语句创建上述三个表。
use图书馆数据库Goc读者表te table图书表(图书号char (20)primary key,书名varchar (50),作者varchar (30),出版社varchar (30),单价float);use图书馆数据库goc读者表te table读者表(读者号char (10)primary key,姓名char (8)not null,性别char (2)not null,办公电话char (8),部门char(30));use图书馆数据库gocreate table借阅表(读者号char (10)not null,图书号char (20)not null,借出日期char (8)not null,归还日期char (8),foreign key (读者号)references 读者表(读者号),foreign key (图书号)references 图书表(图书号));(3)基于图书馆数据库的三个表,用SQL语言完成以下各项操作:1)给图书表增加一列“ISBN”,数据类型为CHAR(10)alter table图书表add ISBN CHAR(10);2)为刚添加的ISBN列增加缺省值约束,约束名为ISBNDEF,缺省值为’7111085949’;ALTER TABLE图书表ADD CONSTRAINT ISBNDEF DEFAULT'7111085949'FOR ISBN; 3)为读者表的’办公电话’列,添加一个CHECK约束,要求前五位’88320’,约束名为CHECKDEF。
数据库原理三、四章作业
![数据库原理三、四章作业](https://img.taocdn.com/s3/m/90f071a885868762caaedd3383c4bb4cf7ecb7fb.png)
数据库原理三、四章作业1.Please answer the questions of chapter3Chapter 3 3.1(a)relationA relation is a uniquely named two-dimensional table with identically structured rows (technically tuples) and distinctly named columns (or attributes).(b)attributean attribute is a named column of a relation.(c)domainset of allowable values for one or more attributes.(d)tuplea record of a relation or a row of a relation.(e)intension and extensionthe database schema is called the intension of the database.Extension: the database instance is called the extension(f)degree and cardinalityThe degree of a relation is the number of attributesThe cardinality of a relation is the number of tuples it contains3.3,Discuss the differences between a relation and a relation schema. What is relational database schema?(1)A relation is a table with columns and rows ;A collection of normalized relations with distinct relation names.(2)A set of relation schemas , each with a distinct name .3.4,d iscuss the properties of a relationA relation has the following properties:a.the relation has a name that is distinct from all otherrelation names in the relational schema;(关系不能重名)b.Each cell of the relation contains exactly one atomic (single)value(关系中每个格子只能取一个单一的原子值)c.Each attribute has a distinct named.The value of an attribute are all from the same domaine.Each tuple is distinct ; there are no dulicate tuples;f.The order of attributes has no significanceg.The order of tuples has no significance, theoretically.(however, in practice,the order may affect the effciency of accessing tuples.)3.5,Discuss the differences between the candidate keys and the primary key of a relation . Explain what is meant by a foreign keys of relations relate to candidate keys ? Given examples to illustrate your answer.(1)candidate keys:Superkey (K) such that no proper subset is a superkey within the table.In each record, values of K uniquely identify that record (uniqueness).No proper subset of K has the uniqueness property (irreducibility).Primary Key:Candidate key selected to identify records uniquely within table.A primary key may consist of only a single attribute or may be concatenatedNo attribute in a primary key can ever be null - referred to as the entity integrity constraintEvery tuple must have, by definition, a unique value for itsprimary keyA attribute, or a minimal group of attributes, that uniquely identifies a tuple(record) within a relation(table).(2)courseC(课程号) CNO(先修课程号)001 ————002 001003 002004 003005 004Primary key:C Foreign key :CNOconstraint pk_course primary key(C),constraint Fk_course foreign key (CNO) references course(C)3.6 define the two principal integrity rules for the relational model. Discuss why it is desirable to enforce these rules.(2)entity integrity and referntial integrity(3)3.7View: The dynamic result of one or more relational operations operating on the base relations to produce another relation .A view is a virtual relation that does not necessarily exist in the database but can be produced upon request by a particular user, at the time of request.Base relation: A named relation corresponding to an entity in the conceptual schema, whose tuples are physically stored in the database.2. Please do the exercises of chapter3&chapter4,and submit the solution.Chapter 33.8(1)Room(roomNo, hotelNo,type,price) foreign key :hotelNoConstraint fk_Room_Hotel foreign key(hotelNo) references Hotel(hotelNo)(2)Booking(hotelNo,guestNo,dataFrom,dataTo,roomNo)foreign key :hotelNo guestNo roomNoConstraint fk_Booking_Room foreign key (hotelNo) references Room(hotelNo)Constraint fk_Booking_Guest foreign key (guestNo) references Room(guestNo)Constraint fk_Booking_Room foreign key (roomNo) references Room(roomNo)Chapter 4 4.8,(1)the coding number of hotels whose room price is more than 50 pounds(2)All the hotels' coding numbers,names and locations ,and their rooms'coding,type and price.(3)All the names of hotels whose room's price is above 50 pounds.(4)All the numbers,names and addresses of travellers,and the hotel codings,the time of checking in and out and the room number of the traveller whose reservation is after january 1th,2002.(5)(6)The coding numbers of hotels that have guest in London and the names of guest.4.12 (only use Relational Algebra),(a)∏hotelName(Hotel)(b)∏roomNo,hotelNo(σtype='single'∧price<50(Room))(c)∏guestName,guestAddress(Guest)(d)∏type,price(σhotelName='Grosvernor'(Hotel∞Room))(e)∏σhotelName='Grosvernor'(Hotel∞Booking∞Guest)(f)(∏σhotelName='Grosvernor'(Hotel∞Room))∞(∏guestName(∏σhotelName='Grosvernor'(Hotel∞Booking∞G uest)) (g)4.13∏city,roomNo,type(σhotelName='Grosvernor'(Hotel∞R oom))make the graph more visiual, the information more comprehensive , reject the various commercial ads, and ensure the security。
数据库系统原理教程课后习题及答案(第三章)汇编
![数据库系统原理教程课后习题及答案(第三章)汇编](https://img.taocdn.com/s3/m/00c7531daf45b307e87197ee.png)
第3章关系数据库标准语言SQL1 .试述sQL 语言的特点。
答:(l)综合统一。
sQL 语言集数据定义语言DDL 、数据操纵语言DML 、数据控制语言DCL 的功能于一体。
(2)高度非过程化。
用sQL 语言进行数据操作,只要提出“做什么”,而无需指明“怎么做”,因此无需了解存取路径,存取路径的选择以及sQL 语句的操作过程由系统自动完成。
(3)面向集合的操作方式。
sQL 语言采用集合操作方式,不仅操作对象、查找结果可以是元组的集合,而且一次插入、删除、更新操作的对象也可以是元组的集合。
(4)以同一种语法结构提供两种使用方式。
sQL 语言既是自含式语言,又是嵌入式语言。
作为自含式语言,它能够独立地用于联机交互的使用方式;作为嵌入式语言,它能够嵌入到高级语言程序中,供程序员设计程序时使用。
(5)语言简捷,易学易用。
2 .试述sQL 的定义功能。
sQL 的数据定义功能包括定义表、定义视图和定义索引。
SQL 语言使用cREATE TABLE 语句建立基本表,ALTER TABLE 语句修改基本表定义,DROP TABLE 语句删除基本表;使用CREATE INDEX 语句建立索引,DROP INDEX 语句删除索引;使用CREATE VIEW 语句建立视图,DROP VIEW 语句删除视图。
3 .用sQL 语句建立第二章习题5 中的4 个表。
答:对于S 表:S ( SNO , SNAME , STATUS , CITY ) ;建S 表:CREATE TABLE S ( Sno C(2) UNIQUE,Sname C(6) ,Status C(2),City C(4));对于P 表:P ( PNO , PNAME , COLOR , WEIGHT );建P 表:CREATE TABLE P(Pno C(2) UNIQUE,Pname C(6),COLOR C(2),WEIGHT INT);对于J 表:J ( JNO , JNAME , CITY);建J 表:CREATE TABLE J(Jno C(2) UNlQUE,JNAME C(8),CITY C(4))对于sPJ 表:sPJ ( sNo , PNo , JNo , QTY);建SPJ 表:SPJ(SNO,PNO,JNO,QTY)CREATE TABLE SPJ(Sno C(2),Pno C(2),JNO C(2),QTY INT))4.针对上题中建立的4 个表试用sQL 语言完成第二章习题5 中的查询。
数据库作业第三章习题答案
![数据库作业第三章习题答案](https://img.taocdn.com/s3/m/f151ed943086bceb19e8b8f67c1cfad6195fe986.png)
数据库作业第三章习题答案数据库作业第三章习题答案数据库作业是数据库课程中非常重要的一部分,通过完成作业可以帮助学生巩固和加深对数据库知识的理解和应用。
第三章习题主要涉及数据库设计和查询语言的使用。
在本篇文章中,我们将回答第三章习题,并探讨一些相关的概念和技巧。
1. 设计一个关系模式,用于存储学生的基本信息,包括学生编号、姓名、性别、年龄和专业。
请给出该关系模式的定义。
答案:学生(学生编号,姓名,性别,年龄,专业)2. 设计一个关系模式,用于存储课程的信息,包括课程编号、课程名称和学分。
请给出该关系模式的定义。
答案:课程(课程编号,课程名称,学分)3. 设计一个关系模式,用于存储学生选课的信息,包括学生编号、课程编号和成绩。
请给出该关系模式的定义。
答案:选课(学生编号,课程编号,成绩)4. 编写一个SQL查询语句,查询学生的姓名和年龄。
答案:SELECT 姓名, 年龄 FROM 学生;5. 编写一个SQL查询语句,查询选修了某门课程的学生的姓名和成绩。
答案:SELECT 学生.姓名, 选课.成绩FROM 学生, 选课WHERE 学生.学生编号 = 选课.学生编号AND 选课.课程编号 = '某门课程编号';6. 编写一个SQL查询语句,查询某个学生的选课情况,包括课程名称和成绩。
答案:SELECT 课程.课程名称, 选课.成绩FROM 课程, 选课WHERE 课程.课程编号 = 选课.课程编号AND 选课.学生编号 = '某个学生编号';通过以上习题的回答,我们可以看到数据库设计和查询语言的基本应用。
关系模式的定义是数据库设计的基础,它描述了数据表的结构和属性。
在查询语言的使用中,我们可以通过SELECT语句来检索和过滤数据,通过WHERE子句来指定查询条件。
除了上述习题的答案,我们还可以进一步探讨数据库设计的一些原则和技巧。
例如,为了提高数据库的性能和可扩展性,我们可以使用索引来加快数据的检索速度。
数据库答案 第三章习题参考答案
![数据库答案 第三章习题参考答案](https://img.taocdn.com/s3/m/834efe919ec3d5bbfd0a74a7.png)
或: Select jno from j where not exists (Select * from spj,s where spj.jno=j.jno and spj.sno=s.sno and s.city=‘天津’);
9
此课件下载可自行编辑修改,供参考! 感谢您的支持,我们努力做得更好!
4
习题三 第5题
1. 找出所有供应商的姓名及其所在城市。 Select sname, city from s; 2. 找出所有零件的名称、颜色、重量。 Select pname, color, weight from p; 3.找出使用供应商S1所供应零件的工程项目代码。 Select jno from spj where sno=‘S1’;
color=‘红’; 或: Select sno from spj Where jno =‘J1’ and pno in
(Select pno from p where color=‘红’色零件的工程号JNO。 Select jno From j Where not exists
(Select sno from s where city=‘上海’);
6
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=‘上海’);
7
7. 找出没有使用天津产的零件的工程项目代码。 Select jno from j where not exists
数据库原理与运用第3章习题答案
![数据库原理与运用第3章习题答案](https://img.taocdn.com/s3/m/b36620f9951ea76e58fafab069dc5022aaea46f1.png)
数据库原理与运用第3章习题答案一、选择题(1)A (2)D (3)B (4)C (5)D(6)D (7)B (8)B (9)C (10) C二、填空题(1)不能重复(或叫惟一),非空(2)m1与m2之积(或为:m1×m2)(3)dom,F(4)关系代数,关系关系演算(5)外键值(6)外联接(7)省时间,省空间,提高效率(8)建立优化语法树的标准格式,形成优化的语法树三、计算题1.设有关系R和S,如下表3.22所示。
表3.22 R和S表R表 S表———————————————A B C A B C—————————————————3 6 7 34 52 5 7 7 2 37 2 3 —————————4 4 4——————————计算:(S)∞S。
R∪S,R-S,R∩S,R×S,π3,2R∪S A B C R-S A B C R R∞S A B C 3 6 7 3 6 7 7 2 3 7 2 3 2 5 7 2 5 77 2 3 4 4 34 4 33 4 5σ(R) A B C RB<’5’7 2 34 4 3 7 2 3 3 4 5R×S R.A R.B R.C S.A S.B S.C π(S) C B3,23 6 7 345 5 43 6 7 7 2 3 3 22 5 734 52 5 7 7 2 37 2 3 3 4 57 2 3 7 2 34 4 3 3 4 54 4 3 7 2 32.设有下面4个关系模式:供应商关系 S(SNO,SNAME,SADDR)零件关系 P(PNO,PNAME,COLOR,WEIGHT)工程关系 J(JNO,JNAME,JCITY,BALANCE)供应关系 SPJ(SNO,PNO,JNO,PRICE,QTY)上述关系模式中属性的含意是:供应商编号(SNO)、供应商名(SNAME)、供应商地址(SADDR)、零件编号(PNO)、零件名(PNAME)、颜色(COLOR)、重量(WEIGHT)、工程编号(JNO)、工程名称(JNAME)、工程所在城市(JCITY)、工程余额(BALANCE)、零件单价(PRICE)、供应数量(QTY)。
自考数据库系统原理(第3章)(代码:4735)练习题3
![自考数据库系统原理(第3章)(代码:4735)练习题3](https://img.taocdn.com/s3/m/b3119b283169a4517723a357.png)
练习题33.1解释下列名词1.函数依赖:设有关系模式R (U ),X 和Y 是属性集U 的子集,函数依赖(functional dependency ,简记为FD )是形为X →Y 的一个命题,只要r 是R 的当前关系,对r 中任意两个元组t 和s ,都有t[X]=s[X]蕴涵t[Y]=s[Y],那么称FD X →Y 在关系模式R (U )中成立。
这里t[X]表示元组t 在属性集X 上的值,其余类同。
X →Y 读作“X 函数决定Y ”,或“Y 函数依赖于X ”。
FD 是对关系模式R 的一切可能的关系r 定义的。
对于当前关系r 的任意两个元组,如果X 值相同,则要求Y 值也相同,即有一个X 值就有一个Y 值与之对应,或者说Y 值由X 值决定。
因而这种依赖称为函数依赖。
2.平凡的函数依赖对于FD X →Y ,如果Y ⊆X ,那么称X →Y 是一个“平凡的FD ”,否则称为“非平凡的FD ”。
正如名称所示,平凡的FD 并没有实际意义,根据规则A1就可推出。
人们感兴趣的是非平凡的FD 。
只有非平凡的FD 才和“真正的”完整性约束条件相关。
从规则A4和A5,立即可得到下面的定理。
定理 3.3 如果A 1……An 是关系模式R 的属性集,那么X →A 1……An 成立的充分必要条件是X →Ai (i=1,…,n )成立。
3.函数依赖集F 的闭包F +(Closure )设F 是函数依赖集,被F 逻辑蕴涵的函数依赖全体构成的集合,称为函数依赖集F 的闭包(Closure ),记为F +。
即F +={ X →Y | F|=X →Y }。
4.属性集X 的闭包X +设F 是属性集U 上的FD 集,X 是U 的子集,那么(相对于F )属性集X 的闭包用X +表示,它是一个从F 集使用FD 推理规则推出的所有满足X →A 的属性A 的集合:X +={属性A | F|=X →A }5.函数依赖的逻辑蕴含设F 是在关系模式R 上成立的函数依赖的集合,X →Y 是一个函数依赖。
数据库原理及应用教程第三章作业
![数据库原理及应用教程第三章作业](https://img.taocdn.com/s3/m/ea117d1352d380eb62946d79.png)
三、设计题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章习题解答PPT教学课件
![数据库第3章习题解答PPT教学课件](https://img.taocdn.com/s3/m/2f77b6a0bb4cf7ec4afed0ac.png)
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。
数据库系统原理与设计万常选版第三章练习题和详细答案
![数据库系统原理与设计万常选版第三章练习题和详细答案](https://img.taocdn.com/s3/m/a02c139f69eae009581becfc.png)
第三章SQL语言一、选择题1. SQL语言是(B )的语言,容易学习。
A.过程化B. 非过程化C.格式化D. 导航式2. SQL语言的数据操纵语句包括SELECT、INSERT、UPDATE、DELETE等。
其中最重要的,也是使用最频繁的语句是(A)。
A. SELECTB. INSERTC. UPDATED. DELETE3. 在视图上不能完成的操作是()。
A. 更新视图B. 查询C. 在视图上定义新的表D. 在视图上定义新的视图4. SQL语言集数据查询、数据操纵、数据定义和数据控制功能于一体,其中,CREATE、DROP、ALTER语句是实现哪种功能()。
A. 数据查询B. 数据操纵C. 数据定义D. 数据控制5. SQL语言中,删除一个视图的命令是()。
A.DELETEB.DROPC.CLEARD.REMOVE6. 在SQL语言中的视图VIEW是数据库的()。
A. 外模式B. 模式C. 内模式D. 存储模式7. 下列的SQL语句中,()不是数据定义语句。
A. CREATE TABLEB. DROP VIEWC. CREATE VIEWD. GRANT8. 若要撤销数据库中已经存在的表S,可用()。
A. DELETE TABLE SB. DELETE SC. DROP TABLE SD. DROP S9. 若要在基本表S中增加一列CN(课程名),可用()。
A.ADD TABLE S(CN CHAR(8))B.ADD TABLE S ALTER(CN CHAR(8))C.ALTER TABLE S ADD(CN CHAR(8))D.ALTER TABLE S (ADD CN CHAR(8))10. 学生关系模式S(S#,Sname,Sex,Age),S的属性分别表示学生的学号、姓名、性别、年龄。
要在表S中删除一个属性“年龄”,可选用的SQL语句是()。
A. DELETE Age from SB. ALTER TABLE S DROP AgeC. UPDATE S AgeD. ALTER TABLE S ‘Age’11. 有关系S(S#,SNAME,SAGE),C(C#,CNAME),SC(S#,C#,GRADE)。
数据库第三章课后习题解答
![数据库第三章课后习题解答](https://img.taocdn.com/s3/m/dc8f724bf7ec4afe04a1df56.png)
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所示。
数据库原理习题与答案 第3章数据库系统结构
![数据库原理习题与答案 第3章数据库系统结构](https://img.taocdn.com/s3/m/c188cb2bb8f67c1cfbd6b822.png)
DML:数据操纵语言,用来对数据库中的数据进行查询、插入、删除和修改的语句。
7.数据与程序的逻辑独立性:当模式改变时,由数据库管理员对各个外模式//模式的映像做相应改变,可以使外模式保持不变。应用程序是依据数据的外模式编写的,从而应用程序不必修改,保证了数据与程序的逻辑独立性,简称数据的逻辑独立性。
数据与程序的物理独立性:当数据库的存储结构改变了,由数据库管理员对模式/内模式映像做相应改变,可以使模式保持不变,从而应用程序也不必改变,保证了数据与程序的物理独立性,简称数据的物理独立性。
数据库管理系统在三级模式之间提供的两层映像保证了数据库系统中的数据能够具有较高的逻辑独立性和物理独立性。
数据库系统的三级模式是对数据的三个抽象级别,它把数据的具体组织留给DBMS管理,使用户能逻辑抽象地处理数据,而不必关心数据在计算机中的表示和存储。
为了能够在内部实现这三个抽象层次的联系和转换,数据库系统在这三级模式之间提供了两层映像:外模式/模式映像和模式/内模式映像,正是这两层映像保证了数据库系统中的数据能够具有较高的逻辑独立性和物理独立性。
数据库原理习题与答案 第3章数据库系统结构
1.
2.
3.
4.
5.的逻辑结构和特征的描述,是数据库用户的数据视图,是与某一应用有关的数据的逻辑表示。模式,亦称逻辑模式,是数据库中全体数据的逻辑结构和特征的描述,是所有用户的公共数据视图。模式描述的是数据的全局逻辑结构,外模式涉及的是数据的局部逻辑结构,通常是模式的子集。内模式,亦称存储模式,是数据在数据库系统内部的表示,即对数据的物理结构和存储方式逻辑结构和特征的描述,是所有用户的公共数据视图。外模式,亦称子模式或用户模式,是数据库用户能够看见和使用的局部数据的逻辑结构和特征的描述,是数据库用户的数据视图,是与某一应用有关的数据的逻辑表示。内模式,亦称存储模式,是数据在数据库系统内部的表示,即对数据的物理结构和存储方式的描述。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数据库原理第三章作业
学号:20112730
姓名:陈佳宇
一. 简答题
1.简述表间数据完整性的实现方式?
⑴外键约束,即在从表上定义外键约束。
⑵利用触发器,即主表的触发器维护主表到从表方向的数据完整性,从表的触发器维护从表到主表方向的参照完整性。
2.简述主键约束的要求?
主键约束针对主键而言,并且值唯一,不能为空值。
3.简述唯一限制的要求?
唯一约束针对候选键而言,并且值唯一,允许有且只有一个空值。
4.SQL-92标准支持的完整性限制是否一定会在SQL SERVER中实现,举例说明?不一定。
比如SOL SERVER不支持“断言”,虽然SOL-92标准是支持的。
5.SQL SERVER中规则的目的?
指明表中某一列的取值范围。
6.SQL SERVER中在定义某些限制时,分列级与表级,其分类的原则是什么?列级方式是:在要定义约束的列本身定义完后,紧接其后定义其约束。
表级方式:表中所有的列都定义往后,再定义所要的约束。
7.简述外键约束限制定义的条件?
定义外键的列必须是另一个表中的主键或候选键。
8.一张表上可定义的触发器个数是多少个?
答:12个
9.关系代数的基本操作符?
selection(选择) :σ
projection(投影):π
union(并)
intersection(交)
difference(差)
cross-product(积)
10.关系代数中对结果有重复元组时,如何处理?
只保留其中一个,另外的被去掉。
11.简述联结的分类?
条件连接连接
自然连接
外连接:左外连接,右外连接,全外连接
12.简述关系运算的种类?
主要有两种,一:元组关系运算,二:域关系运算
二. 单项选择题
1. ( A )不是关系代数的基本操作。
ASelection BProjection CJoin DIntersection 2. ( C )用唯一限制来约束。
A主键B外键C候选键D简单键
3. ( B )与“列”不同义。
A字段B元组C域D属性
三. 判断题(正确打√,错误打×)
1. (√)关系代数中的改名操作既可用于改名也可用于存放临时关系模式结果。
2. (×)对主表,插入操作可能会违背参照完整性限制,但删除和更新不会。
3. (×)等连接是自然连接的特例.
4. (√)关系代数是与关系模型有关的查询语言。
四. 设有三个关系
S(Sid(学号),Sname(姓名),Age(年龄),Sex(性别))
SC(Sid(学号),Cid(课程号),Score(成绩))
C(Cid(课程号),Cname(课程名),Teacher(教师))
试用关系代数表达式表示下列查询语句,并且写出前四个的SQL查询语句:
①检索LIU老师所授课程的课程号和课程名。
表达式:πCid,Cname(σTeacher=’LIU’(C))
对应SQL:select Cid as 课程号,Cname as 课程名from C
Where Teacher=’LIU’.
②检索年龄大于23岁的男学生的学号和姓名。
表达式:πSid,Sname(σAge>23(S))
对应SQL:select Sid as 学号,Sname as 姓名from S
Where Age>23.
③检索学号为S3学生所学课程的课程名与任课教师名。
表达式:πCid,Teacher(σSid=S3(S C) C )。
对应SQL:select Cid 课程名,Teacher 任课教师名from SC,C
Where SC.Sid=S3 and SC.Cid=C.Cid
④检索至少选修LIU老师所授课程中一门课的女学生姓名。
表达式:πSname (σTeacher=LIU(S C)σSex=’女’(S)C)。
⑤检索W ANG同学不学的课程的课程号。
表达式:πCid(SC-(σSname=’WANG’(S)SC))
⑥检索至少选修两门课的学生学号。
表达式:p(temp1,πCid,Sid,Sname(S SC))
P(temp2(1→sid1,2→Sname,3→Cid1,4→Sid2,5→Sname2,6→Cid2),temp1×temp1)
π,Sname1(σSid1=Sid2^Cid1≠Cid2(temp2))
⑦检索全部学生都选修的课程的课程号与课程名。
表达式:πCid,Cname(πCid,Sid(SC)/πCid(C)S)
⑧检索选修课程包含LIU老师所授全部课程的学生学号。