DROP TABLE Command
The SQL DDL statement DROP TABLE permanently deletes one or more database tables. When a table is dumped, its columns, indexes, constraints, triggers, and data are permanently erased from the database. Irreversible data loss occurs after this command is run. To preserve data integrity, DROP TABLE statements lock the table with an ACCESS EXCLUSIVE lock, blocking all concurrent operations, including SELECT statements. If views or foreign key constraints in other tables depend on a table, PostgreSQL will warn you to utilise CASCADE before dropping it.
To override this, add the CASCADE option to the command to delete all dependant objects recursively (views, foreign key constraints) along with the table. RESTRICT prevents drop if dependencies exist by default. Unlike DELETE and TRUNCATE, which erase data but retain table structure. TRUNCATE clears a table instantaneously without a VACUUM, however it violates MVCC semantics. DBeaver, a database administration application, lets users delete database objects, including tables, in diagram edit mode and generate SQL scripts using DROP TABLE instructions.

Keywords in DROP TABLE
DROP TABLE can be used with two essential keywords:
IF EXISTS: The optional clause IF EXISTS keeps an error from happening in the event that the table is not found. SQL scripts frequently utilise this to try dropping tables unconditionally before adding new ones.
CASCADE: To avoid problems with data integrity, DROP TABLE typically fails with an error when a table contains objects that depend on it (like views, indexes, or foreign key controls). The addition of CASCADE will recursively drop all dependant items automatically. In an inheritance structure, for example, removing a parent table with CASCADE will eliminate its descendant tables.
RESTRICT: If the table is dependent on any other objects, this keyword indicates the default behaviour, which is to keep it from being dropped. Maintaining database integrity is aided by this.
Functionality and Impact
After DROP TABLE is run:
Data and Structure Removal: The definition of the table and every row in it are permanently deleted. There is no turning back this action.
Index Removal: Secondary indexes are automatically eliminated if a table is being destroyed. When a unique constraint is dropped, a unique index is likewise dropped.
Constraint Removal: Table constraints involving the dropped table or its columns are eliminated, including CHECK, PRIMARY KEY, and UNIQUE constraints. CASCADE is required if a column in another table is referenced by a foreign key constraint; eliminate COLUMN, a type of ALTER TABLE, would not eliminate that constraint silently.
Locks: To make sure that no other transactions are accessing the table while the operation is underway, DROP TABLE obtains an ACCESS EXCLUSIVE lock on the table that clashes with all other lock modes. Compared to locks that are commonly obtained automatically, this one is more stringent.
MVCC-Safety: DROP TABLE is not MVCC-safe, which means that concurrent transactions that took a snapshot prior to the DROP command committing would see the table as empty after the command commits. This can cause obvious discrepancies between the target table and other database tables.
Synchronous Commit: Some utility actions, like DROP TABLE, must commit synchronously regardless of the synchronous_commit configuration. The server file system and database logical state are guaranteed to be consistent.
Code Example:
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_name TEXT UNIQUE NOT NULL
);
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
product_id INT REFERENCES products(product_id),
quantity INT NOT NULL
);
INSERT INTO products (product_name) VALUES ('Laptop'), ('Phone');
INSERT INTO orders (product_id, quantity) VALUES (1, 5), (2, 3);
DROP TABLE products CASCADE;
DROP TABLE orders;
Output:
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2
DROP TABLE
DROP TABLE
Permissions and Error Handling
Only the table owner or superuser can drop a table. Trying to dump a non-existent table without IF EXISTS fails. A DROP hint… If dependant objects exist and CASCADE is not specified, the error message lists it.
TRUNCATE vs. DELETE DROP TABLE scope differs:
DELETE: DELETE deletes table rows with an optional WHERE clause. Without a WHERE clause, all rows are deleted but the table structure remains. DELETE takes slower than TRUNCATE for large tables since it records row deletions and may need a VACUUM to free up disc space.
TRUNCATE: Without affecting a table’s structure, TRUNCATE removes all rows instantaneously. Due to its lack of row scanning and reporting, it is faster than DELETE on large tables. In conflict with TRUNCATE.The TRUNCATE command deletes the table without vacuuming. Locking the table with ACCESS EXCLUSIVE TRUNCATE stops simultaneous operations.
Historical Context: Remove History Column Previous PostgreSQL versions did not support ALTER TABLE DROP COLUMN. Just add a new table, pick all useful columns (excluding the one to be eliminated), dump the old one, and rename it. Alter TABLE… Drop Column is supported by PostgreSQL.
Advanced Scenarios
Table Partitioning: DROP TABLE is a quick method for deleting old data by deleting a whole partition. Because a child table (partition) prevents the deletion of individual entries, it can swiftly erase millions of records.
Logical Replication: Since DDL operations like DROP need modifying system catalogue tables, which is not allowed in a hot standby environment, they are typically prohibited on hot standby servers. Any users on the standby that are connected to that database will be forced disconnected if DROP DATABASE or ALTER DATABASE… SET TABLESPACE is executed on the main.
Conclusion
This software shows PostgreSQL’s DROP TABLE behaviour with table dependencies and restrictions. By initially constructing a parent table (products) with a unique constraint and a child table (orders) accessing it through a foreign key, the script shows that removing a table with dependent objects usually requires explicit handling. Using DROP TABLE… CASCADE removes the parent table and any constraints or references in other tables, preventing foreign key dependence issues. Finally, PostgreSQL automatically removes indexes, cleans constraints, and resolves dependencies when dropping the child table.