Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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>
share|improve this answer
add comment

Sounds like you just want a basic AFTER INSERT trigger. Then you can access the inserted data via :new. Something like:


CREATE OR REPLACE TRIGGER insert_data
AFTER INSERT
ON OWNER.TABLE
FOR EACH ROW
BEGIN
INSERT INTO OWNER.OTHER_TABLE
(COL1,
COL2,
COL3)
VALUES
(:NEW.COL1,
:NEW,COL2,
:NEW,COL3)
END;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.