|
|
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 operationNext:200 books such as Java e-books and interview questions
|