oracle

Saturday, 13 May 2017

PL/SQL: Backup Trigger.

To make synchronize backup of table, We create a table to store the backup.
Create Table:
create table heros_backup as select * from heros where 1=2;

This will create the table same as heros but does not copy the data of hero table.
Now
Create Backup Trigger:
create or replace trigger heros_bu
before insert or delete or update on heros
for each row
enable
begin
if inserting then
  insert into heros_backup (hero_name) values(:NEW.hero_name);
elsif deleting then
  delete from heros_backup where hero_name=:old.hero_name;
elsif updating then
  update heros_backup set hero_name = :NEW.hero_name where hero_name=:old.hero_name;
end if;

end;

Result:
Trigger HEROS_BU compiled

Now check by inserting data into heros.

insert into heros values('Muhammad Talha Zubair');

To Check:
select * from heros_backup;
Result:
Muhammad Talha Zubair

No comments:

Post a Comment