Instead of record insertion time, a DML trigger can act when a record has been updated on a table. To support this operation, you can use the following formula: CREATE TRIGGER TriggerName
ON TableName
AFTER/FOR UPDATE
AS
TriggerCode
The new keyword in this formula is UPDATE. This indicates that the DML trigger will act when the record has been updated. Everything else is as described for the INSERT operator. Remember to use either AFTER UPDATE or FOR UPDATE.
When a record has been removed from a table, you can apply a DML trigger in response. To make it possible, you can use the following formula: CREATE TRIGGER TriggerName
ON TableName
AFTER/FOR DELETE
AS
TriggerCode
This time, the formula uses the DELETE operator in an AFTER DELETE or a FOR DELETE expression. This is used for record removal. The other factors follow the same description we saw for the INSERT operator. When a DELETE trigger has acted on a table, the database engine creates a special temporary table named deleted. This table holds a copy of the records that were deleted. Eventually, if necessary, you can access this table to find out about those records.
Although we created only one trigger for a table so far, you can go farther than that:
DML triggers present many other characteristics.
In Lesson 11, we saw that, to assist a user with data entry, you can specify that a column would allow or not allow null values. If a column is marked as NOT NULL, during data entry, if the user does not or cannot provide a value for that column, the record cannot be created. If you create a DML trigger that must act against that table, if the nullity rule is violated, the trigger will not run. In Lesson 11, we saw that you can create a check constraint on a table to make sure that every new record, or a record that is being edited, follows a certain rule. We know if the record does not abide by that rule, the record will not be created or changed. If a DML trigger is supposed to act on the table, if this rule is not respected, the trigger would fail. One of the limitations of a check constraint is that it applies only to the table that owns it. A DML trigger can be created to perform a check constraint on more than one table. This provides its advantage over the normal check constraint. In Lesson 16, we studied data relationships and referential integrity. This makes sure that, when a record is edited in a parent table, the change is also made on the child table. This also means that the integrity is applied to more than one table. When a DML trigger runs, if a referential rule is violated, the trigger, which also checks the referential integrity, fails.
Consider the following table and view in a database: CREATE DATABASE SmallBusiness;
GO
USE SmallBusiness;
GO
CREATE TABLE Customers
(
CustomerID int identity(1, 1) primary key not null,
AccountNumber nchar(10),
FullName nvarchar(50)
);
GO
CREATE TABLE DatabaseOperations (
EmployeeName nvarchar(50),
ActionPerformed nvarchar(50),
TimePerformed datetime2
);
GO
From what we have seen so far, when a user opens a table or a view to perform data entry, when a new record has been created, the table or view fires an event. We saw that, using a DML trigger, you can make a notification. For example you can fill out a log to keep track of the changes. By default, when a record is submitted, it gets saved. In some cases, when a user has opened a table and tried to make a change, such as adding a new record, editing an existing record, or deleting one, instead of accepting the change, you can dismiss it. You can then use a DML trigger to make a note. This is the basis of another category of DML triggers: an "instead of" trigger. While an AFTER/FOR trigger acts on a table after the action has occurred, you may want to do something before the event. For example, you may want to prevent the user from adding a new record on a tale, or from changing an existing, or from deleting a record. Of course, it is better to take care of this before the action is performed. One way you can do this is by creating an "instead of" trigger.
While an AFTER trigger can be applied to a table only, an "instead of" trigger can be associated with either a table or a view. Therefore, to create an "instead of" trigger, you use the following formula: CREATE TRIGGER TriggerName
ON TableOrViewName
INSTEAD OF INSERT/UPDATE/DELETE
AS
TriggerCode
You start with the CREATE TRIGGER expression followed by a name for the trigger. After the name of the trigger, type ON followed by the name of either a table or a view on which the trigger will act. From our review of the AFTER trigger, the new expression here is INSTEAD OF. This expression is followed by the type of operation to perform. If you want:
To start the triggering code, type AS and write the desired code. If you use the INSTEAD OF expression, the trigger starts when the table or view is opened but before a change has taken place. The difference with the AFTER trigger is that, this time, you can perform some action(s) before the change is made on the table or view. This also implies that, if the code of the trigger is to create a new record, at this time, the record doest not yet exist, which means you cannot catch that record. At this time also, you can prevent the record from being created (since it has not yet been created anyway). For example, the following code will not accept that a new record be added to the table: USE SmallBusiness;
GO
CREATE TRIGGER CreateCustomer
ON Customers
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO DatabaseOperations
VALUES(SUSER_SNAME(),
N'Attempt to create new record', GETDATE())
END
GO
If you want to get a copy of the record that was affected by the event, you can access it from the inserted (for an INSERT or UPDATE trigger) or from the deleted (for a DELETE) trigger. Here is an example: USE SmallBusiness;
GO
DROP TRIGGER CreateCustomer;
GO
CREATE TRIGGER CreateCustomer
ON Customers
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Customers
SELECT AccountNumber, FullName FROM inserted
END
GO
An AFTER/FOR and an INSTEAD OF triggers have many differences. For example:
In Lesson 3, we saw that the creation of a database uses a Data Definition Language (DDL) command. In Lesson 9, we saw another example of a DDL command that involved creating a table. Each one of these creation operations fires an event. A DDL trigger is a trigger that acts when a certain type of DDL event fires. These include the creation, modification, or removal of an object, not its records. This is the primary difference with a DML trigger that fires when a record is added or acted upon. A DDL trigger gives you the opportunity to do some administrative work in response to the event. For example, you can get a notification, or notify someone else using an automatically generated email, that an (and what) object has been created. Or you can use a DDL trigger to discard the operation.
You create a DDL trigger using code. The basic formula is: CREATE TRIGGER TriggerName
ON DATABASE/ALL SERVER
FOR/AFTER WhatEvent
AS
TriggerCode
You start a DDL trigger with the CREATE TRIGGER expression followed by a name for the new trigger. The name follows the same rules we have applied to objects so far. After the name of the trigger, type the ON keyword:
After specifying the object (the whole server or only the current database) on which the trigger will act, type either FOR or AFTER. This is followed by the event against which the trigger will act. As mentioned already, the events are DDL commands. To specify the event, use the formula of the command with the words separated by an underscore. For example, if you want the trigger to act when a CREATE TABLE command is executed, specify the event as CREATE_TABLE. After specifying the event that will fire, type AS followed by the normal code of the trigger. Here is an example that makes a note and adds it to a table when a new table has been created: USE SmallBusiness;
GO
CREATE TRIGGER LogNewTableCreation
ON DATABASE
FOR CREATE_TABLE
AS
BEGIN
INSERT INTO DatabaseOperations
VALUES(SUSER_SNAME(),
N'A new table was created', GETDATE())
END
GO
Whenever a new table is created in the current database, the trigger runs, gets the name of the user who created the table, the date and time the table was created, and a small message. These pieces of information are then stored in a log table. As mentioned for DML triggers, you manage DDL triggers by modifying or deleting them. These are done using the same description we saw for DML triggers. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||
| Previous | Copyright © 2009 FunctionX, Inc. | Next |
|
|
||