Adding a new column is simple in theory, but in production systems, every detail matters. A single mistake can cause data loss, downtime, or broken queries. In modern databases, the process for adding a new column depends on the engine, the size of the table, and the constraints in place.
In SQL, the common path is straightforward:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This statement works for many relational databases, including PostgreSQL and MySQL. But the execution cost can vary. Large tables may require a table rewrite. In PostgreSQL, adding a column with a default value before version 11 would lock the table. Now, it stores the default in metadata, making the operation faster. In MySQL, ALTER TABLE often blocks writes unless you use ALGORITHM=INPLACE or ALGORITHM=INSTANT in newer versions.
When defining a new column, decide if it should allow NULLs, if it needs a default, and whether it requires an index. Avoid unnecessary indexes during an initial schema change in high-traffic systems; add them after verifying performance. For systems using ORMs, ensure the migration script matches the generated model.