create table R ( id integer, h varchar(20), primary key (id) ); create table S ( id integer, j char(4) check (j >= 'AAAA' and j <= 'ZZZZ') primary key (id), foreign key (id) references R(id) ); create table T ( id integer, k float check (k >= 1.0 and k <= 5.0), primary key (id), foreign key (id) references R(id) );This schema cannot represent:
create table R (
id integer,
h varchar(20),
kind char(1) not null check (kind in ('S','T')),
j char(4) check (j >= 'AAAA' and j <= 'ZZZZ')
k float check (k >= 1.0 and k <= 5.0),
primary key (id)
);
This schema cannot represent:
create table F (
a integer,
c text,
R integer not null,
d text,
primary key (a),
foreign key (R) references G(b)
);
create table G (
b integer,
e text,
primary key (b)
);
create or replace function
seatsAvail(flid integer) return integer
as
totSeats integer;
bookedSeats integer;
begin
select p.nseats into totSeats
from Flights f, Planes p
where f.id = flid and f.plane = p.id;
select count(b.pax) into bookedSeats
from Bookings b
where b.flight = flid;
return totSeats - bookedSeats;
end;
/
create trigger insertFlightTrigger
before insert on Flights
for each row
declare
ns integer;
begin
-- if no such plane, then exception terminates insert
select nseats into ns from Planes where id = :new.plane;
:new.seatsAvail := ns;
end;
create trigger insertBookingTrigger
after insert on Bookings
for each row
begin
update Flights set seatsAvail = seatsAvail - 1 where id = :new.flight;
end;
create trigger deleteBookingTrigger
after delete on Bookings
for each row
begin
update Flights set seatsAvail = seatsAvail + 1 where id = old.flight;
end;
Assumes that the interface (and domain constraints) prevents INSERT
operations being invoked on a full flight.
Pcode -> Product Pcode -> Price Cust# -> Customer Cust# -> Address Cust# -> Phoneor, to simplify and use question notation
(a) Pc -> Pr Pe (b) C# -> Cu Ad Ph
R = (T, Pr, Pc, Pe, Q, C#, Cu, Ad, Ph) with key = (Pc,C#,T,Q)Existence of FD (a) means R is not in BCNF (partial-key dependence), so decompose to
R' = (T, Pc, Q, C#, Cu, Ad, Ph) with key = (Pc,C#,T,Q) P = (Pc, Pr, Pe) with key = (Pc)Table P is already in BCNF (all non-key attributes depend only on whole key)
R'' = (T, Pc, Q, C#) with key = (Pc,C#,T,Q) C = (C#, Cu, Ad, Ph) with key = (C#)Both of the above tables are in BCNF (no FDs violate BCNF rules), so the final schema is:
Prod = (Pc, Pr, Pe) Cust = (C#, Cu, Ad, Ph) Sale = (T, Pc, Q, C#)Allowable variations:
Res = Proj[studio](Sel[director='Peter Weir'](Movie)
PFilms = Proj[title,year](Sel[studio='Paramount'](Movie)) PFilms' = Rename[mtitle,myear](PFilms) Res = Proj[actor](Starring Join Pfilms')
TFilms = Proj[mtitle,myear](Sel[actor='Tom Cruise'](Starring)) NFilms = Proj[mtitle,myear](Sel[actor='Nicole Kidman'](Starring)) Res = TFilms Intersect NFilms
KFilms = Proj[title,year](Sel[director='Stanley Kubrick'](Movie)) KFilms' = Rename[mtitle,myear](KFilms) Res = Starring Divides KFilms'