sql: structured query language chapter 5...

36
1 SQL: Structured Query Language Chapter 5 (cont.) Constraints Triggers

Upload: others

Post on 18-Oct-2019

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

1

SQL: Structured Query Language

Chapter 5 (cont.)

Constraints

Triggers

Page 2: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

2

Integrity Constraints

Constraint describes conditions that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate ICs are disallowed.

Can be used to :

• ensure application semantics

e.g., sid is a key

• prevent inconsistencies

e.g., sname has to be a string,

age must be < 200;

Page 3: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

3

Types of Integrity Constraints

Fundamental:

• Domain constraints,

• Primary key constraints,

• Foreign key constraints

• Plus NOT NULL, UNIQUE, etc.

General:

• Check constraints (often limited to only simple ones)

• Table constraints or assertions (but not available in most systems)

Page 4: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

4

Check or Table Constraints

Can use queries to express constraint.

VERY LIMITED IN MOST DBMSs!!!!

CREATE TABLE Sailors( sid INTEGER,

sname CHAR(10),

rating INTEGER,

age REAL,

PRIMARY KEY (sid),CHECK ( rating >= 1

AND rating <= 10 ))

Page 5: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

5

Explicit Domain Constraints

CREATE TABLE Sailors( sid INTEGER,

sname CHAR(10),

rating values-of-ratings,

age REAL,

PRIMARY KEY (sid))

CREATE DOMAIN values-of-ratings INTEGER

DEFAULT 1

CHECK ( VALUE >= 1 AND VALUE <= 10)

Page 6: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

6

More Powerful Table Constraints

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 that Interlake boats cannot be reserved:

If condition evaluates to FALSE, update is rejected.

Page 7: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

7

Table Constraints

Associated with one table

Only needs to hold TRUE when table is non-empty.

Page 8: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

8

More Table Constraint Examples

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 )

Symmetric constraint, yet associated with Sailors.

If Sailors is empty, the number of Boats tuples can be anything!

Number of boats plus number of sailors is < 100

Page 9: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

9

Assertions: Constraints over Multiple Relations

CREATE TABLE Sailors( sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),)

ASSERTION not associated with either table.

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

Number of boatsplus number of sailors is < 100

Page 10: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

10

Caution !!!

Assertions or Check constraints with subqueries are NOT supported in Oracle !!!

Page 11: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

11

Triggers : Make Database Active

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

Analog to a "daemon" that monitors a database for certain events to occur

Page 12: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

12

Triggers : Make Database Active

Three parts:

Event (activates the trigger)

Condition (tests whether the triggers should run) [Optional]

Action (what happens if the trigger runs)

Semantics:

When an event occurs,

And this condition is satisfied,

Then the associated action is performed.

Page 13: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

13

Triggers – Event,Condition,Action Events could be :

BEFORE|AFTER INSERT|UPDATE|DELETE

ON <tableName>

e.g.: BEFORE INSERT ON Professor

Condition is an SQL expression (An SQL query with a non-empty result means TRUE.)

Action can be many different choices : SQL statements , body of PSM, and even DDL and transaction-

oriented statements like “commit”.

Page 14: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

14

Example Trigger

Assume our DB has a relation schema :

Professor (pNum, pName, salary)

We want to write a trigger that :

Ensures that any new professor inserted

has salary >= 90,000;

Page 15: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

15

Example Trigger

CREATE TRIGGER minSalary BEFORE

INSERT ON Professor

for what context ?

BEGIN

check for violation here ?

END;

Page 16: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

16

Example Trigger

CREATE TRIGGER minSalary BEFORE

INSERT ON Professor

FOR EACH ROW

BEGIN

Violation of Minimum Professor

Salary?

END;

Page 17: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

17

Example Trigger

CREATE TRIGGER minSalary BEFORE

INSERT ON Professor

FOR EACH ROW

BEGIN

IF (:new.salary < 90000)

THEN RAISE_APPLICATION_ERROR

(-20004,

‘Violation of Min Prof Salary’);

END IF;

END;

Page 18: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

18

Example triggerCREATE TRIGGER minSalary BEFORE INSERT ON

Professor

FOR EACH ROW

DECLARE temp int; -- dummy variable

BEGIN

IF (:new.salary < 90000)

THEN RAISE_APPLICATION_ERROR (-20004,

‘Violation of Minimum

Professor Salary’);

END IF;

temp := 10; -- variable

END;

.

Note: In SQLPLUS, don’t forget the “dot” after the trigger.

Page 19: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

19

Details of Trigger Example

BEFORE INSERT ON Professor

This trigger is checked before tuple is inserted

FOR EACH ROW

specifies trigger is performed for each row inserted

:new

refers to new tuple inserted

If (:new.salary < 90000)

then an application error is raised and hence the row is not inserted; otherwise row is inserted.

Use error code: -20004;

Indicates value is in an invalid range

Page 20: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

20

Example Trigger Using Condition

CREATE TRIGGER minSalary BEFORE

INSERT ON Professor

FOR EACH ROW

WHEN (new.salary < 90000)

BEGIN

RAISE_APPLICATION_ERROR (-

20004, ‘Violation of

Minimum Professor Salary’);

END;

Conditions can refer to old/new values of tuples modified by the statement activating the trigger.

Page 21: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

21

Triggers: REFERENCING

CREATE TRIGGER minSalary BEFORE INSERT ON Professor

REFERENCING NEW as newTuple

FOR EACH ROW

WHEN (newTuple.salary < 90000)

BEGIN

RAISE_APPLICATION_ERROR (-20004,

‘Violation of Minimum Professor Salary’);

END;

Page 22: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

22

Example Trigger

CREATE TRIGGER minSalary

BEFORE UPDATE ON Professor

REFERENCING OLD AS oldTuple NEW as

newTuple

FOR EACH ROW

WHEN (newTuple.salary < oldTuple.salary)

BEGIN

RAISE_APPLICATION_ERROR (-20004,

‘Salary Decreasing !!’);

END;

Ensure that salary does not decrease

Page 23: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

23

Another Useful Example Trigger

CREATE TRIGGER minSalary

AFTER UPDATE on avail-parts ON INVENTORY

FOR EACH ROW

WHEN (new.avail-parts < new.reorderThres)

DECLARE x NUMBER;

BEGIN

SELECT COUNT(*) INTO x

FROM Pending_orders WHERE Part_no = :new.Part_no;

IF x = 0 THEN

INSERT INTO Pending_orders

VALUES (:new.Part_no,:new.Reorder_quant, sysdate);

END IF;

END;

Page 24: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

25

Row vs Statement Level Trigger

Row level: activated once per modified tuple

Statement level: activate once per SQL statement

Row level triggers can access new data,

statement level triggers cannot generally do that (depends on DBMS).

Statement level triggers more efficient if we do not need to make row-specific decisions

Page 25: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

26

Row vs Statement Level Trigger

Example: Consider a relation schema :

Account (num, amount)

where we will allow creation of new accounts only during normal business hours.

Page 26: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

27

Example: Statement level trigger

CREATE TRIGGER MYTRIG1 BEFORE INSERT ON Account

FOR EACH STATEMENT

BEGIN

IF (TO_CHAR(SYSDATE,’dy’) IN (‘sat’,’sun’))

OR

(TO_CHAR(SYSDATE,’hh24:mi’) NOT BETWEEN ’08:00’

AND ’17:00’)

THEN

RAISE_APPLICATION_ERROR(-20500,

’Cannot create new account now !!’);

END IF;

END;

Note: “FOR EACH STATEMENT” is default;

so you omit this clause in most systems.

Page 27: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

28

Another Trigger Example (SQL:99)

CREATE TRIGGER youngSailorUpdate

