Presentation is loading. Please wait.

Presentation is loading. Please wait.

Active DatabasesCIS 6711 Triggers: The Problem - Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may.

Similar presentations


Presentation on theme: "Active DatabasesCIS 6711 Triggers: The Problem - Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may."— Presentation transcript:

1 Active DatabasesCIS 6711 Triggers: The Problem - Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may never decrease. 3.Maintain TotalSalary in DEPARTMENT relation as employees and their salaries change. 4.Inform a supervisor whenever a supervisee’s salary becomes larger than the supervisor’s. 5.All new hires for a given job code get the same starting salary, which is available in the STARTING_PAY table. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) DEPARTMENT(DNO, TotalSalary, ManagerSSN) STARTING_PAY(JobCode, StartPay)

2 Active DatabasesCIS 6712 The Problem: Example from BANK Database Branch(BranchID, BranchName, BranchCity, Assets) Customer(CustID, CustName, CustStreet, CustCity) Account(AccountNo, BranchID, Balance) AccountCustomer(AccountNo, CustID) Loan(LoanNo, BranchID, Balance) LoanCustomer(LoanID, CustID) 6.Branch Assets are maintained as sum(Account.Balance) for each branch. 7.Overdrafts do not produce a negative balance. Instead they are treated as a loan. The account balance is set to 0 and a loan is created for the amount of the overdraft.

3 Active DatabasesCIS 6713 The Problem: Example from TEMPERATURE Database TEMPSCityTemp Denver25 Columbus15 Anchorage-15 EXTREMESCityHigh Temp High Date Low Temp Low Date Denver1007/30-101/20 Columbus957/522/14 Anchorage878/3-102/16 8.Given table of temperatures TEMPS, that is periodically updated, keep the table of extreme temperatures EXTREMES up to date. Create the EXTREMES table and populate with all the cities in the TEMPS table, setting the other attributes to null.

4 Active DatabasesCIS 6714 What is Needed? The Event-Condition-Action Model (ECA Model) Rules (or triggers) with three components: –Event triggering the rule. (insert, delete, update) E.g., an employee’s salary changes. –Condition to determine if rule action should be executed. E.g., is new Temp in City higher than HighTemp for that City? –Action to be taken. E.g., update the Department’s Total Salary.

5 Active DatabasesCIS 6715 What is Needed? The Event-Condition-Action Model (ECA Model), continued Actions may apply before or after the triggering event is executed. An SQL statement may change several rows. –Apply action once per SQL statement. –Apply action for each row changed by SQL statement.

6 Active DatabasesCIS 6716 Availability Triggers included in SQL 1999 (SQL 3) –Not in earlier standards. Included much earlier in most products: –Oracle, Sybase, DB2 –As a consequence syntax may differ from the standard.

7 Active DatabasesCIS 6717 The Problem: Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may never decrease. 3.Maintain TotalSalary in DEPARTMENT relation as employees and their salaries change. (EN, Fig. 23.2 (a)) 4.Inform a supervisor whenever a supervisee’s salary becomes larger than the supervisor’s. (EN, Fig. 23.2 (b)) 5.All new hires for a given job code get the same starting salary, which is available in the STARTING_PAY table. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) DEPARTMENT(DNO, TotalSalary, ManagerSSN) STARTING_PAY(JobCode, StartPay)

8 Active DatabasesCIS 6718 1. COMPANY Database Limit all salary increases to 50% before trigger emp_salary_limit create trigger emp_salary_limit before update of EMPLOYEE for each row when (new.Salary > 1.5 * old.Salary) set new.Salary = 1.5 * old.Salary; EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) “new” refers to the new tuple. “old” refers to the old tuple.

9 Active DatabasesCIS 6719 2. COMPANY Database Enforce policy that salaries may never decrease before trigger emp_salary_no_decrease create trigger emp_salary_no_decrease before update of EMPLOYEE for each row when (new.Salary < old.Salary) begin log the event; signal error condition; end Method depends on DBMS. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode)

10 Active DatabasesCIS 67110 5. COMPANY Database: All new hires for a given job code get the same starting salary, which is available in the STARTING_PAY table. before trigger emp_start_pay create trigger emp_start_pay before insert on EMPLOYEE for each row set Salary = (select StartPay fromSTARTING_PAY whereJobCode = new.JobCode) EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) STARTING_PAY(JobCode, StartPay)

11 Active DatabasesCIS 67111 7. BANK Database: Overdrafts do not produce a negative balance. Instead they are treated as a loan. The account balance is set to 0 and a loan is created for the amount of the overdraft. after trigger overdraft_trigger

12 Active DatabasesCIS 67112 7. BANK Database: Overdrafts, continued after trigger overdraft_trigger Branch(BranchID, BranchName, BranchCity, Assets) Customer(CustID, CustName, CustStreet, CustCity) Account(AccountNo, BranchID, Balance) 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.

13 Active DatabasesCIS 67113 7. BANK Database: Overdrafts, continued after trigger overdraft_trigger create trigger overdraft_trigger after update on Account for each row when new.balance < 0 begin insert into Loan values (new.AccountID, new.BranchID, -new.Balance); insert into LoanCustomer (selectCustID, AccountNo fromAccountCustomer AC wherenew.AccountID = AC.AccountID); update Account set Balance = 0 where Account.AccountNo = new.AccountNo; end;

