MySQL's action on the trigger

Source: Internet
Author: User

1. Why should I use a trigger?

Triggers are somewhat similar to functions and need to be declared and executed. However, the execution of the trigger is not called by the program, nor is it started manually, but is triggered by an event, which is activated to achieve execution.

The activation trigger is triggered when the Delete,insert,update statement is triggered, and the other SQL statements do not activate the trigger.

You can use triggers to process database objects when you are working with some business logic, when the table's data changes and you can automate some processing.

The actions of triggers include creating triggers, viewing triggers, and deleting triggers.

2. Create a Trigger

2.1 Creating a trigger that has an execution statement

CREATE TRIGGER |  on  for Each    ROW trigger_stmt;

Examples are as follows:

CREATE TRIGGER INSERT  on  for INSERT  into VALUES (NULL,'t_dept', Now ());

When you insert any record into the department table t_dept, the current time record is inserted into the table t_diary before the insert operation.

2.2 Creating a trigger with multiple execution statements

CREATE TRIGGER |  on  for Each  BEGINEND;

Examples are as follows:

DELIMITER $$CREATE TRIGGERTri_diary beforeINSERT  onT_dept forEach ROWBEGIN INSERT  intoT_diaryVALUES(NULL,'t_dept', now ());INSERT  intoT_diaryVALUES(NULL,'t_dept', now ());END$ $DELIMITER;

In the example above, the "DELIMITER $$" statement is first set to "$$", and then the execution statement list is written between the keyword begin and end, and finally through the "DELIMITER;" The statement reverts the ending symbol to the default Terminator ";".

3. View triggers

3.1 Viewing triggers through the show triggers statement

Show triggers displays a list of all triggers.

3.2 View the trigger by viewing the system table triggers implementation.

4. Delete a trigger

DROP TRIGGER trigger_name;

MySQL's action on the trigger

Related Article

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.