Sub topic 10
Linking Tables with FOREIGN KEY and ON DELETE
Assume you know basic INSERT, UPDATE, and DELETE statements. You should also understand that a relational table stores rows and that a key identifies a row.
A foreign key is a column, or group of columns, in one table that refers to a primary key or unique key in another table. The table containing the foreign key is the child table. The referenced table is the parent table.
For example, an employee can belong to a department. departments is the parent table, and employees is the child table.
Defining a foreign key
The referenced column must have a primary key or unique constraint. Oracle then prevents a child row from referring to a parent value that does not exist.
CREATE TABLE departments (
department_id NUMBER GENERATED ALWAYS AS IDENTITY,
department_name VARCHAR2(100) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id),
CONSTRAINT departments_name_uk UNIQUE (department_name)
);
CREATE TABLE employees (
employee_id NUMBER GENERATED ALWAYS AS IDENTITY,
email VARCHAR2(255) NOT NULL,
department_id NUMBER,
CONSTRAINT employees_pk PRIMARY KEY (employee_id),
CONSTRAINT employees_email_uk UNIQUE (email),
CONSTRAINT employees_dept_fk
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
);employees_dept_fk is an explicitly named foreign key constraint. Naming constraints makes error messages easier to understand and makes later maintenance possible.
Because department_id in employees does not have NOT NULL, an employee may have no department. A non-null value must match an existing departments.department_id.
INSERT INTO departments (department_name)
VALUES ('Sales');
INSERT INTO employees (email, department_id)
SELECT '[email protected]', department_id
FROM departments
WHERE department_name = 'Sales';An insert such as the following fails because department 9999 does not exist:
INSERT INTO employees (email, department_id)
VALUES ('[email protected]', 9999);Choosing an `ON DELETE` action
When a parent row is deleted, Oracle applies the action defined by the foreign key. If you omit ON DELETE, Oracle uses NO ACTION. The delete fails while matching child rows still exist.
| Action | Result when the parent row is deleted |
|---|---|
| `NO ACTION` | Rejects the delete if child rows refer to it |
| `CASCADE` | Deletes the matching child rows |
| `SET NULL` | Sets the child foreign-key columns to `NULL` |
Use SET NULL when the child should remain but no longer belong to the deleted parent:
ALTER TABLE employees DROP CONSTRAINT employees_dept_fk;
ALTER TABLE employees ADD CONSTRAINT employees_dept_fk
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
ON DELETE SET NULL;This action requires employees.department_id to allow nulls. It would be invalid as a design if every employee must always have a department.
Use CASCADE only when child rows have no useful meaning without their parent. For example, deleting an order might also delete its temporary order lines. Avoid it for valuable records such as employees, because one parent delete can remove many rows.
DELETE FROM departments
WHERE department_name = 'Sales';With ON DELETE SET NULL, the employee remains, but its department_id becomes NULL.
Oracle has no `ON UPDATE`
Oracle supports ON DELETE, but it does not support an ON UPDATE action for foreign keys. If you change a referenced parent key while child rows exist, Oracle rejects the update. Usually, primary keys should be stable identifiers, so applications should update descriptive columns instead of key values.
For a self-referencing relationship, such as an employee and manager, the foreign key can reference the same table. The same delete rules apply, and you must choose carefully whether deleting a manager should be blocked, cascade, or set manager references to null.
Exercise
Which ON DELETE action keeps an employee row but removes its department relationship?
Show answer
ON DELETE SET NULL, provided that the child foreign-key column permits NULL.