This article is a mirror article of machine translation, please click here to jump to the original article.

View: 7824|Reply: 1

Triggers for the foundation of the database

[Copy link]
Posted on 3/24/2019 9:20:05 PM | | |



preface

Today I will sort out the use of triggers, although it is generally not recommended to use triggers in projects, but we still need to understand the use of triggers.

Text

Use of triggers. Prepare the table you want to use today. Create a NewTest table and a NewTest_log table



#Triggers

It is a special stored procedure, not called by itself, but automatically touched by performing a certain operation, like an event.
Use: Monitor actions and supplement constraints.
In the introduction, it is mentioned that triggers are generally not recommended for performance and maintenance. In actual projects, it is generally not easy to notice the trigger, which will bring certain difficulties to later maintenance.



All our operations on the table data are temporarily stored in these two tables,"inserted”,“deleted”。 We can use these two temporary tables to record some data, or to constrain and modify the business.

Let's create a trigger for the NewTest table:



After creating this trigger, let's delete the table data "delete from ADB.. NewTest", and then query "select * from  ADB.. NewTestIt was found that the data in this table was not deleted. It is because of the trigger we created that when the table data is deleted, it will trigger the statement of adding new data. We can use triggers to monitor changes in table data and record relevant logs.

Create a trigger for the NewTest table, and once new data is added, we log that data to the relevant log table:



Execute new statements"INSERT [dbo]. [NewTest] ( [Name], [Age], [Gender], [amount]) VALUES ( N'Xiaohuang', 18, N'm', 20)We will be inNewTest_log"The table also generates a record that records the operation time, operation type, and landing person, etc.

So how do we judge in the trigger that the data has been deleted, modified, and added to the operation. I still have to look at the two temporary tables "inserted" and "deleted"

New operation: There is data in the inserted table, but there is no data in the deleted table.
Delete operation: There is no data in the inserted table, and there is data in the deleted table.
Modify the operation: There is data in the inserted table (new data), and there is data in the deleted table (old data).

We can use the data in these two temporary tables to judge the corresponding operations and then execute our corresponding business operations




Epilogue

Cuties, the foundation of the database has come to an end, and the next part will record how to restore the data after mistakenly deleted and modified by mistake.














Previous:Automatic input with a handle cannot be clicked to confirm the operation
Next:200 books such as Java e-books and interview questions
Posted on 3/24/2019 9:33:05 PM |
Passing by @@@@@@@@@@@@@@@@@@@
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com