Trigger to automatically add values to a table in Oracle

oracleoracle-xe

I am new to Oracle.

I am looking for a trigger which helps to automatically insert values into a table when data is entered into another table in the same database.

I have tried a lot and didn't succeed. Can anyone explain how this is done?

Best Answer

This is very simple and there are many tutorials out there.

Here is a sample trigger to demonstrate.

create table aaaa
( 
  a number
);

create table bbbb
(
  b number
);

create trigger aaaa_aitrig
after insert on aaaa
for each row
begin
  insert into bbbb values ( :new.a );
end;
/

In case you were wondering, ":new" is a reference to the newly inserted row, and each column can be referenced individually.

Test case:

SQL> select count(*) from bbbb;

  COUNT(*)
----------
         0

SQL> insert into aaaa values ( 1 );

1 row created.

SQL> select * from bbbb;

         B
----------
         1

SQL>