CPSC-608 Database Systems Fall 2017 Instructor: Jianer Chen Office: HRBB 315C Phone: 845-4259 Email: chen@cse.tamu.edu Notes 5
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.
Meaning of A Query Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Group the tuples selected in step 2 in terms of the values of the attributes in GROUP BY. Apply the projection in the SELECT clause. SELECT attributes FROM tables WHERE conditions GROUP BY attributes Step 4 Step 1 Step 2 Step 3
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
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: 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
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;
Constraints and Triggers
Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce.
Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints.
Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple.
Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.
Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.
Kinds of Constraints
Kinds of Constraints Keys (unique, cannot be NULL).
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity.
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute.
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components.
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components. Assertions. -- any SQL boolean expression.
Foreign Keys Consider the relation Sells(bar, beer, price). Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name.
Foreign Keys Consider the relation Sells(bar, beer, price). Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name. A constraint that requires a beer in Sells to be a beer in Beers is called a foreign-key constraint.
Expressing Foreign Keys
Expressing Foreign Keys Use the keyword REFERENCES, either:
Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys).
Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>)
Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>) Referenced attributes must be declared PRIMARY KEY or UNIQUE in <relation>.
Example: With Attribute Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );
Example: With Attribute Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );
Example: With Attribute Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );
Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );
Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name));
Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name));
Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name)); can be a list of more than one attributes
Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name)); Remark. Attributes in a foreign key MAY have value NULL can be a list of more than one attributes
Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible:
Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S.
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC No Bud Lite
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC No Bud Lite
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC No Bud Lite
Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC ? No Bud Lite
Actions Taken
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways:
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells.
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells. -- Set NULL: Change the beer (in Sells) to NULL.
Sells(bar, beer, price) Beers(name, manf) Example: Cascade
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers:
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’.
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC Budweiser
Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC Budweiser Budweiser Budweiser
Sells(bar, beer, price) Beers(name, manf) Example: Set NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers:
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL.
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL Budweiser NULL
Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL Budweiser NULL NULL
Choosing a Policy
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates.
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE]
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE] Two such clauses may be used.
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE] Two such clauses may be used. Otherwise, the default (reject) is used.
Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, Sells(bar, beer, price) Beers(name, manf) Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE );
Attribute-Based Checks
Attribute-Based Checks Constraints on the value of an attribute.
Attribute-Based Checks Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute.
Attribute-Based Checks Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute. The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery.
Example: Attribute-Based Check Beers(name, manf) Sells(bar, beer, price) Example: Attribute-Based Check CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK ( beer IN (SELECT name FROM Beers)), price REAL CHECK ( price <= 5.00 ) );
Timing of Attribute-based Checks
Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated.
Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5.
Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers))
Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers)) is not checked if a beer is deleted from Beers
Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers)) is not checked if a beer is deleted from Beers -- it is only checked for Sells (unlike foreign-keys).
Tuple-Based Checks
Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element.
Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element. The condition may refer to any attribute of the relation (but any other attributes or relations require a subquery).
Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element. The condition may refer to any attribute of the relation (but any other attributes or relations require a subquery). Checked on insert or update only.
Example: Tuple-Based Check Sells(bar, beer, price) Example: Tuple-Based Check Only Joe’s Bar can sell beer for more than $5 CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK (bar = ’Joe’’s Bar’ OR price <= 5.00) );
Cross-Relation Constraints
Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement.
Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views.
Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views. Assertions: CREATE ASSERTION <name> CHECK (<condition>);
Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views. Assertions: CREATE ASSERTION <name> CHECK (<condition>); Condition may refer to any relation or attribute in the database schema.
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5.
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); bars with an average price above $5
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5 An constraint that no bar can have an average price larger than $5
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); An constraint that no bar can have an average price larger than $5
Drinkers(name, addr, phone) Bars(name, addr, license) Example 2: Assertion In Drinkers(name, addr, phone) and Bars(name, addr, license), there cannot be more bars than drinkers.
Drinkers(name, addr, phone) Bars(name, addr, license) Example 2: Assertion In Drinkers(name, addr, phone) and Bars(name, addr, license), there cannot be more bars than drinkers. CREATE ASSERTION FewBar CHECK ( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) );
Timing of Assertion Checks
Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database.
Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database. A clever system can observe that only certain changes could cause a given assertion to be violated.
Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database. A clever system can observe that only certain changes could cause a given assertion to be violated. -- Example: No change to Beers can affect FewBar. Neither can an insertion to Drinkers. CREATE ASSERTION FewBar CHECK ( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) );