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 addedAdd a new column.
Example 2 (sql)
ALTER TABLE users
DROP COLUMN phone;Output
Column droppedRemove 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.
