数据库系统基础教程第七章答案.docx

上传人:暗伤 文档编号:96473071 上传时间:2023-12-01 格式:DOCX 页数:17 大小:21.42KB
返回 下载 相关 举报
数据库系统基础教程第七章答案.docx_第1页
第1页 / 共17页
数据库系统基础教程第七章答案.docx_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《数据库系统基础教程第七章答案.docx》由会员分享,可在线阅读,更多相关《数据库系统基础教程第七章答案.docx(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、SolutionsChapter 77.1.1a)CREATE TABLE Movies (title year length genre studioName producerC#CHAR(100), INT, INT, CHAR(10), CHAR(30), INT,PRIMARY KEY (title, year),FOREIGN KEY (producerC#) REFERENCES MovieExec(cert#) );orCREATE TABLE Movies (title year length genre studioName producerC#CHAR(100),INT,I

2、NT,CHAR(10),CHAR(30),INTREFERENCES MovieExec(cert#),PRIMARY KEY (title, year) );b)CREATE TABLE Movies (titleyear length genre studioName producerC#CHAR(100),INT,INT,CHAR(10),CHAR(30), INTREFERENCES MovieExec(cert#)ON DELETE SET NULL ON UPDATE SET NULL, PRIMARY KEY (title, year) );c)CREATE TABLE Movi

3、es (title year lengthCHAR(100), INT, INT,genreCHAR(10),studioNameCHAR(30),producerC#INTREFERENCES MovieExec(cert#)ON DELETE CASCADEON UPDATE CASCADE, PRIMARY KEY (title, year) );d)CREATE TABLE StarsIn (movieTitleCHAR(100)REFERENCES Movie(title),movieYearINT,starNameCHAR(30),PRIMARY KEY (movieTItle,

4、movieYear, starName) );e)CREATE TABLE StarsIn (movieTitleCHAR(100)REFERENCES Movie(title)ON DELETE CASCADE,movieYearINT,starNameCHAR(30),PRIMARY KEY (movieTItle, movieYear, starName);7.1.2To declare such a foreign-key constraint between the relations Movie and StarsIn, values of the referencing attr

5、ibutes in Movie should appear in MovieStar as unique values. However, based on primary key declaration in relation StarIn, the uniqueness of movies is guaranteed with movieTitle, movieYear, and starName attributes.Even with titleand year as referencing attributes there is no way of referencing uniqu

6、e movie from StarsIn without starName information. Therefore, such a constraint can not be expressed using a foreign-key constraint.7.1.3ALTER TABLE ProductADD PRIMARY KEY (model);ALTER TABLE PCADD FOREIGN KEY (model) REFERENCES Product (model);ALTER TABLE LaptopADD FOREIGN KEY (model) REFERENCES Pr

7、oduct(model);ALTER TABLE PrinterADD FOREIGN KEY (model)REFERENCES Product (model);ALTER TABLE ClassesADD PRIMARY KEY (class);ALTER TABLE ShipsADD PRIMARY KEY (name);ALTER TABLE ShipsADD FOREIGN KEY (class) REFERENCES Classes (calss);ALTER TABLE BattlesADD PRIMARY KEY (name);ALTER TABLE OutcomesADD F

8、OREIGN KEY (ship) REFERENCES Ships (name);ALTER TABLE OutcomesADD FOREIGN KEY (battle) REFERENCES Battles (name);7.1.5a)ALTER TABLE ShipsADD FOREIGN KEY (class) REFERENCES Classes (class) ON DELETE SET NULL ON UPDATE SET NULL;In addition to the above declaration, class must be declared the primary k

