Adding a new column in a production database is simple in concept and dangerous in practice. The wrong change can lock tables, stall writes, or corrupt data. The right change is planned, tested, and deployed with precision.
In SQL, the basic syntax is:
ALTER TABLE table_name
ADD COLUMN column_name data_type [constraints];
On PostgreSQL, you can add a column instantly if it has no default and is nullable. If you set a default on a large table, it triggers a full table rewrite. The fix is to add the column without the default, then update rows in small batches, and finally set the default for new inserts.
In MySQL, ALTER TABLE operations may be blocking. For large tables in active systems, use tools like gh-ost or pt-online-schema-change to avoid downtime. Always check the engine’s capabilities—InnoDB online DDL can handle many changes without locking reads.