A First Course in Database Systems |
CREATE TABLE Movie ( title VARCHAR(255), year INTEGER, length INTEGER, inColor BIT(1), studioName CHAR(50), producerC# INTEGER, PRIMARY KEY (title, year) ); CREATE TABLE StarsIn ( movieTitle VARCHAR(255), movieYear INTEGER, starName CHAR(30) ); CREATE TABLE MovieExec ( name CHAR(30), address VARCHAR(255), cert# INTEGER PRIMARY KEY, netWorth INTEGER ); CREATE TABLE Studio ( name CHAR(50) PRIMARY KEY, address VARCHAR(255), presC# INTEGER );We have chosen not to add
PRIMARY KEY (movieTitle, movieYear, starName)to the declaration of StarsIn, although we could. Since that relation is likely to be used for going between movies and stars or vice versa, we think an index, which the DBMS might create for us were we to declare a primary key, will be of no use and may waste time.
CREATE TABLE Ships ( name CHAR(30), class CHAR(30) REFERENCES Classes(class), launched INTEGER );In addition to the above declaration,
class
must be
declared the primary key for Classes.
CREATE TABLE Movie ( ... year INTEGER CHECK (year >= 1895), ... )Exercise 6.3.1(c)
Revised 1/9/99.
CREATE TABLE Studio ( name CHAR(30) CHECK (name IN ('Disney', 'Fox', 'MGM', 'Paramount')), ... )Solutions for Section 6.4
Exercise 6.4.2(a)
Revised 9/13/99.Add to the declaration of
Movie
the tuple-based check constraint:CHECK (NOT inColor OR year >= 1939)Exercise 6.4.2(d)
Add to the declaration ofMovieStar
:CHECK (name NOT IN (SELECT name FROM MovieExec))Also, add to the declaration ofMovieExec
:CHECK (name NOT IN (SELECT name FROM MovieStar))Exercise 6.4.3(a)
CREATE ASSERTION CHECK (NOT EXISTS ( (SELECT maker FROM Product NATURAL JOIN PC) INTERSECT (SELECT maker FROM Product NATURAL JOIN Laptop) ) );Exercise 6.4.3(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 ) ) );Solutions for Section 6.5
Exercise 6.5.1(a)
ALTER TABLE Movie ADD CONSTRAINT MovieKey PRIMARY KEY (title, year);Exercise 6.5.1(d)
ALTER TABLE MovieStar ADD CONSTRAINT c1 CHECK (name NOT IN (SELECT name FROM MovieExec)); ALTER TABLE MovieExec ADD CONSTRAINT c2 CHECK (name NOT IN (SELECT name FROM MovieStar));Solutions for Section 6.6
Updated 5/23/97.
Exercise 6.6.2(a)
CREATE TRIGGER LowerPriceTrigger AFTER UPDATE OF price ON PC REFERENCING OLD AS OldTuple NEW AS NewTuple WHEN(NOT(NewTuple.price <= ALL (SELECT price FROM PC WHERE speed = NewTuple.speed ) ) ) DELETE FROM PC WHERE (model, speed, ram, hd, cd, price) IN NewTuple; INSERT INTO PC (SELECT * FROM OldTuple);Exercise 6.6.3(a)
CREATE TRIGGER NewClassTrigger AFTER INSERT ON Classes REFERENCING NEW AS NewClass WHEN(TRUE) INSERT INTO Ships(name, class) (SELECT class, class FROM NewClass );