New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using database-level triggers

From: SQL Server: Triggers, Stored Procedures, and Functions

Video: Using database-level triggers

The triggers we've seen thus far are all at the table level, meaning they react to an INSERT, UPDATE, or DELETE statement issued to one table. SQL Server also supports triggers at the database level. These trigger fire whenever a statement is issued that would change the structure of that database. I have an example staged for you in your exercise files. This trigger we're saying is on the database on line 2, so that means it applies to the entire database.

Using database-level triggers

The triggers we've seen thus far are all at the table level, meaning they react to an INSERT, UPDATE, or DELETE statement issued to one table. SQL Server also supports triggers at the database level. These trigger fire whenever a statement is issued that would change the structure of that database. I have an example staged for you in your exercise files. This trigger we're saying is on the database on line 2, so that means it applies to the entire database.

Line 3 says it is for any attempt to drop a table or any attempt to alter a table. The body of the trigger is very simple. It's going to PRINT a message say you do not have permission to drop or alter to the table and then is going to ROLLBACK. In other word it's going to undo whatever someone was attempting to do. So I'll execute this, and now we can find these triggers under the Programmability menu. We have a folder called Database Triggers. If I refresh that, I see the new trigger I just created.

So let's go ahead and test this trigger. I'm going to attempt to drop a table and the triggers should stop that from happening. It did in fact say you do not have permission to drop or alter tables. It also said the batch has bit aborted. Meaning it completely blocked me from happening. And if I do a refresh, I see clearly the Authors table is still there. So good job trigger! You blocked me from possibly making a mistake. I do implement this type of trigger and production databases often, because in my opinion no one should be changing my production databases.

You can make the triggers slightly more robust and check permissions. So check who is trying to do drop and then possibly allow or do not based on that. But this is a nice way to stop some mistakes from happening or possibly even stop some malicious users from doing bad things to your database.

Show transcript

This video is part of

Image for SQL Server: Triggers, Stored Procedures, and Functions
 
Expand all | Collapse all
  1. 2m 15s
    1. Welcome
      51s
    2. What you should know
      51s
    3. Using the exercise files
      33s
  2. 11m 1s
    1. Comparing triggers, functions, and procedures
      3m 25s
    2. Why use a stored procedure?
      4m 59s
    3. Why use functions?
      1m 27s
    4. Why use triggers?
      1m 10s
  3. 6m 2s
    1. Configuring your environment
      4m 53s
    2. Downloading and installing a sample database
      1m 9s
  4. 26m 25s
    1. Creating a stored procedure
      2m 46s
    2. Modifying a stored procedure
      2m 34s
    3. Returning data using data sets
      3m 45s
    4. Returning data using cursors
      3m 45s
    5. Using input and output parameters
      5m 24s
    6. Using security and permissions
      5m 24s
    7. Using transactions
      2m 47s
  5. 11m 56s
    1. Creating a user-defined function
      4m 59s
    2. Exploring single-value functions
      4m 18s
    3. Exploring table value functions
      2m 39s
  6. 9m 31s
    1. Using "after" triggers
      3m 47s
    2. Using "instead of" triggers
      2m 9s
    3. Using nested triggers
      1m 38s
    4. Using database-level triggers
      1m 57s
  7. 12m 43s
    1. Exploring a real-world INSERT procedure
      5m 32s
    2. Exploring a real-world UPDATE procedure
      3m 13s
    3. Implementing logging on DELETE
      3m 58s
  8. 19m 38s
    1. Understanding the Common Language Runtime (CLR) and the .NET framework
      1m 52s
    2. Using CLR with SQL Server 2012
      4m 11s
    3. Writing stored procedures with C# .NET
      5m 51s
    4. Writing functions with .NET
      5m 7s
    5. Choosing between T-SQL vs. CLR
      2m 37s
  9. 11m 34s
    1. Creating a basic web form and connecting to a database
      2m 56s
    2. Executing a stored procedure
      2m 4s
    3. Passing parameters
      3m 41s
    4. Getting return values
      2m 53s
  10. 1m 43s
    1. Next steps
      1m 43s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.