Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers...

17
base Management Systems 3ed, R. Ramakrishnan and J. Gehrke SQL: Constraints and Triggers Chapter 5, 5.7-5.8

Transcript of Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers...

Page 1: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

SQL: Constraints and Triggers

Chapter 5, 5.7-5.8

Page 2: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Integrity Constraints (Review)

An IC describes conditions that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate IC’s are

disallowed. Can be used to ensure application semantics (e.g.,

sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200)

Types of IC’s: Domain constraints, primary key constraints, foreign key constraints, general constraints. Domain constraints: Field values must be of right

type. Always enforced.

Page 3: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

General Constraints: CHECK Syntax: CHECK conditional-expression. The conditional expression captures more

general ICs than keys. The conditional expressions can use queries. The conditional expressions required to hold

only if the associated table is nonempty. A CHECK constraint may be expressed over

several tables; however, it is often expressed over one single table.

Constraints can be named: CONSTRAINT MyConstraint

CHECK conditional-expression

Page 4: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

CHECK Constraints: Examples

Constraint: Rating must be in the range 1 to 10

CREATE TABLE Sailors( sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),CHECK ( rating >= 1 AND rating <= 10 ))

Page 5: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

CHECK Constraints: Examples

CREATE TABLE Reserves( sname CHAR(10),bid INTEGER,day DATE,PRIMARY KEY (bid,day),CONSTRAINT noInterlakeResCHECK (`Interlake’ <>

( SELECT B.bnameFROM Boats BWHERE B.bid=bid)))

Constraint: Interlake boats cannot be reserved

Page 6: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

General Constraints: ASSERTION

CREATE TABLE Sailors( sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),CHECK ( (SELECT COUNT (S.sid) FROM Sailors S)+ (SELECT COUNT (B.bid) FROM Boats B) < 100 ) This solution is awkward and wrong:

It is wrongfully associated only with Sailors, though it involves both Sailors and Boats.

If Sailors is empty, the number of Boats tuples can be anything, since the conditional expression is not required to hold in such case!

Constraint: Number of boats plus number of sailors is < 100

Page 7: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

General Constraints: ASSERTION

The assertion is not associated with any one of the tables involved.

ASSERTION is the right solution; not associated with either table.

CREATE ASSERTION smallClubCHECK ( (SELECT COUNT (S.sid) FROM Sailors S)+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )

Constraint: Number of boats plus number of sailors is < 100

Page 8: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

General Constraints: Further Examples

Write SQL integrity constraints (domain, key, foreign key, or CHECK constraints; or assertions) for given requirements: Students must have a minimum cgpa of 5. Every TA must also be a student. The total percentage of all assignments to projects for a

given student must be at most 100%. A TA must have a cgpa higher than any student that she

coaches.

Schema: Students(sid: int, sname: string, age: int, cgpa: real)

Works(sid: int, pid: int, pct_time: int) Projects(did: int, budget: real, ta: int)

Page 9: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9

General Constraints: Further Examples (1)Constraint: Students must have a minimum cgpa of 5.

CREATE TABLE Students( sid INTEGER,sname CHAR(10),age REAL,

cgpa REAL,PRIMARY KEY (sid),CHECK ( cgpa >= 5))

Constraint: Every TA must also be a student.

CREATE ASSERTION TAisStudentCHECK ( (SELECT COUNT (*) FROM Projects P WHERE P.ta_id NOT IN (SELECT sid

FROM Students)) =0 )

Page 10: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

General Constraints: Further Examples (2)Constraint: The total percentage of all assignments to

projects for a student must be at most 100%.

CREATE TABLE Works( sid INTEGER,pid INTEGER,pct_time INTEGER,PRIMARY KEY (sid,pid),

FOREIGN KEY (sid) REFERENCES Students, FOREIGN KEY (pid) REFERENCES Projects,

CHECK ((SELECT COUNT (W.stid) FROM Works W

GROUP BY W.stid HAVING SUM(pct_time) > 100) = 0)

Page 11: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

General Constraints: Further Examples (3)

CREATE ASSERTION TAHigherCGPACHECK ((SELECT COUNT(S.stid) FROM Students S, Students TA, Works W, Projects P WHERE S.sid=W.sid AND W.pid=P.pid AND P.ta=TA.sid AND S.cgpa > TA.cgpa) =0)

Constraint: A TA must have a cgpa higher than any student that she coaches.

Page 12: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

Triggers

Trigger: procedure that starts automatically if specified changes occur to the DBMS

Three parts: Event (activates the trigger) Condition (tests whether the triggers should

run) Action (what happens if the trigger runs)

Page 13: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

Triggers BEFORE trigger: action executed before the

activating event occurs. AFTER trigger: action executed after the

activating event occurs. INSTEAD trigger: action executed instead of the

activating event. Row-level trigger: executed once per modified

row (that satisfies the trigger condition). Statement-level trigger: executed once per

modifying statement. Transition variables: NEW, OLD, NEW TABLE, OLD

TABLE.

Page 14: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Triggers: Example (SQL:92)

CREATE TRIGGER incr_count AFTER INSERT ON Sailors

WHEN (new.age < 18)FOR EACH ROWBEGIN

count:=count+1;END

Illustrates use of NEW to refer to newly inserted tuples Exists since SQL:92

Increment a count for each newly inserted sailor whose age < 18.

Page 15: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15

Triggers: Example (SQL:1999)

CREATE TRIGGER youngSailorsUpdateAFTER INSERT ON Sailors

REFERENCING NEW TABLE AS NewSailorsFOR EACH STATEMENT /* This is the default */

INSERTINTO YoungSailors(sid, name, age, rating)SELECT sid, name, age, ratingFROM NewSailors NWHERE N.age <= 18

Save newly inserted sailors aged < 18 in a special table.

Illustrates use of NEW TABLE to refer to a set of newly inserted tuples Exists since SQL:1999

Page 16: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Triggers: More Elaborated Example

CREATE TRIGGER bumpTAcgpaAFTER UPDATE ON Students

WHEN OLD.cgpa < NEW.cgpaFOR EACH ROWBEGIN UPDATE Students S SET S.cgpa = NEW.cgpa WHERE S.cgpa < NEW.cgpa AND S.sid IN (SELECT P.ta

FROM STudents S1, Works W, Projects P WHERE S1.sid = NEW.sid

AND S1.sid = W.sid AND W.sid = P.sid);END

Whenever a student is given a (bonus) raise on his cgpa, the TA’s cgpa must be increased to be at least as high.

Illustrates use of Oracle PL/SQL syntax in the action part.

Page 17: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17

Summary SQL allows specification of rich integrity

constraints Triggers respond to changes in the

database