MySql _ usage of database triggers _ MySQL

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.