In SQL triggers, the system automatically generates two temporary tables, deleted and inserted, which are logical (concept) tables. It can also be understood as a temporary table. For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows: the Inserted Table is a new record, and the Delete table is an old record. And their table structure
In SQL triggers, the system automatically generates two temporary tables, deleted and inserted, which are logical (concept) tables. It can also be understood as a temporary table. For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows: the Inserted Table is a new record, and the Delete table is an old record. And their table structure
SQLTriggerThe system automatically generates twoTemporaryTables: deleted and inserted. They are logical (concept) tables. It can also be understoodTemporaryTable.
For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows:An Inserted Table is a new record, and a Delete table is an old record.In addition, their table structure is exactly the same as that of the original table.
1. When you Insert, the record to be inserted is new, so it can be found in the Insert table.
For example, if N records exist in Table 1, but insert into table1 value ('1') is executedTriggerThe Inserted Table has only the same record.
Select * from Inserted, and the result is: 1 instead of N + 1 records of table 1.
2. When you update, the record to be updated is new, so you can find it in the Inserted Table. The updated record is old, so it can be found in the Deleted table.
For example, Change Record 1 to record 2.
The select * from Deleted result is 1 select * from Inserted result is 2
3. When you delete a record, the record to be Deleted is old, so you can find it in the Deleted table.
For example, if the table contains 1, 2, 3, and 4 records and three records are deleted, the result of select * from deleted is 1, 2, 3.
-------------------------
By the way, when you create a tableTriggerThen, we will discuss it in three cases.
1. Insert)
The Inserted Table has data, and the Deleted table has no data.
2. Delete)
The Inserted Table has no data, and the Deleted table has data.
3. Update)
The Inserted Table has data (new data), and the Deleted table has data (old data)