Syntax for creating a MySQL trigger

Source: Internet
Author: User
MySQL triggers are an important part of the MySQL database. The following describes how to create and delete MySQL triggers in detail.

MySQL triggers are an important part of the MySQL database. The following describes how to create and delete MySQL triggers in detail.

MySQL triggers are an important part of the MySQL database. The following describes how to create and delete MySQL triggers in detail.

1. Create a Mysql trigger:

Syntax:

Create trigger trigger_name trigger_time trigger_event ON tbl_name
FOR EACH ROW
BEGIN
Trigger_stmt
END;

Create trigger trigger_name trigger_time trigger_event ON tbl_name for each row begin trigger_stmt END; uppercase keywords

Trigger_name: Mysql trigger name. My frequently used naming rules are t_name_tableName _ (B | a) (I | u | d), t: trigger ID, name: English name, tableName: table Name,

B (BEFORE): BEFORE the event is triggered,

A (AFTER): AFTER an event is triggered,

I (insert): identifies an insert event,

U (update): identifies an update event,

D (delete): identifies a delete event;

Trigger_time: trigger time (BEFORE or AFTER)

Trigger_event: event name (insert, update, or delete)

Tbl_name: Table Name (must be a permanent table)

Trigger_stmt: Execute the statement (which can be a compound language name). Use the OLD and NEW aliases to reference columns in the table related to the trigger program.

2. Delete the Mysql producer

Syntax:

Drop trigger [schema_name.] trigger_name;

Note: The above operations require the SUPER permission.

Create and delete MySQL triggers
The following article describes how to create a MySQL trigger and how to delete a MySQL trigger. If you create a MySQL trigger correctly, if you are interested in deleting the MySQL trigger, you can click the following article to view it.

1. Create a MySQL trigger:

Syntax:
Create trigger trigger_name trigger_time trigger_event ON tbl_name
FOR EACH ROW
BEGIN
Trigger_stmt
END;
Create trigger trigger_name trigger_time trigger_event ON tbl_name
FOR EACH ROW
BEGIN
Trigger_stmt
END;

Example


Create trigger SetUserHome after insert ON users
FOR EACH ROW
BEGIN
Update 'users' set homeLocationX = 128,
HomeLocationY = 128, homeLocationZ = 30
Where uuid = NEW. uuid
END

The preceding example is incorrect. Updating the table when it is triggered will bring the program into an endless loop.

For more details, please continue to read the highlights on the next page:


Related reading:

High-performance data creation using MySQL triggers

Use of MySQL triggers

MySQL trigger Management

MySQL trigger to process data in this table

MySQL trigger settings in Linux

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.