Professional Documents
Culture Documents
Lec08 - Good Source PDF
Lec08 - Good Source PDF
Lec08 - Good Source PDF
Chapter 5.1-5.8
2/6/11
2/6/11
2/6/11
SELECT [DISTINCT] attr-list Attr1 op Attr2 FROM relation-list OPS: <, >, =, <=, >=, <> Combine using AND, OR, NOT WHERE qualification
(Conceptual) Evaluation:
1. Take cross-product of relation-list 2. Select rows satisfying qualification
Optimizer chooses efficient plan!!
4
2/6/11
Find the names of sailors who have reserved boat #103 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 5
2/6/11
Another Example
Schema:
Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day)
Find the colors of boats reserved by a sailor named rusty SELECT B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND S.sname = rusty
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 7
2/6/11
ORDER BY clause
In relational algebra and calculus, there is no way to specify ordering of results SQL has an extra ORDER BY clause
Attribute(s) in ORDER BY clause must be in SELECT list.
Find the names and ages of all sailors, in increasing order of age
SELECT S.sname, S.age FROM Sailors S ORDER BY S.age [ASC]
2/6/11
Find the names and ages of all sailors, in decreasing order of age
SELECT S.sname, S.age FROM Sailors S ORDER BY S.age DESC
9
ORDER BY clause
SELECT S.sname, S.age, S.rating FROM Sailors S ORDER BY S.age ASC, S.rating DESC What does this query compute?
Find the names, ages, and rankings of all sailors. Sort the result in increasing order of age. If there is a tie, sort those tuples in decreasing order of rating.
2/6/11
10
Union
Find names of sailors who have reserved a red or a green boat
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = red OR B.color = green) SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = red UNION SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = green
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 11
Intersect
Find sids of sailors who have reserved a red and a green boat
SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = red INTERSECT SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = green
2/6/11
12
2/6/11
13
Nested Queries
Query with another query embedded inside
What does this Compute?
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103)
Conceptual evaluation: For each row of Sailors, evaluate the subquery over reserves To find sailors who have not reserved 103, use NOT IN
2/6/11
14
True if value
2/6/11
15
Aggregate Operators
SELECT COUNT (*) FROM Sailors S SELECT COUNT (DISTINCT S.name) FROM Sailors S SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10
COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) Can use Distinct MIN (A) Can use Distinct
single column
SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10
SELECT S.sname FROM Sailors S WHERE S.rating= (SELECT MAX(S2.rating) FROM Sailors S2)
2/6/11
17
2/6/11
18
GROUP BY
Conceptual evaluation
Partition data into groups according to some criterion Evaluate the aggregate for each group Example: For each rating level, find the age of the youngest sailor
SELECT MIN (S.age), S.rating FROM Sailors S GROUP BY S.rating How many tuples in the result?
2/6/11
19
Target-list contains: Attribute names (subset of grouping-list) Aggregate operations (e.g., min(age))
Conceptual Evaluation: 1. Eliminate tuples that dont satisfy qualification 2. Partition remaining data into groups 3. Eliminate groups according to group-qualification 4. Evaluate aggregate operation(s) for each group
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 20
10
Find the age of the youngest sailor with age >= 18, for each rating with at least 2 such sailors
SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) >= 2
sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 71 zorba 10 16.0 64 horatio 7 35.0 29 brutus 1 33.0 58 rusty 10 35.0
rating 1 7 7 8 10 age 33.0 45.0 35.0 55.5 35.0
rating 7 35.0 Answer relation
SQL Examples
2/6/11
22
11
Find the names and ages of all users belonging to a network named Michigan in decreasing order of Age SELECT U.username, U.age FROM User U, Network N, Member M WHERE U.uid = M.uid AND N.nid = M.nid AND N.networkname = Michigan ORDER BY U.age DESC
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 23
Find the names of all unique hometowns, as well as the number of users from each SELECT U.hometown, COUNT(*) FROM User U GROUP BY U.hometown
2/6/11
24
12
For each network with three or more members, find the network id, the network name, and the average age of users belonging to the network SELECT N.nid, N.networkname, AVG(U.age) FROM User U, Network N, Member M WHERE U.uid = M.uid AND N.nid = M.nid GROUP BY N.nid, N.networkname HAVING COUNT(*) >= 3
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 25
Find the names of users who are older than all members Of the network(s) with name Huron High School SELECT U.username FROM User U WHERE U.age > ALL (SELECT U2.age FROM User U2, Network N, Member M WHERE U2.uid = M.uid AND N.nid = M.nid AND N.networkname = Huron High School)
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 26
13
Find the names of all users who are friends with the user with uid 456 (omit duplicates) SELECT DISTINCT U2.username FROM User U, Friend F WHERE F.uid1 = 456 AND F.uid2 = U2.uid
2/6/11
27
Find the uids of all users who are within 1 degree of user 456 SELECT uid2 FROM Friend WHERE uid1 = 456 UNION SELECT F2.uid2 FROM Friend F1, Friend F2 WHERE F1.uid1 = 456 AND F1.uid2 = F2.uid1
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre 28
14
WITH Clause
Assign a name to the result of a query
Scope of the name is within the current query, so this is different from creating a view Helpful when writing complex queries WITH Users_by_Network AS ( SELECT nid, count(*) cnt FROM Member GROUP BY nid) SELECT max(cnt) FROM Users_by_Network
2/6/11
29
2/6/11
30
15
2/6/11
32
16
rating 7 10 8
age 45 NULL 55
2/6/11
33
2/6/11
34
17
2/6/11
35
Table Constraints
More general than key constraints Can use a query to express constraint
Constraints checked each time table updated CHECK constraint always true for empty relation CREATE TABLE Reserves CREATE TABLE Sailors ( sname CHAR(10), ( sid INTEGER, bid INTEGER, sname CHAR(10), day DATE, rating INTEGER, PRIMARY KEY (bid,day), age REAL, CONSTRAINT noInterlakeRes PRIMARY KEY (sid), CHECK (`Interlake <> CHECK ( rating >= 1 ( SELECT B.bname AND rating <= 10 ) FROM Boats B WHERE B.bid=bid)))
2/6/11 EECS 484: Database Management Systems, Kristen LeFevre
36
18
2/6/11
37
Triggers
Trigger: procedure that starts automatically if specified changes occur to the DBMS Three parts:
Event (activates the trigger) Condition (tests whether the trigger should run) Action (what happens if the trigger runs)
Before and After Triggers
Trigger Execution
Row-level Triggers: Once per modified row Statement-level Triggers: Once per SQL statement
2/6/11
38
19
Example
Event: Insert new row into Students Condition: New students age >= 18 Event: Update universitys total enrollment
2/6/11
39
2/6/11
20