Adding a new column should be simple, but in production systems, the details matter. Schema changes can lock tables, block queries, and cause downtime if not handled with care. Choosing the right approach depends on the database engine, data volume, and uptime requirements.
In SQL, the basic syntax is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works for most systems, but scale changes things. For small datasets, the column is added instantly. For large datasets, especially with millions of rows, adding a column with a default value can cause the database to rewrite the table, leading to long locks. Avoid defaults when adding at scale, or add them later in separate steps.
For PostgreSQL, adding a nullable column without a default is fast because it only updates the metadata. Adding a default value writes to every row and can be slow. In MySQL, ALTER TABLE rewrites the table in many cases, but with newer versions or engines like InnoDB, ALGORITHM=INPLACE can reduce the cost.