SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers?...
Transcript of SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers?...
![Page 1: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/1.jpg)
SQL Constraints & Triggers
May 10th, 2002
![Page 2: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/2.jpg)
Agenda
• Big picture– what are constraints & triggers?– where do they appear?– why are they important?
• In SQL context– primary key– foreign key
![Page 3: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/3.jpg)
Constraints & Triggers
• Constraints– model additional important aspects of
real world
• Triggers– specify actions we want our database to execute
whenever certain conditions are met
![Page 4: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/4.jpg)
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
![Page 5: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/5.jpg)
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
![Page 6: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/6.jpg)
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.
![Page 7: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/7.jpg)
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));
![Page 8: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/8.jpg)
Primary Keys with Multiple Attributes
CREATE TABLE MovieStar (
name CHAR(30),
address VARCHAR(255),
gender CHAR(1),
PRIMARY KEY (name, address));
![Page 9: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/9.jpg)
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));
![Page 10: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/10.jpg)
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
![Page 11: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/11.jpg)
Foreign Key Constraints
CREATE TABLE ActedIn (
Name CHAR(30) PRIMARY KEY,
MovieName CHAR(30)
REFERENCES Movies(MovieName),
Year INT);
![Page 12: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/12.jpg)
Foreign Key Constraints
• ORCREATE TABLE ActedIn (
Name CHAR(30) PRIMARY KEY,
MovieName CHAR(30),
Year INT,
FOREIGN KEY MovieName
REFERENCES Movies(MovieName)
• MovieName must be a PRIMARY KEY
![Page 13: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/13.jpg)
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
![Page 14: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/14.jpg)
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)
![Page 15: SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.](https://reader036.fdocuments.net/reader036/viewer/2022082517/56649dbb5503460f94aabee4/html5/thumbnails/15.jpg)
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