9、ey for Classes.b)ALTER TABLE OutcomeADD FOREIGN KEY (battle) REFERENCES Battles (name) ON DELETE SET NULL ON UPDATE SET NULL;c)ALTER TABLE OutcomesADD FOREIGN KEY (ship) REFERENCES Ships (name) ON DELETE SET NULL ON UPDATE SET NULL;a)yearINTCHECK (year = 1915)b)lengthINTCHECK (length= 60 AND length

10、=2.0)b)CREATE TABLE Printer (typeVARCHAR(10)CHECK (type IN (,laser?, , ink-jet ?, ,bubble-jet?);c)CREATE TABLE Product (typeVARCHAR(10)CHECK (type IN(,pc? , laptop?, , printer?);d)CREATE TABLE Product (7.2.1?) )modelCHAR(4)CHECK (model IN (SELECT model FROM PCUNION ALLSELECT model FROM laptopUNION A

11、LLSELECT model FROM printer);* note this doesn?t check the attribute constraint violation caused bydeletions from PC, laptop, or printer7.2.3a)CREATE TABLE StarsIn (starName CHAR(30)CHECK (starName IN (SELECT name FROM MovieStarWHERE YEAR(birthdate) movieYear);b)CREATE TABLE Studio (address CHAR(255

12、);CHECK (address IS UNIQUE)c)CREATE TABLE MovieStar (name CHAR(30);d)CREATE TABLE Studio (Name CHAR(30);e)CREATE TABLE Movies (CHECK (name NOT IN (SELECT name FROM MovieExec)CHECK (name IN (SELECT studioName FROM Movies)CHECK (producerC# NOT IN (SELECT presC# FROM Studio) ORstudioName IN (SELECT nam

13、e FROM StudioWHERE presC# = producerC#);7.2.4a)CHECK (speed = 2.0 OR price = 15 OR hd = 40 OR price 16)b)CHECK (class NOT IN (SELECT class FROM ClassesWHERE numGuns 9 AND bore 14)c)CHECK (ship IN (SELECT s.name FROM Ships s, Battles b, Outcomes oWHERE s.name = o.ship ANDb.name = o.battle ANDs.launch

14、ed YEAR(b.date)7.2.6The constraint in Example 7.6 does not allow NULL value for genderwhile the constraint in Example 7.8 allows NULL.7.3.1a)ALTER TABLE Movie ADD CONSTRAINT myKey PRIMARY KEY (title, year);b)ALTER TABLE Movie ADD CONSTRAINT producerCheck FOREIGN KEY (producerC#) REFERENCES MovieExec

15、 (cert#);c)ALTER TABLE Movie ADD CONSTRAINT lengthCheck CHECK (length = 60 AND length 14);e)ALTER TABLE Ships ADD CONSTRAINT shipDateCheckCHECK (ship IN (SELECT s.name FROM Ships s, Battles b, Outcomes oWHERE s.name = o.ship ANDb.name = o.battle ANDs.launched = YEAR(b.date)7.4.1a)CREATE ASSERTION CH

16、ECK(NOT EXISTS(SELECT maker FROM Product NATURAL JOIN PC)INTERSECT(SELECT maker FROM Product NATURAL JOIN Laptop) );b)CREATE ASSERTION CHECK(NOT EXISTS(SELECT makerFROM Product NATURAL JOIN PCWHERE speed ALL(SELECT L2.speedFROM Product P2, Laptop L2WHERE P2.maker = maker ANDP2.model = L2.model);c)CR

17、EATE ASSERTION CHECK(NOT EXISTS(SELECT modelFROM LaptopWHERE price = ALL(SELECT price FROM PCWHERE PC.ram = ALL(SELECT COUNT(*) FROM Ships GROUP BY class);b)CREATE ASSERTION CHECK(NOT EXISTS(SELECT country FROM ClassesWHERE type = ,bb?)INTERSECT(SELECT country FROM ClassesWHERE type = ,bc?);c)CREATE

18、 ASSERTION CHECK(NOT EXISTS(SELECT o.battle FROM Outcomes o, Ships s, Classes cWHERE o.ship = s.name AND s.class = c.class AND c.numGuns 9)INTERSECT(SELECT o.battle FROM Outcomes o, Ships s, Classes cWHERE o.result = ,sunk? AND o.ship = s.name ANDs.class = c.class AND c.numGuns 9);d)CREATE ASSERTION

19、 CHECK(NOT EXISTS(SELECT s1.name FROM Ships s1WHERE s1.launched = 10000000)2)presC# INT Check(presC# NOT IN (SELECT cert# FROM MovieExecWHERE netWorth (SELECT AVG(netWorth) FROM MovieExec)DELETE FROM MovieExecWHERE (name, address, cert#, netWorth) IN NewStuff;CREATE TRIGGER AvgNetWorthTriggerAFTER D

20、ELETE ON MovieExecREFERENCINGOLD TABLE AS OldStuffFOR EACH STATEMENTWHEN (500000 (SELECT AVG(netWorth) FROM MovieExec)INSERT INTO MovieExec(SELECT * FROM OldStuff);7.5.2a)CREATE TRIGGER LowPricePCTriggerAFTER UPDATE OF price ON PCREFERENCINGOLD ROW AS OldRow,OLD TABLE AS OldStuff,NEW ROW AS NewRow,N

21、EW TABLE AS NewStuffFOR EACH ROWWHEN (NewRow.price (SELECT AVG(price) FROM Laptop)BEGINDELETE FROM LaptopWHERE (model, speed, ram, hd, screen, price) IN NewStuff;INSERT INTO Laptop(SELECT * FROM OldStuff);END;d)CREATE TRIGGER HardDiskTriggerAFTER UPDATE OF hd, ram ON PC REFERENCINGOLD ROW AS OldRow,

22、OLD TABLE AS OldStuff, NEW ROW AS NewRow, NEW TABLE AS NewStuffFOR EACH ROWWHEN (NewRow.hd 35000)UPDATE NewStuff SET displacement = 35000;c)CREATE TRIGGER newOutcomesTriggerAFTER INSERT ON OutcomesREFERENCINGNEW ROW AS NewRowFOR EACH ROWWHEN (NewRow.ship NOT EXISTS (SELECT name FROM Ships)INSERT INT

23、O Ships (name, class, lunched)VALUES (NewRow.ship, NULL, NULL);CREATE TRIGGER newOutcomesTrigger2AFTER INSERT ON OutcomesREFERENCINGNEW ROW AS NewRowFOR EACH ROWWHEN (NewRow.battle NOT EXISTS (SELECT name FROM Battles)INSERT INTO Battles (name, date)VALUES (NewRow.battle, NULL);d)CREATE TRIGGER chan

24、geShipTriggerAFTER INSERT ON ShipsREFERENCINGNEW TABLE AS NewStuffFOR EACH STATEMENTWHEN ( 20 ALL(SELECT COUNT(name) From Ships NATURAL JOIN Classes GROUP BY country)DELETE FROM ShipsWHERE (name, class, launched) IN NewStuff;CREATE TRIGGER changeShipTrigger2AFTER UPDATE ON ShipsREFERENCINGOLD TABLE

25、AS OldStuff,NEW TABLE AS NewStuffFOR EACH STATEMENTWHEN ( 20 ALL(SELECT COUNT(name) From Ships NATURAL JOIN Classes GROUP BY country)BEGINDELETE FROM ShipsWHERE (name, class, launched) IN NewStuff;INSERT INTO Ships(SELECT * FROM OldStuff);END;e)CREATE TRIGGER sunkShipTriggerAFTER INSERT ON OutcomesR

26、EFERENCINGNEW ROW AS NewRowNEW TABLE AS NewStuffFOR EACH ROWWHEN ( (SELECT date FROM Battles WHERE name = NewRow.battle) ALL(SELECT date FROM BattlesWHERE name IN (SELECT battle FROM OutcomesWHERE ship = NewRow.ship ANDresult =“ sunk)DELETE FROM OutcomesWHERE (ship, battle, result) IN NewStuff;CREAT

27、E TRIGGER sunkShipTrigger2AFTER UPDATE ON OutcomesREFERENCINGNEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWFOR EACH ROWWHEN ( (SELECT date FROM Battles WHERE name = NewRow.battle) ALL (SELECT COUNT(*) FROM StarIn s, MovieStar mWHERE s.starName = m.nameGROUP BY s.movieTitle, m.gendar)INSERT INTO

28、 MovieStar(SELECT * FROM OldStuff);1 only considering DELETE from MovieStar since the assumption was the desired condistion was satisfied before any change.2 * not considering INSERT into StarsIn since no gender info can be extracted from a new row for StarsIn.d)CREATE TRIGGER numMoviesTriggerAFTER

29、INSERT ON MoviesREFERENCINGNEW TABLE AS NewStuffFOR EACH STATEMENTWHEN (100 ALL(SELECT COUNT(*) FROM MoviesGROUP BY studioName, year)DELETE FROM MoviesWHERE (title, year, length, genre, StudioName, procedureC#)IN NewStuff;CREATE TRIGGER numMoviesTrigger2AFTER UPDATE ON MoviesREFERENCINGOLD TABLE AS

30、OldStuffNEW TABLE AS NewStuffFOR EACH STATEMENTWHEN (100 ALL(SELECT COUNT(*) FROM MoviesGROUP BY studioName, year)BEGINDELETE FROM MoviesWHERE (title, year, length, genre, StudioName, procedureC#)IN NewStuff;INSERT INTO Movies(SELECT * FROM OldStuff);END;e)CREATE TRIGGER avgMovieLenTriggerAFTER INSERT ON MoviesREFERENCINGNEW TABLE AS NewStuffFOR EACH STATEMENTWHEN (120 ALL(SELECT AVG(length) FROM MoviesGROUP BY year)D

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