14 Active DatabasesCIS 67114 8. TEMPS Database: Create the EXTREMES table and populate with all the cities in the TEMPS table, setting the other attributes to null. after triggers HighTempUpdate and LowTempUpdate TEMPS(City, Temp) EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate) Need two after triggers –one for updating the high temperature ( HighTempUpdate ), –the other for updating the low temperature ( LowTempUpdate ). They will be very similar.

15 Active DatabasesCIS 67115 8. TEMPS Database: continued. High Tempearture Update create trigger HighTempUpdate after update of TEMPS for each row when (new.Temp > (selectHighTemp fromEXTREMES whereCity= new.City) or(selectHighTemp fromEXTREMES whereCity = new.City) is null )) update EXTREMES set HighTemp= new.Temp, HighDate = current date whereCity = new.City; TEMPS(City, Temp) EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate) Initially HighTemp is null. Normal situation. Low Temperature similar.

16 Active DatabasesCIS 67116 8. TEMPS Database: continued. Other Problems Insert a new City into TEMPS. –Must also insert into EXTREMES. –Initial values of HighTemp, HighDate, LowTemp, LowDate must be set. Delete a City from TEMPS. –Leave City in EXTREMES. –Delete City from EXTREMES.

17 Active DatabasesCIS 67117 8. TEMPS Database: continued. Insert a new City into TEMPS : Insert City tuple into EXTREMES. Set initial values of HighTemp, HighDate, LowTemp, LowDate. create trigger NewCity after insert of TEMPS for each row insert into EXTREMES(City,HighTemp, HighDate, LowTemp, LowDate) values(new.City, new.Temp, Current Date, new.Temp, Current Date); TEMPS(City, Temp) EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate)

18 Active DatabasesCIS 67118 8. TEMPS Database: continued. Delete a City from TEMPS : Delete City tuple from EXTREMES. Add Foreign Key constraint for EXTREMES. Alter table EXTREMES add constraint fk foreign key(City) references TEMPS on delete cascade; TEMPS(City, Temp) EXTREMES(City, HighTemp, HighDate, LowTemp, LowDate)

19 Active DatabasesCIS 67119 Problems with Use of Triggers How to guarantee set of triggers is consistent? Recursion is allowed. –How to guarantee termination? Tools are still needed to help address these problems.

20 Active DatabasesCIS 67120 Triggers: The Problem - 9. Another Example from COMPANY Database 1.Add a new field, Span, to the EMPLOYEE relation. For each employee, Span is the number of employees supervised. 2.Initialize Span appropriately. 3.Keep Span correct as the database changes. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) DEPARTMENT(DNO, TotalSalary, ManagerSSN) STARTING_PAY(JobCode, StartPay) EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode, Span)

21 Active DatabasesCIS 67121 9. COMPANY Database For each employee, Span is the number of employees supervised. Idea Add the Span attribute to EMPLOYEE. Initialize the values of Span based on the current database. Create insert, delete and update triggers to keep Span up to date for an employee’s immediate supervisor. Create another update trigger to propagate the change in Span up through the rest of the hierarchy. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode, Span)

22 Active DatabasesCIS 67122 9. COMPANY Database Update Span for the immediate supervisor. create trigger EmpHire after insert on EMPLOYEE for each row update EMPLOYEE set Span = Span + 1 where SSN = new.SupervisorSSN; EMPLOYEE(Name, SSN,..., SupervisorSSN,..., Span) create trigger EmpTransfer after update of SupervisorSSN on EMPLOYEE for each row begin update EMPLOYEE set Span = Span - 1 where SSN = old.SupervisorSSN; update EMPLOYEE set Span = Span + 1 where SSN = new.SupervisorSSN; end; create trigger EmpQuit after delete on EMPLOYEE for each row update EMPLOYEE set Span = Span - 1 where SSN = old.SupervisorSSN;

23 Active DatabasesCIS 67123 9. COMPANY Database Propagate Span up the supervisor tree. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode, Span) A1234564 B4567898 create trigger EmpPropagate after update of Span on EMPLOYEE for each row update EMPLOYEE set Span = Span + (new.Span - old.Span) where SSN = new.SupervisorSSN; Supervisor +(new.Span - old.Span) decrease (4 - 5) = -1 increase (4 - 3) = +1 new value

24 Active DatabasesCIS 67124 10. For each PERSON, record their mother, father and number of descendants. PERSONS(Name, Mother, Father, NumDescendants) create trigger NewMother after insert on PERSONS for each row update PERSONS set NumDescendants = NumDescendants + 1 where Name = new.Mother; create trigger NewFather after insert on PERSONS for each row update PERSONS set NumDescendants = NumDescendants + 1 where Name = new.Father; Then update the maternal and paternal ancestors. After insert, update the mother and father.

25 Active DatabasesCIS 67125 10. For each PERSON, record their mother, father and number of descendants. Update the maternal and paternal ancestors. create trigger MaternalAncestor after update of NumDescendants on PERSONS for each row update PERSONS set NumDescendants = NumDescendants + new.NumDescendants - old.NumDescendants where Name = new.Mother; At n th level of family tree, how many triggers? PERSONS(Name, Mother, Father, NumDescendants) create trigger PaternalAncestor after update of NumDescendants on PERSONS /* Similar. Just replace “Mother” with “Father.” */


Download ppt "Active DatabasesCIS 6711 Triggers: The Problem - Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may."

Similar presentations


Ads by Google