SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers?...

15
SQL Constraints & Triggers May 10 th , 2002

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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