DB trigger to delete duplicate rows

Posted on 2004-11-29
Last Modified: 2013-12-11
I want to write a DB trigger. I dont have any exp with oracle.
I have two tables - profile and product
The issue I have is that for each row in table profile, there are atleast 4 identical rows in table product. I want to clean up table product so that for each profile there is only one product profile.
HOw to do this?
I want to have a DB trigger to check everytime theres an insert on table product...that is to delete duplicate rows.
Thanks in advance
Question by:Vasi04
    LVL 7

    Expert Comment

    Why do you not want to clean up your product table in one operation ?
    Is there a reason you want to use a trigger ?

    Author Comment

    I want to use trigger to avoid future insert of duplicate rows. Is this spossible?
    With Single operation I can get rid of already existing duplicate rows.

    LVL 7

    Accepted Solution

    You should be able to that with a trigger like the following:

    BEFORE INSERT ON product
      DELETE product WHERE <key conditions>

    <key conditions> is the expression that define an "identical" entry where you reference values in the entry to be inserted with new (e.g. new.productId).

    While this works, it is not a good idea, if you have many users working on the database.
    LVL 5

    Expert Comment


    The best way to do this is to create a Primary key on the product profile. If the table already has a product profile, then create a unique index on product profile. This will ensure that duplicates do not occur.


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Introduction A previously published article on Experts Exchange ("Joins in Oracle", makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now