IT tutorials
 
Database
 

SQL Server 2008 R2 : Using DML Triggers (part 1) - Creating DML Triggers

4/9/2013 9:31:25 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

DML triggers are invoked when a DML event occurs in the database. DML events manipulate or modify the data in a table or view. These events include insertions, updates, and deletions.

DML triggers are powerful objects for maintaining database integrity and consistency. They are able to evaluate data before it has been committed to the database. During this evaluation period, these triggers can perform a myriad of actions, including the following:

  • Compare before and after versions of data.

  • Roll back invalid modifications.

  • Read from other tables, including those in other databases.

  • Modify other tables, including those in other databases.

  • Execute local and remote stored procedures.

Based on the nature of these actions, triggers were originally used in many cases to enforce referential integrity. Triggers were used when foreign key columns in one table had to be validated against primary keys or unique index values in another table. The triggers could fire when data was modified, and validations could be performed to ensure that referential integrity was maintained.

The advent of declarative referential integrity (DRI) diminished the need for referential integrity triggers. DRI is now generally implemented with database objects such as foreign key constraints that perform the referential integrity validation internally. Because of this, triggers generally handle more complex integrity concepts and enforce restrictions that cannot be handled through data types, constraints, defaults, or rules. Following are some examples of trigger uses:

  • Maintenance of duplicate and derived data— A denormalized database generally introduces data duplications (that is, redundancy). Instead of exposing this redundancy to end users and programmers, you can keep the data in sync by using triggers. If the derived data is allowed to be out of sync, you might want to consider handling refreshes through batch processing or some other method instead.

  • Complex column constraints— If a column constraint depends on other rows within the same table or rows in other tables, using a trigger is the best method for that column constraint.

  • Complex defaults— You can use a trigger to generate default values based on data in other columns, rows, or tables.

  • Inter-database referential integrity— When related tables are found in two different databases, you can use triggers to ensure referential integrity across the databases.

You can use stored procedures for all these tasks, but the advantage of using triggers is that they fire on all data modifications. Stored procedure code or SQL in application code is executed only when it makes the data modifications. With triggers, all data modifications are subject to the trigger code, except for bulk copy and a few other nonlogged actions. Even if a user utilizes an ad hoc tool, such as SQL Server Management Studio (SSMS) to make changes to the database, the integrity rules cannot be bypassed after the trigger is in place.

Note

Triggers and stored procedures are not mutually exclusive. You can have both triggers and stored procedures that perform modifications and validation on that same table. If desired, you can perform some tasks via triggers and other tasks via stored procedures.


1. Creating DML Triggers

You can create and manage triggers in SQL Server Management Studio or directly via Transact-SQL (T-SQL) statements. The Object Explorer in SSMS provides a simple means of creating triggers that you can use to generate the underlying T-SQL code. You expand the Object Explorer tree to the user table level and then right-click the Triggers node. When you select the New Trigger option, as shown in Figure 1, the trigger template shown in the right pane of Figure 30.1 appears.

Figure 1. Using SSMS to create triggers.

You can populate the trigger template by manually editing it, or you can select the Query menu option Specify Values for Template Parameters. When you select Specify Values for Template Parameters, a screen appears, allowing you to fill in the basic values for the trigger, including the table that the trigger will be on and the events to respond to.

You can launch the New Trigger template and other templates related to triggers via the Template Explorer, which you open by selecting View, Template Explorer in SSMS. Figure 2 shows a partial list of the available templates, including those related to triggers.

Figure 2. The Template Explorer.


All the trigger templates provide a basic framework for you to create a trigger, but the core logic is up to you. Existing triggers or sample triggers are often good alternatives to the templates because they offer more of the core logic. You can right-click a trigger in the Object Explorer and select the Script Trigger As option. This option contains several different methods to script the trigger. After you script a trigger, you can modify it as necessary to meet your needs.

Tip

Using the sys.triggers catalog view is a good way to list all the triggers in a database. To use it, you simply open a new query editor window in SSMS and select all the rows from the view as shown in the following example:

SELECT * FROM sys.triggers


After you have a basic trigger template, you can code the trigger, with limited restrictions. Almost every T-SQL statement you would use in a SQL batch or stored procedure is also available for use in the trigger code. However, you cannot use the following commands in a DML trigger:

  • ALTER DATABASE

  • CREATE DATABASE

  • DISK RESIZE

  • DROP DATABASE

  • LOAD DATABASE and LOAD LOG

  • RECONFIGURE

  • RESTORE DATABASE and RESTORE LOG

The following sections describe the different types of DML triggers that can be coded and some of their common uses.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us