+ All Categories
Home > Documents > Advanced SQL: Triggers & Assertions

Advanced SQL: Triggers & Assertions

Date post: 05-Jan-2016
Category:
Upload: padma
View: 47 times
Download: 2 times
Share this document with a friend
Description:
Advanced SQL: Triggers & Assertions. Instructor: Mohamed Eltabakh [email protected]. Today ’ s Roadmap. Views Triggers Assertions Cursors Stored Procedures. We covered these. To be covered today. Views ’ Recap. An SQL query that we register (store) inside the database - PowerPoint PPT Presentation
36
Advanced SQL: Triggers & Assertions Instructor: Mohamed Eltabakh [email protected] 1
Transcript
Page 1: Advanced SQL: Triggers & Assertions

Advanced SQL: Triggers & Assertions

Instructor: Mohamed Eltabakh [email protected]

1

Page 2: Advanced SQL: Triggers & Assertions

Today’s Roadmap

Views

Triggers

Assertions

Cursors

Stored Procedures

We covered these

To be covered today

Page 3: Advanced SQL: Triggers & Assertions

Views’ Recap

An SQL query that we register (store) inside the database No Data are stored in the database (only the definition) Any query can be a view

3

CREATE VIEW ProfNumStudents AS

SELECT pNumber, count(*) AS CNT

FROM Student

GROUP BY pNumber;

View schema (pNumber, CNT)

View name

Page 4: Advanced SQL: Triggers & Assertions

Views’ Recap (Cont’d)

Query a view as any regular table

4

CREATE VIEW ProfNumStudents AS

SELECT pNumber, count(*) AS CNT

FROM Student

GROUP BY pNumber;

Query: Select the professor number who has more that 20 students

Query the view

Select pNumber

From ProfNumStudents

Where CNT > 20;

Create the view

Page 5: Advanced SQL: Triggers & Assertions

Triggers

5

Page 6: Advanced SQL: Triggers & Assertions

Triggers: Introduction The application constraints need to be captured inside the database

Some constraints can be captured by: Primary Keys, Foreign Keys, Unique, Not NULL, and domain constraints

6

CREATE TABLE Students(sid: CHAR(20), name: CHAR(20) NOT NULL, login: CHAR(10), age: INTEGER, gpa: REAL Default 0,

Constraint pk Primary Key (sid), Constraint u1 Unique (login),Constraint gpaMax check (gpa <= 4.0) );

These constraints are defined in CREATE TABLE or ALTER TABLE

Page 7: Advanced SQL: Triggers & Assertions

Triggers: Introduction Some application constraints are complex

Need for assertions and triggers

Examples: Sum of loans taken by a customer does not exceed 100,000 Student cannot take the same course after getting a pass

grade in it Age field is derived automatically from the Date-of-Birth field

7

Page 8: Advanced SQL: Triggers & Assertions

Trigger Components

Three components Event: When this event happens, the trigger is activated Condition (optional): If the condition is true, the trigger

executes, otherwise skipped Action: The actions performed by the trigger

Semantics When the Event occurs and Condition is true, execute

the Action

8

Lets see how to define these components

Lets see how to define these components

Page 9: Advanced SQL: Triggers & Assertions

Trigger: Events Three event types

Insert Update Delete

Two triggering times Before the event After the event

Two granularities Execute for each row Execute for each statement

9

Page 10: Advanced SQL: Triggers & Assertions

1) Trigger: Event

Example

10

Create Trigger <name>

Before|After Insert|Update|Delete ON <tablename>

….

That is the event

Create Trigger ABC

Before Insert On Students

….

This trigger is activated when an insert statement is issued, but before the new record is inserted

Create Trigger XYZ

After Update On Students

….

This trigger is activated when an update statement is issued and after the update is executed

Trigger name

Page 11: Advanced SQL: Triggers & Assertions

Granularity of Event

A single SQL statement may update, delete, or insert many records at the same time E.g., Update student set gpa = gpa x 0.8;

Does the trigger execute for each updated or deleted record, or once for the entire statement ? We define such granularity

11

Create Trigger <name>

Before| After Insert| Update| Delete

For Each Row | For Each Statement

….

This is the event

This is the granularity

Page 12: Advanced SQL: Triggers & Assertions

Example: Granularity of Event

12

Create Trigger XYZ

After Update ON <tablename>

For each statement

