Adding a new column to a database table should be simple, but in production the smallest change can ripple into outages, broken APIs, and corrupted data. The process demands precision, speed, and zero downtime.
A new column in SQL starts with ALTER TABLE. The syntax is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works in MySQL, PostgreSQL, and other relational systems with minor differences. Always define the column type, set NULL or NOT NULL, and decide if a default value is needed. Defaults avoid breakage in existing code that reads or writes the table before the new column is fully supported in the application layer.
For large datasets, adding a new column can lock the table. On high-traffic systems, this means blocking reads and writes. Use online schema change tools like gh-ost or pg_repack to keep queries flowing while the schema updates. If your database supports it, leverage ADD COLUMN IF NOT EXISTS to skip redundant changes.