Вы находитесь на странице: 1из 5

Active Databases: Constraints (Assertions)

Constraints (Assertions)
Triggers Integrity Constraints
(Basic Relational DB Theory)
Domain Constraints
Douglas S. Kerr Key constraints (Primary Keys)
Referential Integrity (Foreign Keys)
CIS 671
General constraints (Assertions) (EN 8.6)
Elmasri & Navathe, EN 23.1
E.g., Salary may not exceed $200,000.
Triggers (EN 23.1)
Active Databases CIS 671 1 Active Databases CIS 671 2

Triggers: The Problem - The Problem:


Examples from COMPANY Database Example from BANK Database
EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) Branch(BranchID, BranchName, BranchCity, Assets)
DEPARTMENT(DNO, TotalSalary, ManagerSSN) Customer(CustID, CustName, CustStreet, CustCity)
STARTING_PAY(JobCode, StartPay) Account(AccountNo, BranchID, Balance)
AccountCustomer(AccountNo, CustID)
1. Limit all salary increases to 50%.
2. Enforce policy that salaries may never decrease. Loan(LoanNo, BranchID, Balance)
LoanCustomer(LoanID, CustID)
3. Maintain TotalSalary in DEPARTMENT relation as
employees and their salaries change. 6. Branch Assets are maintained as sum(Account.Balance)
for each branch.
4. Inform a supervisor whenever a supervisees salary
7. Overdrafts do not produce a negative balance. Instead
becomes larger than the supervisors.
they are treated as a loan. The account balance is set to 0
5. All new hires for a given job code get the same starting and a loan is created for the amount of the overdraft.
salary, which is available in the STARTING_PAY table.
Active Databases CIS 671 3 Active Databases CIS 671 4

The Problem: What is Needed?


Example from TEMPERATURE Database The Event-Condition-Action Model
TEMPS City Temp
EXTREMES City High
Temp
High Low Low
Date Temp Date
(ECA Model)
Denver 25 Denver 100 7/30 -10 1/20

Columbus 15 Columbus 95 7/5 2 2/14 Rules (or triggers) with three components:
Anchorage -15 Anchorage 87 8/3 -10 2/16 Event triggering the rule. (insert, delete, update)
E.g., an employees salary changes.
8. Given table of temperatures TEMPS, that is periodically Condition to determine if rule action should be
updated, keep the table of extreme temperatures EXTREMES executed.
up to date. E.g., is new Temp in City higher than HighTemp for that City?
Action to be taken.
Create the EXTREMES table and populate with all the cities
E.g., update the Departments Total Salary.
in the TEMPS table, setting the other attributes to null.

Active Databases CIS 671 5 Active Databases CIS 671 6

1
What is Needed?
The Event-Condition-Action Model
Availability
(ECA Model), continued
Triggers included in SQL 1999 (SQL 3)
Actions may apply before or after the Not in earlier standards.
triggering event is executed.
Included much earlier in most products:
An SQL statement may change several
rows. Oracle, Sybase, DB2
Apply action once per SQL statement. As a consequence syntax may differ from the
Apply action for each row changed by SQL standard.
statement.

Active Databases CIS 671 7 Active Databases CIS 671 8

The Problem: 1. COMPANY Database


Examples from COMPANY Database Limit all salary increases to 50%
EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) before trigger emp_salary_limit
DEPARTMENT(DNO, TotalSalary, ManagerSSN)
STARTING_PAY(JobCode, StartPay) EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode)
1. Limit all salary increases to 50%.
2. Enforce policy that salaries may never decrease.
3. Maintain TotalSalary in DEPARTMENT relation as create trigger emp_salary_limit
employees and their salaries change. (EN, Fig. 23.2 (a)) before update of EMPLOYEE
for each row
4. Inform a supervisor whenever a supervisees salary when (new.Salary > 1.5 * old.Salary)
becomes larger than the supervisors. (EN, Fig. 23.2 (b)) set new.Salary = 1.5 * old.Salary;
5. All new hires for a given job code get the same starting
salary, which is available in the STARTING_PAY table. new refers to old refers to
the new tuple. the old tuple.
Active Databases CIS 671 9 Active Databases CIS 671 10

