QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Changing data and transactions

What to check before DELETE, TRUNCATE or DROP?

Before DELETE, TRUNCATE or DROP three questions are answered: where am I, what will I touch and can it be undone. That covers database, environment and user; rows, related tables, cascades and triggers; transaction, copy and privileges. If one has no answer, the command is not run. Before COMMIT all three roll back in PostgreSQL, only DELETE in MySQL.

SQL · Changing data and transactions

You have a database connection open on a shared environment. What to check before running DELETE, TRUNCATE or DROP?

The difference between the three commands is covered in a separate lesson. Here it is about something else: what to do before pressing "execute". Mistakes of this kind are rarely syntax errors. They are a correct command on the wrong connection, on the wrong data, or with no way back.

How it works and why. A safe destructive deletion rests on three questions. Where am I? Which database, which environment, which user. What will I touch? Which rows, which related tables, whether there are cascades and triggers. Can it be undone? Whether inside a transaction, whether there is a copy, which privileges. If one of the three has no answer, the command is not run.

-- where am I
SELECT current_database(), current_user, inet_server_addr();

-- a dry run (PostgreSQL): the effects are visible, then a rollback
BEGIN;
DELETE FROM sessions WHERE expires_at < now();
-- DELETE 1240   <- is this the expected count?
ROLLBACK;

What exactly rolls back before COMMIT depends on the command and the database: in PostgreSQL all three do, in MySQL only DELETE (the comparison is in the lesson “How do DELETE, TRUNCATE and DROP differ?”). After COMMIT there is no rollback in any database: only a copy or a restore from backup, which is usually done by someone else and takes hours.

Example: a DROP that failed. The old users_old table has to be removed.

DROP TABLE users_old;
-- ERROR:  cannot drop table users_old because other objects depend on it
-- DETAIL:  view v_users depends on table users_old   (shortened)
-- HINT:  Use DROP ... CASCADE to drop the dependent objects too.

-- safer: rename and wait
ALTER TABLE users_old RENAME TO users_old_to_drop_20261121;

The error says that the view v_users depends on the table. CASCADE would remove it too, and perhaps a report that relies on it. Renaming takes the table out of service for code that uses it by name: if something breaks, the name is restored. A view in PostgreSQL, however, references the table by its internal id, so v_users keeps working after the rename and the final DROP fails again. So first find out who reads v_users, rewrite or drop it, and only then drop the table.

Before a destructive command

Wrong

Run the selection on a ready connection

Neither the database, nor the count, nor the relations are visible. There is no way back.

Right

Where am I, what will I touch, how to undo

A connection check, a SELECT with the same condition, a dry run in a transaction, a copy and a rename instead of DROP.

Figure 1. Three questions cost a minute and a mistake costs hours of restoring from a backup.

Limits and alternatives. A dry run in a transaction is safe in PostgreSQL, but TRUNCATE and DROP take ACCESS EXCLUSIVE and hold it until the end of the transaction, so be quick. In MySQL DDL cannot be checked this way. Privileges save better than discipline: the QA role gets SELECT, INSERT, UPDATE, DELETE on the test schema, without TRUNCATE and DROP; that is a question for the database administrator. One more layer, a backup or snapshot before a big run, allows going back when everything else failed.

Common mistakes.

  • Running the whole script instead of one selected statement, or the other way round.
  • Two tabs with identical connection names to different environments.
  • CASCADE "to make it pass" without finding out what depends.
  • Counting on a rollback in MySQL for TRUNCATE or DROP.
  • A TRUNCATE dry run left in a transaction for long, blocking the table for everybody.

Check yourself

Why is a dry run BEGIN; TRUNCATE t; … ROLLBACK; safe in PostgreSQL but not in MySQL?

Answer

In PostgreSQL TRUNCATE is transactional and rolls back. In MySQL it is DDL with an implicit COMMIT: the data is gone for good. Also in PostgreSQL the table is locked for everyone until the rollback.

DROP TABLE users_old fails because of a dependent view. Why not just add CASCADE?

Answer

CASCADE removes the dependent objects too (views, constraints), and you do not know which. First find out what depends on what, and where possible rename the table instead of dropping it.

In an interview. Junior: Before deleting I write a SELECT with the same condition and look at which rows I will touch. I make the change in a transaction and, if the count is wrong, ROLLBACK. On production I change nothing without agreement. Middle/Senior: I check where I am connected, what depends on the object (cascades, views, triggers) and whether there is a way back. In PostgreSQL TRUNCATE and DROP roll back before COMMIT, in MySQL they do not. Instead of DROP I rename, and I would ask for the TRUNCATE and DROP rights on a shared environment to stay with the administrator.

More articles in this chapter