Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


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

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

2 Outline of Course Representing things by tables E-R model (Ch. 4)
Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs ) SQL languages in DDL/DML (Ch. 6) Query processing (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )

3 Outline of Course Representing things by tables E-R model (Ch. 4)
Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs ) SQL languages in DDL/DML (Ch. 6) Query processing (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )

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

5 Constraints and Triggers

6 Constraints and Triggers
A constraint is a relationship among data elements that the DBMS is required to enforce.

7 Constraints and Triggers
A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints.

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

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

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

11 Kinds of Constraints

12 Kinds of Constraints Keys (unique, cannot be NULL).

13 Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key
-- referential-integrity.

14 Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key
-- referential-integrity. Value-based constraints. -- constrain values of a particular attribute.

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

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

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

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

19 Expressing Foreign Keys

20 Expressing Foreign Keys
Use the keyword REFERENCES, either:

21 Expressing Foreign Keys
Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys).

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

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

24 Example: With Attribute
Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );

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

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

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

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

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

30 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

31 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

32 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:

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

34 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

35 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

36 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

37 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

38 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

39 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

40 Actions Taken

41 Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price)
Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers.

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

43 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:

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

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

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

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

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

49 Sells(bar, beer, price) Beers(name, manf) Example: Cascade

50 Example: Cascade Delete the Bud tuple from Beers:
Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers:

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

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 Sells(bar, beer, price) Beers(name, manf) Example: Set NULL

61 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:

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

63 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

64 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

65 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

66 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

67 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

68 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

69 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

70 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

71 Choosing a Policy

72 Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates.

73 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]

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

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

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

77 Attribute-Based Checks

78 Attribute-Based Checks
Constraints on the value of an attribute.

79 Attribute-Based Checks
Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute.

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

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

82 Timing of Attribute-based Checks

83 Timing of Attribute-based Checks
Attribute-based checks performed only when a value for that attribute is inserted or updated.

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

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

86 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

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

88 Tuple-Based Checks

89 Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element.

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

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

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

93 Cross-Relation Constraints

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

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

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

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

98 Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5.

99 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

100 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

101 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

102 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

103 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

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

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

106 Timing of Assertion Checks

107 Timing of Assertion Checks
In principle, we must check every assertion after every modification to any relation of the database.

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

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


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google