《数据库系统基础教程第七章答案(共17页).doc》由会员分享,可在线阅读,更多相关《数据库系统基础教程第七章答案(共17页).doc(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上7.1.1a)CREATE TABLE Movies (title CHAR(100),year INT,lengthINT,genreCHAR(10),studioNameCHAR(30),producerC#INT,PRIMARY KEY (title, year),FOREIGN KEY (producerC#) REFERENCES MovieExec(cert#);orCREATE TABLE Movies (title CHAR(100),year INT,lengthINT,genreCHAR(10),studioNameCHAR(30),produce
2、rC#INTREFERENCES MovieExec(cert#),PRIMARY KEY (title, year);b)CREATE TABLE Movies (title CHAR(100),year INT,lengthINT,genreCHAR(10),studioNameCHAR(30),producerC#INTREFERENCES MovieExec(cert#)ON DELETE SET NULLON UPDATE SET NULL,PRIMARY KEY (title, year);c)CREATE TABLE Movies (title CHAR(100),year IN
3、T,lengthINT,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, movieYear, starName);e)CREATE T
4、ABLE StarsIn (movieTitleCHAR(100) REFERENCES Movie(title)ON DELETE CASCADE,movieYearINT,starNameCHAR(30),PRIMARY KEY (movieTItle, movieYear, starName);7.1.2 To declare such a foreign-key constraint between the relations Movie and StarsIn, values of the referencing attributes in Movie should appear i
5、n 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 title and year as referencing attributes there is no way of referencing unique movie from StarsIn without
6、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 Product(model);ALTER TABLE Prin
7、terADD FOREIGN KEY (model) REFERENCES Product (model);7.1.4ALTER 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 FOREIGN KEY (ship) REFER
8、ENCES 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 key for Classes.b)ALTER
9、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;7.2.1a)year INTCHECK (year = 1915)b)lengthINTCHECK (length = 60 AND length = 2.0);b)CREATE
10、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 ( modelCHAR(4) CHECK (model IN (SELECT model FROM PCUNION ALL SELECT model FROM laptopUNION ALL SELECT model FROM printer)
11、 );* note this doesnt check the attribute constraint violation caused by deletions from PC, laptop, or printer7.2.3a)CREATE TABLE StarsIn ( starName CHAR(30) CHECK (starName IN (SELECT name FROM MovieStar WHERE YEAR(birthdate) movieYear);b)CREATE TABLE Studio ( address CHAR(255)CHECK (address IS UNI
12、QUE);c)CREATE TABLE MovieStar ( name CHAR(30)CHECK (name NOT IN (SELECT name FROM MovieExec);d)CREATE TABLE Studio ( Name CHAR(30)CHECK (name IN (SELECT studioName FROM Movies);e)CREATE TABLE Movies ( CHECK (producerC# NOT IN (SELECT presC# FROM Studio) OR studioName IN (SELECT name FROM Studio WHER
13、E 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 o WHERE s.name = o.ship AND b.name = o.battle AND s.launched YEAR(b.da
14、te)7.2.6The constraint in Example 7.6 does not allow NULL value for gender while 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 producerCheckFOREIGN KEY (producerC#) REFERENCES MovieExec (cert#);c)A
15、LTER TABLE Movie ADD CONSTRAINT lengthCheckCHECK (length = 60 AND length 14);e)ALTER TABLE Ships ADD CONSTRAINT shipDateCheck CHECK (ship IN (SELECT s.name FROM Ships s, Battles b, Outcomes o WHERE s.name = o.ship AND b.name = o.battle AND s.launched = YEAR(b.date)7.4.1a) CREATE ASSERTION CHECK (NOT
16、 EXISTS ( (SELECT maker FROM Product NATURAL JOIN PC) INTERSECT (SELECT maker FROM Product NATURAL JOIN Laptop) ) );b)CREATE ASSERTION CHECK (NOT EXISTS (SELECT maker FROM Product NATURAL JOIN PC WHERE speed ALL (SELECT L2.speed FROM Product P2, Laptop L2 WHERE P2.maker = maker AND P2.model = L2.mod
17、el ) ) );c)CREATE ASSERTION CHECK (NOT EXISTS (SELECT model FROM Laptop WHERE price = ALL(SELECT price FROM PC WHERE PC.ram = ALL(SELECT COUNT(*) FROM Ships GROUP BY class) );b)CREATE ASSERTION CHECK (NOT EXISTS(SELECT country FROM Classes WHERE type = bb )INTERSECT(SELECT country FROM Classes WHERE
18、 type = bc ) );c)CREATE ASSERTION CHECK (NOT EXISTS(SELECT o.battle FROM Outcomes o, Ships s, Classes c WHERE o.ship = s.name AND s.class = c.class AND c.numGuns 9 ) INTERSECT(SELECT o.battle FROM Outcomes o, Ships s, Classes c WHERE o.result = sunk AND o.ship = s.name AND s.class = c.class AND c.nu
19、mGuns 9 ) );d) CREATE ASSERTION CHECK (NOT EXISTS (SELECT s1.name FROM Ships s1 WHERE s1.launched = )2)presC# INT Check(presC# NOT IN (SELECT cert# FROM MovieExec WHERE netWorth (SELECT AVG(netWorth) FROM MovieExec)DELETE FROM MovieExecWHERE (name, address, cert#, netWorth) IN NewStuff;CREATE TRIGGE
20、R AvgNetWorthTriggerAFTER DELETE ON MovieExecREFERENCINGOLD TABLE AS OldStuffFOR EACH STATEMENTWHEN ( (SELECT AVG(netWorth) FROM MovieExec)INSERT INTO MovieExec(SELECT * FROM OldStuff);7.5.2a)CREATE TRIGGER LowPricePCTriggerAFTER UPDATE OF price ON PCREFERENCING OLD ROW AS OldRow,OLD TABLE AS OldStu
21、ff,NEW ROW AS NewRow,NEW 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 PCREFEREN
22、CINGOLD ROW AS OldRow,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 OutcomesREFERENCING NEW ROW AS NewRowFOR EACH ROWWHEN (NewRow.ship NOT EXISTS (SELECT name
23、 FROM Ships)INSERT INTO Ships (name, class, lunched)VALUES (NewRow.ship, NULL, NULL);CREATE TRIGGER newOutcomesTrigger2AFTER INSERT ON OutcomesREFERENCING NEW ROW AS NewRowFOR EACH ROWWHEN (NewRow.battle NOT EXISTS (SELECT name FROM Battles)INSERT INTO Battles (name, date)VALUES (NewRow.battle, NULL
24、);d) CREATE TRIGGER changeShipTriggerAFTER 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 S
25、hipsREFERENCINGOLD TABLE 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 sunkShipTrigg
26、erAFTER INSERT ON OutcomesREFERENCINGNEW ROW AS NewRowNEW TABLE AS NewStuff FOR EACH ROWWHEN ( (SELECT date FROM Battles WHERE name = NewRow.battle) ALL (SELECT date FROM Battles WHERE name IN (SELECT battle FROM Outcomes WHERE ship = NewRow.ship AND result = “sunk” ) ) ) DELETE FROM OutcomesWHERE (
27、ship, battle, result) IN NewStuff;CREATE 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 date FROM Battles WHERE name IN (SELECT battle FROM Outcomes WHE
28、RE ship = NewRow.ship AND result = “sunk” ) ) ) BEGINDELETE FROM OutcomesWHERE (ship, battle, result) IN NewStuff;INSERT INTO Outcomes(SELECT * FROM OldStuff);END;7.5.4CREATE TRIGGER changeStarsInTriggerAFTER INSERT ON StarsInREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.starName NOT EXISTS
29、(SELECT name FROM MovieStar)INSERT INTO MovieStar(name) VALUES(NewRow.starName);CREATE TRIGGER changeStarsInTrigger2AFTER UPDATE ON StarsInREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.starName NOT EXISTS (SELECT name FROM MovieStar)INSERT INTO MovieStar(name) VALUES(NewRow.starName);b)CREATE TRIGGER changeMovieExecTriggerAFTER INSERT ON MovieExecREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.cert# NOT EXISTS (SELECT presC# FROM Studio) UNION ALL SELECT producerC# FROM Movies) )INSERT INTO Mov
限制150内