DEV Community

umzzil nng
umzzil nng

Posted on • Originally published at oraerror.com

Oracle ORA-02253 Error: Causes and Solutions Complete Guide

ORA-02253: Constraint Specification Not Allowed Here — What It Means and How to Fix It

ORA-02253 is an Oracle SQL syntax error that occurs when a constraint definition is placed in a location where Oracle does not permit it. This typically happens when developers attempt to add constraints inline within an ALTER TABLE MODIFY clause or misplace constraint definitions inside CREATE TABLE statements. Understanding where Oracle allows constraint declarations is the fastest way to resolve and prevent this error.


Top 3 Causes and Fixes

Cause 1: Using Inline Constraints in ALTER TABLE MODIFY

The most common trigger for ORA-02253 is attempting to define a constraint directly inside a MODIFY clause. Oracle's MODIFY clause is strictly for changing column attributes like data type or default value — not for adding constraints.

Incorrect (causes ORA-02253):

ALTER TABLE employees
MODIFY (employee_id NUMBER(10) PRIMARY KEY);
Enter fullscreen mode Exit fullscreen mode

Correct approach — separate MODIFY from ADD CONSTRAINT:

-- Step 1: Modify column attributes
ALTER TABLE employees
MODIFY (employee_id NUMBER(10) NOT NULL);

-- Step 2: Add the constraint separately
ALTER TABLE employees
ADD CONSTRAINT pk_employees PRIMARY KEY (employee_id);

-- Adding a foreign key the right way
ALTER TABLE employees
ADD CONSTRAINT fk_dept
FOREIGN KEY (department_id)
REFERENCES departments(department_id);
Enter fullscreen mode Exit fullscreen mode

Cause 2: Misplaced Constraints in CREATE TABLE

Developers sometimes attempt to define composite primary keys or table-level constraints inline at the column level, which Oracle does not support.

Incorrect (causes ORA-02253):

CREATE TABLE order_items (
    order_id   NUMBER(10) PRIMARY KEY,
    item_id    NUMBER(10) PRIMARY KEY,  -- invalid: second PK
    quantity   NUMBER(5),
    unit_price NUMBER(10,2)
);
Enter fullscreen mode Exit fullscreen mode

Correct — move composite constraints to the table level:

CREATE TABLE order_items (
    order_id   NUMBER(10)   NOT NULL,
    item_id    NUMBER(10)   NOT NULL,
    quantity   NUMBER(5)    NOT NULL,
    unit_price NUMBER(10,2) DEFAULT 0,
    -- Table-level constraints defined after all columns
    CONSTRAINT pk_order_items PRIMARY KEY (order_id, item_id),
    CONSTRAINT chk_quantity   CHECK (quantity > 0),
    CONSTRAINT chk_price      CHECK (unit_price >= 0)
);
Enter fullscreen mode Exit fullscreen mode

Cause 3: Attempting to Add Constraints Inside a View Definition

Oracle views do not support standard constraint declarations. Developers copying table DDL scripts and pasting them into view creation scripts often hit this error.

Incorrect (causes ORA-02253):

CREATE VIEW emp_view AS
SELECT employee_id NUMBER PRIMARY KEY,  -- constraints not allowed in views
       first_name,
       last_name
FROM employees;
Enter fullscreen mode Exit fullscreen mode

Correct — define the view without constraints:

CREATE OR REPLACE VIEW emp_view AS
SELECT
    employee_id,
    first_name,
    last_name,
    department_id
FROM employees
WHERE department_id IS NOT NULL
WITH CHECK OPTION;  -- only this form is allowed in views

-- Apply actual constraints on the base table instead
ALTER TABLE employees
ADD CONSTRAINT pk_employees PRIMARY KEY (employee_id);
Enter fullscreen mode Exit fullscreen mode

Quick Fix Checklist

  • ✅ Never add PRIMARY KEY, UNIQUE, or FOREIGN KEY inside ALTER TABLE MODIFY
  • ✅ Always use ALTER TABLE ... ADD CONSTRAINT for constraint additions
  • ✅ Place composite or table-level constraints after all column definitions in CREATE TABLE
  • ✅ Never declare constraints inside view definitions (except WITH CHECK OPTION)
  • ✅ Validate existing data before adding constraints to avoid cascading errors like ORA-02437 or ORA-02291

Prevention Tips

Adopt a consistent DDL template. Standardize your team's DDL scripts by always separating column definitions from constraint definitions using clear comments. This eliminates positional errors before they reach the database.

CREATE TABLE example_table (
    -- Column definitions
    col1 NUMBER(10)    NOT NULL,
    col2 VARCHAR2(100) NOT NULL,
    col3 DATE          DEFAULT SYSDATE,
    -- Constraint definitions (always last)
    CONSTRAINT pk_example  PRIMARY KEY (col1),
    CONSTRAINT uq_example  UNIQUE (col2)
);
Enter fullscreen mode Exit fullscreen mode

Always validate data before adding constraints. Before running ADD CONSTRAINT on an existing table, run a quick data quality check to avoid chained errors:

-- Check for duplicates before adding PRIMARY KEY
SELECT employee_id, COUNT(*)
FROM employees
GROUP BY employee_id
HAVING COUNT(*) > 1;
Enter fullscreen mode Exit fullscreen mode

Related Oracle Errors

  • ORA-02264 — Constraint name already in use
  • ORA-02437 — Primary key constraint violated by existing data
  • ORA-02291 — Integrity constraint violated (parent key not found)
  • ORA-00907 — Missing right parenthesis (often appears alongside syntax errors in DDL)

📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.

Top comments (0)