CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II...

32
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/ ~tina.tian

Transcript of CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II...

Page 1: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

CMPT 258 Database SystemsSQL: Queries, Constraints, Triggers(Chapter 5) Part IIhome.manhattan.edu/~tina.tian

Page 2: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.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

Page 3: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

3

Agenda

•Introduction•Basic SQL Query•Union, Intersection and Except•Nested Queries•Aggregate Operations•Complex Integrity Constraints in SQL•Triggers

Page 4: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 5: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

•Union (supported in MySQL)•Intersect•Except/Minus

5

Page 6: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 7: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’

Page 8: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’

Page 9: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 10: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 11: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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?

Page 12: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 13: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 14: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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?

Page 15: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

15

•Q: Find the names of sailors who have reserved a red boat

Page 16: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

16

•Q: Find the names of sailors who have not reserved a red boat

Page 17: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 18: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

Rewriting EXCEPT Queries

Q: Find sid’s of sailors who’ve reserved a red but not a green boat:

Page 19: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’

Page 20: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

20

Find the names of sailors who’ve reserved a red and a green boat

Page 21: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’)

Page 22: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’)

Page 23: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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’)

Page 24: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

24

Set-Comparison Operators•Q: Find the sailors with the highest

rating.

Page 25: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

Summary

•ANY•ALL•>,>=,<,<= [ANY/ALL]•IN is equivalent to =ANY•NOT IN is equivalent to <>ANY

25

Page 26: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 27: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 28: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 29: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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

Page 30: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

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.

Page 31: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

•IN•NOT IN•EXISTS•NOT EXISTS

31

Page 32: CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

Readings

•Chapter 5