Tutorial 11 Part2.Constraint Trigger
Tutorial 11 Part2.Constraint Trigger
1
Constraints
5
Choosing a Policy
6
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
);
7
Attribute-Based Checks
9
Example
Only Mary‟s Bar can sell beer for more than $5:
CREATE TABLE Sells (
bar CHAR(20),
beer CHAR(20),
price REAL,
CHECK (bar = “Mary’s Bar” OR
price <= 5.00)
);
10
Assertions
Assertions are database-schema elements, like
relations or views.
CREATE ASSERTION <name>
CHECK ( <condition> );
Condition may refer to any relation or attribute in the
database schema
In principle, assertions are checked after every
modification to any relation of the database
11
Example
12
Exercise
13
Solution
14
Triggers: Motivation
16
Preliminary Example
Write a trigger so that, whenever there is
an insertion into Sells(bar, beer, price) with
unknown beers, add that beer name to
Beers table, with manufacturer set to
NULL.
17
Example
CREATE TRIGGER BeerTrigger The event
AFTER INSERT ON Sells
REFERENCING NEW ROW AS NewTuple
FOR EACH ROW
The condition
WHEN (NewTuple.beer NOT IN
(SELECT name FROM Beers))
INSERT INTO Beers(name)
The action
VALUES(NewTuple.beer);
18
Options: CREATE TRIGGER
CREATE TRIGGER <name>
Option:
19
Options: The Event
20
Options: REFERENCING
AFTER INSERT ON Sells
REFERENCING NEW ROW AS NewTuple
INSERT statements imply a new row or new table
(i.e., a set of inserted tuples)
DELETE implies an old row or old table
21
Options: FOR EACH ROW
Statement-Level Triggers:
Execute once for an SQL statement,
regardless of how many tuples are modified
22
Options: The Condition
23
Options: The Action
There can be one or more SQL statement in
the action
Surround by BEGIN . . . END if there is more than
one
24
Example
Sells(bar, beer, price)
RipOffBars(bar)
Write a trigger so that any bar that raises the
price of any beer by more than $1 will be
inserted into RipOffBars relation
25
The Trigger The event –
When there is
change
CREATE TRIGGER PriceTrigger to the price
AFTER UPDATE OF price ON Sells
REFERENCING Since it’s Updates, we have
OLD ROW AS oldRow old and new tuples
NEW ROW AS newRow Condition:
We need to consider a raise in
FOR EACH ROW each price change price > $1
WHEN(newRow.price > oldRow.price + 1.00)
INSERT INTO RipOffBars When the price change
VALUES(newRow.bar); is great enough, add
the bar to RipoffBars
26
Exercise
Given the following relational schema:
Employee(ID, name, address, position)
CompanyStats(numEmployee, numProduct,
revenue)
Create two triggers that will result in the automatic
tracking of the number of employees a company
manages.
27
Solution - 1
The first trigger increments the number of employees each time a
new person is hired
28
Solution - 2
The second trigger decrements the number of employees each
time an employee leaves the company
29
Exercise
We have a Student table that contains the student
ID (SID), the GPA of student, etc.
For each new student with GPA > 4.0, he/she will be
automatically be enrolled in the honor seminar
course (CS123)
The Enroll table is defined as:
Enroll (SID, courseNumber)
Write a trigger to automatically enroll the student in
honor seminar
30
Solution
CREATE TRIGGER HonorSeminarEnrollment
AFTER INSERT ON Student
REFERENCING NEW ROW AS newStudent
FOR EACH ROW
WHEN (newStudent.GPA > 4.0)
Insert Into Enroll
Values (newStudent.SID, „CS123‟);
31