ShowTriggers\G******************************1.row****************** ************ &"/> ShowTriggers\G******************************1.row****************** ************ &">

Home  >  Article  >  Database  >  What happens to the trigger when we delete the table that has that trigger?

What happens to the trigger when we delete the table that has that trigger?

WBOY
WBOYforward
2023-09-01 16:41:10911browse

What happens to the trigger when we delete the table that has that trigger?

#If we destroy the table associated with it, the trigger will also be destroyed. This is called explicit destruction of the trigger. It can be understood through an example -

Example

Suppose we have a trigger named "trigger_before_delete_sample" on a table named "Sample". Now, if we delete the table, then the above triggers associated with the table will also be deleted.

mysql> Show Triggers\G
*************************** 1. row ***************************
              Trigger: trigger_before_delete_sample
                Event: DELETE
                Table: sample
            Statement: BEGIN
         SET @count = if (@count IS NULL, 1, (@count+1));
         INSERT INTO sample_rowaffected values (@count);
END
               Timing: BEFORE
              Created: 2017-11-21 12:31:58.70
             sql_mode: ONLY_FULL_GROUP_BY,
                       STRICT_TRANS_TABLES,
                       NO_ZERO_IN_DATE,
                       NO_ZERO_DATE,
                       ERROR_FOR_DIVISION_BY_ZERO,
                       NO_AUTO_CREATE_USER,
                       NO_ENGINE_SUBSTITUTION
              Definer: root@localhost
 character_set_client: cp850
 collation_connection: cp850_general_ci
   Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
              Trigger: before_inser_studentage
                Event: INSERT
                Table: student_age
            Statement: IF NEW.age < 0 THEN SET NEW.age = 0;
END IF
               Timing: BEFORE
              Created: 2017-11-21 11:26:15.34
             sql_mode: ONLY_FULL_GROUP_BY,
                       STRICT_TRANS_TABLES,
                       NO_ZERO_IN_DATE,
                       NO_ZERO_DATE,
                       ERROR_FOR_DIVISION_BY_ZERO,
                       NO_AUTO_CREATE_USER,
                       NO_ENGINE_SUBSTITUTION
              Definer: root@localhost
 character_set_client: cp850
 collation_connection: cp850_general_ci
   Database Collation: latin1_swedish_ci
2 rows in set (0.01 sec)

The above result set shows that there are two triggers in the database, one of which is associated with the table named "Sample". Now, with the help of the following statement, we will delete the table -

mysql> DROP table sample;
Query OK, 0 rows affected (0.26 sec)

Now with the help of the following statement, we can see that the triggers associated with the table named "Sample" are also destroyed.

mysql> Show Triggers\G
*************************** 1. row ***************************
              Trigger: before_inser_studentage
                Event: INSERT
                Table: student_age
            Statement: IF NEW.age < 0 THEN SET NEW.age = 0;
END IF
               Timing: BEFORE
              Created: 2017-11-21 11:26:15.34
             sql_mode: ONLY_FULL_GROUP_BY,
                       STRICT_TRANS_TABLES,
                       NO_ZERO_IN_DATE,
                       NO_ZERO_DATE,
                       ERROR_FOR_DIVISION_BY_ZERO,
                       NO_AUTO_CREATE_USER,
                       NO_ENGINE_SUBSTITUTION
              Definer: root@localhost
 character_set_client: cp850
 collation_connection: cp850_general_ci
   Database Collation: latin1_swedish_ci
1 row in set (0.01 sec)

The above is the detailed content of What happens to the trigger when we delete the table that has that trigger?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete