Use bitsCN.com for MySql _ database triggers
Use of triggers in the MySql database:
The trigger uses the SQL statement before or after insert/delete/update.
When creating a trigger, you must add the delimiter separator to write the trigger in the middle of // and end.
Create a user1 table
Create table user1 (
Id int PRIMARY key AUTO_INCREMENT,
Username varchar (20 ),
Createtime date );
Create another user_log table
Create table user_log (
Id int PRIMARY key AUTO_INCREMENT,
Ext int );
Create a trigger (the trigger executed after insert)
Delimiter //
Create trigger trg_user_insert after insert on user1
For EACH row
Begin
Insert into user_log (ext) values (NEW. id); new. id indicates to pass the id in user1 table to ext in user_log table.
End;
//
Test: the trigger is automatically executed after the data in the user1 table is inserted.
Insert into user1 values (null, 'hangsan11', now ());
Delete the trigger
Drop trigger trg_user_insert;
New and old in the trigger:
There are new virtual tables in insert, old virtual tables in delete, and new and old virtual tables in update.
Before in the trigger:
It is often used in update to modify if... then... end if
I have learned so much about it. if you have any questions, please reply at 16:58:23 ,.
BitsCN.com