Adding a new column to a database table sounds simple. In production, it’s not. Schema changes can block queries, lock tables, and break downstream code. The wrong ALTER TABLE can take your system down for seconds—or hours.
The safest way to add a new column is to plan the type, constraints, defaults, and migration path before touching the database. Decide whether the column should allow NULLs during rollout to avoid failing writes. Use a default only if the database engine can backfill instantly, or migrate the data incrementally after the column exists.
For relational databases like PostgreSQL, MySQL, and MariaDB, the syntax is straightforward:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP NULL;
In reality, syntax is the easy part. The hard part is avoiding downtime and contention. For large tables, the LOCK behavior differs across engines and versions. PostgreSQL supports many column additions without a full table rewrite, but older MySQL releases may not. Always test the migration on a realistic dataset before deploying.