Solved

Why is it saying "unique constraint violated"?

Posted on 2009-05-19
5
774 Views
Last Modified: 2013-12-07
I am copying a table from one database to another i.e. ACCOUNTING.EXPENSES to FINANCING.EXPENSES
I want to maintain the primary keys (column 'ID') so I have my triggers off. However when I proceed to copy my tables it kept saying 'unique constraint violated' on the primary keys.

I don't know what's wrong with it. Pls help. Thanks.
INSERT INTO FINANCING.EXPENSES (ID, EMPLOYEE_ID, AMOUNT, DATE_ENTERED, DESCRIPTION, ACTUAL_INVOICE_RATE)
SELECT ID, EMPLOYEEID, AMOUNT, DATE, COMMENTS, RATE
FROM ACCOUNTING.EXPENSES 
WHERE EMPLOYEEID = 12345;

Open in new window

0
Comment
Question by:Corinne_09
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24426626
as the error message says: the primary key constraint is violated by the existing + inserted data.
means that either you copy duplicated key values, OR at least 1 row of the inserted data has a primary key value that is already in the destination table.

to solve, you need to either use the MERGE statement, or put a LEFT JOIN + WHERE to discard the rows that are already present, and eventually add a UPDATE statement (best before the insert) to do the updates, if needed (=> or as said, use the MERGE statement instead):
http://www.psoug.org/reference/OLD/merge.html
0
 

Author Comment

by:Corinne_09
ID: 24426658
The reason why I'm asking this question is because FINANCING.EXPENSES is an empty table, so there is no way the PK constraint is being violated if I'm just inserting new values. Also, both FINANCING.EXPENSES and ACCOUNTING.EXPENSES have a PK constraint enabled, so I don't understand why there would be duplicated keys in ACCOUNTING.EXPENSES in the first place.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24426702
>so there is no way the PK constraint is being violated if I'm just inserting new values.
yes, if the inserted data itself contains duplicate values
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 24432588
or there are triggers on the inserted table doing some  other inserts to other tables which creates problem... did you check FINANCING.EXPENSES? maybe there are some active insert triggers...

or run this query to see whether the query returns duplicate data
SELECT   ID, COUNT (*) rec_count
    FROM accounting.expenses
   -- WHERE employeeid = 12345 -- remove comment to see all duplicate data
GROUP BY ID
  HAVING COUNT (*) > 1;

Open in new window

0
 

Author Closing Comment

by:Corinne_09
ID: 31583240
All the primary keys are unique and all triggers were disabled. There was an issue with case-insensitive data so any difference in lowercase and uppercase values were treated differently, thus causing duplicate values.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 34 99
su - oracle could not open session 6 77
Help on model clause 5 31
ORA-02288: invalid OPEN mode 2 31
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

777 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