当前位置:网站首页>Database composition trigger
Database composition trigger
2022-07-26 11:33:00 【Hua Weiyun】
17.1 Create trigger
MySQL You can use CREATE TRIGGER sentence .MySQL Triggers in can contain an execution statement , It can also contain multiple execution statements .
17.1.1 Grammar format
The syntax format for creating triggers is as follows :
CREATE [DEFINER = user] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW [trigger_order] trigger_body The syntax format is described as follows :
·trigger_name: Name of the trigger created .
·trigger_time: Identify when to execute the trigger , Two options are supported , Respectively BEFORE and AFTER. among ,BEFORE Means to trigger before an event ,AFTER It means to trigger after an event .
·trigger_event: Triggered events , Support INSERT、UPDATE and DELETE operation .
·tbl_name: Data table name , Indicates on which data table the trigger is created .
·trigger_body: Executed in trigger SQL sentence , There can be one SQL sentence , It can also be multiple SQL sentence .
17.1.2 Create trigger example
Triggers can be triggered before an event occurs , It can also be triggered after an event occurs .
1. Create a test data table
Create data table test_trigger and test_trigger_log.
mysql> CREATE TABLE test_trigger ( -> id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -> t_note VARCHAR(30) -> ); Query OK, 0 rows affected (0.12 sec) mysql> CREATE TABLE test_trigger_log ( -> id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -> t_log VARCHAR(30) -> ); Query OK, 0 rows affected (0.01 sec) SQL Statement executed successfully , And then to test_trigger Add data table 、 Delete and modify operations , Use triggers to test_trigger The data change log in the data table is written test_trigger_log In the data table .
2.BEFORE INSERT trigger
for example , Create name as before_insert The trigger of , towards test_trigger Data table before inserting data , towards test_trigger_log Insert... Into the data table before_insert Log information .
mysql> DELIMITER $$ mysql> CREATE TRIGGER before_insert -> BEFORE INSERT ON test_trigger -> FOR EACH ROW -> INSERT INTO test_trigger_log -> (t_log) -> VALUES -> ('before_insert') -> ; -> $$ Query OK, 0 rows affected (0.16 sec) mysql> DELIMITER ; SQL Statement executed successfully , towards test_trigger Insert data into the data table .
mysql> INSERT INTO test_trigger (t_note) VALUES (' test BEFORE INSERT trigger '); Query OK, 1 row affected (0.00 sec) SQL Statement executed successfully , see test_trigger Data in the data table .
mysql> SELECT * FROM test_trigger; +----+--------------------------------+ | id | t_note | +----+--------------------------------+ | 1 | test BEFORE INSERT trigger | +----+--------------------------------+ 1 row in set (0.01 sec) towards test_trigger A piece of data was successfully inserted into the data table .
see test_trigger_log Data in the data table .
mysql> SELECT * FROM test_trigger_log; +----+---------------+ | id | t_log | +----+---------------+ | 1 | before_insert | +----+---------------+ 1 row in set (0.00 sec) towards test_trigger Data table before inserting data , Execute trigger to test_trigger_log The data table is inserted before_insert Information .
3. establish AFTER INSERT trigger
for example , Create name as after_insert The trigger of , towards test_trigger After inserting data into the data table , towards test_trigger_log Insert... Into the data table after_insert Log information .
mysql> DELIMITER $$ mysql> CREATE TRIGGER after_insert -> AFTER INSERT ON test_trigger -> FOR EACH ROW -> BEGIN -> INSERT INTO test_trigger_log -> (t_log) -> VALUES -> ('after_insert'); -> END; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; towards test_trigger Insert data into the data table .
mysql> INSERT INTO test_trigger (t_note) VALUES (' test AFTER INSERT trigger '); Query OK, 1 row affected (0.00 sec) SQL Statement executed successfully , see test_trigger Data in the data table .
mysql> SELECT * FROM test_trigger; +----+--------------------------------+ | id | t_note | +----+--------------------------------+ | 1 | test BEFORE INSERT trigger | | 2 | test AFTER INSERT trigger | +----+--------------------------------+ 2 rows in set (0.00 sec) see test_trigger_log Data in the data table .
mysql> SELECT * FROM test_trigger_log; +----+---------------+ | id | t_log | +----+---------------+ | 1 | before_insert | | 2 | before_insert | | 3 | after_insert | +----+---------------+ 3 rows in set (0.00 sec) test_trigger_log A clause is inserted in the data table before_insert Information and a after_insert Information , This is because before_insert Trigger and after_insert trigger , Direction test_trigger When inserting data into a data table , Will execute first before_insert Trigger direction test_trigger_log Insert data into the data table , Re execution after_insert Trigger direction test_trigger_log Insert data into the data table , So they will ask test_trigger_log Insert two pieces of data into the data table .
Be careful :BEFORE/AFTER UPDATE Trigger and BEFORE/AFTER DELETE The creation method of trigger is the same as BEFORE/AFTER INSERT Triggers are created in the same way , No more details here .
17.2 Check triggers
MySQL Supported in SHOW TRIGGERS and SHOW CREATE TRIGGER Statement to view trigger information . meanwhile , stay MySQL Trigger information will be stored in information_schema In the database triggers In the data table , So it can also be in trigger View trigger information in the data table .
17.2.1 Use SHOW TRIGGERS Statement to view trigger information
Use SHOW TRIGGERS Statement to view trigger information , The syntax is as follows :
SHOW TRIGGERS [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr] ·SHOW TRIGGERS: View trigger information SQL keyword .
·[{FROM | IN} db_name]:{FROM | IN} Indicates which database to view triggers from .db_name Represents the database name ; This item can be omitted , When omitted , View the current MySQL Trigger information of the database where the command line is located .
·[LIKE 'pattern' | WHERE expr]: Check the condition statement that matches when the trigger .
for example , View the current MySQL Trigger information under the database where the command line is located .
mysql> SHOW TRIGGERS \G *************************** 1. row *************************** Trigger: before_insert Event: INSERT Table: test_trigger Statement: INSERT INTO test_trigger_log (t_log) VALUES ('before_insert') Timing: BEFORE Created: 2019-12-26 22:30:44.31 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_ DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Definer: [email protected] character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8mb4_0900_ai_ci *************************** 2. row *************************** Trigger: after_insert Event: INSERT Table: test_trigger Statement: BEGIN INSERT INTO test_trigger_log (t_log) VALUES ('after_insert'); END Timing: AFTER Created: 2019-12-26 22:41:57.51 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_ DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Definer: [email protected] character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8mb4_0900_ai_ci ################ Omit here n Line code ########################## 6 rows in set (0.00 sec) The results show that MySQL All trigger information under the current database of the command line , And displays the name of each trigger 、 event 、 The data table where the trigger is located and triggered when the trigger is executed SQL Statement and other information .
17.2.2 Use SHOW CREATE TRIGGER Statement to view trigger information
Use SHOW CREATE TRIGGER Statement to view trigger information , The syntax is as follows :
SHOW CREATE TRIGGER trigger_name among ,trigger_name Indicates the name of the trigger .
for example , See the name before_insert Trigger information .
mysql> SHOW CREATE TRIGGER before_insert \G *************************** 1. row *************************** Trigger: before_insert sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION SQL Original Statement: CREATE DEFINER=`root`@`localhost` TRIGGER `before_insert` BEFORE INSERT ON `test_trigger` FOR EACH ROW INSERT INTO test_trigger_log (t_log) VALUES ('before_insert') character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8mb4_0900_ai_ci Created: 2019-12-26 22:30:44.31 1 row in set (0.44 sec) The name of the trigger is displayed in the view result 、 Triggered when the trigger is executed SQL sentence .
17.2.3 By looking at triggers Data in the data table view trigger information
stay MySQL in , The trigger information will be stored in information_schema In the database triggers In the data table . By looking at information_schema In the database triggers Use the data in the data table to view the trigger information .
The syntax is as follows :
SELECT * FROM information_schema.triggers WHERE condition for example , See the name before_insert Trigger information for .
mysql> SELECT * FROM information_schema.triggers WHERE trigger_name = 'before_insert' \G *************************** 1. row *************************** TRIGGER_CATALOG: def TRIGGER_SCHEMA: goods TRIGGER_NAME: before_insert EVENT_MANIPULATION: INSERT EVENT_OBJECT_CATALOG: def EVENT_OBJECT_SCHEMA: goods EVENT_OBJECT_TABLE: test_trigger ACTION_ORDER: 1 ACTION_CONDITION: NULL ACTION_STATEMENT: INSERT INTO test_trigger_log (t_log) VALUES ('before_insert') ACTION_ORIENTATION: ROW ACTION_TIMING: BEFORE ACTION_REFERENCE_OLD_TABLE: NULL ACTION_REFERENCE_NEW_TABLE: NULL ACTION_REFERENCE_OLD_ROW: OLD ACTION_REFERENCE_NEW_ROW: NEW CREATED: 2019-12-26 22:30:44.31 SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION DEFINER: [email protected] CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_general_ci DATABASE_COLLATION: utf8mb4_0900_ai_ci 1 row in set (0.00 sec) The result shows before_insert The database where the trigger is located 、 Data sheet 、 Trigger Name 、 Event type and triggered when the trigger is executed SQL Statement and other information .
When no condition is specified , Will query all trigger information .
mysql> SELECT * FROM information_schema.triggers \G *************************** 1. row *************************** TRIGGER_CATALOG: def TRIGGER_SCHEMA: sys TRIGGER_NAME: sys_config_insert_set_user EVENT_MANIPULATION: INSERT EVENT_OBJECT_CATALOG: def EVENT_OBJECT_SCHEMA: sys EVENT_OBJECT_TABLE: sys_config ACTION_ORDER: 1 ACTION_CONDITION: NULL ACTION_STATEMENT: BEGIN IF @sys.ignore_sys_config_triggers != true AND NEW.set_by IS NULL THEN SET NEW.set_by = USER(); END IF; END ACTION_ORIENTATION: ROW ACTION_TIMING: BEFORE ACTION_REFERENCE_OLD_TABLE: NULL ACTION_REFERENCE_NEW_TABLE: NULL ACTION_REFERENCE_OLD_ROW: OLD ACTION_REFERENCE_NEW_ROW: NEW CREATED: 2019-11-24 12:46:55.84 SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION DEFINER: [email protected] CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_0900_ai_ci DATABASE_COLLATION: utf8mb4_0900_ai_ci ############# Omit here n Row data ####################### *************************** 8. row *************************** TRIGGER_CATALOG: def TRIGGER_SCHEMA: goods TRIGGER_NAME: after_delete EVENT_MANIPULATION: DELETE EVENT_OBJECT_CATALOG: def EVENT_OBJECT_SCHEMA: goods EVENT_OBJECT_TABLE: test_trigger ACTION_ORDER: 1 ACTION_CONDITION: NULL ACTION_STATEMENT: BEGIN INSERT INTO test_trigger_log (t_log) VALUES ('after_delete') ; END ACTION_ORIENTATION: ROW ACTION_TIMING: AFTER ACTION_REFERENCE_OLD_TABLE: NULL ACTION_REFERENCE_NEW_TABLE: NULL ACTION_REFERENCE_OLD_ROW: OLD ACTION_REFERENCE_NEW_ROW: NEW CREATED: 2019-12-26 23:35:19.34 SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION DEFINER: [email protected] CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_general_ci DATABASE_COLLATION: utf8mb4_0900_ai_ci 8 rows in set (0.00 sec) 17.3 Delete trigger
When confirming in the database that a trigger is no longer in use , You can delete triggers that are no longer used , stay MySQL in , Use DROP TRIGGER Statement delete trigger .
17.3.1 Grammar format
The syntax format of delete trigger is as follows :
DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name The syntax format is described as follows :
·DROP TRIGGER: Delete trigger's SQL keyword .
·schema_name: The name of the database where the trigger is located , It can be omitted . When omitted , Will delete MySQL Trigger under the database where the command line is located .
·trigger_name: Trigger name .
17.3.2 Delete trigger example
for example , Delete the name as after_delete The trigger of .
mysql> DROP TRIGGER after_delete; Query OK, 0 rows affected (0.11 sec) SQL Statement executed successfully , see alter_delete Trigger information .
mysql> SHOW CREATE TRIGGER after_delete \G ERROR 1360 (HY000): Trigger does not exist Results show ,MySQL Report errors , non-existent alter_delete trigger , explain alter_delete The trigger has been successfully deleted .
边栏推荐
- easyui04
- Ten year structure five year life-06 impulse to leave
- How to configure the jdbcrealm data source?
- Creation and modification of basic tables and data in them by SQL statements of SQL Server
- easyui02
- List ascending and descending
- ESP8266-Arduino编程实例-开发环境搭建(基于Arduino IDE)
- Simple use of MySQL database
- Load orb dictionary
- Pyechart离线部署
猜你喜欢
随机推荐
MySQL locking mechanism
Harbor2.2 用户角色权限速查
QT - connect USB camera
QT——LCDNumber
Synchronized与ReentrantLock
剑指 Offer 25. 合并两个排序的链表
easyui05
AuthorizingRealm简介说明
[学习进度]5月
Pyechart离线部署
【云驻共创】为了写好代码,你坚持了哪些好习惯?
[报错]Exception: Found duplicate column(s) in the data schema: `value`;
复现php一句话木马
MySql基础知识汇总
如何配置JdbcRealm数据源呢?
Simple use of MySQL database
X 2 Earn必须依靠旁氏启动?Gamefi的出路在哪?(上)
Classic Bluetooth connection process
为什么放弃npm转向yarn了
702 horsepower breaks through 100 in only 4.5 seconds! The strongest pickup truck comes, safe and comfortable


![[开发工具] IEDA报红](/img/2d/eec1f74c33ff21ae4951eae44b9369.png)

![[idea]如何新建一个项目](/img/33/f210d59ccd3664487f401929dac24c.png)




![[报错]Exception: Found duplicate column(s) in the data schema: `value`;](/img/df/ca676633ca6d5e8c0a870be0732707.png)