CREATE TABLE table_unique_1 ( id NUMBER(1,0) , id1 NUMBER(1,0) , id2 NUMBER(1,0) , CONSTRAINT cp_table_unique_1_id PRIMARY KEY (id) -- PRIMARY KEY , CONSTRAINT cu_table_unique_1_id UNIQUE (id1, id2) -- UNIQUE );
CREATE TABLE table_foreign_key_1 ( id NUMBER(1,0) , CONSTRAINT cr_table_foreign_key_1_id FOREIGN KEY (id) REFERENCES table_unique_1 (id) -- the name of the foreign key constraint is explicit: "CR_table_foreign_key_1_ID" );Important: The column (
id
) referenced by the foreign key must have either a PRIMARY KEY
or a UNIQUE
constraint.
Oracle will return an error if you attempt to reference a column without these constraints.CREATE TABLE table_foreign_key_2 ( id NUMBER(1,0) , FOREIGN KEY (id) REFERENCES table_unique_1 (id) -- the foreign key constraint name will be dynamically generated by Oracle );Composite Foreign Keys: When the foreign key consists of multiple columns, all columns must be specified in the same order as they appear in the referenced unique or primary key constraint:
CREATE TABLE table_foreign_key_3 ( id NUMBER(1,0) , fk_id1 NUMBER(1,0) , fk_id2 NUMBER(1,0) , CONSTRAINT cr_table_foreign_key_3_id FOREIGN KEY (fk_id1, fk_id2) REFERENCES table_unique_1 (id1, id2) );
CREATE TABLE table_foreign_key_4 ( id NUMBER(1,0) );Then add the foreign key constraint using ALTER TABLE with an explicit constraint name:
ALTER TABLE table_foreign_key_4 ADD CONSTRAINT cr_table_foreign_key_4_id FOREIGN KEY (id) REFERENCES table_unique_1 (id); -- the foreign key constraint name is explicit: "CR_TABLE_FOREIGN_KEY_4_ID"Alternatively, you can let Oracle generate the constraint name automatically:
ALTER TABLE table_foreign_key_4 ADD FOREIGN KEY (id) REFERENCES table_unique_1 (id); -- the foreign key constraint name will be dynamically generated by OracleNote: When adding a foreign key to a table with existing data, Oracle validates that all existing foreign key values have corresponding entries in the referenced table. If any orphaned rows exist, the ALTER TABLE statement will fail.
CREATE TABLE table_foreign_key_5 ( id NUMBER(1,0) , CONSTRAINT cr_table_foreign_key_5_id FOREIGN KEY (id) REFERENCES table_unique_1 (id) ON DELETE CASCADE );The "
ON DELETE CASCADE
" option means that if a row is deleted from the "table_unique_1
" table,
then any rows in the "table_foreign_key_5
" table that reference it will also be deleted automatically.SELECT constraint_name, constraint_type, generated, delete_rule FROM user_constraints WHERE table_name = 'TABLE_FOREIGN_KEY_5';Query result:
+---------------------------+-----------------+----------------+-------------+ | CONSTRAINT_NAME | CONSTRAINT_TYPE | GENERATED | DELETE_RULE | +---------------------------+-----------------+----------------+-------------+ | CR_TABLE_FOREIGN_KEY_5_ID | R | GENERATED NAME | CASCADE | +---------------------------+-----------------+----------------+-------------+Demonstration:
INSERT INTO table_unique_1 (id, id1, id2) VALUES (0,0,0); INSERT INTO table_unique_1 (id, id1, id2) VALUES (1,0,1); INSERT INTO table_unique_1 (id, id1, id2) VALUES (2,1,0);
INSERT INTO table_foreign_key_5 (id) VALUES (0); INSERT INTO table_foreign_key_5 (id) VALUES (1); INSERT INTO table_foreign_key_5 (id) VALUES (2);Delete parent rows to trigger the cascade:
DELETE FROM table_unique_1 WHERE id < 2;Check the data in "
table_foreign_key_5
" after deleting data from "table_unique_1
".
Notice that the child rows with id values 0 and 1 were automatically deleted:SELECT id FROM table_foreign_key_5;
+----+ | ID | +----+ | 2 | +----+
CREATE TABLE table_foreign_key_6 ( id NUMBER(1,0) , CONSTRAINT cr_table_foreign_key_6_id FOREIGN KEY (id) REFERENCES table_unique_1 (id) ON DELETE SET NULL );The "
ON DELETE SET NULL
" option means that if a row is deleted from the "table_unique_1
" table,
then the foreign key column values in the "table_foreign_key_6
" table that reference it will be set to NULL, preserving the child rows.SELECT constraint_name, constraint_type, generated, delete_rule FROM user_constraints WHERE table_name = 'TABLE_FOREIGN_KEY_6';Query result:
+---------------------------+-----------------+----------------+-------------+ | CONSTRAINT_NAME | CONSTRAINT_TYPE | GENERATED | DELETE_RULE | +---------------------------+-----------------+----------------+-------------+ | CR_TABLE_FOREIGN_KEY_6_ID | R | GENERATED NAME | SET NULL | +---------------------------+-----------------+----------------+-------------+Demonstration:
INSERT INTO table_unique_1 (id, id1, id2) VALUES (0,0,0); INSERT INTO table_unique_1 (id, id1, id2) VALUES (1,0,1); INSERT INTO table_unique_1 (id, id1, id2) VALUES (2,1,0);
INSERT INTO table_foreign_key_6 (id) VALUES (0); INSERT INTO table_foreign_key_6 (id) VALUES (1); INSERT INTO table_foreign_key_6 (id) VALUES (2);Delete parent rows to trigger the SET NULL action:
DELETE FROM table_unique_1 WHERE id < 2;Check the data in "
table_foreign_key_6
" after deleting data from "table_unique_1
".
Notice that the child rows remain but their foreign key values are set to NULL:SELECT id FROM table_foreign_key_6;
+--------+ | ID | +--------+ | (null) | | (null) | | 2 | +--------+
ALTER TABLE table_foreign_key_1 DISABLE CONSTRAINT cr_table_foreign_key_1_id;Important: When a foreign key constraint is disabled, Oracle does not validate data inserted or updated in the child table. Re-enabling the constraint will validate all existing data, and the operation will fail if any referential integrity violations are found.
ALTER TABLE table_foreign_key_1 ENABLE CONSTRAINT cr_table_foreign_key_1_id;Note: If any existing data violates the foreign key constraint, the ENABLE operation will fail with an error. You must either correct the data or use the ENABLE NOVALIDATE option to enable the constraint without validating existing data (not recommended for data integrity).
ALTER TABLE table_foreign_key_1 DROP CONSTRAINT cr_table_foreign_key_1_id;Warning: All foreign key constraints referencing a table must be dropped before the referenced table can be dropped. Oracle prevents dropping tables that are referenced by foreign keys to maintain referential integrity:
DROP TABLE table_unique_1;Query result:
Error starting at line 1 in command: DROP TABLE TABLE_UNIQUE_1 Error report: SQL Error: ORA-02449: unique/primary keys in table referenced by foreign keys. *Cause: An attempt was made to drop a table with unique or primary keys referenced by foreign keys in another table. *Action: Before performing the above operation, drop the foreign key constraints in the other tables. You can see which constraints reference a table by issuing the following command: SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TABNAM';Alternative: You can use the CASCADE CONSTRAINTS option with DROP TABLE to automatically drop all foreign key constraints that reference the table being dropped:
DROP TABLE table_unique_1 CASCADE CONSTRAINTS;