Adding a new column is one of the most common changes in database design, yet it can bring risk if done without care. It changes the shape of your data. It can affect queries, indexes, and application logic. The right approach keeps downtime low and data intact.
In SQL, the basic syntax is simple:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This command appends the last_login column to the users table. But there is more to consider. Should the column allow nulls? Does it need a default value? Will the column be part of a composite index or used in foreign keys? These decisions affect performance and integrity.
For MySQL and PostgreSQL, ALTER TABLE locks can vary. On large datasets, a blocking operation can stall production. Newer versions support non-blocking ADD COLUMN in specific scenarios, but you must check the documentation for your engine. For PostgreSQL, adding a column with a constant default before 11 rewrites the table. In 11 and later, it avoids the rewrite.