Adding a new column to a database is one of the most common and critical schema changes. Done well, it preserves uptime and data integrity. Done wrong, it locks queries, corrupts data, or triggers downtime during production hours.
The first step is to define the purpose of the new column. Decide its data type, nullability, and default value. For migrations in production, use a phased approach. Add the column with defaults or as nullable to avoid full table locks. Then backfill data in controlled batches. Finally, enforce constraints once the data is consistent.
In SQL, a simple example looks like:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
For large datasets, use online schema change tools or database-native features like gh-ost, pt-online-schema-change, or PostgreSQL’s ALTER TABLE ... ADD COLUMN in transactions that avoid heavy locks. Always measure the impact with query plans and monitor write latency.