Lecture05 14f
Lecture05 14f
1
Long Reflection: DB design
• Step 1: Requirements Analysis
– What data to store in the database?
• Step 2: Conceptual Database Design
– Come up with the design: Entity-Relation (ER) model
– Sketch the design with ER diagrams
• Step 3: Logical Database Design
– Implement the design: relational data model
– Map ER diagrams to relational tables
2
Recent Reflection: DB design
• Last lecture:
– Query language: how to ask questions about the [relational] database?
– Mathematical query language: Relational Algebra
• This lecture
– A real query language: SQL (Structured Query Language)
3
Review: Relational Algebra
• A query is applied to table(s), and the result of a query is
also a table.
• Find the names of sailors who have reserved boat 103
πsname((σbid = 103 Reserves) ∞ Sailors)
4
Example Table Definitions
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
5
Review: Relational Algebra
• Basic relational algebra operators:
– Selection (σ, pronounced sigma): Select a subset of rows from a
table.
– Projection (π): Delete unwanted columns from a table.
– Cross-product ( X ): Combine two tables.
– Set-difference ( - ): Tuples in table 1, but not in table 2.
– Union ( U ): Tuples in tables 1 or 2.
6
Review: Relational Algebra (more)
• Additional relational algebra operators:
– Intersection (∩) : tuples in both tables 1 and 2.
– Join (∞): conditional cross product
– Division (/)
– Renaming (p)
• Operations composed into complex query expression
• Query in English?
πsid (σ age > 20 Sailors) –
πsid ((σ color = ‘red’ Boats) ∞ Reserves ∞ Sailors)
7
Relational Algebra to SQL
• Relational operators → SQL commands
Relational Algebra:
πsname (σbid = 103 (Sailors∞ Reserves))
SQL:
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103
• Guess the mapping?
– Notice the difference between SELECT (SQL) and σ
8
W
SQL: Queries, Constraints, Triggers
Chapter 5
9
Lecture Outline
• Basic Query • Null Values
– SELECT • Integrity Constraints
• Set Constructs – CHECK, CREATE ASSERTION
– UNION, INTERSECT,
EXCEPT, IN, ANY, ALL, • Triggers
EXISTS – CREATE TRIGGER, FOR
• Nested Queries EACH ROW
• Aggregate Operators
– COUNT, SUM, AVG, MAX,
MIN, GROUP BY, HAVING
10
Example Table Definitions
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
11
Basic SQL Query
SELECT [DISTINCT] target-list
FROM relation-list
WHERE qualification
12
How to evaluate a query?
SELECT [DISTINCT] target-list
FROM relation-list
WHERE qualification
• Conceptual query evaluation using relational operators:
1) Compute the cross-product of relation-list.
2) Discard resulting tuples if they fail qualifications.
3) Delete attributes that are not in target-list. (called column-list)
4) If DISTINCT is specified, eliminate duplicate rows.
• Only conceptual because of inefficiency computation
– An optimizer can find better strategy
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103
13
Example of Conceptual Evaluation
(1)
SELECT S.sname (1) Compute the cross-
FROM Sailors S, Reserves R product of relation-list.
WHERE S.sid=R.sid AND R.bid=103
Sailors Reserves
sid sname rating age sid bid day
22 dustin 7 45.0 22 101 10/10/96
X
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0
14
Example of Conceptual Evaluation
(2)
SELECT S.sname (2) Discard tuples if they fail
FROM Sailors S, Reserves R qualifications.
WHERE S.sid=R.sid AND R.bid=103
Sailors X Reserves
S.sid sname rating age R.sid bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96 15
Example of Conceptual Evaluation
(3)
SELECT S.sname (3) Delete attribute columns that
FROM Sailors S, Reserves R not in target-list.
WHERE S.sid=R.sid AND R.bid=103 sname
Sailors X Reserves rusty
21
Find sid’s of sailors who’ve reserved
a red and a green boat
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
INTERSECT
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
22
SET Construct: UNION ALL
• UNION, INTERSECT, and EXCEPT delete duplicate by
default.
• To retain duplicates, use UNION ALL, INTERSECT ALL, or
EXCEPT ALL.
24
Nested Queries with Correlation
SELECT S.sname Correlation: subquery finds all
FROM Sailors S reservations for
WHERE EXISTS (SELECT * bid 103 from current sid
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid )
• EXISTS is another set operator, like IN.
– (EXISTS S) returns true when S is not empty.
• What is the above query in English?
– Find sailors who have reserved boat #103
• In case of correlation, subquery must be re-computed for each
Sailors tuple.
25
Nested Queries with UNIQUE
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
26
More on Set-Comparison Operators
• Have seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS, and
NOT UNIQUE.
• Also available: op ANY, op ALL, where op can be >, <, =, ≠, ≤, ≥
– (a > ANY B) returns true when a is greater than any one element in set B.
– (a > ALL B) returns true when a is greater than all elements in set B.
SELECT *
FROM Sailors S
WHERE S.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=‘Horatio’)
• What is the above query in English?
– Find sailors whose rating is greater than that of some sailor called Horatio.
• What is the above query in English if > ANY is replaced by > ALL?
– Find sailors whose rating is greater than all sailors called Horatio.
27
Find sid’s of sailors who’ve reserved a
red and a green boat
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
INTERSECT
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
28
Rewriting INTERSECT Using IN
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
AND S.sid IN (SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R2
Find sids who’ve WHERE S2.sid=R2.sid AND R2.bid=B2.bid
reserved a green AND B2.color=‘green’)
boat
• Find sid’s of Sailors who’ve reserved red but not green boats
(EXCEPT)
– Replace IN with NOT IN.
29
Division in SQL
• Find sailors who’ve SELECT S.sname
reserved all boats. FROM Sailors S
WHERE NOT EXISTS
• Strategy? ((SELECT B.bid
– Find all boats that have FROM Boats B)
EXCEPT
been reserved by a sailor (SELECT R.bid
– Compare with all boats FROM Reserves R
– Do the sailor’s reserved WHERE
R.sid=S.sid))
boats include all boats?
• Yes → include this sailor (A EXCEPT B)
• No → exclude this sailor returns tuples
in A but not in
B.
30
Division in SQL Sailors
sid sname rating age
22 dustin 7 45.0
• Can you do it the hard way, without 31 lubber 8 55.5
EXCEPT & with NOT EXISTS?
• Strategy: Boats
– For each sailor, check that there is no boat
that has not been reserved by this sailor. bid bname color
SELECT S.sname 101 xyz red
FROM Sailors S 103 abc green
WHERE NOT EXISTS (
SELECT B.bid Reserves
FROM Boats B
sid bid day
WHERE NOT EXISTS (
SELECT R.bid 22 101 10/10/96
FROM Reserves R 31 101 11/12/96
WHERE R.bid = B.bid AND R.sid = S.sid)) 31 103 12/12/96
31
Aggregate Operators
• COUNT (*)
• COUNT ( [DISTINCT] A)
– A is a column
• SUM ( [DISTINCT] A)
• AVG ( [DISTINCT] A)
• MAX (A)
• MIN (A)
• Count the number of sailors
SELECT COUNT (*)
FROM Sailors S
32
Find the average age of sailors with
rating = 10
Sailors(sid: integer, sname: string, rating: integer, age: real)
33
Count the number of different sailor
names
Sailors(sid: integer, sname: string, rating: integer, age: real)
SELECT COUNT (DISTINCT S.sname)
FROM Sailors S
34
Find the age of the oldest
sailor
Sailors(sid: integer, sname: string, rating: integer, age: real)
SELECT MAX(S.AGE)
FROM Sailors S
35
Find name and age of the oldest
sailor(s)
SELECT S.sname, MAX (S.age)
FROM Sailors S
• This is illegal, but why?
– Cannot combine a column with a value (unless we use
GROUP BY)
SELECT S.sname, S.age
FROM Sailors S
WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2)
• Okay, but not supported in every system
– Convert a table (of a single aggregate value) into a
single value for comparison
36
GROUP BY and HAVING
• So far, aggregate operators are applied to all (qualifying)
tuples.
– Can we apply them to each of several groups of tuples?
• Example: find the age of the youngest sailor for each
rating level.
– In general, we don’t know how many rating levels exist, and what
the rating values for these levels are!
– Suppose we know that rating values go from 1 to 10; we can write
10 queries that look like this:
SELECT MIN (S.age)
For i = 1, 2, ... , 10: FROM Sailors S
WHERE S.rating = i
37
Find the age of the youngest sailor
for each rating level
Sid Sname Rating Age
SELECT S.rating, MIN (S.age) as age
FROM Sailors S 22 Dustin 7 45.0
GROUP BY S.rating 31 Lubber 8 55.5
(1) The sailors tuples are put into “same 85 Art 3 25.5
rating” groups. 32 Andy 8 25.5
(2) Compute the Minimum age for each 95 Bob 3 63.5
rating group.
Rating Age
3 25.5
Rating Age (1)
3 63.5
3 25.5 (2) 7 45.0
7 45.0 8 55.5
38
8 25.5 8 25.5
Find the age of the youngest sailor for each
rating level that has at least 2 members
SELECT S.rating, MIN (S.age) as Sid Sname Rating Age
minage 22 Dustin 7 45.0
FROM Sailors S 31 Lubber 8 55.5
GROUP BY S.rating 85 Art 3 25.5
HAVING COUNT(*) > 1
32 Andy 8 25.5
1. The sailors tuples are put into
“same rating” groups. 95 Bob 3 63.5
2. Eliminate groups that have < 2 Rating Age
members. 3 25.5
3. Compute the Minimum age for Rating Minage 3 63.5
each rating group. 3 25.5 7 45.0
8 25.5 8 55.5
39
8 25.5
Queries With GROUP BY and
HAVING
SELECT [DISTINCT] target-list
FROM relation-list SELECT S.rating, MIN (S.age)
WHERE qualification as age
GROUP BY grouping-list FROM Sailors S
HAVING group-qualification GROUP BY S.rating
HAVING S.rating > 5
• The target-list contains (i) attribute names (ii) terms with
aggregate operations (e.g., AVG (S.age)).
• The attribute list (e.g., S.rating) in target-list must be in
grouping-list.
• The attributes in group-qualification must be in grouping-
list.
40
Say if Attribute list is not in
grouping-list
Sid Sname Rating Age
SELECT S.sname, S.rating,
AVG (S.age) as age 22 Dustin 7 45.0
FROM Sailors S 31 Lubber 8 55.5
GROUP BY S.rating 85 Art 3 25.5
HAVING COUNT(S.rating) > 1
32 Andy 8 25.5
95 Bob 3 63.5
Sname Rating Age
Art 3 25.5
Sname Rating Age
Bob 3 63.5
? 3 44.5
Dustin 7 45.0
? 8 40.5
Lubber 8 55.5
41
Andy 8 25.5
Say if attributes in the Group
qualification is not in grouping-list
Sid Sname Rating Age
SELECT S.rating, AVG (S.age) 22 Dustin 7 45.0
as age 31 Lubber 8 55.5
FROM Sailors S
85 Art 3 25.5
GROUP BY S.rating
HAVING S.sname ≠ ‘Dustin’ 32 Andy 8 25.5
95 Bob 3 63.5
Sname Rating Age
? Art 3 25.5
Rating Age
Bob 3 63.5
Dustin 7 45.0
Lubber 8 55.5
42
Andy 8 25.5
Conceptual Evaluation
• Without GROUP BY and HAVING:
– Compute cross-product of relation-list
– Remove tuples that fail qualification
– Delete unnecessary columns
• With GROUP BY and HAVING, continue with
– Partition remaining tuples into groups by the value of attributes in
grouping-list (specified in GROUP-BY clause)
– Remove groups that fail group-qualification (specified in HAVING
clause).
– Compute one answer tuple per qualifying group.
43
For each red boat, find the number
of reservations for this boat
SELECT B.bid, COUNT (*) AS SELECT B.bid, COUNT (*) AS
num_reservations num_reservations
FROM Boats B, Reserves R FROM Boats B, Reserves R
WHERE R.bid=B.bid AND WHERE R.bid=B.bid
B.color=‘red’ GROUP BY B.bid
GROUP BY B.bid HAVING B.color=‘red’
• Illegal, why?
– B.color does not appear in
group-list
44
Find the age of the youngest sailor with age > 18 for
each rating with at least 2 sailors (of any age)
46
CREATE TABLE Sailors
Table Constraints ( sid INTEGER,
sname CHAR(10),
rating INTEGER,
• Specify constraints over a age REAL,
single table PRIMARY KEY (sid),
– Useful when more general CHECK ( rating >= 1
ICs than keys are involved. AND rating <= 10 )
CREATE TABLE Reserves
( sname CHAR(10), The boat
‘Interlake’ cannot
bid INTEGER,
• Constraints can be day DATE,
be reserved
named. PRIMARY KEY (bid,day),
CONSTRAINT noInterlakeRes
CHECK (`Interlake’ ≠
( SELECT R.bname
FROM Reservers R
WHERE R.bid=bid)))
47
Assertions: Constraints Over
Multiple Tables
CREATE TABLE Sailors Number of boats
( sid INTEGER, plus number of
• Awkward and sname CHAR(10), sailors is < 100
rating INTEGER,
wrong!
age REAL,
– If Sailors is empty,
PRIMARY KEY (sid),
the number of CHECK
Boats tuples can( (SELECT COUNT (S.sid) FROM Sailors S)
be anything! + (SELECT COUNT (B.bid) FROM Boats B) < 100 )
• ASSERTION is the
right solution; not
CREATE ASSERTION smallClub
associated withCHECK
either table. ( (SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )
48
Triggers
• Trigger: procedure that starts automatically if specified
changes occur to the DBMS
• A trigger has three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run)
– Action (what happens if the trigger runs)
51
Starwar Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
52
Starwar Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
SELECT name
FROM characters c
WHERE not exists (
SELECT p.name FROM planets p
WHERE affiliation='rebels' and p.name NOT IN
(SELECT pname from timetable t where
t.cname=c.name and t.pname=p.name))
53
Starwar Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
54
Starwar Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
• For each character and for each neutral planet, how much
time total did the character spend on the planet?
55