Skip to main content

Delete Trigger on Oracle Database Tables - Record deleted rows from your database

I have been very busy with work lately and I had put my blogging career on hold, I am back now..

For this article I will try to be very precise two simple steps.

STEP ONE


Have a similar table as the table you are listening in for delete actions. On your new backup table you might want to have more fields like the delete_user, the computer_name where this was done and the time_stamp.
You can easily create the backup table by copying the existing table then adding the extra columns.
There are many ways to do this so i will let you decide on which one you prefer but I would normally quickly do it as follows;

Run the following sql
Create table mytablename_after_delete as select * from mytable;
--then  empty the table
TRUNCATE TABLE mytablename_after_delete;
--the add your extra columns using alter command
alter table mytablename_after_delete add sysdate timestamp; --you can make it varchar if need be
alter table mytablename_after_delete add deleted_by varchar(30);
--add as many columns as you need.

STEP TWO

Once your table is done proceed with setting up the trigger.

CREATE OR REPLACE TRIGGER mytablename_after_delete
AFTER DELETE
   ON mytablename
   FOR EACH ROW
--declare your variables like machine names and user names
DECLARE
   v_username varchar2(10);
--declared username of delete user
BEGIN

   -- Find username of person performing the DELETE on the table
   SELECT user INTO v_username
   FROM dual;

   -- Insert record into backup mytablename_after_delete table
   INSERT INTO mytablename_after_delete
   ( mytablename_fieldnames,
--enter all fields in your original table here the new backup table should have similar fields or more since you now have delete user and time
delete_date,
deleted_by)
   VALUES
   (
--to fetch your deleted fields you must start with semicolon and the keyword old
:old.mytablename_fieldname,
--add all your fields as above
sysdate,
--remember we did not initialize sysdate as this does not require declaration
v_username );

END;


As you can see i made it very brief, in case you have any questions please feel free to ask.
I will be more than delighted to share the little knowledge i have.

Comments

Post a Comment

Popular posts from this blog

Ten output devices, advantages and disadvantages, inter-site back-up mechanism, Expert systems for medical diagnosis,information systems security

(i)Printer Printer enables us to produce information output on paper. It is one of the most popular computer output devices we often use to get information on paper - called hard copy. Advantage They produce high quality paper output for presentation at a speedy rate. It is also possible to share the printer among different users in a network. Disadvantage The cost for maintenance of the printer equipment as well printing ink is cumulatively high. (ii) Plotters These devices are used to produce graphical outputs on paper. They have automated pens that make line drawings on paper Advantage They can produce neat drawings on a piece of paper based on user commands. In Computer Aided Design (CAD) they are used to produce paper prototypes that aid in design of the final system. Disadvantage They are more expensive than printers. Further, the command based interface is difficult to use. (iii)Monitor It is...

Start Wamp server on windows automatically permanently

For those that have completely refused to use linux platforms for development, you might find this useful. As with all (aspiring) web developers, it’s always important to test your projects locally before putting it out there for the entire web community to see. One must-have developer tool for this purpose is WAMPServer. We’ve all wished it’s automatically up and running when we need it. These easy steps will help you automate WAMPServer to run on system start-up. For those unfamiliar with WAMPServer, it is a development package that lets you run web development projects locally. WAMP stands for Windows, Apache, MySQL, PHP/Perl/Python. It’s basically four programs packaged to work as one. WAMP basically turns any Windows PC into a localized web server. The Linux counterpart is called LAMP, obviously. Once WAMPServer is installed in your PC, you’ll be able to test your web projects before putting it into the live environment. But I always found it a hassle to manually s...

simple basic object oriented java code for employee salary calculation

import java.io.*; import java.util.Scanner; public class Employees {     Scanner scan=new Scanner(System.in);     String Fname;   int EmpID;  int DOB; double Allowance;   double Salary;     public void getDetails(){   System.out.println("Enter the first name");   Fname=scan.next();   System.out.println("Enter the ID number");   EmpID=scan.nextInt();   System.out.println("Enter the date of birth");   DOB=scan.nextInt();   System.out.println("Enter the salary");   Salary=scan.nextDouble();   Allowance=0.6*Salary;     }    public void printReport(){    System.out.println(Fname+"\t"+EmpID+"\t"+calGross()+"\t"+calPayee()+"\t"+calNetIncome());    }    public double calGross(){        return Salary + Allowance;    }    public double calPayee(){     ...