….

This trigger is activated once (per UPDATE statement) after all records are updated

Create Trigger XYZ

Before Delete ON <tablename>

For each row

….

This trigger is activated before deleting each record

Page 13: Advanced SQL: Triggers & Assertions

2) Trigger: Condition

This component is optional Trigger executes only if the condition is true

13

Create Trigger <name>

Before| After Insert| Update| Delete On <tableName>

For Each Row | For Each Statement

When <condition>

That is the condition

If the employee salary > 150,000 then some actions will be taken

Create Trigger EmpSal

After Insert or Update On Employee

For Each Row

When (new.salary >150,000)

Page 14: Advanced SQL: Triggers & Assertions

3) Trigger: Action Action depends on what you want to do, e.g.:

Check certain values Fill in some values Inserts/deletes/updates other records Check that some business constraints are satisfied Commit (approve the transaction) or roll back (cancel the

transaction)

In the action, you may want to reference: The new values of inserted or updated records (:new) The old values of deleted or updated records (:old)

14

Page 15: Advanced SQL: Triggers & Assertions

Trigger: Referencing Values In the action, you may want to reference:

The new values of inserted or updated records (:new) The old values of deleted or updated records (:old)

15

Create Trigger EmpSal

After Insert or Update On Employee

For Each Row

When (new.salary >150,000)

Begin

if (:new.salary < 100,000) …

End;

Trigger body

Inside “When”, the “new” and “old” should not have “:”

Inside the trigger body, they should have “:”

Page 16: Advanced SQL: Triggers & Assertions

Trigger: Referencing Values (Cont’d) Insert Event

Has only :new defined

Delete Event Has only :old defined

Update Event Has both :new and :old defined

Before triggering (for insert/update) Can update the values in :new Changing :old values does not make sense

After triggering Should not change :new because the event is already done

16

Page 17: Advanced SQL: Triggers & Assertions

Example 1

17

If the employee salary increased by more than 10%, make sure the ‘rank’ field is not empty and its value has changed, otherwise reject the update

Create or Replace Trigger EmpSal

Before Update On Employee

For Each Row

Begin

IF (:new.salary > (:old.salary * 1.1)) Then

IF (:new.rank is null or :new.rank = :old.rank) Then

RAISE_APPLICATION_ERROR(-20004, 'rank field not correct');

End IF;

End IF;

End;

/

If the trigger exists, then drop it first

Compare the old and new salaries

Make sure to have the “/” to run the command

Page 18: Advanced SQL: Triggers & Assertions

Example 2

18

If the employee salary increased by more than 10%, then increment the rank field by 1.

Create or Replace Trigger EmpSal

Before Update Of salary On Employee

For Each Row

Begin

IF (:new.salary > (:old.salary * 1.1)) Then

:new.rank := :old.rank + 1;

End IF;

End;

/

In the case of Update event only, we can specify which columns

We changed the new value of rank field

The assignment operator has “:”

Page 19: Advanced SQL: Triggers & Assertions

Example 3: Using Temp Variable

19

If the newly inserted record in employee has null hireDate field, fill it in with the current date

Create Trigger EmpDate

Before Insert On Employee

For Each Row

Declare

temp date;

Begin

Select sysdate into temp from dual;

IF (:new.hireDate is null) Then

:new.hireDate := temp;

End IF;

End;

/

Oracle way to select the current date

Updating the new value of hireDate before inserting it

Declare section to define variables

Since we need to change values, then it must be “Before” event

Page 20: Advanced SQL: Triggers & Assertions

Example 4: Maintenance of Derived Attributes

20

Keep the bonus attribute in Employee table always 3% of the salary attribute

Create Trigger EmpBonus

Before Insert Or Update On Employee

For Each Row

Begin

:new.bonus := :new.salary * 0.03;

End;

/

The bonus value is always computed automatically

Indicate two events at the same time

Page 21: Advanced SQL: Triggers & Assertions

Combining Multiple Events in One Trigger

If you combine multiple operations Sometimes you need to know what is the current operation

Create Trigger EmpBonus

Before Insert Or Update On Employee

For Each Row

Begin

IF (inserting) Then … End IF;

IF (updating) Then … End IF;

End;

/

Combine Insert and Update

Can do something different under each operation

Page 22: Advanced SQL: Triggers & Assertions

Before vs. After Before Event

