Adding a new column to a database table is simple in syntax but critical in impact. Schema changes touch production data, shape queries, and can shift application performance. Done without care, they lock tables and block writes. Done well, they expand capability without downtime.
In SQL, the ALTER TABLE command is the primary tool. A basic example in PostgreSQL:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This single line changes the table structure instantly. But the real work is planning. Consider column type, default values, nullability, and indexing. Defaults on large tables can rewrite every row, causing heavy load. Adding indexes immediately after column creation can block for minutes or hours. On live systems, these risks demand mitigation.
For large datasets, break the change into steps. Add the column with NULL allowed. Backfill data in batches. Then set the NOT NULL constraint and apply indexes. This staged approach reduces locks and keeps the system responsive.