How do I use SQL triggers to back up a database?

Source: Internet
Author: User
Tags insert key backup
sql| Triggers | backup | Database First, you need to set up a test data table, one for inserting data: Test3, and the other as a backup: Test3_bak

The following is an example of a reference fragment:

CREATE TABLE test3 (ID int primary key NOT NULL
Identity (1,1), uname varchar (), uage int);
CREATE TABLE Test3_bak (ID int primary KEY not
Null identity (1,1), bid int,uname varchar (20),
Uage int,active char (1));

The second step is to write a backup trigger, which is only triggered when it is updated or inserted.
The following is an example of a reference fragment:
Alter TRIGGER Test3_bak_insert_update
On Test3
For Insert,update
As
DECLARE @id int
DECLARE @uname varchar (20)
DECLARE @uage int
Begin
Select @id =id, @uname =uname, @uage =uage from inserted
If @id <>0
Begin
Update Test3_bak set active= ' 0 ' where bid= @id
Insert into Test3_bak (bid,uname,uage,active)
VALUES (@id, @uname, @uage, ' 1 ')
End
End

The third step is to test the data:
The following is an example of a quoted fragment:
Insert into TEST3 (uname,uage) VALUES (' FLB ', 20)
Insert into TEST3 (uname,uage) VALUES (' FLB1 ', 21)
Insert into TEST3 (uname,uage) VALUES (' FLB2 ', 22)
Update test3 set uage=100 where id=27
Delete from test3 where id=20

Finally, you can use the following query to trace data changes in two tables:
The following is an example of a reference fragment:
SELECT * FROM Test3
SELECT * FROM Test3_bak

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.