MySQL Development basic series 19 triggers

Source: Internet
Author: User

A trigger is a database object that is related to a table, and a trigger can only be a permanent table created and not a temporary table.

1.1 Creating a Trigger

-- Syntax: CREATE TRIGGER trigger_name trigger_time trigger_event  on  for Each ROW trigger_stmt

Trigger_time: Is the trigger time, either before or after, before is triggered before the constraint is checked, and after is the check constraint.
Trigger_event: A trigger event for a trigger, which can be insert,update,delete.
The same trigger event for the same table with the same trigger time, only one trigger can be defined. Use the alias old and new to refer to the record content that has changed in the trigger.

--   View City_memory data First (view table data before triggering)SELECT* from City_memory;

--insert trigger to create City table using after trigger timeDELIMITER $$CREATE TRIGGERtri_city AfterINSERT  onCity forEach ROWBEGININSERT  intocity_memory (country_id, CityName, Citycode)VALUES(new.country_id, New.cityname, new.     Citycode); END; $ $DELIMITER;--Insert City table data, trigger City table Insert triggerINSERT  intoCity (country_id, CityName, Citycode)VALUES(2,'China Trigger','001 Triggering');
-- View SELECT * from City_memory again;

Here is the city table

2. View triggers

-- use Information_schema.triggers to view SELECT *  from WHERE = ' tri_city '

3. Delete a Trigger

DROP TRIGGER tri_city;

Summary: Limitations of Triggers:

(1) The triggering program cannot call the stored program that returns the data to the client.

(2) statements that begin or end a transaction explicitly or implicitly cannot be used in a trigger.

Writing overly complex triggers or adding too many triggers to a record's insert, UPDATE, and delete operations will definitely have a more serious impact. Do not rely too much on triggers to handle the processing logic of your app.

MySQL Development basic series 19 triggers

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.