Adding a new column is one of the most common tasks when working with evolving databases. It changes the shape of your data, unlocks new use cases, and enables updated queries. Yet a simple schema change can break production if done without care.
In relational databases like PostgreSQL or MySQL, creating a new column starts with defining the type. Choose the smallest, most precise type that fits the data. This keeps storage lean and queries fast. For example:
ALTER TABLE orders
ADD COLUMN discount_percentage NUMERIC(5,2) DEFAULT 0;
Defaults matter. They prevent null values from breaking existing logic. Consider constraints like NOT NULL or foreign keys. Index only if the column will filter queries often, as unnecessary indexes slow writes.
For large tables, adding a column can lock the table and block writes. If downtime is unacceptable, use online migrations. Tools like pg_add_column in PostgreSQL, or altering in small batches for MySQL, reduce impact. Test in staging with realistic data volumes before touching production.