Adding a new column to a database table is simple in syntax but critical in impact. It changes data structure, indexes, performance, and application behavior. Done right, it powers new features. Done wrong, it breaks production.
The first decision: schema migration strategy. For relational databases like PostgreSQL or MySQL, the standard SQL syntax is:
ALTER TABLE table_name ADD COLUMN column_name data_type;
This works instantly for small tables. On large datasets, it can lock writes and block transactions. To reduce downtime, use online DDL or tools like pt-online-schema-change or gh-ost.
Next, define the correct data type. A wrong type leads to typecasting costs and bugs in application logic. Decide if the new column should allow NULL or have a default value. Defaults can backfill instantly or lazily, but beware of the overhead on hot tables.
Indexes are a double-edged sword. Adding an index to a new column can speed up queries but slow down writes. Test queries without indexes before committing them to production.