2. COMPANY Database 5. COMPANY Database: All new hires for a given


Enforce policy that salaries may never decrease job code get the same starting salary, which is
before trigger emp_salary_no_decrease available in the STARTING_PAY table.
EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) before trigger emp_start_pay

create trigger emp_salary_no_decrease EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode)


before update of EMPLOYEE STARTING_PAY(JobCode, StartPay)
for each row
Method depends on
when (new.Salary < old.Salary)
DBMS. create trigger emp_start_pay
begin
before insert on EMPLOYEE
log the event;
for each row
signal error condition;
set Salary =
end
(select StartPay
from STARTING_PAY
where JobCode = new.JobCode)

Active Databases CIS 671 11 Active Databases CIS 671 12

2
7. BANK Database: Overdrafts, continued
7. BANK Database: Overdrafts do not produce a
after trigger overdraft_trigger
negative balance. Instead they are treated as a loan.
Branch(BranchID, BranchName, BranchCity, Assets)
The account balance is set to 0 and a loan is created Customer(CustID, CustName, CustStreet, CustCity)
for the amount of the overdraft. Account(AccountNo, BranchID, Balance)
after trigger overdraft_trigger AccountCustomer(AccountNo, CustID)
Loan(LoanNo, BranchID, Balance)
LoanCustomer(LoanID, CustID)
Insert a new tuple in the Loan relation, using same branch as
the account. Make LoanNo the same as the AccountNo and the
Balance the amount of the overdraft.

Insert a new tuple in the LoanCustomer relation relating the


new loan to this customer.
Set the Balance of the Account tuple to 0.
Active Databases CIS 671 13 Active Databases CIS 671 14

7. BANK Database: Overdrafts, continued 8. TEMPS Database: Create the EXTREMES table and populate
with all the cities in the TEMPS table, setting the other attributes to null.
after trigger overdraft_trigger

create trigger overdraft_trigger after update on Account after triggers HighTempUpdate and LowTempUpdate
for each row
when new.balance < 0 TEMPS(City, Temp)
begin EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate)
insert into Loan values
(new.AccountID, new.BranchID, -new.Balance); Need two after triggers
insert into LoanCustomer
(select CustID, AccountNo one for updating the high temperature (HighTempUpdate),
from AccountCustomer AC the other for updating the low temperature (LowTempUpdate).
where new.AccountID = AC.AccountID); They will be very similar.
update Account
set Balance = 0
where Account.AccountNo = new.AccountNo;
end;

Active Databases CIS 671 15 Active Databases CIS 671 16

8. TEMPS Database: continued. High Tempearture Update


TEMPS(City, Temp)
8. TEMPS Database: continued.
EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate) Other Problems
create trigger HighTempUpdate
after update of TEMPS Normal Insert a new City into TEMPS.
for each row situation.
when (new.Temp > Must also insert into EXTREMES.
( select HighTemp Initial values of HighTemp, HighDate, LowTemp,
from EXTREMES
Initially
where City= new.City) HighTemp LowDate must be set.
or (selectHighTemp is null.
from EXTREMES Delete a City from TEMPS.
where City = new.City) is null ) ) Leave City in EXTREMES.
update EXTREMES
set HighTemp = new.Temp, Low Temperature Delete City from EXTREMES.
HighDate = current date similar.
where City = new.City;
Active Databases CIS 671 17 Active Databases CIS 671 18

3
8. TEMPS Database: continued.
8. TEMPS Database: continued.
Insert a new City into TEMPS:
Delete a City from TEMPS:
Insert City tuple into EXTREMES.
Delete City tuple from EXTREMES.
Set initial values of HighTemp, HighDate,
Add Foreign Key constraint for EXTREMES.
LowTemp, LowDate.

