Adding a new column to a production database is simple in theory and dangerous in practice. It changes the schema, impacts queries, and can break code paths if not handled with precision. The right approach keeps systems up, migrations smooth, and performance unshaken.
When creating a new column, first define its purpose and constraints. Decide on NULL vs. NOT NULL. Set default values where needed. Consider indexing only if queries will filter or join on it. Columns without a clear usage plan bloat the schema and slow down writes.
For relational databases like PostgreSQL or MySQL, the basic syntax is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works, but production environments require more than a single command. Run migrations during low-traffic windows or use tools that apply changes online. For large tables, adding a new column with a default value can lock writes; instead, add it without the default, then backfill in batches.