Creating a new column in a database is simple in syntax but high in impact. It changes schemas, reshapes queries, and affects every downstream process that touches the table. Done right, it improves performance and clarity. Done wrong, it creates technical debt that lasts years.
Use ALTER TABLE when you need to add a column without replacing the table. Example in SQL:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP DEFAULT NOW();
This command works in PostgreSQL, MySQL, and most relational databases with minor type adjustments. Always define the column type, constraints, and defaults in one operation. This reduces lock time and avoids partial states that break queries.
In NoSQL databases like MongoDB, adding a new column is conceptual. You add new fields to documents via an updateMany operation or by inserting new documents with the field present. The schema-on-read nature means the field may not exist on older data, so queries must handle null or undefined values.
Before adding a new column in production, audit query plans. Check indexes. A new column often needs indexing, especially if it will appear in WHERE clauses or joins. Adding an index after the column creation can be expensive for large datasets, so plan for it in the same migration.