Adding a new column should be precise, fast, and safe. In SQL, the ALTER TABLE command is the standard way to extend a table’s schema. The syntax is simple:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This tells the database to add a column named last_login with the TIMESTAMP type. Most relational databases—PostgreSQL, MySQL, and MariaDB—support this pattern. The details matter. Choose column types that match how the data will be used. Set constraints to protect integrity. Avoid adding columns inside large transactional locks unless necessary, as this may block reads or writes.
When adding a new column with a DEFAULT value in PostgreSQL, older versions rewrote the entire table, impacting performance. Newer versions optimize this to be near-instant. In MySQL, adding a column near the end of the table definition may be faster than inserting it between existing columns. Always run schema changes in a staging environment before production.
If you need a new column that must be non-null, first create it as nullable, backfill it with correct values, then update the schema to enforce NOT NULL. This avoids downtime and deadlocks in production environments.