Sub topic 9
Constraint Violations and How to Read Oracle DML Errors
This chapter assumes that you can write basic SELECT, INSERT, UPDATE, and DELETE statements. It also assumes that you understand primary keys, foreign keys, and NOT NULL constraints.
A constraint is a rule that protects the data in a table. Oracle checks applicable constraints during DML. If a statement breaks a constraint, Oracle rejects that statement and returns an error.
Read the error message in layers
Oracle errors usually include an error number, a message, and sometimes the object that caused the problem.
For example:
ORA-00001: unique constraint (HR.SYS_C0012345) violatedRead this as follows:
ORA-00001is the error code.unique constraintidentifies the type of rule that failed.HR.SYS_C0012345identifies the owner and constraint name.- The statement that caused the error was not applied.
The constraint name may be system-generated. You can inspect its definition in the data dictionary:
SELECT constraint_name,
constraint_type,
table_name,
search_condition,
r_constraint_name
FROM user_constraints
WHERE constraint_name = 'SYS_C0012345';Constraint names are stored in uppercase unless they were created with quoted identifiers.
Common constraint errors
ORA-00001: unique constraint violated
A primary key and a unique constraint both require distinct values. This statement fails if employee 1 already exists:
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (1, 'New Person', 10, 6000);The usual fixes are to use a new key, update the existing row, or use MERGE when the operation should insert new rows and update existing rows.
ORA-02291: parent key not found
A foreign key must refer to an existing parent row. This insert fails because department 99 does not exist:
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (3, 'Lin', 99, 6500);Insert the parent first, or use a valid dept_id.
ORA-02292: child record found
Oracle prevents you from deleting a parent row while child rows still reference it. If employees belong to department 10, this statement fails:
DELETE FROM dept_demo
WHERE dept_id = 10;Find and handle the child rows first. Depending on the business rule, you might reassign them, delete them, or define the foreign key with an appropriate cascading action.
ORA-01400 and ORA-12899
ORA-01400 means that a NULL value was supplied for a NOT NULL column. ORA-12899 means that a value is too large for its column.
INSERT INTO emp_demo (emp_id, emp_name, dept_id)
VALUES (4, NULL, 10);This fails because emp_name is NOT NULL.
A practical diagnosis path
A reliable workflow is:
- Save the complete error text.
- Identify the ORA code.
- Look up the named constraint.
- Query the rows involved.
- Correct the statement or data.
- Verify the result before
COMMIT.
Exercise
An insert into EMPDEMO fails with ORA-02291 for the foreign key on DEPTID. What should you check first?
Show answer
Check whether the specified DEPTID exists in DEPTDEMO. Insert the parent department first or use an existing department ID, then retry the insert.