Adding a new column should be precise, fast, and safe. Done well, it expands your data model without breaking production. Done wrong, it triggers downtime, performance hits, or silent bugs.
In SQL, adding a new column is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This command works in PostgreSQL, MySQL, and most relational databases. But the simple syntax hides critical details. You must consider:
- Default values: Avoid slow table rewrites on large datasets.
- Null behavior: Decide if existing rows can be null, or backfill them.
- Indexing: Add indexes only after verifying usage patterns.
- Locking: Understand whether the change takes an exclusive lock or uses online DDL.
For PostgreSQL, using ADD COLUMN with a constant default before Postgres 11 can rewrite the table. On massive tables, this is dangerous. For MySQL, use ALGORITHM=INPLACE if supported to minimize locking.