当我们删除具有该触发器的表时,该触发器会发生什么?

如果我们要销毁与其关联的表,则触发器也将被销毁。这称为显式销毁触发器。可以借助示例来理解-

示例

假设我们在名为“ Sample”的表上有一个名为“ trigger_before_delete_sample”的触发器。现在,如果我们将删除该表,则与该表关联的上述触发器也将被删除。

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)

上面的结果集显示数据库中有两个触发器,其中有一个与名为“ Sample”的表相关联的触发器。现在借助以下语句,我们将删除表-

mysql> DROP table sample;

现在,借助以下语句,我们可以看到与名为“ Sample”的表相关联的触发器也被销毁了。

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)

以上是 当我们删除具有该触发器的表时,该触发器会发生什么? 的全部内容, 来源链接: utcz.com/z/354302.html

回到顶部