Sub topic 11
What Depends on a Table
Assume you know basic SELECT, CREATE TABLE, primary keys, and foreign keys. A database object depends on a table when it uses that table and would become invalid if the table changed or disappeared.
Common dependencies include:
- A foreign key in another table.
- A view that reads from the table.
- A trigger attached to the table.
- An index or constraint belonging to the table.
- Stored procedures, functions, or policies that refer to the table.
Dependencies matter most when you alter or remove a table. The database may reject the change, remove dependent objects, or leave some objects invalid, depending on the database system and the option you choose.
Referencing and referenced tables
Suppose orders.customer_id points to customers.id. The orders table references customers. The customers table is the referenced, or parent, table.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id)
);
CREATE VIEW customer_orders AS
SELECT
c.id AS customer_id,
c.name,
COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;The foreign key makes customers a dependency of orders in one direction: orders needs customers to enforce its rule. The view also depends on both tables because its query reads both tables.
What happens when you drop a table
A plain DROP TABLE usually uses restrictive behavior. The database refuses to remove a table when another object depends on it.
-- PostgreSQL example. This fails while the foreign key and view exist.
DROP TABLE customers;This protection prevents an accidental schema change from breaking related objects. You can inspect the error and decide whether to remove the dependencies first.
CASCADE tells the database to remove dependent objects as part of the operation:
-- PostgreSQL example
DROP TABLE customers CASCADE;This removes the customers table, the foreign key constraint that refers to it, and the customer_orders view. It does not remove the orders table itself. The orders table remains, but its customer_id column no longer has that foreign key constraint.
RESTRICT makes the intended protection explicit:
DROP TABLE customers RESTRICT;Some systems use RESTRICT by default. Syntax and cascade behavior differ between database products, so check the documentation for your system before using CASCADE.
Choosing a safe approach
For a planned removal:
- Find foreign keys, views, triggers, functions, and application code that use the table.
- Decide whether each dependency should be changed, removed, or preserved.
- Prefer explicitly dropping known dependencies when the change must be reviewed.
- Use
CASCADEonly when you understand the complete dependency chain. - Test the migration against a copy of the database first.
Exercise
If orders has a foreign key to customers and customer_orders is a view that reads customers, what can happen when you run DROP TABLE customers?
Show answer
A restrictive drop fails because dependencies exist. A PostgreSQL DROP TABLE customers CASCADE removes customers, the foreign key constraint, and the view, but keeps the orders table.