AFTER INSERT ON SAILORS

REFERENCING NEW TABLE AS NewSailors

FOR EACH STATEMENT

INSERT

INTO YoungSailors(sid, name, age, rating)

SELECT sid, name, age, rating

FROM NewSailors N

WHERE N.age <= 18

Note: Oracle uses “ FOR EACH STATEMENT” as

default, hence you would simply not list this. They also do

not support reference to such a delta-table as above.

Page 28: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

29

When to use BEFORE/AFTER

Based on semantics and on efficiency considerations.

Suppose we perform statement-level after insert,then all the rows are inserted first, then if the condition fails, and all the inserted rows must be “rolled back”

Not very efficient !

Page 29: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

30

Combining multiple events into one trigger

CREATE TRIGGER salaryRestrictions

AFTER INSERT OR UPDATE ON Professor

FOR EACH ROW

BEGIN

IF (INSERTING AND :new.salary < 90000) THEN

RAISE_APPLICATION_ERROR (-20004, 'below min

salary'); END IF;

IF (UPDATING AND :new.salary < :old.salary)

THEN RAISE_APPLICATION_ERROR (-20004,

‘Salary Decreasing !!'); END IF;

END;

Page 30: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

31

Summary : Trigger Syntax

CREATE TRIGGER <triggerName>

BEFORE|AFTER INSERT|DELETE|UPDATE

[OF <columnList>] ON <tableName>|<viewName>

[REFERENCING [OLD AS <oldName>] [NEW AS

<newName>]]

[FOR EACH ROW] (default is “FOR EACH STATEMENT”)

[WHEN (<condition>)]

<PSM body>;

Page 31: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

32

Points about Triggers

Check the system tables : user_triggers

user_trigger_cols

SELECT Trigger_type, Triggering_event, Table_name

FROM USER_TRIGGERS

WHERE Trigger_name = 'REORDER';

TYPE TRIGGERING_STATEMENT TABLE_NAME

------------------------------------------------

AFTER EACH ROW UPDATE INVENTORY

Page 32: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

33

Points about Triggers

ORA-04091: mutating relation problem

In a row level trigger, you cannot have the body refer to the table specified in the event as it would see inconsistent data

Also INSTEAD OF triggers can be specified on views to model updateable views

Page 33: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

34

Triggers and Oracle

Note 1: Running Triggers. End CREATE TRIGGER statement with a dot and run:

Type the body of the trigger

Press '.' to terminate the sql statement

Type 'run' to create the trigger

Example:> CREATE TRIGGER minSalary BEFORE ….

>…. END;

>.

> run;

Running the CREATE TRIGGER statement only creates the trigger; it does not execute it.

Only a triggering event, such as an insertion, causes the trigger to execute.

Page 34: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

35

Triggers and Oracle

Note : Displaying Trigger Definition Errors

If you get compilation errors, you can see the error

messages by typing in:

show errors trigger <trigger_name>;

Example:

> show errors trigger minSalary;

However , reported line numbers where

errors occur are not always useful.

Page 35: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

36

Constraints versus Triggers

Constraints are useful for database consistency Use IC when sufficient

More opportunity for optimization

Not restricted to insert/delete/update

Triggers are flexible and more powerful Alerters

Event logging for auditing

Security enforcement

Analysis of table accesses (statistics)

Workflow and business logic

But can be hard to understand …… Several triggers (Arbitrary order unpredictable !?)

Chain triggers (When to stop ?)

Recursive triggers (Termination?)

Page 36: SQL: Structured Query Language Chapter 5 (cont.)web.cs.wpi.edu/~cs542/s17/material/Ch5_SQL_TRIGGER_part3.pdf · Constraints are useful for database consistency Use IC when sufficient

37

Summary

SQL allows specification of rich integrity constraints and their efficient maintenance

Triggers respond to changes in the database: powerful for enforcing application semantics