Intro
In this part I will be talking about:
- Trigger limitations
- Trigger use cases
Trigger limitation and use cases
Let’s begin by discussing some of the advantages of using Triggers:
- Used for Database Integrity purposes
- We can enforce business rules and store them directly in the databases, this makes it easier to change and update the applications that are using the database, because the business logic is encapsulated inside the database itself
- Triggers give us control over which statements are allowed in a database
- Implementation of complex business logic triggered by a single event
- Auditing the database and user activities
now let’s see what the Disadvantages are:
- Triggers are not easy to manage in a centralized manner, because they are difficult to be detected or viewed
- they are invisible to a client application or when debugging code
- because of their complex code, we can’t sometime trace their logic when troubleshooting
- They can affect the server and make it slower by overusing them
Finding the server-level trigger
From all those pros and cons, we can conclude that we should document our triggers and make them as simpler as we can.
Because Triggers can be implemented on many levels (system, database, tables, etc) SQL Server gave us a way to view that information about triggers in one place
- [1] Getting system-level triggers
|
|
- [2] Getting the database and table triggers
|
|
Trigger type and definition
**The type of the trigger (database or table) can be determined from the ‘parent_class_desc’ column
** We can view triggers definition graphically using MS management studio:
head over to the Triggers folder and right-click on the trigger name and choose ‘Script Trigger as -> CREATE TO -> New Query Edit Window’
** SQL system views are like virtual tables in the database, helping to reach the information that cannot be reached otherwise
|
|
** We can also use the OBJECT_DEFINITION() function and pass it the id of the trigger
|
|
** the last option we can use to use the ‘sp_helptext’ procedure, which uses a parameter called ‘objname’
|
|
but this option is not the most common one, it’s rarely used
Triggers best practice
- Make sure your database is well-documented
- keep your trigger logic simple
- avoid overusing triggers
Time to practice
Creating a report of existing triggers on the database
|
|
using OBJECT_DEFINITION
|
|
Use cases for AFTER triggers (DML)
A common use for AFTER triggers is to store historical data in other tables (Having a history of changes performed on a table).
Best practice:
Keep an overview of the changes for the most important tables in your database
For example, let’s say the customer on the Customers table changes his phone number, so we keep this change as well as the old phone number on the ‘CustomersHistory’ table
The previous table is obtained using an AFTER Trigger
|
|
Table auditing using triggers
- Another major use of AFTER triggers is to audit changes occurring in the database
Auditing means: Tracking any changes that occur within the defined scope
usually, the scope of the audit is comprised of very important tables from the database
In the following query, we create a trigger called ‘OrderAudit’ that keeps track of any changes that occur to the ‘Orders Table’ it will fire for any DML statements,
inside the trigger we’ve two Boolean variables that will check the special tables “inserted” and “deleted” when one of the special tables contains data, the associated variables will be set to “true”
the combination of variables will tell us if the operation is an INSERT, UPDATE, or DELETE
these changes will be kept inside ‘TablesAudit’ Table
|
|
- Another use case is ‘Notifying users’:
which means we can send notifications to different users using triggers
most of the notifications will be about events happening in the database
In this query, the Sales department must be notified when new orders are placed
The stored procedure will be executed when an INSERT query happens
|
|
time to practice
Copy Customer changes to the History table
|
|
Keep track of any modifications made to the contents of Orders
|
|
Uses cases for INSTEAD OF Trigger (DML)
- Preventing certain operations from happening
- Enforcing data integrity
- Control the database statements
An example of a trigger that prevents and notifies admin
Let’s say we don’t want the regular users to make certain operations on the database tables [like updating or deleting] and when they make so, we send the admin a notification
|
|
Triggers with conditional logic
Triggers are not just limited to the prevention of operations, we can also use it to decide whether or not some of the operations should succeed
here we prevent inserting any new orders when there is not sufficient stock of the product
|
|
time for practice
|
|
Use cases for DDL Triggers
As you know DDL Triggers can be created at different levels (Database level, Server level)
[1] Database Level
A trigger created at the database level can respond to statements related to tables, view interactions, and index management, as well as more specific statements to do with permissions management or statistics
CREATE_TABLE, ALTER_TABLE, DROP_TABLE
CREATE_VIEW, ALTER_VIEW, DROP_VIEW
CREATE_INDEX, ALTER_INDEX, DROP_INDEx
[2] Server Level
at the server level, the trigger used can respond to database management and controlling server permissions and the use of credentials
CREATE_DATEBASE, ALTER_DATABASe, DROP_DATEBASE
GRANT_SERVER, DENY_SERVER, REVOKE_SERVER
CREATE_CREDENTIAL, ALTER_CREDENTIAL, DROP_CREDENTIAL
check the online documentation for the full list of DDL
Databasse auditing
we can keep track of the changes at the database level
|
|