When checking certain conditions that may cause the operation to be cancelled E.g., if the name is null, do not insert

When modifying values before the operation E.g., if the date is null, put the current date

After Event When taking other actions that will not affect the current operations

The insert in table X will cause an update in table Y

Before Insert Trigger:

:new.x := …. //Changing value x that will be inserted

After Insert Trigger:

:new.x := … //meaningless because the value is already inserted

Page 23: Advanced SQL: Triggers & Assertions

Row-Level vs. Statement-Level Triggers Example: Update emp set salary = 1.1 * salary;

Changes many rows (records)

Row-level triggers Check individual values and can update them Have access to :new and :old vectors

Statement-level triggers Do not have access to :new or :old vectors (only for row-level) Execute once for the entire statement regardless how many records are

affected Used for verification before or after the statement

23

Page 24: Advanced SQL: Triggers & Assertions

Example 5: Statement-level Trigger

24

Store the count of employees having salary > 100,000 in table R

Create Trigger EmpBonus

After Insert Or Update of salary Or Delete On Employee

For Each Statement

Begin

delete from R;

insert into R(cnt) Select count(*) from employee where salary > 100,000;

End;

/

Delete the existing record in R, and then insert the new count.

Indicate three events at the same time

Page 25: Advanced SQL: Triggers & Assertions

Order Of Trigger Firing

25

Before Trigger(statement-level)Before Trigger(statement-level)

After Trigger(statement-level)

After Trigger(statement-level)

Before Trigger(row-level)

After Trigger(row-level)

Event

(row-level)

Event

(row-level)

Loop over each affected record

Page 26: Advanced SQL: Triggers & Assertions

Some Other Operations

Dropping Trigger

If creating trigger with errors

26

SQL> Drop Trigger <trigger name>;

SQL > Show errors;

It displays the compilation errors

Page 27: Advanced SQL: Triggers & Assertions

Key Points in Triggers

Must understand what type of trigger to create Before or After Under which operation: Insert, Update, or Delete Row-level or Statement-level

:old and :new variables Update both are available Insert Only :new is available Delete Only :old is available

27

Page 28: Advanced SQL: Triggers & Assertions

Assertions

28

Page 29: Advanced SQL: Triggers & Assertions

Assertions

An expression that should be always true

When created, the expression must be true

DBMS checks the assertion after any change that may violate the expression

29

Must return True or False

Page 30: Advanced SQL: Triggers & Assertions

Example 1

30

Sum of loans taken by a customer does not exceed 100,000

Create Assertion SumLoans Check

( 100,000 >= ALL

Select Sum(amount)

From borrower B , loan L

Where B.loan_number = L.loan_number

Group By customer_name );

Must return True or False (not a relation)

Page 31: Advanced SQL: Triggers & Assertions

Example 2

31

Number of accounts for each customer in a given branch is at most two

Create Assertion NumAccounts Check

( 2 >= ALL

Select count(*)

From account A , depositor D

Where A.account_number = D.account_number

Group By customer_name, branch_name );

Page 32: Advanced SQL: Triggers & Assertions

Example 3

32

Customer city is always not null

Create Assertion CityCheck Check

( NOT EXISTS (

Select *

From customer

Where customer_city is null));

Page 33: Advanced SQL: Triggers & Assertions

Assertions vs. Triggers Assertions do not modify the data, they only check certain

conditions

Triggers are more powerful because the can check conditions and also modify the data

Assertions are not linked to specific tables in the database and not linked to specific events

Triggers are linked to specific tables and specific events

Page 34: Advanced SQL: Triggers & Assertions

Assertions vs. Triggers (Cont’d)

All assertions can be implemented as triggers (one or more)

Not all triggers can be implemented as assertions

Oracle does not have assertions

Page 35: Advanced SQL: Triggers & Assertions

Example: Trigger vs. Assertion

All new customers opening an account must have opening balance >= $100. However, once the account is opened their balance can fall below that amount.

We need triggers, assertions cannot be usedTrigger Event: Before Insert

Create Trigger OpeningBal

Before Insert On Customer

For Each Row

Begin

IF (:new.balance is null or :new.balance < 100) Then RAISE_APPLICATION_ERROR(-20004, 'Balance should be >= $100');

End IF;

End;

Page 36: Advanced SQL: Triggers & Assertions

Triggers & Assertions

36

Any Questions


Recommended