Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-608 Database Systems

Similar presentations


Presentation on theme: "CPSC-608 Database Systems"— Presentation transcript:

1 CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 7

2 SQL: Structured Query language
a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C or Java. Database management system figures out the “best” way to execute queries * called “query optimization” For both data definition and data manipulation.

3 Aggregations SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column.

4 Aggregations SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. Also, COUNT(*) counts the number of tuples.

5 Example: Aggregation From Sells(bar, beer, price), find the average price of Bud: SELECT AVG(price) FROM Sells WHERE beer = ’Bud’;

6 Eliminating Duplicates in Aggregation
Sells(bar, beer, price) Eliminating Duplicates in Aggregation

7 Eliminating Duplicates in Aggregation
Sells(bar, beer, price) Eliminating Duplicates in Aggregation Use DISTINCT inside an aggregation.

8 Eliminating Duplicates in Aggregation
Sells(bar, beer, price) Eliminating Duplicates in Aggregation Use DISTINCT inside an aggregation. Example: find the number of different prices charged for Bud:

9 Eliminating Duplicates in Aggregation
Sells(bar, beer, price) Eliminating Duplicates in Aggregation Use DISTINCT inside an aggregation. Example: find the number of different prices charged for Bud: SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = ’Bud’;

10 NULL is Ignored in Aggregation
NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column.

11 NULL is Ignored in Aggregation
NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column. But if there are no non-NULL values in a column, then the result of the aggregation is NULL.

12 Example: Effect of NULL’s
Sells(bar, beer, price) Example: Effect of NULL’s SELECT count(*) FROM Sells WHERE beer = ’Bud’; SELECT count(price)

13 Example: Effect of NULL’s
Sells(bar, beer, price) Example: Effect of NULL’s SELECT count(*) FROM Sells WHERE beer = ’Bud’; SELECT count(price) The number of bars that sell Bud.

14 Example: Effect of NULL’s
Sells(bar, beer, price) Example: Effect of NULL’s SELECT count(*) FROM Sells WHERE beer = ’Bud’; SELECT count(price) The number of bars that sell Bud. The number of bars that sell Bud at a known price.

15 Grouping We may follow a SELECT-FROM- WHERE expression by GROUP BY and a list of attributes.

16 Grouping We may follow a SELECT-FROM- WHERE expression by GROUP BY and a list of attributes. The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.

17 Example: Grouping From Sells(bar, beer, price), find the average price for each beer:

18 Example: Grouping From Sells(bar, beer, price), find the average price for each beer: SELECT beer, AVG(price) FROM Sells GROUP BY beer;

19 Example: Grouping From Sells(bar, beer, price), find the average price for each beer: SELECT beer, AVG(price) FROM Sells GROUP BY beer; Output one tuple for each group

20 Sells(bar, beer, price) Frequents(drinker, bar) Example: Grouping From Sells and Frequents, find for each drinker the average price of Bud at the bars they frequent:

21 Sells(bar, beer, price) Frequents(drinker, bar) Example: Grouping From Sells and Frequents, find for each drinker the average price of Bud at the bars they frequent: SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = ’Bud’ AND Frequents.bar = Sells.bar GROUP BY drinker;

22 Sells(bar, beer, price) Frequents(drinker, bar) Example: Grouping From Sells and Frequents, find for each drinker the average price of Bud at the bars they frequent: compute drinker-bar- price for Bud tuples first, then group by drinker. SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = ’Bud’ AND Frequents.bar = Sells.bar GROUP BY drinker;

23 Restriction on SELECT Lists With Aggregation
If any aggregation is used, then each element of the SELECT list must be either: Aggregated, or An attribute on the GROUP BY list.

24 Sells(bar, beer, price) Illegal Query Example You might think you could find the bar that sells Bud the cheapest by: SELECT bar, MIN(price) FROM Sells WHERE beer = ’Bud’; But this query is illegal in SQL.

25 HAVING Clauses HAVING <condition> may follow a GROUP BY clause.
If so, the condition applies to each group, and groups not satisfying the condition are eliminated.

26 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s.

27 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. SELECT beer, AVG(price) FROM Sells GROUP BY beer

28 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer

29 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); at least 3 bars appear in the beer group

30 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); at least 3 bars appear in the beer group

31 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); at least 3 bars appear in the beer group

32 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); at least 3 bars appear in the beer group beers made by Pete’s

33 Sells(bar, beer, price) Beers(name, manf) Example. From Sells and Beers, find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s. group tuples (bar, beer, price) in Sells in terms of beer SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); at least 3 bars appear in the beer group the beer is made by Pete’s beers made by Pete’s

34 Requirements on HAVING Conditions
These conditions may refer to any relation or tuple-variable in the FROM clause.

35 Requirements on HAVING Conditions
These conditions may refer to any relation or tuple-variable in the FROM clause. They may refer to attributes of those relations, as long as the attribute makes sense within a group; i.e., it is either: A grouping attribute, or Aggregated.

36 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING

37 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 1, input

38 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 1, input step 2, pick the proper tuples

39 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 1, input step 2, pick the proper tuples step 3, group the picked tuples

40 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 1, input step 2, pick the proper tuples step 3, group the picked tuples step 4, pick the proper groups

41 Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 5, compute the output step 1, input step 2, pick the proper tuples step 3, group the picked tuples step 4, pick the proper groups

42 Database Modifications

43 Database Modifications
A modification command does not return a result (as a query does), but changes the database in some way.

44 Database Modifications
A modification command does not return a result (as a query does), but changes the database in some way. Three kinds of modifications: Insert a tuple or tuples. Delete a tuple or tuples. Update the value(s) of an existing tuple or tuples.