TEMPS(City, Temp) TEMPS(City, Temp)


EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate) EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate)

create trigger NewCity


after insert of TEMPS Alter table EXTREMES
for each row add constraint fk
insert into EXTREMES(City, HighTemp, HighDate, foreign key(City) references TEMPS
LowTemp, LowDate) on delete cascade;
values(new.City, new.Temp, Current Date,
new.Temp, Current Date);
Active Databases CIS 671 19 Active Databases CIS 671 20

Triggers: The Problem -


Problems with Use of Triggers 9. Another Example from
COMPANY Database
How to guarantee set of triggers is consistent? EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode)
DEPARTMENT(DNO, TotalSalary, ManagerSSN)
Recursion is allowed. STARTING_PAY(JobCode, StartPay)
How to guarantee termination? 1. Add a new field, Span, to the EMPLOYEE relation.
For each employee, Span is the number of employees
Tools are still needed to help address these problems. supervised.
EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode,
Span)
2. Initialize Span appropriately.
3. Keep Span correct as the database changes.

Active Databases CIS 671 21 Active Databases CIS 671 22

9. COMPANY Database 9. COMPANY Database


For each employee, Span is the Update Span for the immediate supervisor.
number of employees supervised. EMPLOYEE(Name, SSN, ..., SupervisorSSN,..., Span)

EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode, create trigger EmpHire


create trigger EmpTransfer
Span) after insert on EMPLOYEE
after update of SupervisorSSN
for each row
Idea update EMPLOYEE
on EMPLOYEE
set Span = Span + 1 for each row
Add the Span attribute to EMPLOYEE. where SSN = begin
update EMPLOYEE
new.SupervisorSSN;
Initialize the values of Span based on the current database. set Span = Span - 1
create trigger EmpQuit where SSN =
Create insert, delete and update triggers to keep Span up to after delete on EMPLOYEE old.SupervisorSSN;
for each row update EMPLOYEE
date for an employees immediate supervisor. update EMPLOYEE set Span = Span + 1
set Span = Span - 1 where SSN =
Create another update trigger to propagate the change in Span where SSN = new.SupervisorSSN;
up through the rest of the hierarchy. old.SupervisorSSN; end;
Active Databases CIS 671 23 Active Databases CIS 671 24

4
9. COMPANY Database 10. For each PERSON, record their mother,
Propagate Span up the supervisor tree.
father and number of descendants.
EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode, Span) PERSONS(Name, Mother, Father, NumDescendants)
A 123 456 4
new value
B 456 789 8
After insert, update the mother and father.
Supervisor create trigger NewMother create trigger NewFather
after insert on PERSONS after insert on PERSONS
create trigger EmpPropagate for each row for each row
after update of Span on update PERSONS update PERSONS
EMPLOYEE set NumDescendants set NumDescendants
+(new.Span - old.Span) = NumDescendants + 1 = NumDescendants + 1
for each row decrease (4 - 5) = -1
where Name = new.Mother; where Name = new.Father;
update EMPLOYEE increase (4 - 3) = +1
set Span = Span +
(new.Span - old.Span)
where SSN =
Then update the maternal and paternal ancestors.
new.SupervisorSSN;
Active Databases CIS 671 25 Active Databases CIS 671 26

10. For each PERSON, record their mother,


father and number of descendants.
PERSONS(Name, Mother, Father, NumDescendants)

Update the maternal and paternal ancestors.


create trigger MaternalAncestor
after update of NumDescendants create trigger
on PERSONS PaternalAncestor after
for each row update of NumDescendants
update PERSONS on PERSONS
set NumDescendants
= NumDescendants /* Similar.
+ new.NumDescendants Just replace Mother
- old.NumDescendants with Father. */
where Name = new.Mother;

At nth level of family tree, how many triggers?


Active Databases CIS 671 27

Вам также может понравиться