Advanced SQL
Advanced SQL
& Assertions
1
Triggers
2
Triggers: Introduction
The application constraints need to be captured inside the database
3
Triggers: Introduction
Other application constraints are more 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
4
Triggers
A procedure that runs automatically when a
certain event occurs in the DBMS
5
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
Two granularities
Execute for each row
Execute for each statement
7
1) Trigger: Event
Trigger name
Create Trigger <name>
Before|After Insert|Update|Delete ON <tablename> That is the event
Example
Create Trigger ABC Create Trigger XYZ
Before Insert On Students After Update On Students
. .
9
Example: Granularity of Event
10
2) Trigger: Condition
This component is optional
Create Trigger <name>
Before| After Insert| Update| Delete On <tableName>
For Each Row | For Each Statement
If the employee salary > 150,000 then some actions will be taken
12
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)
13
Trigger: Referencing Values (Contd)
Insert Event
Has only :new defined
Delete Event
Has only :old defined
Update Event
Has both :new and :old defined
After triggering
Should not change :new because the event is already done
14
Example 1
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
15
Example 2
If the employee salary increased by more than 10%, then increment the
rank field by 1.
16
Example 3: Using Temp Variable
If the newly inserted record in employee has null hireDate field, fill it in
with the current date
17
Example 4: Maintenance of
Derived Attributes
Keep the bonus attribute in Employee table always 3% of the salary
attribute
18
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
19
Example 5: Statement-level
Trigger
Store the count of employees having salary > 100,000 in table R
20
Order Of Trigger Firing
Even
Before Trigger Before Trigger t After Trigger After Trigger
(statement-level) (row-level) (row- (row-level) (statement-level)
level)
21
Some Other Operations
Dropping Trigger
SQL> Create Trigger <trigger name>;
22
Assertions
23
Assertions
An expression that should be always true
24
Example 1
25
Example 2
26
Example 3
27
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
Any Questions
31