Mysql (10) trigger 1 bitsCN.com 
Mysql (10) trigger 1
 
 
 
Related links:
 
Mysql (1) installation of mysql
 
Http: // database/201210/162314 .html;
 
Mysql-related operations (2)
 
Http: // database/201210/162315 .html;
 
Mysql operations on data tables (3)
 
Http: // database/201210/162316 .html;
 
Mysql (4) Data Table query operations
 
Http: // database/201210/162317 .html;
 
Mysql operations (5)
 
Http: // database/201210/162318 .html;
 
Mysql (6) string pattern matching
 
Http: // database/201210/163969 .html;
 
Mysql (7) in-depth select query
 
Http: // database/201210/163970 .html;
 
Mysql Index (8)
 
Http: // database/201210/163971 .html;
 
Mysql common functions
 
Http: // database/201210/164229 .html
 
 
 
Mysql 5.0.2 and later versions support the trigger function.
 
What is a trigger? A trigger is a database object related to a table. it is triggered when the defined conditions are met and the statements defined in the trigger are executed.
 
Let's take a look at the syntax structure of the trigger:
 
 
 
SQL code
 
Create trigger trigger_name trigger_time trigger_event
 
On table_name
 
For each row
 
Begin
 
Trigger_stmt
 
End;
 
 
 
Trigger_name indicates the trigger name;
 
Trigger_time indicates the trigger time, after, before;
 
Trigger_event indicates the trigger event, such as delete, update, and insert;
 
Trigger_stmt is the thing statement that describes the trigger to execute, that is, what you want to do. it is written here.
 
 
 
Example:
 
Create a database:
 
 
 
SQL code
 
Create database db_test;
 
Create a film table:
 
SQL code
 
Create table film (
 
Id smallint unsigned not null,
 
Name varchar (40 ),
 
Txt text,
 
Primary key (id)
 
);
 
 
 
Here is a simple trigger example:
 
 
 
When inserting data into the film table, the business rule also inserts a data entry into the log table film_text.
 
 
 
Of course, you have to create a film_text table:
 
 
 
SQL code
 
Create table film_text (
 
Id smallint unsigned not null auto_increment,
 
Name varchar (40 ),
 
Txt text,
 
Primary key (id)
 
);
 
 
 
Now you can write the trigger according to the syntax structure of the trigger.
 
Trigger code:
 
 
 
SQL code
 
Create trigger trigger_film -- trigger_film indicates the trigger name.
 
After -- after indicates the time when the trigger will occur.
 
Insert -- insert is the condition insert operation when a trigger occurs.
 
On film -- create the table name of the trigger
 
For each row -- indicates that the trigger is a row-level trigger.
 
Begin
 
-- Trigger execution logic
 
Insert into film_text (id, name, txt) values(new.id,new.name,new.txt );
 
End;
 
 
 
After this trigger is run, after data is inserted in another film table, a data entry is added to film_text.
 
 
BitsCN.com