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