ORACLE trigger insert, update, delete, data synchronization, two-Table Synchronization

Source: Internet
Author: User

Table creation:

Create Table user_info (
Id integer not null,
Username varchar (30) not null,
Password varchar (20) not null,
Createdate date not null,
Status integer not null,
Constraint pk_user_info primary key (ID)
);

Create Table user_info_temp (
Id integer not null,
Username varchar (30) not null,
Password varchar (20) not null,
Createdate date not null,
Status integer not null,
Constraint pk_user_info_temp primary key (ID)
);

Trigger Syntax:

Create or replace trigger usertotemp after insert or update or delete
On user_info for each row
Declare
Integrity_error exception;
Errno integer;
Errmsg char (200 );
Dummy integer;
Found Boolean;

Begin
If inserting then
Insert into user_info_temp (ID, username, password, createdate, status) values (: New. ID,: New. username,: New. password,: New. createdate,: New. status );
Elsif updating then
Update user_info_temp Set ID =: New. ID, username =: New. username, password =: New. Password, status =: New. status where id =: Old. ID;
Elsif deleting then
Delete from user_info_temp where id =: Old. ID;
End if;
Exception
When integrity_error then
Raise_application_error (errno, errmsg );
End;

Test data:
Insert into user_info (ID, username, password, createdate, status) values (1, 'xier ', '20170101', to_date ('2017-10-11 ', 'yyyy-mm-dd'), 1)

Update user_info U set U. Status = 3, U. Username = 'xier 'where u. ID = 1

Delete from user_info u where u. ID = 1

Related Article

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.