CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
SQL 2015, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
CPSC-310 Database Systems
CS 440 Database Management Systems
Relational Database Systems 1
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-310 Database Systems
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CPSC-310 Database Systems Professor Jianer Chen Room 315C HRBB Lecture #14

Our Running Example Our SQL queries will be based on the following database schema (Underline indicates key attributes) Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

SQL Basic Query Statement: SELECT attributes FROM tables WHERE conditions

SQL Basic Query Statement: SELECT attributes Other structures: FROM tables WHERE conditions Other structures: -- subqueries -- set operations -- joins

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

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.

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

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

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

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:

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

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.

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.

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

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.

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.

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

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.

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

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

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

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:

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;

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;

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.

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.

HAVING Clauses

HAVING Clauses HAVING <condition> may follow a GROUP BY clause.

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.

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.

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.

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

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

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

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

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

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

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

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 HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’);

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

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.

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

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

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

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

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

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

Database Modifications

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

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.

Insertion

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

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

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>.

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:

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.

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.

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

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

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.

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

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

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

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 …

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

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

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. INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = ’Sally’ AND d2.drinker <> ’Sally’ AND d1.bar = d2.bar);

Deletion

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

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:

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

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

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

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

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.

Semantics of Deletion

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

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.

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.

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.

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. (?)

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? ?

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.

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:

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.

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.

Updates

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

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 555-1212: UPDATE Drinkers SET phone = ’555-1212’ WHERE name = ’Fred’;

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 555-1212: UPDATE Drinkers SET phone = ’555-1212’ 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;