MySQL trigger creation trigger
Triggers play a very important role in the database system development process, such as preventing harmful data from being entered into the database. You can change or cancel the execution of insert, update, and delete statements and monitor changes in data in the database in a session.
Then we have previously introduced several articles about the application of MySQL views "The application of MySQL views - Creating views" "The application of MySQL views - Modifying views》 and "Application View of MySQL View", then our article will mainly introduce MySQL triggers~
If the user intends to implement a certain action in the database through a trigger To listen, you should first create a trigger, which is created under the "Command Prompt".
Technical Points
The format of a MySQL database creation trigger is as follows:
create trigger <触发器名称> { before | after} {insert | update | delete} on <表名> for each row <触发器SQL语句>
create trigger
{ before | after}: Used to specify whether to trigger before the insert, update or delete statement is executed or after the statement is executed.
on
For each row: The execution interval of the trigger, for each row notifies the trigger to perform an action every other row, rather than once for the entire table.
Implementation process
(1) Create the data table tb_test under the "Command Prompt". The code is as follows:
create table tb_test(t_id varchar(20),t_name varchar(20))
(2) Convert the newline mark to "//". The code is as follows:
delimiter //
(3) Create a trigger to make the content of field t_name "mrsoft" no matter what data the user adds to table tb_test. The code is as follows:
create trigger test_tri before insert on tb_test for each row set new.t_name='mrsoft'
(4) Add a record to table tb_test and view the added results. The code is as follows:
insert into tb_test(t_id,t_name) values('mr0001', '123')// select * from tb_test
Then we enter the above implementation process step by step in the "Command Prompt", and the output result is as follows:
About We have just introduced the creation of MySQL triggers here. Isn’t it very simple? I believe everyone can master it quickly. Then our next article will continue to introduce MySQL triggers. For details, please read "MySQL Triggers" View trigger》!
【Recommended related tutorials】
1.【MYSQL online free video tutorial】
2. Recommended related video courses : "Power node mysql basic video tutorial"
The above is the detailed content of MySQL trigger creation trigger. For more information, please follow other related articles on the PHP Chinese website!