Stop 10 · Schema Design & ConstraintsTopics · 2/11

ALTER TABLE and DROP TABLE

Part of the SQL Roadmap

In short

Requirements change: the app now needs a phone number, or a column name made sense two years ago and doesn't today. ALTER TABLE reshapes a live table, and DROP TABLE removes one for good.

ALTER TABLE changes an existing table, like adding or renaming a column, and DROP TABLE deletes the table and all its data permanently.

The jaadu in a hard hat fixes a new balcony onto the side of a small house, while a bulldozer waits idle in the background.

Think of it like this

Renovating vs demolishing

The Sharma family in Pune adds a balcony, knocks out a wall and repaints the kitchen while still living in the flat. That's ALTER TABLE: the home and everything in it stays. Demolishing the whole building is DROP TABLE. Nothing is left, and you can't move back in.

Watch it happen

Changing a table vs removing it

ALTER TABLE

  • Add, rename or drop columns
  • Change types, defaults and constraints
  • Existing rows stay in place

DROP TABLE

  • Removes the table and all its rows
  • Indexes and constraints go with it
  • Only a backup brings it back

ALTER when the shape changes, DROP only when the table is truly finished with.

The big ideas

What you need to know

Add columns safely

New columns get NULL in every existing row, or the DEFAULT you give them.

Rename with care

RENAME COLUMN is quick, but every query, report and app using the old name will break.

NOT NULL needs a default

Adding a NOT NULL column to a table with rows fails unless you also give a DEFAULT to fill them.

DROP is permanent

Use IF EXISTS to avoid errors, and take a backup first. CASCADE also drops views that depend on it.

See it in code

Walk through the code

sql
1ALTER TABLE students2  ADD COLUMN phone VARCHAR(15);3 4ALTER TABLE students5  ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT TRUE;6 7ALTER TABLE students8  RENAME COLUMN full_name TO student_name;9 10ALTER TABLE students11  ALTER COLUMN city SET DEFAULT 'Pune';12 13ALTER TABLE students14  DROP COLUMN phone;15 16DROP TABLE IF EXISTS old_students;

1/5A new nullable column. Existing students get NULL.

Common ALTER operations, then a DROP, in PostgreSQL. MySQL 8+ supports the same RENAME COLUMN and SET DEFAULT forms and uses MODIFY COLUMN to change a type. SQL Server renames with EXEC sp_rename and omits the COLUMN keyword in ADD.

Save yourself the pain

Common mistakes

Oops

Running ALTER TABLE ... ADD COLUMN x INT NOT NULL on a table that already has rows.

Do this instead

Add a DEFAULT in the same statement, or add the column as nullable, fill it, then set NOT NULL.

Oops

Dropping a table on the live database to 'start fresh'.

Do this instead

Use TRUNCATE to clear data, and keep DROP for tables you're sure nobody needs, after a backup.

Your mission

Evolve a table

~20 min

Quick check

You run ALTER TABLE students ALTER COLUMN city SET DEFAULT 'Pune'. What happens to students already stored with city 'Nagpur'?

Go deeper

  1. 1Add a new column with a default value to an existing table.
  2. 2Practise DROP TABLE only on a test database.
  3. 3Learn IF EXISTS to make scripts safe to re-run.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge