Adding a new column to a database table is simple in theory yet risky in production. One bad command can lock writes, break code paths, or cause cascading failures. In high-traffic systems, schema changes need precision.
In SQL, the basic syntax is direct:
ALTER TABLE table_name
ADD COLUMN column_name data_type [constraints];
This works for most relational databases—MySQL, PostgreSQL, MariaDB, and more. The core considerations are the same: data type, nullability, default values, and indexing. Choosing the wrong type leads to wasted storage or future migrations. Setting NOT NULL without a default will fail if rows already exist.
In PostgreSQL, adding a nullable column without a default is near-instant. Adding a column with a constant default rewrites the table—costly on large datasets. MySQL behaves differently: it usually rewrites the table regardless, though ALGORITHM=INPLACE can help in certain cases.