Adding a new column in a database is one of the most common schema changes. It can be simple, but in high-traffic systems, even small changes carry risk. Poor execution can lock tables, slow queries, or break downstream processes. The goal is to add the column fast, safe, and without downtime.
First, define the column with precision. Decide on the name, type, nullability, and default value. Avoid vague names. Consider indexing only if queries will filter or join on it. Unnecessary indexes on new columns add write overhead.
In PostgreSQL, a basic command looks like:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
For MySQL:
ALTER TABLE users ADD COLUMN last_login DATETIME;
On large tables, such commands can lock writes. To avoid this, use tools like pg_online_schema_change for PostgreSQL or gh-ost for MySQL. They create the new column in a shadow table and migrate data without blocking production traffic.