Download presentation
Presentation is loading. Please wait.
1
CPSC-608 Database Systems
Fall 2017 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 5
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
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
4
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.
5
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.
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
Requirements on HAVING Conditions
These conditions may refer to any relation or tuple-variable in the FROM clause.
14
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.
15
Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING
16
Requirements on HAVING Conditions
It is easier to understand this from an implementation viewpoint: SELECT FROM WHERE GROUP BY HAVING step 1, input
17
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
18
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
19
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
20
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
21
Database Modifications
22
Database Modifications
A modification command does not return a result (as a query does), but changes the database in some way.
23
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.
24
Insertion
25
Insertion To insert a single tuple:
INSERT INTO <relation> VALUES (<list of values>);
26
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’);
27
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>.
28
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.
29
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’);
30
Inserting Many Tuples We may insert the entire result of a query into a relation, using the form: INSERT INTO <relation> (<subquery>);
31
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.
32
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);
33
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) ……
34
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) ……
35
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 …
36
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
37
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
38
Deletion
39
Deletion To delete tuples satisfying a condition from some relation:
DELETE FROM <relation> WHERE <condition>;
40
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:
41
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’;
42
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
43
Example: Delete Many Tuples
Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer.
44
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);
45
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.
46
Semantics of Deletion
47
name manf Bud Anheuser-Busch Bud Lite Semantics of Deletion Suppose Anheuser-Busch makes only Bud and Bud Lite.
48
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.
49
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.
50
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.
51
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. (?)
52
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? ?
53
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.
54
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:
55
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.
56
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.
57
Updates
58
Updates To change certain attributes in certain tuples of a relation:
UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>;
59
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’;
60
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;
61
Constraints and Triggers
62
Constraints and Triggers
A constraint is a relationship among data elements that the DBMS is required to enforce.
63
Constraints and Triggers
A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints.
64
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.
65
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.
66
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.
67
Kinds of Constraints
68
Kinds of Constraints Keys (unique, cannot be NULL).
69
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key
-- referential-integrity.
70
Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key
-- referential-integrity. Value-based constraints. -- constrain values of a particular attribute.
71
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.
72
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.
73
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.
74
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.
75
Expressing Foreign Keys
76
Expressing Foreign Keys
Use the keyword REFERENCES, either:
77
Expressing Foreign Keys
Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys).
78
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>)
79
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>.
80
Example: With Attribute
Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );
81
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 );
82
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 );
83
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) );
84
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));
85
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));
86
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
87
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
88
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:
89
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.
90
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
91
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
92
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
93
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
94
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
95
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
96
Actions Taken
97
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price)
Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers.
98
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.
99
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:
100
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.
101
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.
102
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.
103
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.
104
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.
105
Sells(bar, beer, price) Beers(name, manf) Example: Cascade
106
Example: Cascade Delete the Bud tuple from Beers:
Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers:
107
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’.
108
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
109
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
110
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
111
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
112
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
113
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
114
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
115
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
116
Sells(bar, beer, price) Beers(name, manf) Example: Set NULL
117
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:
118
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.
119
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
120
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
121
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
122
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
123
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
124
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
125
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
126
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
127
Choosing a Policy
128
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates.
129
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]
130
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.
131
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.
132
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 );
133
Attribute-Based Checks
134
Attribute-Based Checks
Constraints on the value of an attribute.
135
Attribute-Based Checks
Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute.
136
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.
137
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 ) );
138
Timing of Attribute-based Checks
139
Timing of Attribute-based Checks
Attribute-based checks performed only when a value for that attribute is inserted or updated.
140
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.
141
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))
142
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
143
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).
144
Tuple-Based Checks
145
Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element.
146
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).
147
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.
148
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) );
149
Cross-Relation Constraints
150
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.
151
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.
152
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>);
153
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.
154
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5.
155
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
156
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
157
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
158
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
159
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
160
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.
161
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) );
162
Timing of Assertion Checks
163
Timing of Assertion Checks
In principle, we must check every assertion after every modification to any relation of the database.
164
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.
165
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) );
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.