CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II...
-
Upload
gerard-david-cook -
Category
Documents
-
view
222 -
download
5
Transcript of CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II...
CMPT 258 Database SystemsSQL: Queries, Constraints, Triggers(Chapter 5) Part IIhome.manhattan.edu/~tina.tian
Tutors Available!
•M 10-11am•T 3-4pm•W 12-1pm, 1-2pm•R 2-3pm•F 10-11am
•RLC 203
3
Agenda
•Introduction•Basic SQL Query•Union, Intersection and Except•Nested Queries•Aggregate Operations•Complex Integrity Constraints in SQL•Triggers
Examples
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
sid bid day
22 101 10/10/9658 103 11/12/96
Reserves
Sailors
Boats
4
•Union (supported in MySQL)•Intersect•Except/Minus
5
Find sid’s of sailors who’ve reserved a red or a green boat
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’UNIONSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
supported in MySQL
Find sid’s of sailors who’ve reserved a red and a green boat
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’INTERSECTSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
Find sid’s of sailors who’ve reserved a red boat but not a green boat
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’EXCEPTSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
Example
SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid = R.sid AND R.bid=103
Find names of sailors who’ve reserved boat #103:
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
ReservesSailors
Nested Queries
• Understand semantics of nested queries ▫For each Sailors tuple, check
the qualification by computing the subquery.
SELECT snameFROM Sailors WHERE sid IN (SELECT sid FROM Reserves WHERE bid=103)
Find names of sailors who’ve reserved boat #103:
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
Nested QueriesFind names of sailors who’ve NOT reserved boat #103:
SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid = R.sid AND R.bid<>103
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
ReservesSailors
Is this correct?
Nested Queries
“Find names of sailors whose ids do not appear amongthe ids that reserved boat #103”is different with“Find names of sailors who have reserved a boat that is not 103”
Find names of sailors who’ve NOT reserved boat #103:
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
ReservesSailors
Nested Queries
SELECT snameFROM Sailors WHERE sid NOT IN (SELECT sid FROM Reserves WHERE bid=103)
Find names of sailors who’ve NOT reserved boat #103:
A very powerful feature of SQL: a WHERE clause can itself contain an SQL query!
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
14
•Q: Find the sids of sailors who have reserved a red boat
SELECT R.sidFROM Reserves R, Boats BWHERE R.bid = B.bid and B.color=‘red’
sid bid day
22 101 10/10/9658 103 11/12/96
Reserves
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
Boats
Rewrite the queryusing IN?
15
•Q: Find the names of sailors who have reserved a red boat
16
•Q: Find the names of sailors who have not reserved a red boat
Rewriting INTERSECT Queries Using INQ: Find sid’s of sailors who’ve reserved both a red and a green boat:
sid bid day
22 101 10/10/9658 103 11/12/96
Reserves
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
Boats
Rewriting EXCEPT Queries
Q: Find sid’s of sailors who’ve reserved a red but not a green boat:
19
Find the names of sailors who’ve reserved a red and a green boat
• Is this query correct? Why
SELECT S.snameFROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
20
Find the names of sailors who’ve reserved a red and a green boat
Set-Comparison Operators•Also available: op ANY, op ALL
▫OP is one of , <>•Q: Find sailors whose rating is equal to that
of some sailor called Horatio:
• Can you rewrite the query without using set-comparison operators?
, , , , ,
SELECT *FROM Sailors WHERE rating = ANY (SELECT rating FROM Sailors WHERE sname=‘Horatio’)
Set-Comparison Operators
•IN is equivalent to =ANY•NOT IN is equivalent to <>ANY
SELECT *FROM Sailors WHERE rating <> ANY (SELECT rating FROM Sailors WHERE sname=‘Horatio’)
23
Set-Comparison Operators•Q: Find sailors whose rating is better than
every sailor called Horatio.SELECT *FROM Sailors WHERE rating > ALL (SELECT rating FROM Sailors WHERE sname=‘Horatio’)
24
Set-Comparison Operators•Q: Find the sailors with the highest
rating.
Summary
•ANY•ALL•>,>=,<,<= [ANY/ALL]•IN is equivalent to =ANY•NOT IN is equivalent to <>ANY
25
Nested Queries with Correlation
SELECT S.snameFROM Sailors SWHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)
Q: Find names of sailors who’ve reserved boat #103:
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
ReservesSailors
Nested Queries with Correlation
SELECT S.snameFROM Sailors SWHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)
Q: Find names of sailors who’ve reserved boat #103:
SELECT S.snameFROM Sailors SWHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103
SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid and R.bid=103
Nested Queries with Correlation
• EXISTS is another set comparison operator, like IN.
• NOT EXISTS
SELECT S.snameFROM Sailors SWHERE NOT EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)
Q: Find names of sailors who’ve not reserved boat #103:
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
Reserves Sailors
Division in SQL
Find names of sailors thatthere is no boat that has not been reserved by this sailor
Q: Find names of sailors who’ve reserved all boats.
sid bid day
22 101 10/10/9658 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0Reserves
Sailors
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
Boats
Division in SQL
SELECT S.snameFROM Sailors SWHERE NOT EXISTS
(SELECT bidFROM BoatsWHERE bid NOT IN
(SELECT R.bidFROM Reserves RWHERE R.sid=S.sid))
For each sailor we check that there is no boat that has not been reserved by this sailor
Q: Find names of sailors who’ve reserved all boats.
•IN•NOT IN•EXISTS•NOT EXISTS
31
Readings
•Chapter 5