Unique constraint violated oracle ошибка

Error message looks like this

Error message => ORA-00001: unique constraint (schema.unique_constraint_name) violated

ORA-00001 occurs when: «a query tries to insert a «duplicate» row in a table». It makes an unique constraint to fail, consequently query fails and row is NOT added to the table.»

Solution:

Find all columns used in unique_constraint, for instance column a, column b, column c, column d collectively creates unique_constraint and then find the record from source data which is duplicate, using following queries:

-- to find <<owner of the table>> and <<name of the table>> for unique_constraint

select *
from DBA_CONSTRAINTS
where CONSTRAINT_NAME = '<unique_constraint_name>';

Then use Justin Cave’s query (pasted below) to find all columns used in unique_constraint:

  SELECT column_name, position
  FROM all_cons_columns
  WHERE constraint_name = <<name of constraint from the error message>>
   AND owner           = <<owner of the table>>
   AND table_name      = <<name of the table>>

    -- to find duplicates

    select column a, column b, column c, column d
    from table
    group by column a, column b, column c, column d
    having count (<any one column used in constraint > ) > 1;

you can either delete that duplicate record from your source data (which was a select query in my particular case, as I experienced it with «Insert into select») or modify to make it unique or change the constraint.

There’re only two types of DML statement, INSERT and UPDATE, may throw the error, ORA-00001: unique constraint violated. ORA-00001 means that there’s a constraint preventing you to have the duplicate value combination. Most likely, it’s an unique constraint. That’s why your INSERT or UPDATE statement failed to work.

Let’s see some cases.

1. INSERT

We inserted into a row that violate the primary key.

SQL> insert into employees (employee_id, last_name, email, hire_date, job_id) values (100, 'Chen', 'EDCHEN', to_date('17-JAN-22', 'DD-MON-RR'), 'AC_MGR');
insert into employees (employee_id, last_name, email, hire_date, job_id) values (100, 'Chen', 'EDCHEN', to_date('17-JAN-22', 'DD-MON-RR'), 'AC_MGR')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.EMP_EMP_ID_PK) violated

In the error message, it told us that we specifically violate HR.EMP_EMP_ID_PK.

Please note that, not all primary keys are unique, it’s allowable to have non-unique primary keys.

2. UPDATE

We updated a row that violate an unique index.

SQL> update employees set email = 'JCHEN' where employee_id = 100;
update employees set email = 'JCHEN' where employee_id = 100
*
ERROR at line 1:
ORA-00001: unique constraint (HR.EMP_EMAIL_UK) violated

In the error message, it told us that we specifically violate HR.EMP_EMAIL_UK.

Solution

To solve ORA-00001, we should use a different value to perform INSERT INTO or UPDATE SET statement. The solution may sound easy to say, but hard to do, because we may not know what columns we violated.

Check Constraint Columns

So let’s see how we check unique columns.

SQL> column table_name format a20;
SQL> column column_name format a20;
SQL> select table_name, column_name, position from all_cons_columns where owner = 'HR' and constraint_name = 'EMP_EMAIL_UK';

TABLE_NAME           COLUMN_NAME            POSITION
-------------------- -------------------- ----------
EMPLOYEES            EMAIL                         1

The above query tells us that the column combination in the output is violated. To comply with the unique constraint, you can almost do nothing except for checking the existing row.

Drop Unique Constraint to Prevent ORA-00001

An alternative solution is to drop the unique index if it’s not necessary anymore. Dropping a primary or unique index needs more skills, otherwise you might see ORA-02429.

In a multithread environment, you may check whether the row is existing or not, then do your INSERT in order to prevent ORA-00001.

declare
  v_row_counts number;
begin
  select count(*) into v_row_counts from employees where employee_id = 100;
  if v_row_counts = 0 then
    -- Insert the row
  else
    -- Do not insert the row
  end if;
end;
/

In the above block of code, if the row count is 0, then we can do INSERT right after counting, elsewhere don’t do it.

totn Oracle Error Messages


Learn the cause and how to resolve the ORA-00001 error message in Oracle.

Description

When you encounter an ORA-00001 error, the following error message will appear:

  • ORA-00001: unique constraint (constraint_name) violated

Cause

You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index.

Resolution

The option(s) to resolve this Oracle error are:

Option #1

Drop the unique constraint.

Option #2

Change the constraint to allow duplicate values.

Option #3

Modify your SQL so that a duplicate value is not created.

Note

If you are not sure which unique constraint was violated, you can run the following SQL:

SELECT DISTINCT table_name
FROM all_indexes
WHERE index_name = 'CONSTRAINT_NAME';

Oracle PLSQL

In our example (see picture above), our constraint name would be SYS_C002459 and we would execute the following SQL:

SELECT DISTINCT table_name
FROM all_indexes
WHERE index_name = 'SYS_C002459';

This would return the name of the table whose unique constraint we violated.

Понравилась статья? Поделить с друзьями:
  • Uniform 01 division 2 ошибка
  • Uniel rs 1 500 ошибка
  • Uniel rs 1 1500 ошибка
  • Unicum кофейный автомат ошибка закрытие группы
  • Unicum rosso ошибки