Adding a new column sounds simple. In practice, it demands precision. In SQL, ALTER TABLE is the command. It appends a field to an existing schema without losing existing data. You define the column name, data type, and constraints. For example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP DEFAULT NOW();
This statement creates a new column, sets its type, and applies a default value for every future row. In relational databases, selecting the right data type prevents wasted space and avoids costly type conversions later.
When adding a column to a large table in production, timing matters. DDL changes can lock writes, block reads, or trigger full table rewrites depending on the database engine. PostgreSQL can add certain columns with defaults in constant time from version 11 onward. MySQL may rebuild the table. Always check the documentation for your specific version.
Backfilling a column is separate from creating it. Adding the column with NULL as the default avoids heavy locking during the change. Then, update data in controlled batches to prevent load spikes. Use an indexed column only when queries demand it; each index slows down inserts and updates.