The query ran. The table was clean. But the product team wanted more data, and you needed it fast. You opened your editor, fingers ready to add a new column.
Adding a new column to a database table is straightforward, but the long-term impact depends on how you do it. The wrong choice can lock you into bad schema designs or force painful migrations later. The right approach keeps your application fast, reliable, and scalable.
First, decide if the new column belongs in the existing table. Storing it here means faster joins and simpler queries, but it can also bloat row size. For columns with infrequently accessed or optional data, consider a separate table linked by a foreign key.
For relational databases like PostgreSQL or MySQL, the basic SQL syntax is:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP WITH TIME ZONE;
Use explicit data types. Avoid TEXT or VARCHAR without lengths unless truly required. Name the column for clarity and avoid abbreviations. Set sensible defaults only when necessary; they can add overhead during table rewrites.