Adding a new column is simple if you plan for it. Done wrong, it locks production, slows queries, or corrupts data. The key is to design the migration so it works on live systems without downtime.
Start by defining the purpose of the new column. Is it a nullable string, a boolean flag, or a timestamp? Choose the smallest data type that fits the use case. Smaller types use less disk, reduce I/O, and improve cache efficiency.
In SQL, the basic syntax is:
ALTER TABLE orders ADD COLUMN shipped_at TIMESTAMP;
On large tables, run this in a way that avoids full table rewrites. Many databases now support ADD COLUMN as a metadata-only change when no default value is set. If a default is required, set it in application code for new rows, then backfill in small batches to avoid locking.
For PostgreSQL, use ADD COLUMN without NOT NULL, add NOT NULL in a second step after backfilling. For MySQL with InnoDB, check if your version supports instant DDL for column addition. For cloud-managed databases, review limits and execution plans before running the migration.