Adding a new column is one of the most common operations in database management. Done wrong, it can bring down a service. Done right, it becomes a seamless extension of your data model. A precise approach prevents downtime, locks, and migration errors.
To create a new column in SQL, the ALTER TABLE command is your core tool. The syntax is straightforward:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This modifies the existing table in place. The column definition must match the intended data type and constraints. For large datasets, consider whether NULL values are acceptable or if you need a DEFAULT value.
Performance matters. Altering massive tables can block writes and reads, depending on the database engine. In MySQL, plan for operations that use ALGORITHM=INPLACE when possible. In PostgreSQL, simple column additions without defaults are fast, but adding default values for millions of rows can lock the table.