From the course: Complete Guide to Advanced SQL Server
Automate SQL Server with triggers - SQL Server Tutorial
From the course: Complete Guide to Advanced SQL Server
Automate SQL Server with triggers
Welcome to Section Two, in which we'll explore the wonderful world of database triggers. This section builds on what we've just learned about programmability objects in SQL Server, such as stored procedures and functions. But triggers add an automation component that allows the database to perform operations all on its own. There are several benefits to adding triggers to your database. Projects like stored procedures, triggers contain server commands and other instructions that can automatically enforce business logic and help validate data. But unlike stored procedures, triggers fire whenever specific tables, database or server level events occur, which means that they'll execute exactly when they're needed without needing to be monitored and kicked off by application code or direct administrative actions. Adding triggers to your database is a great way to ensure that specific activities are performed accurately every time an actionable event occurs on your system. Now, before we dive into triggers, we need to perform a quick bit of housekeeping. I want to create a new empty database to keep things organized. In Management Studio, I'll click on the New Query button and we'll create a new database called KinetEcoTRG. That'll get added over here in the Object Explorer window. Let me just refresh my database folder, and there it is right there. All of the scripts for Section Two will create tables in this new KinetEcoTRG database. At the end of most scripts in this section are a few lines that will reset this database by dropping the objects that are built during that movie. If you're playing around with the examples and things go awry, you can always scroll to the bottom of the script, drop the objects, and then start at the top again to recreate them.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
Automate SQL Server with triggers1m 48s
-
(Locked)
What are SQL Server triggers?3m 37s
-
(Locked)
Two triggers types: INSTEAD OF and AFTER2m 34s
-
(Locked)
Create a trigger to print a message6m 17s
-
Timestamp a table modification5m 31s
-
(Locked)
Firing INSTEAD OF triggers3m 23s
-
(Locked)
Challenge: Create a trigger58s
-
(Locked)
Solution: Create a trigger7m 10s
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-