ORA-02270 – can FK in Oracle be defined on VARCHAR2 column?

All we need is an easy explanation of the problem, so here it is.

SQL is more than words:

CREATE TABLE "currency" ("id" NUMBER(10) NOT NULL,
"currency" VARCHAR2(50) NOT NULL,
"name" VARCHAR2(1020) DEFAULT NULL NULL,
PRIMARY KEY("id"));
CREATE TABLE "user" ("id" NUMBER(10) NOT NULL,
"name" VARCHAR2(1020) DEFAULT NULL NULL,
"currency" VARCHAR2(50) DEFAULT NULL NULL,
PRIMARY KEY("id"));
CREATE UNIQUE INDEX IDX_6956883FFF467AE9 ON "currency" ("currency");
ALTER TABLE "user" ADD CONSTRAINT FK_8D93D6496956883F6956883F695
FOREIGN KEY ("currency") REFERENCES "currency" ("currency");
ORA-02270: no matching unique or primary key for this column-list

repro on dbfiddle

The column types are the same and unique index is added.

Why the FK cannot be added?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

A unique index does not include a unique constraint.

Add the constraint and try again:

alter table "currency" add unique("currency") using index IDX_6956883FFF467AE9;

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply