CSC 411/511: DBMS Design
Dr. Nan Wang CSC411_L6_SQL(1)1
SQL: Queries, Constraints, Triggers
Chapter 5 – Part 1
CSC411_L6_SQL(1)Dr. Nan Wang22
Agenda
• Introduction• Basic SQL Query• Union, Intersection and Except• Nested Queries• Aggregate Operations• Null Values• Complex Integrity Constraints in SQL• Triggers and Active Databases• Design Active Database
CSC411_L6_SQL(1)Dr. Nan Wang3
About the examples
3
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0 sid bid day
22 101 10/10/9658 103 11/12/96
CSC411_L6_SQL(1)Dr. Nan Wang4
Example Instances
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0
sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0
sid bid day
22 101 10/10/9658 103 11/12/96
R1
S1
S2
• We will use these instances of the Sailors and Reserves relations in our examples.
• If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ?
CSC411_L6_SQL(1)Dr. Nan Wang5
Basic SQL Query
• relation-list A list of relation names (possibly with a range-variable after each name).
• target-list A list of attributes of relations in relation-list
• qualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND, OR and NOT.
• DISTINCT is an optional keyword indicating that the answer should not contain duplicates. – Default is that duplicates are not eliminated!
SELECT [DISTINCT] target-listFROM relation-listWHERE qualification
, , , , ,
CSC411_L6_SQL(1)Dr. Nan Wang66
CSC411_L6_SQL(1)Dr. Nan Wang77
CSC411_L6_SQL(1)Dr. Nan Wang8
Query FROM More than One Table Using JOINs
• A join combines the rows of two tables, based on a rule called a join condition; this compares values from the rows of both tables to determine which rows should be joined.
• There are three basic types of join:– inner join, created with the INNER JOIN keywords– outer join, which comes in three varieties:
• LEFT OUTER JOIN • RIGHT OUTER JOIN • FULL OUTER JOIN
– cross join, created with the CROSS JOIN keywords
8
CSC411_L6_SQL(1)Dr. Nan Wang9
Inner Join
• Select a, b• From A, B• Where A.a=B.b
9
CSC411_L6_SQL(1)Dr. Nan Wang1010
CSC411_L6_SQL(1)Dr. Nan Wang1111
CSC411_L6_SQL(1)Dr. Nan Wang12
• Select a, b• From A, B
12
CSC411_L6_SQL(1)Dr. Nan Wang13
Conceptual Evaluation Strategy
• Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:– Compute the cross-product of relation-list.– Discard resulting tuples if they fail qualifications.– Delete attributes that are not in target-list.– If DISTINCT is specified, eliminate duplicate rows.
• This strategy is probably the least efficient way to compute a query! – An optimizer will find more efficient strategies to compute
the same answers.
CSC411_L6_SQL(1)Dr. Nan Wang14
Example of Conceptual Evaluation
(sid) sname rating age (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
Select S.snameFrom Sailors as S inner join Reserves as R on S.sid = R.sidWhere R.bid = 103
CSC411_L6_SQL(1)Dr. Nan Wang15
A Note on Range Variables
• Really needed only if the same relation appears twice in the FROM clause.
• The previous query can also be written as:
SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND bid=103
SELECT snameFROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103
It is good style,however, to userange variablesalways!
OR
CSC411_L6_SQL(1)Dr. Nan Wang1616
Query Examples
• Find the sids of sailors who have reserved a red boat (Q16)
• Find the name of sailors who have reserved a red boat (Q2)
SELECT B.sidFROM Reserves R, Boats BWHERE R.bid=B.bid AND B.color = ‘red’
SELECT S.snameFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND R.bid = B.bid AND B.color = ‘red’
CSC411_L6_SQL(1)Dr. Nan Wang17
• Find the color of the boats reserved by Lubber (Q3)
17
SELECT B.colorFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND R.bid = B.bid AND S.name = ‘Lubber’
CSC411_L6_SQL(1)Dr. Nan Wang18
Find sailors who’ve reserved at least one boat
• Would adding DISTINCT to this query make a difference?
• What is the effect of replacing S.sid by S.sname in the SELECT clause? – Would adding DISTINCT to this variant of the query make a
difference?
SELECT S.sidFROM Sailors S, Reserves RWHERE S.sid=R.sid
CSC411_L6_SQL(1)Dr. Nan Wang19
Expressions and Strings
• Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters.
• Illustrates use of arithmetic expressions and string pattern matching: – AS and = are two ways to name fields in result.
• LIKE is used for string matching. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters.
• Comparison operators (=, <, >, etc.) can be used for string comparison
SELECT S.age, age1=S.age-5, 2*S.age AS age2FROM Sailors SWHERE S.sname LIKE ‘B_%B’
CSC411_L6_SQL(1)Dr. Nan Wang2020
Expressions and Strings
• Compute the increments for the ratings of persons who have sailed two different boats on the same day.
• Each item in a qualification can be as general as expression1=expression2
SELECT S.sname, S.rating+1 as ratingFROM Sailors S, Reserves R1, Reserves R2WHERE S.sid=R1.sid AND S.sid = R2.sid
AND R1.day = R2.day AND R1.bid <> R2.bid
SELECT S.sname AS name1, S2.sname AS name2FROM Sailors S1, Sailors S2WHERE 2*S1.rating = S2.rating - 1
CSC411_L6_SQL(1)Dr. Nan Wang21
• (Q18) Find the ages of sailors whose name begins and ends with B and has at least three characters
21
CSC411_L6_SQL(1)Dr. Nan Wang22
• Union• Intersect• except
22
CSC411_L6_SQL(1)Dr. Nan Wang23
Find sid’s of sailors who’ve reserved a red or a green boat
• UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).
• If we replace OR by AND in the first version, what do we get?
• Also available: EXCEPT (What do we get if we replace UNION by EXCEPT?)
SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)
SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’UNIONSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
CSC411_L6_SQL(1)Dr. Nan Wang24
Find sid’s of sailors who’ve reserved a red and a green boat
• INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples.
• Some systems don’t support it.
• Contrast symmetry of the UNION and INTERSECT queries with how much the other versions differ.
SELECT S.sidFROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’)
SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
CSC411_L6_SQL(1)Dr. Nan Wang2525
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 S2.snameFROM Sailors S2, Boats B2, Reserves R2WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’
There are two sailors with the same name Horatio!
CSC411_L6_SQL(1)Dr. Nan Wang2626
Find the sids of all sailors who have reserved red boats but not green boats
SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’EXCEPTSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
CSC411_L6_SQL(1)Dr. Nan Wang2727
Find the sids of sailors who have a rating of 10 or reserved boat 104
• In contrast to the default that duplicates are not eliminated unless DISTINCT is specified in the basic query form, the default for UNION queries is that duplicates are eliminated!
• To retain duplicates, use UNION ALL, INTERSECT ALL and EXCEPT ALL
SELECT S.sidFROM Sailors SWHERE S.rating = 10 UNIONSELECT R.sidFROM Reserves RWHERE R.bid=104
CSC 411/511: DBMS Design
Dr. Nan Wang CSC411_L6_SQL(1)28
Questions?