当前位置:网站首页>How to set MySQL triggers is a simple tutorial for novices

How to set MySQL triggers is a simple tutorial for novices

2022-06-26 06:30:00 faint trace of something

What is a trigger ?

Triggers are database objects related to tables , Triggered when the defined conditions are met , And execute the set of statements defined in the trigger . This feature of trigger can help to ensure the integrity of data in database .

The trigger is divided into three events :INSERT( newly added )、DELETE( Delete )、UPDATE( to update ).

mysql The trigger of is equivalent to a simple stored procedure , Personally, I think it's better not to use this kind of , It would be better to write it in the program , Unless it is a large project that must be used , This kind of thing is not easy to check and migrate , But it's fast , Very convenient also ( It's contradictory. Ha ha ).


MYSQL How to set the trigger , Simple tutorial novices will

mysql

Today we will briefly talk about data synchronization between different tables in the same database INSERT New event

The following is a brief introduction. For example, a table is added with content, and another table is also generated synchronously

For example, there are two watches , The main table is qw_single, Secondary table is synchronous table qw_single_copy

We use Nacicat This software , useful , First type a database qw_single, Design table , Click to trigger function


MYSQL How to set the trigger , Simple tutorial novices will

mysql trigger


MYSQL How to set the trigger , Simple tutorial novices will

mysql trigger

name : Trigger name

Trigger :After( Let's talk about after and before The difference between :after It is to add, delete and modify data first , Trigger again , The triggered statement is later than the monitored add / delete operation , Can not affect the previous addition, deletion and modification actions ; That is to say, insert the order record first , Update the quantity of goods ;before It's to complete the trigger first , Add, delete and modify , The triggered statement precedes the monitored addition, deletion and modification , We have a chance to judge , Modify what's going to happen .)

function : With insertion , modify , Delete , We choose to insert

Then write in the following definition

begin        insert into qw_single_copy (title, pid,seotitle,seokeywords,seodescription,content,logo,url,o)          values( new.title, new.pid, new.seotitle, new.seokeywords,new.seodescription,new.content,new.logo,new.url,new.o);  end

Table fields in brackets , Isn't that easy , In this way, you only need to write data in the main table , The secondary table will be synchronized soon , This kind of writing is not commonly used by me anyway , This method is usually used Synchronization between different databases , Data table synchronization between different servers We'll talk about this next time .

原网站

版权声明
本文为[faint trace of something]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260624278340.html