Using Enable a foreign key in Oracle

This Oracle tutorial explains how to allow a overseas key in Oracle with syntax and examples.

Description

You can also come across a foreign key in Oracle that has been disabled. You can allow the foreign key the usage of the ALTER TABLE statement.

Syntax

The syntax for enabling a overseas key in Oracle/PLSQL is:

ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

Example

If you had created a foreign key as follows:

CREATE TABLE supplier
( supplier_id numeric(10) not null,
  supplier_name varchar2(50) not null,
  contact_name varchar2(50),
  CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products
( product_id numeric(10) not null,
  supplier_id numeric(10) not null,
  CONSTRAINT fk_supplier
    FOREIGN KEY (supplier_id)
    REFERENCES supplier(supplier_id)
);

In this example, we have created a fundamental key on the supplier desk called supplier_pk. It consists of only one field – the supplier_id field. Then we have created a overseas key called fk_supplier on the merchandise desk that references the supplier desk primarily based on the supplier_id field.

If the overseas key had been disabled and we desired to enable it, we should execute the following command:

ALTER TABLE products
ENABLE CONSTRAINT fk_supplier;