🧩 How to Resolve ORA-00001: Unique Constraint Violated in Oracle Database
🧩 Introduction
The ORA-00001: Unique Constraint Violated error is one of the most common issues Oracle DBAs and developers encounter.
It occurs when you try to insert or update a record that would create a duplicate value in a column or group of columns that are protected by a unique constraint or primary key.
For example, if a table has a primary key on the column EMP_ID, and you try to insert another row with the same EMP_ID, Oracle will raise this error.
This post explains the root causes, troubleshooting steps, and different solutions to fix the problem.
---
Step 1: Identify the Error in Action
You may see an error like this when running an INSERT or UPDATE statement:
INSERT INTO employees (emp_id, name, department)
VALUES (101, 'John Doe', 'Finance');
-- Error Output:
ORA-00001: unique constraint (HR.EMPLOYEES_PK) violated
➡️ The message tells you the constraint name (HR.EMPLOYEES_PK) that was violated.
---
Step 2: Find the Constraint Definition
Use the constraint name from the error message to check which column(s) it applies to:
SELECT constraint_name, constraint_type, table_name
FROM user_constraints
WHERE constraint_name = 'EMPLOYEES_PK';
➡️ This shows whether it’s a PRIMARY KEY or UNIQUE constraint and on which table.
To check the exact columns:
SELECT column_name
FROM user_cons_columns
WHERE constraint_name = 'EMPLOYEES_PK';
---
Step 3: Check Existing Data for Duplicates
Before inserting, verify if the value already exists in the table:
SELECT emp_id, COUNT(*)
FROM employees
GROUP BY emp_id
HAVING COUNT(*) > 1;
➡️ If duplicates exist, you may need to clean them up.
---
Step 4: Common Solutions
🔹 Option 1: Correct the Data
Ensure you’re not trying to insert a duplicate key value:
INSERT INTO employees (emp_id, name, department)
VALUES (102, 'Jane Smith', 'HR');
🔹 Option 2: Use a Sequence for Primary Keys
If the primary key column is numeric, use a sequence to auto-generate unique values:
CREATE SEQUENCE emp_seq START WITH 1000 INCREMENT BY 1;
INSERT INTO employees (emp_id, name, department)
VALUES (emp_seq.NEXTVAL, 'Alice Johnson', 'IT');
🔹 Option 3: Merge Instead of Insert
If you want to insert a new row but update the existing one when a duplicate is found:
MERGE INTO employees e
USING (SELECT 101 emp_id, 'John Doe' name, 'Finance' dept FROM dual) src
ON (e.emp_id = src.emp_id)
WHEN MATCHED THEN
UPDATE SET e.name = src.name, e.department = src.dept
WHEN NOT MATCHED THEN
INSERT (emp_id, name, department)
VALUES (src.emp_id, src.name, src.dept);
---
Step 5: Prevent Future Issues
Always use sequences for primary keys in Oracle.
Define business rules to prevent duplicate inserts.
Add application-side checks before performing insert or update operations.
---
Conclusion
The ORA-00001: Unique Constraint Violated error is a clear indicator that Oracle is enforcing data integrity by preventing duplicates.
By understanding the constraint definition, checking existing data, and using sequences or merge statements, you can fix the issue and prevent it from happening again.
👉 Always remember: This error is not a bug — it’s Oracle protecting your data consistency.

Comments
Post a Comment