How can you delete a table from a database?

Instruction: Using PostgreSQL, show how to drop a disposable practice table named practice_orders. Explain how DROP differs from DELETE, how dependencies affect it, and when a transaction can roll it back.

Context: This question evaluates the candidate's knowledge of data management and their ability to safely remove database structures.

Updated

Official answer

In PostgreSQL, DROP TABLE removes the table itself. For a disposable practice table named practice_orders, the statement is:

DROP TABLE practice_orders;

This differs from DELETE FROM practice_orders, which removes rows while retaining the table, and TRUNCATE, which removes its rows using different locking and dependency rules.

PostgreSQL drops a table's own indexes, rules, triggers and constraints with it. Dependencies such as a view can prevent the drop under the default RESTRICT behavior. Do not add CASCADE merely to silence that error: it can remove dependent objects too. Other database engines can have different DDL and transaction behavior.

For an actual migration, confirm the database, schema, ownership, dependencies and recovery plan. In PostgreSQL, a normal table drop inside an explicit transaction can be rolled back before commit. After committing, recovery requires an available restoration path; a backup is useful only if it can be restored to the needed state.

Assessment: State the engine, distinguish deleting rows from dropping the object, and describe dependency and transaction behavior accurately. Practice destructive statements only on intentionally disposable data.

Source

PostgreSQL: DROP TABLE.

Related Questions