Upload
hoangngoc
View
284
Download
0
Embed Size (px)
Citation preview
PROCEDURAL DATABASE PROGRAMMING
( PL/SQL AND T-SQL)
Database Programming
Instructor: Michael Kremer, Ph.D.Technology & Information Management
Class 7
AGENDA
9. Database Triggers
9.1 Overview of Triggers
9.2 DML Trigger Concepts
9.3 Basic DML Triggers
9.4 Advanced DML Triggers
9.5 Instead of Triggers
9.1 OVERVIEW OF TRIGGERS
Triggers are program units where procedural code is executed.
Triggers are executed when certain events take place.
Different kinds of triggers:
DML Triggers: Executed through CRUD operations. Perform validation,
additional data updates, default values, auditing of data.
DDL Triggers: Executed when DDL statements are executed. Auditing of
DDL object creation, prevent certain DDL statements.
Database Triggers: Executed when database starts up or is shut down.
SQL Server only has Logon trigger.
Instead Of Triggers: Alternatives to DML triggers. Execute when DML
operations are about to occur, you specify what code to execute instead of
the regular DML statement.
Keep in mind that DML triggers are part of a transaction.
Transaction is not complete until the trigger completes.
167
9.2 DML TRIGGER CONCEPTS
DML triggers fire when a DML statement is executed.
DML triggers are the most commonly used ones.
Triggers are categorized by three attributes: what causes them to execute, when do they execute, and how often they execute.
Event (What): Statement that causes the firing of a trigger is called the triggering event. Trigger will be defined on a table and on Update, Delete, and/or Insert.
Action Time (When): Trigger can execute BEFORE or AFTER the triggering statement Trigger activation time or action time.
Granularity (How often): Statement or row triggers. A statement trigger executes once per statement; a row trigger executes once for each modified row.
There are total of 12 possible trigger combinations:
Row/Statement/Before/After Insert
Row/Statement/Before/After Update
Row/Statement/Before/After Delete
168
9.2 DML TRIGGER CONCEPTS
Triggers cannot:
accept arguments.
In general, perform a commit or rollback (since triggers are in the middle of a DML process)
query or even update the table that the trigger belongs to (mutating table error).
In a trigger, you can reference the “before” and “after” value.
Insert and Delete triggers affect the entire row, but Update triggers can be programmed to execute on a column basis.
Write one trigger to combine Update, Delete, and Insert trigger.
Use special keywords to check for a particular DML action.
169
9.2 DML TRIGGER CONCEPTS
To determine
the DML action
Simple keywords
in Oracle.
In SQL Server,
use system shadow tables Inserted and Deleted.
Example Update: Inserted table contains new, updated values,
Deleted table contains old values before the update.
You can create multiple triggers on one table. not
recommended.
Only when you really need to break down large chunk of code
into multiple units.
You can specify the order of those triggers (new in Oracle 11g).
170
9.3 BASIC DML TRIGGERS
Syntax to create
triggers
ON keyword is used
to specify the table
or view this trigger
is attached to.
For/After or Instead Of specifies when the trigger fires.
INSERT/UPDATE/DELETE specifies the DML action when the
trigger fires, one or more actions are allowed.
AS starts the trigger body. Triggers do not return any data nor do
they have input parameters.
171
9.3 BASIC DML TRIGGERS
System tables deleted and inserted are used to store values
before and after the trigger operation.
During a delete operation, only the deleted table is populated.
During an insert operation, only the inserted table is populated.
During an update operation, both inserted and deleted tables
are populated. Deleted hold the previous values, whereas
inserted holds the updated values.
If Update(column) is used to execute statements only when
specific columns are updated.
IF Columns Updated() uses bitwise comparison to determine
which columns were updated.
172
9.3 BASIC DML TRIGGERS
Before/After/Instead Of
the timing of the trigger.
Oracle has a true before
trigger!
INSERT/DELETE/UPDATE
UPDATE OF to specify the
DML action when trigger
fires.
ON table/view determines the table or view the trigger belongs
to.
For Each Row sets the trigger as a row level trigger as opposed
to as a statement trigger.
IF Updating/Inserting/Deleting can be used to execute
statements based on DML conditions.
173
9.3 ADVANCED DML TRIGGERS
Triggers seem like a simple tool to implement business rules.
Exhaust first any other possibilities before using trigger since
maintenance (scattered code), testing and debugging proves to
be more difficult.
New in Oracle 11g: Compound triggers and variables
Mutating Table Error
Whenever a trigger is fired, a DML operation is performed on a
table. Oracle does not allow a row level trigger to read or even
update the table that is undergoing the change.
SQL Server does not raise this error. However, to implement
code that reads the data in a table before the insert is
theoretically not possible in SQL Server as it does not have a
Before trigger!
180
9.3 ADVANCED DML TRIGGERS
Status Transition Rules
Another common application for triggers is the enforcement of
transition rules.
For example, employee status starts with a P for new employees.
Certain rules concerning the transitioning from one status to
another.
185
9.3 ADVANCED DML TRIGGERS
Simple Audit Example
Both SQL Server and Oracle contain built-in auditing
mechanisms that you can turn on and configure.
Create customized audit system through the use of triggers.
The following sample triggers determines the DML action and
monitors the salary column of the employees table.
The audit table stores information about the table name, column
name, the old and the new value, date and time, and the user
who performed the change.
189
9.3 INSTEAD OF TRIGGERS
In SQL Server, Instead Of triggers are used to simulate Before
triggers since they are also allowed on tables.
In Oracle, Instead Of triggers control insert, update, merge, and
delete on views only, not tables.
Sometimes you want to present a view to the users (especially
when it is a one-to-one) so that they can see all their data at
once.
In general, views are nonupdateable, due to the nature of
relationships between the tables involved in the view.
The idea behind the Instead Of trigger is that the developer
designs custom programming logic to update data in the
corresponding tables in such a way that no relational rules are
violated and that it makes sense from a business perspective.
193