SQL ยท Chapter 30 of 42

ALTER TABLE

ALTER TABLE changes an existing table's schema: add/drop columns, rename, change type, add constraints.

On big tables, altering columns can lock the table โ€” check your DB's docs.

Example 1 (sql)
ALTER TABLE users
  ADD COLUMN phone VARCHAR(20);
Output
Column added

Add a new column.

Example 2 (sql)
ALTER TABLE users
  DROP COLUMN phone;
Output
Column dropped

Remove a column (irreversible!).

Key points

  • ADD COLUMN adds a column.
  • DROP COLUMN removes a column (data lost).
  • RENAME COLUMN renames it.
  • ALTER COLUMN changes type or nullability.
๐Ÿ’ก Note: In production, prefer additive migrations (add nullable column, backfill, then enforce NOT NULL) over destructive ones.

๐Ÿ“ Quick Quiz

1. Which adds a new column?

2. DROP COLUMN:

3. Altering a big column can: