Adding a new column is one of the most common database changes. It should be fast, predictable, and safe. Yet in production, schema changes can break queries, block writes, or cause downtime if done carelessly. The right approach depends on your database, the size of your data, and the constraints you need.
In SQL, the basic syntax is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works for small datasets or non-critical systems. But in high-traffic applications, an ALTER TABLE can lock the table, blocking reads or writes until the change is complete. On large tables, that can mean minutes or hours of impact.
To add a new column without downtime, use methods designed for online schema changes. MySQL offers tools like pt-online-schema-change or gh-ost that create a shadow table, apply changes, and swap it in with minimal lock time. PostgreSQL can often add nullable columns instantly, but adding columns with defaults before version 11 rewrites the whole table. Modern versions handle defaults more efficiently, but you should still measure the impact before deploying.