如果我們銷毀與其關(guān)聯(lián)的表,觸發(fā)器也會(huì)被銷毀。這稱為觸發(fā)器的顯式銷毀。可以通過一個(gè)例子來理解 –
示例
假設(shè)我們?cè)诿麨椤癝ample”的表上有一個(gè)名為“trigger_before_delete_sample”的觸發(fā)器。現(xiàn)在,如果我們刪除該表,那么與該表關(guān)聯(lián)的上述觸發(fā)器也將被刪除。
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)
登錄后復(fù)制
上面的結(jié)果集顯示數(shù)據(jù)庫中有兩個(gè)觸發(fā)器,其中一個(gè)觸發(fā)器與名為“Sample”的表關(guān)聯(lián)。現(xiàn)在,借助以下語句,我們將刪除該表 –
mysql> DROP table sample; Query OK, 0 rows affected (0.26 sec)
登錄后復(fù)制
現(xiàn)在借助以下語句,我們可以看到與名為“Sample”的表關(guān)聯(lián)的觸發(fā)器也被銷毀了。
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)
登錄后復(fù)制
以上就是當(dāng)我們刪除具有該觸發(fā)器的表時(shí),觸發(fā)器會(huì)發(fā)生什么情況?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!