IC and Triggers in SQL. Find age of the youngest sailor with age

16
IC and Triggers in SQL

Transcript of IC and Triggers in SQL. Find age of the youngest sailor with age

Page 1: IC and Triggers in SQL. Find age of the youngest sailor with age

IC and Triggers in SQL

Page 2: IC and Triggers in SQL. Find age of the youngest sailor with age

Find age of the youngest sailor with age <=18, for each rating with at least 2 such sailors

SELECT S.rating, MIN (S.age) AS minageFROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) > 1

sid sname rating age

22 dustin 7 45.0

29 brutus 1 33.0

31 lubber 8 55.5

32 andy 8 25.5

58 rusty 10 35.0

64 horatio 7 35.0

71 zorba 10 16.0

74 horatio 9 35.0

85 art 3 25.5

95 bob 3 63.5

96 frodo 3 25.5

Answer relation:

Sailors instance:

rating minage 3 25.5 7 35.0 8 25.5

HAVING Clause

Page 3: IC and Triggers in SQL. Find age of the youngest sailor with age

Integrity Constraints (Review)

• An IC describes conditions that every legal instance of a relation must satisfy

• To disallow inserts/deletes/updates that violate IC’s

• Types of IC’s: Domain constraints, primary key constraints, foreign key constraints, non-null, general constraints

Page 4: IC and Triggers in SQL. Find age of the youngest sailor with age

An Example via CHECK Clause

CREATE TABLE Stu (sid INTEGER,

sname CHAR(10),

rating INTEGER,

age REAL,

PRIMARY KEY (sid),CHECK (rating >= 1 AND rating <= 10 )

)

Page 5: IC and Triggers in SQL. Find age of the youngest sailor with age

CHECK Example

CREATE TABLE Stu (sid INTEGER,

sname CHAR(10),

rating INTEGER,

age REAL,

PRIMARY KEY (sid),CONSTRAINT checkRatingCHECK (rating >= 1 AND rating <= 10 )

)

Page 6: IC and Triggers in SQL. Find age of the youngest sailor with age

ASSERTION ExampleConstraints Over Multiple Relations• Consider a very small school: the count of students and professors should be less than 500• The following is a poor integrity test as it is associated with one relation (the Stu table could be

empty and thus the integrity rule is never checked!• Disassociate from the Stu table

CREATE TABLE Stu (sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),CHECK ((SELECT COUNT (S.sid) FROM Stu S) +(SELECT COUNT (P.pid) FROM Prof P) < 500 )

)

Page 7: IC and Triggers in SQL. Find age of the youngest sailor with age

ASSERTION ExampleConstraints Over Multiple Relations

CREATE ASSERTION smallSchoolCHECK (

(SELECT COUNT (S.sid) FROM Stu S) +(SELECT COUNT (P.pid) FROM Prof P) < 500

)

Page 8: IC and Triggers in SQL. Find age of the youngest sailor with age

ASSERTION ExampleThe KEY Constraint

CREATE ASSERTION KeyCHECK (

(SELECT COUNT (DISTINCT sid) FROM Stu) =(SELECT COUNT (*) FROM Stu)

);

• Note: ASSERTION is in standard SQL but not implemented

Page 9: IC and Triggers in SQL. Find age of the youngest sailor with age

Triggers

• Event-Condition-Action rules: procedures that start automatically if a change occurs to the DBMS

When event occurs, check condition; if true do action

• Why: to move monitoring logic from applications into the DBMS (more modular, consistency among applications, no duplications)

• Allow automatic repair• Database stores triggers just as regular data so they are persistent

and accessible to all database operations• Useful for security purposes• Triggers make a passive database “active”

Page 10: IC and Triggers in SQL. Find age of the youngest sailor with age

Advantages

• To move application logic and business rules into database

• Allows more functionality for DBAs to establish vital constraints/rules of applications

• Rules managed in some central “place”• Rules automatically enforced by DBMS, no

matter which applications later come on line

Page 11: IC and Triggers in SQL. Find age of the youngest sailor with age

The Event-Condition-Action Model

• 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

Page 12: IC and Triggers in SQL. Find age of the youngest sailor with age

The 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)

Page 13: IC and Triggers in SQL. Find age of the youngest sailor with age

Limit all salary increases to 50%

create trigger emp_salary_limitbefore update of EMPLOYEEfor each row

when (new.Salary > 1.5 * old.Salary)set new.Salary = 1.5 * old.Salary;

“new” refers to the new tuple.

“old” refers to the old tuple.

Page 14: IC and Triggers in SQL. Find age of the youngest sailor with age

Enforce policy that salaries may never decrease

create trigger emp_salary_no_decreasebefore update of EMPLOYEEfor each row

when (new.Salary < old.Salary)begin

log the event;signal error condition;

end

Method depends on DBMS.

Page 15: IC and Triggers in SQL. Find age of the youngest sailor with age

All new hires for a given job code get the same starting salary

create trigger emp_start_paybefore insert on EMPLOYEEfor each row

set Salary =(select StartPayfrom STARTING_PAYwhere JobCode = new.JobCode)

EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode)STARTING_PAY(JobCode, StartPay)

Page 16: IC and Triggers in SQL. Find age of the youngest sailor with age

Trigger Syntax

CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) BEGIN --- sql statements END;