MySQL trigger, mysql trigger instance

Source: Internet
Author: User

MySQL trigger, mysql trigger instance

MySQL versions 5.0.2 and later support triggers. A trigger has some time with commands to trigger certain operations.Events include insert statements, delete statements, and update statements..Triggers can be used to record database operations.

1. Create a mysql trigger:

(1) create a trigger with a single execution statement

Create trigger name before | after trigger event

On Table name for each row execution statement

 

Before | after: Specifies the trigger execution time.

Foreach row: indicates that the trigger event is triggered when the operation on any record is met.

 

Example:

Create a table:

Create table timelog (

Id int (11) primary key auto_increment,

Savetime varchar (50) not null,

Saveinfo varchar (50) not null

);

Create a saveTimeTrigger trigger:

Delimiter //

Create trigger saveTimeTrigger before insert

On studentinfo for each row

Insert into timelog (savetime) values (now ());

//

 

Before you insert data to the studentinfo table, the database automatically inserts the current operation time to timelog.

More: http://hovertree.com/menu/mysql/

(2) create a trigger with multiple execution statements

Create trigger name before | after trigger event

On Table name for each row

Begin

List of executed statements

End

For example:

Delimiter //

Create trigger saveTimeTrigger before insert

On studentinfo for each row

Begin

Insert into timelog (savetime) values (now ());

Insert into timelog (saveinfo) values ('insert ');

End

//

 

2. view trigger commands

Showtriggers;

 

Select * from information_schema.triggers where TRIGGER_NAME = 'trigger name ';

Information_schema.triggers: a data table used to record trigger information in the database;

TRIGGER_NAME: used to specify the name of the trigger to be viewed

 

3. delete a trigger

Droptrigger trigger name;

 

Recommended: http://www.cnblogs.com/roucheng/p/mysqlhanshu.html

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.