Computer Science

Delete Trigger SQL

A delete trigger SQL is a type of SQL statement that is executed automatically when a record is deleted from a database table. It is used to perform additional actions or checks before the deletion is completed. This helps to ensure data integrity and prevent accidental or unauthorized deletions.

Written by Perlego with AI-assistance

6 Key excerpts on "Delete Trigger SQL"

  • Book cover image for: SQL: 1999
    eBook - PDF

    SQL: 1999

    Understanding Relational Language Components

    • Jim Melton, Alan R. Simon(Authors)
    • 2001(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    As you learned in section 10.5.2, Referential Constraint Actions, referential actions are invoked whenever the referential integrity constraint with which they are defined is violated by the actions of some SQL statement. The referential actions that you can define can take action only when row values are modified or when rows are deleted, and the actions that can be taken are limited. The refer- encing rows can be modified in the same way as the rows they reference, they can be deleted, or the columns containing the referencing values can be set to null or to their default values. In particular, no referential actions can be specified to address the insertion of rows into a referencing or referenced table. Triggers Triggers, on the other hand, are considerably more flexible in the events that cause them to take action and in the actions that they are allowed to take. 11.3 Triggers 397 As we will show you shortly, you can define triggers that are invoked (or fired, if you prefer) whenever you insert one or more rows into a specified table, update one or more rows in a specified table, or delete one or more rows from a specified table. These triggers can, generally speaking, take any sort of action you find appropriate for your applications. They can be fired once per INSERT,UPDATE, or DELETE statement or once per row being inserted, updated, or deleted. The table with which the trigger is defined is called the subject table of the trigger, and the SQL statement that causes a trigger to be fired is called the triggering SQL statement. When a trigger is fired, it causes another SQL statement, called the triggered SQL statement, to be executed. Triggers can be fired even before the actions of the triggering SQL statement.
  • Book cover image for: Mastering SQL Server 2008
    • Michael Lee, Gentry Bieker(Authors)
    • 2009(Publication Date)
    • Sybex
      (Publisher)
    Check constraints allow you to use an expression that returns a Boolean result to determine if the row should be allowed in a table. In situations where evaluation of a row takes multiple steps, or requires queries to other tables, triggers can be very useful. Use Foreign Key constraints when possible to enforce referential integrity. A Foreign Key constraint can only create relationships based on equality comparisons, so for more complex relationships, a trigger may be required. When evaluating the use of constraints or triggers, always use constraints if they can meet the functional needs of the application. A trigger should be considered if custom error mes-sages are required in response to error conditions, or in situations where more complex logic is required than constraints can provide. U NDERSTANDING I NSERTED AND D ELETED T ABLES When a DML trigger is executing, it has access to two memory-resident tables that allow access to the data that was modified: Inserted and Deleted. These tables are available only within the body of a trigger for read-only access, and they are automatically created and managed by SQL Server. The structures of the inserted and deleted tables are the same as the structure of the table on which the trigger is defined. 178 | CHAPTER 6 MANAGING DATA INTEGRITY For insert operations, all inserted rows are available in the inserted table. For delete opera-tions, all deleted rows are available in the deleted table. An update can be thought of as a delete followed by an insert. Both the inserted and deleted tables are available within an UPDATE trigger. The deleted table stores the records before modifi-cation (old values), and the inserted table stores records after they were updated (new values). H OW T RIGGERS E XECUTE Triggers execute in response to data modifications. When a modification occurs, the following steps occur: 1. The statement is executed ( INSERT , UPDATE , or DELETE ).
  • Book cover image for: Beginning Microsoft SQL Server 2012 Programming
    • Paul Atkinson, Robert Vieira(Authors)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    so many SQL Server people I meet are) with the distorted notion that triggers are evil and should never be used. Neither will you side with at the other end of the spectrum, who think that triggers are the solution to all the world’s problems. The right answer in this respect is that triggers can do a lot for you, but they can also cause a lot of problems. The trick is to use them when they are the right things to use, and not to use them when they aren’t.
    Some common uses of triggers include:
    • Enforcing referential integrity: Although I recommend using Declarative Referential Integrity (DRI) whenever possible, there are many things that DRI won’t do (for example, referential integrity across databases or even servers, many complex types of relationships, and so on).
    • Creating audit trails: This means writing out records that keep track of not just the most current data, but also the actual change history for each record. This may eventually become less popular with the change-data tracking that SQL Server 2008 added, but triggers are still a pretty popular choice.
    • Creating functionality similar to a CHECK constraint: Unlike CHECK constraints, this can work across tables, databases, or even servers.
    • Substituting your own statements in the place of a user’s action statement: This is typically used to enable inserts in complex views.
    In addition, you have the new, but rarer case of the Data Definition Language (DDL) trigger, which is about monitoring changes in the structure of your table. And these are just a few. So, with no further ado, it’s time to look at exactly what a trigger is.

    WHAT IS A TRIGGER?

    A trigger is a special kind of stored procedure that fires in response to specific events. There are two kinds of triggers: Data Definition Language (DDL) triggers and Data Manipulation Language (DML) triggers.
    DDL triggers fire in response to someone changing the structure of your database in some way (CREATE , ALTER , DROP
  • Book cover image for: Professional Microsoft SQL Server 2008 Programming
    • Robert Vieira(Author)
    • 2010(Publication Date)
    • Wrox
      (Publisher)
    And these are just a few. So, with no further ado, let's look at exactly what a trigger is. What Is a Trigger?
    A trigger is a special kind of stored procedure that responds to specific events. There are two kinds of triggers: Data Definition Language (DDL) triggers and Data Manipulation Language (DML) triggers.
    DDL triggers fire in response to someone changing the structure of your database in some way (CREATE , ALTER , DROP , and similar statements). These were first added back in SQL Server 2005 and are critical to some installations (particularly high-security installations) but are pretty narrow in use. In general, you will need to look into using these only where you need extreme auditing of changes/history of your database structure. We will save these until last.
    DML triggers are pieces of code that you attach to a particular table or view. Unlike sprocs, where you needed to explicitly invoke the code, the code in triggers is automatically run whenever the event(s) you attached the trigger to occurs in the table. Indeed, you can't explicitly invoke triggers—the only way to do this is by performing the required action in the table that they are assigned to.
    Beyond not being able to explicitly invoke a trigger, you'll find two other things that exist for sprocs but are missing from triggers: parameters and return codes.
    While triggers take no parameters, they do have a mechanism for figuring out what records they are supposed to act on (we'll investigate this further later in the chapter). And, while you can use the RETURN keyword, you cannot return a specific return code (because you didn't explicitly call the trigger, what would you return a return code to?).
    What events can you attach triggers to? The three “action” query types you use in SQL. So, you wind up with triggers based in inserts, updates, and/or deletes (you can mix and match to what events you want the trigger to be attached).
  • Book cover image for: How to Cheat at Securing SQL Server 2005
    • Mark Horninger(Author)
    • 2011(Publication Date)
    • Syngress
      (Publisher)
    Chapter 7

    DDL Triggers

    Solutions in this chapter:
     DDL Triggers Explained
     Implementing DDL Triggers
     Managing DDL Triggers
     Scenarios for Deploying DDL Triggers
     Summary
     Solutions Fast Track
     Frequently Asked Questions

    Introduction

    This chapter introduces DDL triggers. It explains what they are, demonstrates how to implement them, and shows you when to implement them. It also provides real-world examples of how and when DDL triggers can be used to help secure an SQL Server.

    DDL Triggers Explained

    Data Definition Language (DDL) is the subset of T-SQL instructions and statements that define structure, whether that structure is objects like tables and views, schemas, or security principals like server logins and database users. Although triggers have been a part of the SQL Server product, they only applied to certain Data Manipulation Language (DML) instructions, including INSERT, UPDATE, and DELETE. Until this latest version of SQL Server, there was no capability to fire a trigger on a DDL statement (such as DROP TABLE).
    Since SQL Server 2000, triggers can fire either before a statement executes, intercepting that statement, or afterward. Triggers that fire before a statement executes are called INSTEAD OF triggers, because they execute “instead of” the statement itself. Triggers that fire after the statement executes (but before the transaction or batch process completes) are called AFTER triggers. AFTER triggers were available prior to SQL Server 2000, and as a result, represent the default trigger behavior. DDL triggers can only be defined as AFTER
  • Book cover image for: The Best Damn Exchange, SQL and IIS Book Period
    • Henrik Walther, Mark Horninger, Chris Adams(Authors)
    • 2011(Publication Date)
    • Syngress
      (Publisher)
    Chapter 25

    DDL Triggers

    Solutions in this chapter:
     DDL Triggers Explained
     Implementing DDL Triggers
     Managing DDL Triggers
     Scenarios for Deploying DDL Triggers
     Summary
     Solutions Fast Track
     Frequently Asked Questions

    Introduction

    This chapter introduces DDL triggers. It explains what they are, demonstrates how to implement them, and shows you when to implement them. It also provides real-world examples of how and when DDL triggers can be used to help secure an SQL Server.

    DDL Triggers Explained

    Data Definition Language (DDL) is the subset of T-SQL instructions and statements that define structure, whether that structure is objects like tables and views, schemas, or security principals like server logins and database users. Although triggers have been a part of the SQL Server product, they only applied to certain Data Manipulation Language (DML) instructions, including INSERT , UPDATE , and DELETE . Until this latest version of SQL Server, there was no capability to fire a trigger on a DDL statement (such as DROP TABLE ).
    Since SQL Server 2000, triggers can fire either before a statement executes, intercepting that statement, or afterwards. Triggers that fire before a statement executes are called INSTEAD OF triggers, because they execute “instead of” the statement itself. Triggers that fire after the statement executes (but before the transaction or batch process completes) are called AFTER triggers. AFTER triggers were available prior to SQL Server 2000, and as a result, represent the default trigger behavior. DDL triggers can only be defined as AFTER
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.