45 Insertion

46 Insertion To insert a single tuple:
INSERT INTO <relation> VALUES (<list of values>);

47 Insertion To insert a single tuple:
Likes(drinker, beer) Insertion To insert a single tuple: INSERT INTO <relation> VALUES (<list of values>); Example: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes VALUES(’Sally’, ’Bud’);

48 Insertion To insert a single tuple:
Likes(drinker, beer) Insertion To insert a single tuple: INSERT INTO <relation> VALUES (<list of values>); Example: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes VALUES(’Sally’, ’Bud’); We may add a list of attributes to <relation>.

49 Insertion To insert a single tuple:
Likes(drinker, beer) Insertion To insert a single tuple: INSERT INTO <relation> VALUES (<list of values>); Example: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes VALUES(’Sally’, ’Bud’); We may add a list of attributes to <relation>. Two reasons for doing so: Forget the order of attributes for the relation. Don’t have values for all attributes, and want the system to fill in missing ones with default values.

50 Insertion To insert a single tuple:
Likes(drinker, beer) Insertion To insert a single tuple: INSERT INTO <relation> VALUES (<list of values>); Example: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes VALUES(’Sally’, ’Bud’); We may add a list of attributes to <relation>. Two reasons for doing so: Forget the order of attributes for the relation. Don’t have values for all attributes, and want the system to fill in missing ones with default values. So another solution for the above example: INSERT INTO Likes(beer, drinker) VALUES(‘Bud’, ’Sally’);

51 Inserting Many Tuples We may insert the entire result of a query into a relation, using the form: INSERT INTO <relation> (<subquery>);

52 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents.

53 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar);

54 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar); (Sally, Joe’s, Tom, Joe’s) (Sally, Sue’s, Jeff, Sue’s) (Sally, Sue’s, Mary, Sue’s) ……

55 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. 2. collect the drinkers INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar); (Sally, Joe’s, Tom, Joe’s) (Sally, Sue’s, Jeff, Sue’s) (Sally, Sue’s, Mary, Sue’s) ……

56 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. 2. collect the drinkers INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar); Tom Jeff Mary

57 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. 2. collect the drinkers INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar); Tom Jeff Mary 3. add the drinkers to PotBuddies

58 Frequents(drinker, bar)
Example. Using Frequents, enter into the new relation PotBuddies(name) all of Sally’s “potential buddies,” i.e., those drinkers who frequent at least one bar that Sally also frequents. 1. find all potential buddies of Sally by pairing Sally with those who frequent the bars Sally frequents. 2. collect the drinkers PotBuddies D2.drinker Tom Jeff Mary INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar); 3. add the drinkers to PotBuddies

59 Deletion

60 Deletion To delete tuples satisfying a condition from some relation:
DELETE FROM <relation> WHERE <condition>;

61 Deletion To delete tuples satisfying a condition from some relation:
Likes(drinker, beer) Deletion To delete tuples satisfying a condition from some relation: DELETE FROM <relation> WHERE <condition>; Example. Delete from Likes the fact that Sally likes Bud:

62 Deletion To delete tuples satisfying a condition from some relation:
Likes(drinker, beer) Deletion To delete tuples satisfying a condition from some relation: DELETE FROM <relation> WHERE <condition>; Example. Delete from Likes the fact that Sally likes Bud: DELETE FROM Likes WHERE drinker = ’Sally’ AND beer = ’Bud’;

63 Deletion To delete tuples satisfying a condition from some relation:
Likes(drinker, beer) Deletion To delete tuples satisfying a condition from some relation: DELETE FROM <relation> WHERE <condition>; Example. Delete from Likes the fact that Sally likes Bud: DELETE FROM Likes WHERE drinker = ’Sally’ AND beer = ’Bud’; To make the relation Likes empty: Note that no WHERE clause is needed

64 Example: Delete Many Tuples
Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer.

65 Example: Delete Many Tuples
Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer. DELETE FROM Beers b WHERE EXISTS ( SELECT name FROM Beers WHERE manf = b.manf AND name <> b.name);

66 Example: Delete Many Tuples
Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer. DELETE FROM Beers b WHERE EXISTS ( SELECT name FROM Beers WHERE manf = b.manf AND name <> b.name); Beers with the same manufacturer and a different name from the name of the beer represented by tuple b.

67 Semantics of Deletion

68 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite.

69 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first.

70 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud.

71 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud.

72 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion ? Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?)

73 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion ? Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? ?

74 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well.

75 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well. Reason: Deletion proceeds in two stages:

76 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well. Reason: Deletion proceeds in two stages: -- Mark all tuples for which WHERE condition holds.

77 name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well. Reason: Deletion proceeds in two stages: -- Mark all tuples for which WHERE condition holds. -- Delete the marked tuples.

78 Updates

79 Updates To change certain attributes in certain tuples of a relation:
UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>;

80 Updates To change certain attributes in certain tuples of a relation:
Drinkers(name, addr, phone) Updates To change certain attributes in certain tuples of a relation: UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>; Change Fred’s phone number to : UPDATE Drinkers SET phone = ’ ’ WHERE name = ’Fred’;

81 Updates To change certain attributes in certain tuples of a relation:
Drinkers(name, addr, phone) Sells(bar, beer, price) Updates To change certain attributes in certain tuples of a relation: UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>; Change Fred’s phone number to : UPDATE Drinkers SET phone = ’ ’ WHERE name = ’Fred’; Make $4 the maximum price for beer (make updates for several tuples): UPDATE Sells SET price = 4.00 WHERE price > 4.00;


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google