Thursday, July 16, 2009

Solution for-ORA-00001:unique constraint (string.string) violated

SOLUTION:
This Error will Throw when you violate the unique constraint on the table .because if you created the unique constraint for the table it wont allow you to insert/update the duplicate values into the table.

the following example explains how this error happen.

Employee
------------------------------------------------------------------
NAME AGE DEPT EMP_NO
------------------------------------------------------------------
PIETER 25 SALES 10
JOHN 30 ACCOUNTS 11
RITCHIE 30 SALES 12

The above Employee table contains 3 records and if i create the unique constraint for this Employee table on EMP_NO column or if the unique constraint is already created .......

SYNTAX FOR CREATE UNIQUE CONSTRAINT:

ALTER TABLE table_name add CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);

FOR THE ABOVE EXAMPLE CONSTRAINT IS :

ALTER TABLE Employee add CONSTRAINT EmpNo_unique UNIQUE (EMP_NO);

Note:

Here EmpNo_unique is the constraint name

in this Scenario if we insert/update the duplicate values in to the table

For Insert:Insert into Employee(NAME,AGE ,DEPT, EMP_NO)values('JOLIE',25,'SALES',10);

For Update:Update Employee set EMP_NO=12 where EMP_NO=11

For the above two Scenario it will throw the Error as

ORA-00001:unique constraint (Db_Name.EmpNo_unique ) violated

So if you want to overcome with this Error in 2 ways.
1.don't insert/update the duplicate the values in to the table
2.Else Drop the unique constraint

Syntax for Drop the unique Constraint:

ALTER TABLE table_name drop CONSTRAINT constraint_name;

Example:

ALTER TABLE Employee drop CONSTRAINT EmpNo_unique;


Thanks
A.T.J





No comments:

Post a Comment