SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. Types of Triggers in DBMS

Text copied!

« Previous
Next »

Types of Triggers in DBMS

July 11, 2023, 12:06 a.m. under SQL

  • Hi! You should check ACID properties in DBMS post first.
    Types of Triggers in DBMS :

    Trigger are special type of stored procedure that automatically executes in response to certain events or actions occurring in the database. These events could include data manipulation operations such as INSERT, UPDATE or DELETE statements.

    Triggers enforce rules, keep data accurate and ensure consistency in databases. Triggers can run before or after an event (BEFORE or AFTER triggers) and can operate on each affected row (ROW-level triggers) or on the entire action (STATEMENT-level triggers).

    In SQL Server, there are two main types of triggers in DBMS :
    After Triggers (AFTER INSERT, UPDATE, DELETE) :

    These triggers execute after the triggering event has occurred and the data modifications have been made to the database. They are commonly used for auditing purposes, logging changes, or performing additional actions after the main operation.

    Instead Of Triggers (INSTEAD OF INSERT, UPDATE, DELETE) :

    These triggers execute instead of the triggering action (INSERT, UPDATE, DELETE). They allow you to override the default behavior of the data modification operation. Instead Of triggers are often used to enforce complex business rules or to implement custom logic before the actual data modification occurs.

    Triggers are created using the CREATE TRIGGER statement and are associated with a specific table or view. They can be defined to execute for INSERT, UPDATE, or DELETE operations, either before or after the event.

    In SQL Server, below is the general syntax for creating triggers in DBMS :
    CREATE TRIGGER trigger_name
    ON table_name
    {AFTER | INSTEAD OF} {INSERT, UPDATE, DELETE}
    AS
    BEGIN
    
    {sql_statements}
    
    END
            

    • trigger_name : This is the name you give to the trigger. It must be unique within the database and follow the rules for naming identifiers in SQL Server.

    • table_name : This is the name of the table or view on which the trigger operates.

    • {AFTER | INSTEAD OF} : This specifies whether the trigger should execute after the triggering action (AFTER) or instead of the triggering action (INSTEAD OF).

    • {INSERT, UPDATE, DELETE} : This specifies the type of data modification operation that will trigger the execution of the trigger.

    • AS : This keyword indicates the beginning of the trigger's body.

    • {sql_statements} : These are the SQL statements that constitute the body of the trigger. These statements can be any valid SQL statements, including data manipulation, procedural logic, or function calls.

    Here's a basic example of creating a simple triggers in DBMS in SQL Server :

    1. Let's consider a table named '[Product]' containing the following values :

    Types of Triggers in DBMS

    2. Let's establish a trigger on the Product table so that upon any insertion operation, it will automatically execute and update the modified date field for all affected records within the same Product table. Execute below query :

    CREATE TRIGGER Product_trgAfterInsert
    ON Product
    AFTER INSERT
    AS
    BEGIN
    
    UPDATE [dbo].[Product]
    	SET [ModifiedDate] = GETDATE()
    
        -- Trigger logic here
        PRINT 'Modified date is updated into product table.';
    
    END;
            
    Types of Triggers in DBMS

    3. Now, let's execute the following SQL statement to insert a row into the Product table. Populating three fields while leaving the modified date field empty. Run this SQL statement :

    INSERT INTO [dbo].[Product] (
    	    [Product_Id], [Name], [GroupName] )
            VALUES 
    		(2, 'Soap', 'Group-B');
            
    Types of Triggers in DBMS

    4. If you observed, the created trigger was automatically executed immediately after we inserted a row into the Product table. In the trigger, we've specified the `GETDATE()` function, indicating that the modified date should be updated with the current date automatically. Now, let's examine the Product table.

    Types of Triggers in DBMS

    5. As evident from the successful insertion, the modified date was also updated by the trigger. This concludes our overview of "types of triggers in DBMS" in SQL Server and their functionality in DBMS.

    Thank you for exploring "types of triggers in DBMS".

    Frequently Asked Questions :

    What are the two types of triggers in DBMS in SQL Server?

    The two types of triggers in DBMS in SQL Server are After Triggers (AFTER INSERT, UPDATE, DELETE) and Instead Of Triggers (INSTEAD OF INSERT, UPDATE, DELETE). After Triggers execute after data modifications, often used for auditing, logging, or additional actions. Instead Of Triggers intervene before data modifications, enabling custom processing or validation.

    How many types of triggers are there in PL SQL?

    There are two types of triggers in PL/SQL: Before Triggers and After Triggers. Before Triggers execute before data modifications, while After Triggers execute after data modifications.

    How many types of triggers are present in DDL?

    DDL contains multiple types of triggers. In DDL, the types of triggers present are categorized by their specific actions and functions. So, how many types of triggers are present in DDL? There are multiple types of triggers categorized by their specific actions and functions.

    How many types of triggers are present in DML?

    There are two types of triggers present in DML operations: Before Triggers and After Triggers.
    Thank You! You should check What is SQL post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy