Sub topic 10
Making Scripts Re-runnable with `IF EXISTS`
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and have used CREATE TABLE, primary keys, and foreign keys. This chapter focuses on making table-removal scripts safe to run more than once.
Why re-runnable scripts matter
A database script is re-runnable when executing it again produces the intended result instead of failing because an earlier run already changed the database. Re-runnable scripts help with:
- Development setup and cleanup
- Automated tests
- Deployment retries
- Database migration recovery
- Local database resets
Without IF EXISTS, this statement fails if the table has already been removed:
DROP TABLE obsolete_orders;The IF EXISTS clause tells the database to continue when the named table is absent:
DROP TABLE IF EXISTS obsolete_orders;The statement still fails for other reasons. For example, the user might lack permission, the object might have dependencies, or the database might reject the syntax.
A re-runnable cleanup script
This PostgreSQL-compatible example creates a temporary table and then removes it:
CREATE TABLE IF NOT EXISTS import_buffer (
id INTEGER PRIMARY KEY,
raw_value VARCHAR(200) NOT NULL
);
DROP TABLE IF EXISTS import_buffer;On the first run, the table is created and removed. On the second run, CREATE TABLE IF NOT EXISTS creates it again, and DROP TABLE IF EXISTS removes it again.
IF EXISTS changes how the database handles an absent object. It does not verify that the object has the correct columns, constraints, or data. A table with the wrong definition still satisfies IF EXISTS.
`IF EXISTS` does not ignore dependencies
Suppose orders.customer_id references customers.id. This statement may still fail:
DROP TABLE IF EXISTS customers;The table exists, so IF EXISTS does not suppress the foreign-key dependency error. You must remove or change the dependency first, or choose a database-specific CASCADE option.
IF EXISTS also does not make a destructive operation safe. Dropping a table removes its definition and stored rows. Use it only when removing that object is part of the intended operation.
Dialect differences
Common systems support DROP TABLE IF EXISTS, but support for IF EXISTS on other ALTER TABLE operations differs. For example, ADD COLUMN IF NOT EXISTS is available in PostgreSQL and some other systems, but is not portable SQL. Check the documentation for the database engine and version before using it in a migration.
| Operation | Purpose | Important limitation |
|---|---|---|
| `DROP TABLE IF EXISTS t` | Remove `t` if present | Dependencies can still block the drop |
| `CREATE TABLE IF NOT EXISTS t` | Create `t` if absent | Does not repair an existing wrong schema |
| `ALTER TABLE ... ADD COLUMN IF NOT EXISTS` | Add a column if absent | Syntax varies by database |
Exercise
Why can DROP TABLE IF EXISTS customers still fail when orders has a foreign key that references customers?
Show answer
The table exists, so the existence check succeeds; the foreign-key dependency can still prevent the drop.