Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-25 · container image gvenzl/oracle-free:23-slim
A foreign key can only point to columns that are covered by a PRIMARY KEY or UNIQUE constraint on the parent table, with exactly the same column list. If they are not, ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY (or CREATE TABLE) fails with ORA-02270, and the * points to the column list after REFERENCES.
It is a DDL error, so it does not depend on the data in the tables.
01The parent column has no primary key
DEPT was created without any key on dept_id, and the foreign key EMP.dept_id → DEPT.dept_id failed.
After adding ALTER TABLE dept ADD CONSTRAINT dept_pk PRIMARY KEY (dept_id), the same foreign key statement worked (Table altered.).
Setup
CREATE TABLE dept (dept_id NUMBER, dept_name VARCHAR2(30));
CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, name VARCHAR2(20), dept_id NUMBER);Table created.
Table created.ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id);ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
Help: https://docs.oracle.com/error-help/db/ora-02270/ALTER TABLE dept ADD CONSTRAINT dept_pk PRIMARY KEY (dept_id);Table altered.The key must be on the parent side. Create it on the parent table first, then run the same FOREIGN KEY statement again.
ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id);Table altered.02Only a unique index exists, not a UNIQUE constraint
PRODUCT.product_code had a unique index PRODUCT_CODE_UX, but the foreign key from ORDER_ITEMS still failed. The check shows why: the index is UNIQUE, but the only constraint on PRODUCT is the primary key (type P) on another column. A foreign key looks at constraints, not indexes.
ALTER TABLE product ADD CONSTRAINT product_code_uk UNIQUE (product_code) USING INDEX product_code_ux created the constraint on top of the existing index, and then the foreign key was added.
Setup
CREATE TABLE product (product_id NUMBER PRIMARY KEY, product_code VARCHAR2(10), name VARCHAR2(30));
CREATE UNIQUE INDEX product_code_ux ON product (product_code);
CREATE TABLE order_items (order_id NUMBER, product_code VARCHAR2(10), qty NUMBER);Table created.
Index created.
Table created.ALTER TABLE order_items ADD CONSTRAINT oi_product_fk FOREIGN KEY (product_code) REFERENCES product (product_code);ALTER TABLE order_items ADD CONSTRAINT oi_product_fk FOREIGN KEY (product_code) REFERENCES product (product_code)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
Help: https://docs.oracle.com/error-help/db/ora-02270/SELECT index_name, uniqueness FROM user_indexes WHERE table_name = 'PRODUCT' AND index_name = 'PRODUCT_CODE_UX';
SELECT constraint_name, constraint_type FROM user_constraints WHERE table_name = 'PRODUCT' ORDER BY constraint_type;INDEX_NAME UNIQUENES
------------------------------ ---------
PRODUCT_CODE_UX UNIQUE
1 row selected.
CONSTRAINT_NAME C
------------------------------ -
SYS_C009100 P
1 row selected.ALTER TABLE product ADD CONSTRAINT product_code_uk UNIQUE (product_code) USING INDEX product_code_ux;Table altered.USING INDEX reuses the index you already have, so no second index is built.
ALTER TABLE order_items ADD CONSTRAINT oi_product_fk FOREIGN KEY (product_code) REFERENCES product (product_code);Table altered.03Referencing only part of a composite key
The primary key of ORDER_LINES is two columns, (order_id, line_no) (positions 1 and 2 in the check). The foreign key referenced only order_id, which alone is not unique, so it failed.
Referencing the whole key, FOREIGN KEY (order_id, line_no) REFERENCES order_lines (order_id, line_no), was accepted.
Setup
CREATE TABLE order_lines (order_id NUMBER, line_no NUMBER, product VARCHAR2(20), CONSTRAINT order_lines_pk PRIMARY KEY (order_id, line_no));
CREATE TABLE shipments (ship_id NUMBER PRIMARY KEY, order_id NUMBER, line_no NUMBER, shipped_at DATE);Table created.
Table created.ALTER TABLE shipments ADD CONSTRAINT ship_line_fk FOREIGN KEY (order_id) REFERENCES order_lines (order_id);ALTER TABLE shipments ADD CONSTRAINT ship_line_fk FOREIGN KEY (order_id) REFERENCES order_lines (order_id)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
Help: https://docs.oracle.com/error-help/db/ora-02270/SELECT column_name, position FROM user_cons_columns WHERE constraint_name = 'ORDER_LINES_PK' ORDER BY position;COLUMN_NAME POSITION
------------------------------ ----------
ORDER_ID 1
LINE_NO 2
2 rows selected.The child needs the same number of columns as the parent key. Check the key columns in USER_CONS_COLUMNS in POSITION order.
ALTER TABLE shipments ADD CONSTRAINT ship_line_fk FOREIGN KEY (order_id, line_no) REFERENCES order_lines (order_id, line_no);Table altered.FAQ
I have a unique index on the column. Why do I still get ORA-02270?
A foreign key needs a PRIMARY KEY or UNIQUE constraint. In the test a unique index alone failed, and adding UNIQUE ... USING INDEX fixed it.
How do I find the primary key columns of the parent table?
Query USER_CONS_COLUMNS for the key's constraint name, ordered by POSITION, as in the test. USER_CONSTRAINTS shows the key type (P = primary, U = unique).
Can a foreign key reference only one column of a composite primary key?
No. In the test, referencing only order_id of (order_id, line_no) gave ORA-02270. Reference all key columns.
Does ORA-02270 mean the data does not match?
No. It is about the definition of the parent table (its keys). When an inserted child row has no matching parent row, the error is ORA-02291 instead.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0