Sunteți pe pagina 1din 6

UNIVERSITY OF TECHNOLOGY, JAMAICA

Advanced Databases Lab


FACULTY:

Engineering & Computing (FENC)

SCHOOL/DEPT:

School of Computing & Information Technology

COURSE OF STUDY:

Bachelor of Science in Computing

YEAR:

Three (3)

Introductory Notes:
Today we will be looking at the triggers and how they are used in our SQL Server Environment.

A database trigger is a special type of stored procedure that is automatically executed in


response to certain events on a database or a database object in particular table or view in a
database.
Triggers are primarily used for auditing, enforcing business rules and to maintain data
consistency and integrity. It should be noted that triggers are used to overwrite what
would be considered the normal actions of the database, Eg. Overwriting what action an
insert statement would take.
Examples
Auditing uses of trigger - Who changed what and when? What is the old data versus the new
data?
Data Consistency example - when a new record (representing a customer) is added to the
marketing list, new records should be created also in the tables of the Loyalty ID and campaign
offers.
Types of Triggers
Triggers can be used on data manipulation (DML) events, data definition (DDL) events and
logon events. We will focus on the DML triggers. DML triggers relate to the Update, Delete
and Insert statements.
Execution Time of Triggers
Triggers have the ability to be execute before or after a DML statement has been executed and
completely. In SQL server before triggers are defined by using the INSTEAD OF keyword,
which allows you to overwrite the normal operation of the event. To have the trigger execute
after the DML statement has been completed you use the AFTER key word.

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

UNIVERSITY OF TECHNOLOGY, JAMAICA


Advanced Databases Lab
1 Creating Triggers
INSERT TRIGGER
We want to place a trigger on a customer table that will display the record that that is trying to be
added INSTEAD OF being inserted into the customer table. This means no inserts will happen on the
customer table you have overwritten the action to now just display it.
Example 1
CREATE TRIGGER before_insert_customer
ON customer INSTEAD OF INSERT
AS
Select * from Inserted

Inserted is a temporary table that


Comment [RW1]:
stores the record that is currently trying to be inserted. It will
have the same fields as the table the trigger is being placed
on. So if the trigger is on marketing list, inserted will have
the same fields as marketing list. Inserted is dynamic and
takes the same field list for any table a trigger is placed on.

NB This trigger will return the result set of the record you are trying to insert. Our
expected result is that we should not see the record in our customer table. Instead we
should see the record being displayed because thats what we told it to do.
Example 2
CREATE TRIGGER before_insert_customer
ON customer INSTEAD OF INSERT
AS
Select DOB, last_name from Inserted

Comment [RW2]:
temporary row.

PLEASE DROP THE TRIGGERS AFTER YOU HAVE TRIED IT. IF YOU LEAVE THE TRIGGER IT MEANS
THAT YOU CAN NO LONGER INSERT.

Exercise 1
a) Develop the trigger in example 1 for marketing list.
I.
II.
III.

Try to insert a record.


What happens? Is it in the record in the marketing list table?
What is the purpose of inserted?

b) Create a table called Child List that has the same fields as Marketing List. Develop a
trigger that will be placed on the marketing list table that will insert a record into the
2

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

Notice you can list out the fields for

UNIVERSITY OF TECHNOLOGY, JAMAICA


Advanced Databases Lab
child table if the persons age is less than 18 once an insert is attempted. The trigger
will also place records in the customer table if the person is 18 and over.
c) Insert 4 new records into customer, 2 with DOBs before 1991 and 2 with DOBs after
2000.
DELETE TRIGGER
We want to place a trigger on the customer table that will display the record that is trying to deleted
INTEAD OF being deleted from the customer table.
Example 3 Before trigger
CREATE TRIGGER before_delete_customer
ON customer INSTEAD OF DELETE
AS
Select * from deleted

Comment
[R3]:
of the record.

Example 4 After Trigger (Lets say we wanted to display the recorded after it was deleted.)
CREATE TRIGGER before_delete_customer
ON customer AFTER DELETE
AS
Select * from deleted

Exercise 2
a) Try the trigger in example 3 for marketing list. Try to delete some records what are your
observations? [Remember to drop your trigger]
b) Create a table called Audit_Log that has the same fields as marketing list with 3
additional fields: Audit_Action, Audit_Table, Action_Date.
c) Develop triggers that will be placed on both the customer and child tables that will track
any record deleted and place them in the Audit_Log Table while populating the three
additional audit fields.
Hint:
INSERT INTO customer (first_name, last_name, sign_up_date )
SELECT first_name, last_name, getdate() from mailing_list ;

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

Notice that deleted stores the old version

UNIVERSITY OF TECHNOLOGY, JAMAICA


Advanced Databases Lab
When inserting to the table Audit_Log you should use the values for three additional
fields to tell where the record is coming from and what action was being taken.
Example
Audit_Action Deleted or Updated
Audit_Table Marketing List or Child
Action_Date Todays Date

d) Delete a specific record from the Child and Customer Table.


e) Display the contents of the Audit_Log Table.

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

UNIVERSITY OF TECHNOLOGY, JAMAICA


Advanced Databases Lab
2 Accessing the Updated row
When an update trigger is fired the original row and the updated row are stored. It should be noted that
the new row is stored in our temporary table INSERTED while the original (old) row is stored in
DELETED.
Example 1
CREATE TRIGGER update _customer
ON customer FOR UPDATE
AS
Select * from Inserted

Comment [RW4]:

Select * from Deleted

Inserted stores our newly updated row

Deleted stores our original row, we still


Comment [RW5]:
have access to the same fields as the table on which the
trigger was placed

PLEASE DROP THE TRIGGER AFTER YOU HAVE TRIED IT.

Example 1
CREATE TRIGGER update _customer
ON customer AFTER UPDATE
AS
Select DOB, last_name from Inserted

Comment
[RW6]:
temporary row.

Select DOB, last_name from Deleted

Comment
[RW7]:
temporary row.

Exercise 3
a) Try the two examples above for marketing _list. Ensure for example 2 you are updating
the last_name and DOB of a customer.
b) Alter your marketing_list table and add a Date created field. Default the value to the
current date.
c) Create a trigger that only allows updates to records that are less than three days old.
d) Create a trigger that stores the old version of a record to the audit_log table with the
appropriate audit fields updated.
e) Create a trigger that will only update a record if the old record is different from the new.
5

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

Notice you can list out the fields for


Notice you can list out the fields for

UNIVERSITY OF TECHNOLOGY, JAMAICA


Advanced Databases Lab
3 Checking if a field has been updated
We also have the ability to check if a specific field has been updated. We cannot specify this in
the declaration of the Trigger like we do in Oracle. We need to fire the trigger first and then
check it in the body of the trigger.
Example 3 Checking if a field has been updated
CREATE TRIGGER customer_check_lastname
ON customer AFTER Update
AS
If UPDATE(last_name)

UPDATE is a keyword used to test if a


Comment
[RW8]:
field has been updated.

BEGIN
Select The last name was changed
END
PLEASE DROP THE TRIGGER AFTER YOU HAVE TRIED IT.

Exercise 4
a) Create an update trigger on Marketing_list and Child that checks if the DOB has been
updated. If the new DOB is 18 and over in Child move the record to Customer table and
vice versa for Customer.

Advanced Databases Lab Manual

Prepared by Ray - St. Michael Williams

S-ar putea să vă placă și