MySQL trigger simple example grammar CREATE TRIGGER <trigger name> -- Triggers must have a name, up to 64 characters, which may be followed by separators. It is named much like other objects in MySQL. { BEFORE | AFTER } -- The trigger has an execution time setting: it can be set before or after the event occurs. { INSERT | UPDATE | DELETE } -- You can also set the events that trigger: they can be triggered during the execution of insert, update or delete. ON <table name> --The trigger belongs to a certain table: when an insert, update or delete operation is performed on this table, the trigger is activated. We cannot assign two triggers to the same event for the same table. FOR EACH ROW -- trigger execution interval: FOR EACH ROW clause tells the trigger to perform the action every other row, rather than once for the entire table. < Trigger SQL statement > --The trigger contains the SQL statement to be triggered: the statement here can be any legal statement, including compound statements, but the restrictions on the statements here are the same as those of functions. --You must have considerable privileges to create a trigger (CREATE TRIGGER), if you are already the root user, then that is enough. This is different from the SQL standard. Examples example1: Create table tab1 DROP TABLE IF EXISTS tab1; CREATE TABLE tab1( tab1_id varchar(11) ); Create table tab2 DROP TABLE IF EXISTS tab2; CREATE TABLE tab2( tab2_id varchar(11) ); Create a trigger: t_afterinsert_on_tab1 Function: After adding records in the tab1 table, the records will be automatically added to the tab2 table DROP TRIGGER IF EXISTS t_afterinsert_on_tab1; CREATE TRIGGER t_afterinsert_on_tab1 AFTER INSERT ON tab1 FOR EACH ROW BEGIN insert into tab2(tab2_id) values(new.tab1_id); END; Test it out INSERT INTO tab1(tab1_id) values('0001'); See the results SELECT * FROM tab1; SELECT * FROM tab2; example2: Create trigger: t_afterdelete_on_tab1 Function: After deleting the records in the tab1 table, the corresponding records in the tab2 table will be automatically deleted. DROP TRIGGER IF EXISTS t_afterdelete_on_tab1; CREATE TRIGGER t_afterdelete_on_tab1 AFTER DELETE ON tab1 FOR EACH ROW BEGIN delete from tab2 where tab2_id=old.tab1_id; END; Test it out DELETE FROM tab1 WHERE tab1_id='0001'; See the results SELECT * FROM tab1; SELECT * FROM tab2; Thank you for reading, I hope it can help you, thank you for your support of this site! You may also be interested in:
|
<<: How to deploy DoNetCore to Alibaba Cloud with Nginx
>>: Detailed explanation of Nodejs array queue and forEach application
1. What is As a markup language, CSS has a relati...
1. Query process show processlist 2. Query the co...
After entering the Docker container, if you exit ...
environment Linux 3.10.0-693.el7.x86_64 Docker ve...
This article example shares the specific code of ...
Forwarding between two different servers Enable p...
1. Get the mysql image docker pull mysql:5.6 Note...
Table of contents 0. What is Webpack 1. Use of We...
Table of contents 1. Introduction 2. Composition ...
Introduction to AOP The main function of AOP (Asp...
Table of contents Preface Confusing undefined and...
As shown below: LOCATE(substr,str) Returns the fi...
To achieve an effect similar to Windows forms, dr...
How to solve VMware workstation virtual machine c...
A colleague reported that a MySQL instance could ...