Академический Документы
Профессиональный Документы
Культура Документы
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
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.
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.
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.
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;
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.
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