版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、a)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),producerC#INTREFERENCES M
2、ovieExec(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 INT,lengthINT,genreC
3、HAR(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 TABLE StarsIn (movi
4、eTitleCHAR(100) REFERENCES Movie(title)ON DELETE CASCADE,movieYearINT,starNameCHAR(30),PRIMARY KEY (movieTItle, movieYear, starName); To declare such a foreign-key constraint between the relations Movie and StarsIn, values of the referencing attributes in Movie should appear in MovieStar as unique v
5、alues. 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 starName information. T
6、herefore, such a constraint can not be expressed using a foreign-key constraint.ALTER 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 PrinterADD FOREIGN KEY (model) R
7、EFERENCES 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 FOREIGN KEY (ship) REFERENCES Ships (name);ALTER TABLE Ou
8、tcomesADD FOREIGN KEY (battle) REFERENCES Battles (name);a)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 TABLE OutcomeADD FOREIGN KEY (battle)
9、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)year INTCHECK (year >= 1915)b)lengthINTCHECK (length >= 60 AND length <= 250)c)studioNameCHAR(30) CHECK (studioName IN
10、 (Disney, Fox, MGM, Paramount) )a)CREATE TABLE Laptop ( speedDECIMAL(4,2)CHECK (speed >= 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 ( modelCHAR(4) C
11、HECK (model IN (SELECT model FROM PCUNION ALL SELECT model FROM laptopUNION ALL SELECT model FROM printer) );* note this doesnt check the attribute constraint violation caused by deletions from PC, laptop, or printera)CREATE TABLE StarsIn ( starName CHAR(30) CHECK (starName IN (SELECT name FROM Movi
12、eStar WHERE YEAR(birthdate) > movieYear);b)CREATE TABLE Studio ( address CHAR(255)CHECK (address IS UNIQUE);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 Mo
13、vies ( CHECK (producerC# NOT IN (SELECT presC# FROM Studio) OR studioName IN (SELECT name FROM Studio WHERE presC# = producerC#); a)CHECK (speed >= 2.0 OR price <= 600)b)CHECK (screen >= 15 OR hd >= 40 OR price <= 1000)a)CHECK (class NOT IN (SELECT class FROM Classes WHERE bore > 1
14、6)b)CHECK (class NOT IN (SELECT class FROM ClassesWHERE numGuns > 9 AND bore > 14)c) CHECK (ship IN (SELECT FROM Ships s, Battles b, Outcomes o WHERE = o.ship AND = o.battle AND s.launched > YEAR(b.date)The constraint in Example 7.6 does not allow NULL value for gender
15、while the constraint in Example 7.8 allows NULL.a)ALTER TABLE Movie ADD CONSTRAINT myKey PRIMARY KEY (title, year);b)ALTER TABLE Movie ADD CONSTRAINT producerCheckFOREIGN KEY (producerC#) REFERENCES MovieExec (cert#);c)ALTER TABLE Movie ADD CONSTRAINT lengthCheckCHECK (length >= 60 AND length <
16、;= 250);d)ALTER TABLE MovieStar ADD CONSTRAINT noDupInExec CHECK (name NOT IN (SELECT name FROM MovieExec);ALTER TABLE MovieExec ADD CONSTRAINT noDupInStar CHECK (name NOT IN (SELECT name FROM MovieStar);e)ALTER TABLE Studio ADD CONSTRAINT noDupAddrCHECK (address is UNIQUE);a)ALTER TABLE Classes ADD
17、 CONSTRAINT myKey PRIMARY KEY (class, country);b)ALTER TABLE Outcomes ADD CONSTRAINT battleCheckFOREIGN KEY (battle) REFERENCES Battles (name);c)ALTER TABLE Outcomes ADD CONSTRAINT shipCheckFOREIGN KEY (ship) REFERENCES Ships (name);d)ALTER TABLE Ships ADD CONSTRAINT classGunCheckCHECK (class NOT IN
18、 (SELECT class FROM Classes WHERE numGuns > 14);e)ALTER TABLE Ships ADD CONSTRAINT shipDateCheck CHECK (ship IN (SELECT FROM Ships s, Battles b, Outcomes o WHERE = o.ship AND = o.battle AND s.launched >= YEAR(b.date)a) CREATE ASSERTION CHECK (NOT EXISTS ( (SELECT maker FRO
19、M 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.model ) ) );c)CREATE ASSE
20、RTION CHECK (NOT EXISTS (SELECT model FROM Laptop WHERE price <= ALL(SELECT price FROM PC WHERE PC.ram < Laptop.ram) ) ) );d)CREATE ASSERTION CHECK (EXISTS (SELECT p2.model FROM Product p1, PC p2 WHERE p1.type = pc AND P1.model = p2.model) UNION ALL (SELECT l.model FROM Product p, Laptop l WHE
21、RE p.type = laptop AND p.model = l.model) UNION ALL (SELECT p2.model FROM Product p1, Printer p2 WHERE p1.type = printer AND P1.model = p2.model) ); a)CREATE ASSERTION CHECK ( 2 >= ALL(SELECT COUNT(*) FROM Ships GROUP BY class) );b)CREATE ASSERTION CHECK (NOT EXISTS(SELECT country FROM Classes WH
22、ERE type = bb )INTERSECT(SELECT country FROM Classes WHERE type = bc ) );c)CREATE ASSERTION CHECK (NOT EXISTS(SELECT o.battle FROM Outcomes o, Ships s, Classes c WHERE o.ship = AND s.class = c.class AND c.numGuns > 9 ) INTERSECT(SELECT o.battle FROM Outcomes o, Ships s, Classes c WHERE o.r
23、esult = sunk AND o.ship = AND s.class = c.class AND c.numGuns < 9 ) );d) CREATE ASSERTION CHECK (NOT EXISTS (SELECT FROM Ships s1 WHERE s1.launched < (SELECT s2.launched FROM Ships s2 WHERE = s1.class ) ) ); e)CREATE ASSERTION CHECK (ALL (SELECT class FROM Classes c) IN
24、(SELECT class FROM Ships GROUP BY class) ); 1)presC# INT CHECK(presC# IN (SELECT cert# FROM MovieExec WHERE netWorth >= 10000000 )2)presC# INT Check(presC# NOT IN (SELECT cert# FROM MovieExec WHERE netWorth < 10000000 )CREATE TRIGGER AvgNetWorthTriggerAFTER INSERT ON MovieExecREFERENCINGNEW TA
25、BLE AS NewStuffFOR EACH STATEMENTWHEN (500000 > (SELECT AVG(netWorth) FROM MovieExec)DELETE FROM MovieExecWHERE (name, address, cert#, netWorth) IN NewStuff;CREATE TRIGGER AvgNetWorthTriggerAFTER DELETE ON MovieExecREFERENCINGOLD TABLE AS OldStuffFOR EACH STATEMENTWHEN (500000 > (SELECT AVG(ne
26、tWorth) FROM MovieExec)INSERT INTO MovieExec(SELECT * FROM OldStuff);a)CREATE TRIGGER LowPricePCTriggerAFTER UPDATE OF price ON PCREFERENCING OLD ROW AS OldRow,OLD TABLE AS OldStuff,NEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWWHEN (NewRow.price < ALL(SELECT PC.price FROM PC WHERE PC.speed
27、= NewRow.speed)BEGINDELETE FROM PCWHERE (model, speed, ram, hd, price) IN NewStuff;INSERT INTO PC(SELECT * FROM OldStuff);END;b)CREATE TRIGGER NewPrinterTriggerAFTER INSERT ON PrinterREFERENCINGNEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWWHEN (NOT EXISTS (SELECT * FROM Product WHERE Product.m
28、odel = NewRow.model)DELETE FROM PrinterWHERE (model, color, type, price) IN NewStuff;c)CREATE TRIGGER AvgPriceTriggerAFTER UPDATE OF price ON LaptopREFERENCINGOLD TABLE AS OldStuff,NEW TABLE AS NewStuffFOR EACH STATEMENTWHEN (1500 > (SELECT AVG(price) FROM Laptop)BEGINDELETE FROM LaptopWHERE (mod
29、el, speed, ram, hd, screen, price) IN NewStuff;INSERT INTO Laptop(SELECT * FROM OldStuff);END;d) CREATE TRIGGER HardDiskTriggerAFTER UPDATE OF hd, ram ON PCREFERENCINGOLD ROW AS OldRow,OLD TABLE AS OldStuff,NEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWWHEN (NewRow.hd < NewRow.ram * 100)BEGI
30、NDELETE FROM PCWHERE (model, speed, ram, hd, price) IN NewStuff;INSERT INTO PC(SELECT * FROM OldStuff);END;e)CREATE TRIGGER DupModelTriggerBEFORE INSERT ON PC, Laptop, PrinterREFERENCINGNEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWWHEN (EXISTS (SELECT * FROM NewStuff NATUAL JOIN PC) UNION ALL
31、(SELECT * FROM NewStuff NATUAL JOIN Laptop) UNION ALL (SELECT * FROM NewStuff NATUAL JOIN Printer)BEGINSIGNAL SQLSTATE 10001 (Duplicate Model Insert Failed);END;a)CREATE TRIGGER NewClassTriggerAFTER INSERT ON ClassesREFERENCINGNEW ROW AS NewRowFOR EACH ROWBEGININSERT INTO Ships (name, class, lunched
32、)VALUES (NewRow.class, NewRow.class, NULL);END;b)CREATE TRIGGER ClassDisTriggerBEFORE INSERT ON ClassesREFERENCINGNEW ROW AS NewRow,NEW TABLE AS NewStuffFOR EACH ROWWHEN (NewRow.displacement > 35000)UPDATE NewStuff SET displacement = 35000;c) CREATE TRIGGER newOutcomesTriggerAFTER INSERT ON Outco
33、mesREFERENCING NEW ROW AS NewRowFOR EACH ROWWHEN (NewRow.ship NOT EXISTS (SELECT name 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
34、(SELECT name FROM Battles)INSERT INTO Battles (name, date)VALUES (NewRow.battle, NULL);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 ShipsWH
35、ERE (name, class, launched) IN NewStuff;CREATE TRIGGER changeShipTrigger2AFTER UPDATE ON ShipsREFERENCINGOLD 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, laun
36、ched) IN NewStuff;INSERT INTO Ships(SELECT * FROM OldStuff);END;e) CREATE TRIGGER sunkShipTriggerAFTER 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
37、 battle FROM Outcomes WHERE ship = NewRow.ship AND result = “sunk” ) ) ) DELETE FROM OutcomesWHERE (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 WHER
38、E name = NewRow.battle) < ALL (SELECT date FROM Battles WHERE name IN (SELECT battle FROM Outcomes WHERE ship = NewRow.ship AND result = “sunk” ) ) ) BEGINDELETE FROM OutcomesWHERE (ship, battle, result) IN NewStuff;INSERT INTO Outcomes(SELECT * FROM OldStuff);END;CREATE TRIGGER changeStarsInTrig
39、gerAFTER INSERT ON StarsInREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.starName NOT EXISTS (SELECT name FROM MovieStar)INSERT INTO MovieStar(name) VALUES(NewRow.starName);CREATE TRIGGER changeStarsInTrigger2AFTER UPDATE ON StarsInREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.starNam
40、e 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) )IN
41、SERT INTO Movies(procucerC#) VALUES(NewRow.cert#);* insert into the relation Movies rather than Studio since theres no associated info with Studio.CREATE TRIGGER changeMovieExecTrigger2AFTER UPDATE ON MovieExecREFERENCINGNEW ROW AS NewRow,FOR EACH ROWWHEN (NewRow.cert# NOT EXISTS (SELECT presC# FROM
42、 Studio) UNION ALL SELECT producerC# FROM Movies) )INSERT INTO Movies(procucerC#) VALUES(NewRow.cert#);c)CREATE TRIGGER changeMovieTriggerAFTER DELETE ON MovieStarREFERENCINGOLD TABLE AS OldStuff,FOR EACH STATEMENTWHEN ( 1 > ALL (SELECT COUNT(*) FROM StarIn s, MovieStar mWHERE s.starName = GROUP BY s.movieTitle, m.gendar) )INSERT INTO MovieStar(SELECT * FROM OldStuff);* only con
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度新能源项目开发合同项目投资估算与风险评估
- 2024年度版权许可使用合同标的:音乐作品版权3篇
- 泡茶课件教学课件
- 物流信息技术与应用 课件 9.项目九 数据交换与共享技术 下
- 《餐饮单位索证索票》课件
- 2024年度租赁合同租金调整及违约金规定3篇
- 2024年度教育培训与人才输出服务合同2篇
- 《s教学课件选区》课件
- 《公务员保险与福利》课件
- 体位摆放课件
- 车间加工生产记录表格模板
- 大兔子和小兔子的故事 完整版
- 托克托发电公司二期水处理200吨除盐水系统方案优化
- 强夯地基处理技术规程
- (精选)一年级班干部竞选演讲稿范文5篇
- 物品出入库明细表格
- 加固柱抹灰施工方案
- 清华大学博士学位论文自动格式模板
- 什么是Framelock(帧锁定)与Genlock(同步锁定)
- 外立面幕墙拆除方案
- 雪地里的小画家评课稿
评论
0/150
提交评论