Create a trigger
Trigger is a special stored procedure, the difference is that the stored procedure needs to be called by call, while the trigger does not need to be called by call, it only needs to be called automatically when the defined event occurs.
1. Create a trigger that executes a single statement
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW trigger_stmt
tigger_time can take values before and after
trigger_event can take values insert, delete, update
trigger_stmt is the target to achieve.
For example, after adding a record in the table tb_index_zone, add a record in the tb_student table
CREATE TRIGGER addStu before insert ON tb_index_zone FOR EACH ROW insert into tb_student values ('10','123456789012','10');
To view triggers use: show triggers \G;
2. Create a trigger with multiple executed statements
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW
BEGIN
multiple sql statements
END
For example, when a record is added to the table tb_index_zone, two records are added to the tb_student table
CREATE TRIGGER addMultStu before insert ON tb_index_zone FOR EACH ROW
BEGIN
insert into tb_student values ('2,'TOM','11');
insert into tb_student values ('3,'TOM2','12');
END
The second way to view the trigger:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE custom condition;
The way to delete the trigger:
DROP TRIGGER [DB].[trigger_name]
For example: DROP TRIGGER test_db.addMultStu;