1000 Lines MySQL study notes (eight)

Source: Internet
Author: User

/ * Trigger */------------------

a trigger is a named database object that is related to a table, and when a specific event occurs, the object is activated for monitoring: Record additions, modifications, and deletions.

--Create a trigger

CREATE TRIGGER trigger_name trigger_time trigger_event on Tbl_name for each ROW trigger_ stmt
Parameters:

Trigger_time
is the action time of the triggering program. It can be before or after to indicate that the trigger was triggered before or after the statement that activated it.
Trigger_event
indicates the type of statement that activated the trigger
Insert : Activates the trigger when inserting a new row into a table
UPDATE: Activating a trigger when a row is changed
Delete: Activates the trigger when a row is deleted from the table
tbl_name: A table that listens, must be a persistent table, and cannot associate a trigger with a temporary table or view.
TRIGGER_STMT
: The statement executed when the trigger is activated. To execute multiple statements, you can use the BEGIN ... End Compound statement structure

--Delete

DROP TRIGGER [schema_name.] The trigger_name can use old and new to replace older and newer data updates, which are pre-existing and updated with new. Delete operation, only old. Add operation, only new.

--Note
1. There can be no two triggers for a given table with the same trigger action time and event.

--Character join function

concat (str1[, str2,...])

--Branch statements

if condition Then execute statement elseif condition then execute statement
Else EXECUTE statement
End If;

--Modify the outermost statement Terminator
delimiter custom end symbol SQL statement custom end symbol delimiter; --Change back to the original semicolon

--statement block wrapping

begin Statement block
End

--Special execution

1. The program is triggered whenever a record is added.
2. insert into on duplicate key update syntax triggers: if No duplicates are recorded, before insertis triggered, after Insert; If there is duplicate record and update, it will trigger before insert, before update, after update; Trigger before insert, before update If there are duplicate records but no updates have occurred
3. Replace Syntax If there is a record, execute before insert, before delete, after delete, After insert

(Shocker Source: http://www.cnblogs.com/shockerli/p/1000-plus-line-mysql-notes.html)

1000 Lines MySQL study notes (eight)

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.