Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.

Similar presentations


Presentation on theme: "SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL."— Presentation transcript:

1 SQL Constraints & Triggers May 10 th, 2002

2 Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL context –primary key –foreign key

3 Constraints & Triggers Constraints –model additional important aspects of real world Triggers –specify actions we want our database to execute whenever certain conditions are met

4 Where do they appear? Requirements analysis Conceptual design: use ER, ODL, … –constraints: keys, referential integrity, single-value, domain, … Logical schema design & refinement: –translate from ER/ODL to relational tables –use FD & MVD to refine/decompose tables Schema specification: use SQL –specify key, primary key, foreign key, … –create triggers Database modification –verify that constraints hold, activate triggers

5 Why are they important? Maintain integrity of a database –e.g., account balance always > 0 –relieve the mundane work from application writer Allow databases to be more “intelligent” –do more on our behalf –e.g., flight status changed to “cancelled” automatically notify all booked customers

6 SQL: Constraints and Triggers Chapter 6 Ullman and Widom Chapter 7 in the Complete Book Certain properties we’d like our database to hold Modification of the database may break these properties Build handlers into the database definition Key constraints Referential integrity constraints.

7 Declaring a Primary Keys in SQL CREATE TABLE MovieStar ( name CHAR(30) PRIMARY KEY, address VARCHAR(255), gender CHAR(1)); OR: CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1) PRIMARY KEY (name));

8 Primary Keys with Multiple Attributes CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1), PRIMARY KEY (name, address));

9 Other Keys CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), phone CHAR(10) UNIQUE, gender CHAR(1), petName CHAR(50), PRIMARY KEY (name), UNIQUE (gender, petName));

10 PRIMARY KEY vs. UNIQUE Can have multiple UNIQUEs A foreign key can only reference a primary key Primary key can influence the physical layout of the relation

11 Foreign Key Constraints CREATE TABLE ActedIn ( Name CHAR(30) PRIMARY KEY, MovieName CHAR(30) REFERENCES Movies(MovieName), Year INT);

12 Foreign Key Constraints OR CREATE TABLE ActedIn ( Name CHAR(30) PRIMARY KEY, MovieName CHAR(30), Year INT, FOREIGN KEY MovieName REFERENCES Movies(MovieName) MovieName must be a PRIMARY KEY

13 How do we Maintain them? Given a change to DB, there are several possible violations: –Insert new tuple with bogus foreign key value –Update a tuple to a bogus foreign key value –Delete a tuple in the referenced table with the referenced foreign key value –Update a tuple in the referenced table that changes the referenced foreign key value

14 How to Maintain? Recall, ActedIn has FK MovieName... Movies(MovieName, year) (Fatal Attraction, 1987) ActedIn(ActorName, MovieName) (Michael Douglas, Fatal Attraction) insert: (Rick Moranis, Strange Brew)

15 How to Maintain? Policies for handling the change… –Reject the update (default) –Cascade (example: cascading deletes) –Set NULL Can set update and delete actions independently in CREATE TABLE MovieName CHAR(30) REFERENCES Movies(MovieName)) ON DELETE SET NULL ON UPDATE CASCADE


Download ppt "SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL."

Similar presentations


Ads by Google