MySQL database supports the function of triggers. Triggers are related to the objects of table operations. A trigger fires when certain conditions are met and executes the SQL statement operations defined in the trigger. There are also triggers that can only be created on real tables, not temporary tables; the triggering events of triggers are before or after. The following uses a specific example to illustrate that the trigger is triggered after inserting data, as shown in the following figure:
##1. The first step is to create the database table t_class_info, Use the create table statement:
create table t_class_info( id int(8), t_no int(10), t_name varchar(20), t_addr varchar(60) );
as shown below:
2. The second step, View the table t_class_info description and use the command:
desc t_class_info;
as shown below:
3. In the third step, create the trigger info_tri. The trigger time is after inserting data into the table t_class_info. The trigger event is to insert a record into the table class_info, as shown in the following figure:
4. The fourth step is to create the database table class_info and use the command:
create table class_info( id int(8), t_no int(10), t_name varchar(20), t_addr varchar(60) );
As shown below:
5. The fifth step is to view the class_info database table structure and use the command:
desc class_info;
6. Step 6. Check the database tables t_class_info and class_info data and find that there is no data in the two tables. At this time Call the insert statement, insert a record into the database table t_class_info, and check whether there are new records in the database table class_info, as shown in the following figure:
##Instructions
Understand the triggering time and conditions of MySQL triggers
Proficient use MySQL trigger
The above is the detailed content of How to use MySQL database to trigger trigger after inserting data. For more information, please follow other related articles on the PHP Chinese website!