Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MySQL database promotion-triggers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Day06 MySQL database promotion-triggers

I. Overview of triggers:

What is the trigger? What's the use? Can bring us what kind of methods to operate the database this is here to learn one by one to tell you. The trigger principle is similar to the trigger principle, when you click the trigger, there will be a corresponding follow-up action. Let's create and use triggers in detail.

2. Trigger (trigger):

1. Create a trigger

Create trigger trigger name before | after trigger event on table name for each row begin execution statement; end Translation: before | after: indicates the time when the trigger executes. The former indicates the execution before triggering. The latter means that the trigger event is executed after the trigger time: the trigger event represents the three action table names of "insert", "update" and "delete": the name of the table that triggers the time operation specified for each row: indicates that the action on any record satisfies the trigger event will trigger the trigger begin. End: represents the start-to-end label. Execution statement: indicates the program that executes after the trigger is triggered.

Example 1:

Create a trigger create trigger dlt after delete on student for echo row begin delete username from student; end that responds to a delete operation Created trigger name: dlt trigger execution time: after event name: delete operation (delete) table name: student execution statement: delete statement between begin.end.

2. Create multiple triggers that execute statements

Create trigger trigger name before | after trigger event on table name for each row begin execution statement 1; execution statement 2; execution statement 3; end Translation: before | after: indicates the time when the trigger executes. The former indicates the execution before triggering. The latter means that the trigger event is executed after the trigger time: the trigger event represents the three action table names of "insert", "update" and "delete": the name of the table that triggers the time operation specified for each row: indicates that the action on any record satisfies the trigger event will trigger the trigger begin. End: represents the start-to-end label. Execution statement 1: indicates program 1 that executes after the trigger is triggered. Execution statement 2: indicates the program 2 that executes after the trigger is triggered. Execution statement 3: indicates program 3 that executes after the trigger is triggered.

Example 2:

Create multiple triggers create trigger dlt after delete on student for echo row begin insert into timelog (savetime) values (new ()); insert into timeinfo (info) values ('deleteact'); end in response to the delete operation Created trigger name: dlt trigger execution time: after event name: delete operation (delete) table name: student execution statement body: delete statements between begin.end.

3. View triggers

Show trigger

3.1.View trigger information in trigger table

Select * from infomation_schema.trigger; translation: infomation_schema: a database that exists by default in MySQL, a data table used to record trigger information

3.2, method 2 check

Select * from infomation_schema.trigger where trigger_name=' trigger name'

4. Apply trigger

The application of a trigger is to execute the corresponding sql statement in the table separately after you have successfully created it. Use the query to see if the sql statement you executed is successful. Check the order in which the insertion of the timeinfo data table is good and is executed first.

5. Delete trigger

Drop trigger trigger name

III. Concluding remarks:

That's all we've learned about triggers, and we can use triggers to back up data. When you delete a piece of data, I'll create a new table and store it in another table. All of these can be realized by triggers. If you want to operate the trigger well, you will continue to experiment and use it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report