Adding a new column to a database table is simple, but doing it right avoids broken queries, corrupted data, and downtime. Whether you are scaling a product, refactoring schemas, or supporting new features, the process must be precise.
The fastest way to add a new column is with the ALTER TABLE command:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This works on most relational databases, but each engine handles schema changes differently. In MySQL, adding a new column locks the table; in PostgreSQL, adding a nullable column without a default is instant. For large tables, that difference matters.
Before creating a new column, define its data type with care. Match it to the smallest type that holds the intended values. Set NOT NULL only if you can backfill every row. Use DEFAULT sparingly, as it can force a table rewrite.
For production systems, add columns in a safe migration process. Use transactional DDL where supported. In PostgreSQL, a migration adding a nullable column is quick and safe. In MySQL with InnoDB, consider ghost migrations with tools like pt-online-schema-change for zero downtime.