Database Fields use database triggers to fill in related knowledge points

Source: Internet
Author: User

There are two special logical tables in the trigger:
Inserted and deleted

These two tables are automatically generated by the system.
Inserted records newly added or modified content.
The content recorded in deleted is the content before deletion or modification.

 

For example, see the following table:
A
ID, name
1 aa
2 bb

1. Add
Execute: insert into a (ID, name) values (3, 'cc ')
In this case
The content of the inserted Table is:
3 cc
The content of the deleted table is empty.

2. Modify
Run: update a set name = Name + '1' where ID <3
In this case
The content of the inserted Table is:
1 aa1
2 bb1
The content of the deleted table is empty.
1 aa
2 bb

3. Delete
Run: delete from a where id = 2
In this case
The content of the inserted Table is null.
The content of the deleted table is empty.
2 bb

 

Therefore, your trigger can be written as follows:
Method 1
Create trigger AA on your table
For insert, delete, update
If exists (select * From inserted)
If exists (select * From deleted)
...... -- Record the operation as a modified processing statement
Else
...... -- Record the operation as a new processing statement
Else
...... -- Record operation as a delete statement


Method 2. Separate write
Create trigger AA on your table
For insert
As
...... -- Record the operation as a new processing statement

Create trigger AA on your table
For update
As
...... -- Record the operation as a modified processing statement

Create trigger AA on your table
For Delete
As
...... -- Record the operation as a delete statement

 

Example:

Note:
1. The trigger runs after being inserted. If a primary key conflict occurs before, the trigger cannot be reached.
2. Automatic + 1 rules. When multiple users perform operations, they cannot ensure that the generated keywords are not repeated. What should I do when there are duplicate keywords?
3. The instead of insert trigger can avoid problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.