CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

SQL CSET 3300.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
CPSC-310 Database Systems
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Database Design: DBS CB, 2nd Edition
Introduction to Database Systems, CS420
CPSC-310 Database Systems
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
SQL: Constraints and Triggers
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CPSC-310 Database Systems
CMSC-461 Database Management Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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

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. 13-14) SQL languages in DDL/DML (Ch. 6) Query processing (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

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. 13-14) SQL languages in DDL/DML (Ch. 6) Query processing (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

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)

Constraints and Triggers

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

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

Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple.

Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.

Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.

Kinds of Constraints

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

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

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

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

Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components. Assertions. -- any SQL boolean expression.

Foreign Keys Consider the relation Sells(bar, beer, price). Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name.

Foreign Keys Consider the relation Sells(bar, beer, price). Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name. A constraint that requires a beer in Sells to be a beer in Beers is called a foreign-key constraint.

Expressing Foreign Keys

Expressing Foreign Keys Use the keyword REFERENCES, either:

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

Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>)

Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>) Referenced attributes must be declared PRIMARY KEY or UNIQUE in <relation>.

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

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

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

Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );

Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name));

Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name));

Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name)); can be a list of more than one attributes

Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name)); Remark. Attributes in a foreign key MAY have value NULL can be a list of more than one attributes

Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible:

Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S.

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC No Bud Lite

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC No Bud Lite

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC No Bud Lite

Enforcing Foreign-Key Constraints Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to “dangle.” Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 Bud Lite 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 name manf Bud A.B. Miller MBC ? No Bud Lite

Actions Taken

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

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected.

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways:

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification.

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells.

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple.

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells.

Actions Taken Suppose R = Sells, S = Beers. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells. -- Set NULL: Change the beer (in Sells) to NULL.

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

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

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’.

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC Budweiser

Example: Cascade Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC Budweiser Budweiser Budweiser

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

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

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL.

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL Budweiser NULL

Example: Set NULL Delete the Bud tuple from Beers: Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: -- the same change as above. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC bar beer price Joe’s Bud 3.00 Sue’s Miller 3.50 3.20 name manf Bud A.B. Miller MBC NULL NULL Budweiser NULL NULL

Choosing a Policy

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

Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE]

Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE] Two such clauses may be used.

Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE] Two such clauses may be used. Otherwise, the default (reject) is used.

Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, Sells(bar, beer, price) Beers(name, manf) Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE );

Attribute-Based Checks

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

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

Attribute-Based Checks Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute. The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery.

Example: Attribute-Based Check Beers(name, manf) Sells(bar, beer, price) Example: Attribute-Based Check CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK ( beer IN (SELECT name FROM Beers)), price REAL CHECK ( price <= 5.00 ) );

Timing of Attribute-based Checks

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

Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5.

Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers))

Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers)) is not checked if a beer is deleted from Beers

Timing of Attribute-based Checks Attribute-based checks performed only when a value for that attribute is inserted or updated. -- Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. -- Example: (in the relation Sells(bar, beer, price)) CHECK (beer IN (SELECT name FROM Beers)) is not checked if a beer is deleted from Beers -- it is only checked for Sells (unlike foreign-keys).

Tuple-Based Checks

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

Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element. The condition may refer to any attribute of the relation (but any other attributes or relations require a subquery).

Tuple-Based Checks CHECK (<condition>) may be added as a relation-schema element. The condition may refer to any attribute of the relation (but any other attributes or relations require a subquery). Checked on insert or update only.

Example: Tuple-Based Check Sells(bar, beer, price) Example: Tuple-Based Check Only Joe’s Bar can sell beer for more than $5 CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK (bar = ’Joe’’s Bar’ OR price <= 5.00) );

Cross-Relation Constraints

Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement.

Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views.

Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views. Assertions: CREATE ASSERTION <name> CHECK (<condition>);

Cross-Relation Constraints Remark. All constraints we discussed so far are given in the declaration of a relation, i.e., in a CREATE TABLE statement. Constraints can also be given as database- schema elements, like relations or views. Assertions: CREATE ASSERTION <name> CHECK (<condition>); Condition may refer to any relation or attribute in the database schema.

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

Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); bars with an average price above $5

Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5

Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5

Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); no bar can have an average price larger than $5 bars with an average price above $5 An constraint that no bar can have an average price larger than $5

Sells(bar, beer, price) Example 1: Assertion In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price)) ); An constraint that no bar can have an average price larger than $5

Drinkers(name, addr, phone) Bars(name, addr, license) Example 2: Assertion In Drinkers(name, addr, phone) and Bars(name, addr, license), there cannot be more bars than drinkers.

Drinkers(name, addr, phone) Bars(name, addr, license) Example 2: Assertion In Drinkers(name, addr, phone) and Bars(name, addr, license), there cannot be more bars than drinkers. CREATE ASSERTION FewBar CHECK ( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) );

Timing of Assertion Checks

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

Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database. A clever system can observe that only certain changes could cause a given assertion to be violated.

Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database. A clever system can observe that only certain changes could cause a given assertion to be violated. -- Example: No change to Beers can affect FewBar. Neither can an insertion to Drinkers. CREATE ASSERTION FewBar CHECK ( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) );