Download presentation
Presentation is loading. Please wait.
1
CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 8
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
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.
4
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.
5
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.
6
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.
7
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.
8
Expressing Foreign Keys
9
Expressing Foreign Keys
Use the keyword REFERENCES, either:
10
Expressing Foreign Keys
Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys).
11
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>)
12
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>.
13
Example: With Attribute
Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );
14
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 );
15
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 );
16
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) );
17
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));
18
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));
19
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
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)); Remark. Attributes in a foreign key MAY have value NULL can be a list of more than one attributes
21
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:
22
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.
23
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
24
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
25
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
26
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
27
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
28
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
29
Actions Taken
30
Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price)
Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers.
31
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.
32
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:
33
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.
34
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.
35
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.
36
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.
37
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.
38
Sells(bar, beer, price) Beers(name, manf) Example: Cascade
39
Example: Cascade Delete the Bud tuple from Beers:
Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers:
40
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’.
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
Sells(bar, beer, price) Beers(name, manf) Example: Set NULL
50
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:
51
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.
52
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
53
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
54
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
55
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
56
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
57
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
58
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
59
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
60
Choosing a Policy
61
Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates.
62
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]
63
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.
64
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.
65
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 );
66
Attribute-Based Checks
67
Attribute-Based Checks
Constraints on the value of an attribute.
68
Attribute-Based Checks
Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute.
69
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.
70
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 ) );
71
Timing of Attribute-based Checks
72
Timing of Attribute-based Checks
Attribute-based checks performed only when a value for that attribute is inserted or updated.
73
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.
74
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))
75
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
76
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).
77
Tuple-Based Checks
78
Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element.
79
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).
80
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.
81
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) );
82
Cross-Relation Constraints
83
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.
84
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.
85
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>);
86
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.
87
Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5.
88
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
89
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
90
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
91
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
92
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
93
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.
94
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) );
95
Timing of Assertion Checks
96
Timing of Assertion Checks
In principle, we must check every assertion after every modification to any relation of the database.
97
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.
98
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.