Oracle trigger (when A table A is inserted or deleted or modified, the new data can be synchronized to another table B with the same structure)

Source: Internet
Author: User

Create or replace trigger testT
AFTER INSERT OR UPDATE OR DELETE ON
For each row
Declare
-- Local variables here
Begin
IF INSERTING THEN
Insert into B (a, B) VALUES (: NEW. a,: NEW. B );
ELSIF DELETING THEN
Delete from B WHERE a =: OLD.;
ELSE
UPDATE B SET B =: NEW. B WHERE a =: OLD.;
End if;
End testT;

SQL> desc
Name Type Nullable Default Comments
---------------------------------------
A varchar2 (20) y
B varchar2 (20) y

SQL> desc B;
Name type nullable default comments
---------------------------------------
A varchar2 (20) y
B varchar2 (20) y

SQL> insert into a values ('A', 'B ');

1 row inserted

SQL> commit;

Commit complete

SQL> select * from B;

A B
----------------------------------------
A B

SQL> Update a set B = 'C' where a = 'a ';

1 row updated

SQL> commit;

Commit complete

SQL> select * from B;

A B
----------------------------------------
A c

SQL> delete from a where a = 'a ';

1 row deleted

SQL> commit;

Commit complete

SQL> select * from B;

A B
----------------------------------------

SQL>

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.