Skip to content
Advertisement

ORACLE CREATE TABLE with FOREIGN KEY error

I am a beginner to Oracle and have been having problems creating a table with a foreign key, I have created the parent table region as well and have been receiving the same error. I have researched on W3school and used the same syntax but still no help.

CREATE TABLE region(
    region_id INTEGER NOT NULL PRIMARY KEY,
    region_name VARCHAR2(20)
);
CREATE TABLE warehouse(
    warehouse_id INTEGER NOT NULL PRIMARY KEY,
    warehouse_address VARCHAR2(20),
    warehouse_postcode VARCHAR2(8),
    warehouse_phonenm INTEGER,
    region_id FOREIGN KEY REFERENCES region(region_id)
);

Advertisement

Answer

Leave “FOREIGN KEY” out, if you want to declare a foreign key constraint inline.

CREATE TABLE warehouse(
warehouse_id INTEGER NOT NULL PRIMARY KEY,
warehouse_address VARCHAR2(20),
warehouse_postcode VARCHAR2(8),
warehouse_phonenm INTEGER,
region_id REFERENCES region(region_id));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement