Home > Database > Mysql Tutorial > MySQL trigger creation trigger

MySQL trigger creation trigger

黄舟
Release: 2017-05-07 16:38:05
Original
2670 people have browsed it

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语句>
Copy after login

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

: used to specify the table name that responds to the trigger.

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.

: The SQL statement to be executed by the trigger. If the trigger wants to execute multiple SQL statements, multiple statements must be placed in the begin...end block.

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))
Copy after login

(2) Convert the newline mark to "//". The code is as follows:

delimiter //
Copy after login

(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=&#39;mrsoft&#39;
Copy after login

(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(&#39;mr0001&#39;, &#39;123&#39;)//
select * from tb_test
Copy after login

Then we enter the above implementation process step by step in the "Command Prompt", and the output result is as follows:

MySQL trigger creation trigger

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template