Adding a new column is one of the most common database schema changes. Done well, it is fast, safe, and repeatable. Done poorly, it locks tables, breaks queries, and stalls deploys. A new column should be more than a quick ALTER TABLE—it should be part of a migration strategy that keeps data reliable and services up.
Use migrations over ad-hoc changes. In SQL, the standard syntax to add a column is:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works, but it can trigger heavy locks on large tables. For production systems, run the change in small steps:
- Add the column as nullable with no default.
- Backfill data in batches.
- Apply constraints and indexes after the table is populated.
If you need a new column with a default value, use explicit UPDATE statements instead of adding the default at creation time on large datasets. This reduces transaction size and downtime.