Adding a new column to an existing database table sounds simple, but the decision has ripple effects. It changes schema versions, affects queries, impacts indexing, and can alter application logic. The wrong move can lock tables, stall deployments, or corrupt data under load.
Start with the basics. In SQL, the ALTER TABLE statement is the standard way to add a new column:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works, but production environments demand more care. You must check for default values, null constraints, and data type compatibility. Adding a column with a default on large tables can rewrite every row, causing performance hits or downtime. Use nullable columns without defaults if you want a faster migration, then backfill asynchronously.
Indexing a new column is another strategic decision. Adding an index during migration can lock writes. Instead, add the column first, then create the index in a separate step, possibly using concurrent index creation if the database supports it.
If your system uses ORMs, update the schema definition in code to match the database change. Run migrations in staging before production. Confirm the new column appears in queries and that no downstream services break on the changed schema.