views:

866

answers:

4

Hi, I have a table:

CREATE TABLE MY_TABLE (
  MY_ID NUMBER NOT NULL,
  COLUMN_1 NUMBER,
  COLUMN_2 NUMBER
);
ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID);

at a later point, when executing the following sql, I get an error:

ALTER TABLE MY_TABLE DROP PRIMARY KEY DROP INDEX;
ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID) 

ORA-02437: cannot validate PK_FOO - primary key violated

My table only contains 3 entries all with a different primary key which is also not null. Anyone has an idea what this could be?

Thanks,

Peter

A: 

Are 2 primary keys identical?

This error is usually thrown when you try to create/enable a primary key on a table

Heiko Hatzfeld
As I wrote before... the table contains 3 *different*, *non-null* values for the primary key.
blackicecube
A: 

from here

Cause: You tried to tried to enable a primary key constraint, but the columns in the primary key either contained NULL values or duplicates..

Aziz
As I wrote before... the table contains 3 *different*, *non-null* values for the primary key.
blackicecube
may I ask, what are those primary-key values?
Aziz
Here's my table definition:Column Name Data Type Nullable Column Id PKMY_ID NUMBER No 1 1COLUMN_2 NUMBER Yes 2 COLUMN_3 NUMBER Yes 3
blackicecube
Grrr... formatting does not work in here :-(All columns are defined as number, MY_ID is defined as primary key.
blackicecube
+5  A: 

You must forgive a certain amount of scepticism on our part. Because that error definitely indicates a duplicate value.

What you need to do is use the exceptions clause. This will show you the ROWIDs of the records which are violating your constraint. You may need to create the target table: by default the script creates a table called EXCEPTIONS:

SQL> ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID);
ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID)
                                    *
ERROR at line 1:
ORA-02437: cannot validate (APC.PK_FOO) - primary key violated


SQL> @%ORACLE_HOME%\rdbms\admin\utlexpt1.sql

Table created.

SQL> ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID)
  2  exceptions into exceptions
  3  /
ALTER TABLE MY_TABLE ADD CONSTRAINT PK_FOO PRIMARY KEY (MY_ID)
                                    *
ERROR at line 1:
ORA-02437: cannot validate (APC.PK_FOO) - primary key violated


SQL> select * from exceptions
  2  /

ROW_ID             OWNER TABLE_NAME CONSTRAINT
------             ----- ---------- ----------
AABQXcAAEAAAXUPAAD APC   MY_TABLE   PK_FOO        
AABQXcAAEAAAXUPAAB APC   MY_TABLE   PK_FOO        

SQL>

Edit

You need to figure out what is different between your install code and the simplification you posted here. The chances are you have one or more INSERT statements which are accidentally executed more than once while the constraint is not in force. Adding the EXCEPTIONS INTO clause to your code might help you track it down.

APC
Thanks, I'll tried it... Unfortunately I could not reproduce the problem when simply executing these few sql snippets. The error happens only as part of a big installation procedure where these lines get executed.
blackicecube
+1 ...nice to be reminded of this Oracle feature
dpbradley
A: 

I'm upvoting APC's answer, but since you seem to have some problems implementing it, could you just post the results of this query:

select my_id, count(*) from my_table group by my_id having count(*) >1

This will give us (and you) some idea of the problematic keys.

dpbradley
select my_id, count(*) from my_table group by my_id having count(*) > 1 doesn't return any rows.select my_id, count(*) from my_table group by my_id returns 3 rows with 3 different id's and count = 1.
blackicecube
OK - I see now with the additional comments that this is happening as part of a more involved process - I thought you would have access to the table with the offending rows prior to the constraint being applied.
dpbradley