Adding a new column to a database table should be simple. In production systems, it can be the difference between a clean release and a cascading failure. The decisions you make—data type, nullability, default values, constraints—carry real consequences for performance, integrity, and migration speed.
A new column in SQL starts with the ALTER TABLE command. This is the core:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Choosing the right data type matters. Use INTEGER or BIGINT for counters, TEXT for unstructured strings, and TIMESTAMP or DATETIME for tracking events. Avoid overly generic types. They waste space and can lock you into slower queries.
Null constraints define if the new column can be empty. In legacy tables with large row counts, adding a NOT NULL column with a default can lock writes for seconds—or minutes—depending on your engine and version.
On PostgreSQL, adding a NULL column is instant. Setting a default writes new values to every row. For MySQL, adding a column often requires rewriting the table, unless you use online DDL in InnoDB with ALGORITHM=INPLACE.