CREATE TABLE player ( playerno SMALLINT, pfname VARCHAR(15), plname VARCHAR(20), pdob DATE, PRIMARY KEY (playerno)); CREATE TABLE competition ( compno SMALLINT, location VARCHAR(20), comptitle VARCHAR(20), datebegin DATE, PRIMARY KEY (compno)); CREATE TABLE field ( playerno SMALLINT, compno SMALLINT, PRIMARY KEY (playerno, compno), CONSTRAINT fkplayer1 FOREIGN KEY(playerno) REFERENCES player(playerno), CONSTRAINT fkcomp1 FOREIGN KEY(compno) REFERENCES competition(compno)); CREATE TABLE golfround( roundno SMALLINT, rounddate DATE, compno SMALLINT, PRIMARY KEY (roundno, compno), CONSTRAINT fkcomp2 FOREIGN KEY(compno) REFERENCES competition(compno)); CREATE TABLE hole ( holeno SMALLINT, length SMALLINT, par SMALLINT, roundno SMALLINT, compno SMALLINT, PRIMARY KEY (holeno, roundno, compno), CONSTRAINT fkround FOREIGN KEY(roundno, compno) REFERENCES golfround(roundno, compno)); CREATE TABLE score ( strokes SMALLINT, holeno SMALLINT, roundno SMALLINT, compno SMALLINT, playerno SMALLINT, PRIMARY KEY (holeno, roundno, compno, playerno), CONSTRAINT fkhole FOREIGN KEY(holeno, roundno, compno) REFERENCES hole(holeno, roundno, compno), CONSTRAINT fkplayer2 FOREIGN KEY(playerno) REFERENCES player(playerno));
SELECT itemname FROM item JOIN lineitem ON item.itemno = lineitem.itemno WHERE lineqty > 1;
itemname | lineqty |
---|---|
Safari chair | 50 |
Hammock | 50 |
Tent - 8 person | 8 |
SELECT itemname FROM item
WHERE itemtype = 'F'
AND EXISTS (SELECT * FROM lineitem
WHERE lineitem.itemno = item.itemno); SELECT DISTINCT(itemname) FROM item JOIN lineitem
ON item.itemno = lineitem.itemno
WHERE itemtype = 'F';
itemname |
---|
Hammock |
Safari chair |
Tent - 2 person |
Tent - 8 person |
SELECT saleno, SUM(lineqty * lineprice)
FROM lineitem
GROUP BY saleno;
saleno | sum |
---|---|
1 |
4.50 |
2 |
72.25 |
3 |
502.25 |
4 |
712.25 |
5 |
5109.00 |
One has to create a third entity to store facts about the m:m relationship.
The plus sign near a relationship arc signifies that the identifier of the one-end of the relationship is part of the identifier for the entity at the many-end.
SELECT dphone FROM donor
WHERE dlname = 'Hays' OR dlname = 'Jefts';;
dphone |
---|
1352 |
8103 |
SELECT COUNT(donorno) FROM gift WHERE year = 1999;
expr1000 |
---|
10 |
SELECT SUM (amount) FROM gift WHERE year = 2000;
expr1000 |
---|
$6,768.00 |
SELECT dlname, dfname, AVG(amount)
FROM donor JOIN gift
ON donor.donorno = gift.donorno
GROUP BY dlname, dfname
HAVING AVG(amount) > (SELECT 2*AVG(amount) FROM gift);
dfname |
dlname |
expr1002 |
---|---|---|
Gulsen | Beckman | $2,405.00 |
SELECT DSTATE, SUM(amount)
FROM donor JOIN gift
ON donor.donorno = gift.donorno
WHERE year = 2001 GROUP BY dstate;
dstate | expr1001 |
---|---|
AK | $332.00 |
AZ | $155.00 |
GA | $671.00 |
MA | $499.00 |
MN | $835.00 |
NC | $966.00 |
ND | $345.00 |
WA | $5,208.00 |
This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson |