Adding a new column is one of the most common schema changes in relational databases. It sounds simple, but done without care, it can block writes, lock tables, or break existing queries. The right approach is precise and fast.
In SQL, the syntax is straightforward:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This adds a new column to the users table without removing or altering any existing data. But details matter. Defining nullability, default values, and data types at the start avoids migrations later. For large datasets, adding a column with a default non-null value can force a full table rewrite. In high-throughput systems, that can cause outages.
For PostgreSQL, adding a nullable column is usually fast. Adding a column with DEFAULT and NOT NULL can be expensive because every row must be updated. The solution is often to add the column nullable, populate it in batches, then enforce NOT NULL. MySQL behaves differently. Depending on the storage engine, adding a column may still require a full table copy. In production, that means planning around downtime or using online DDL tools.