Course contentsShow
← Databases & SQL
Lesson 42 of 1,9531. Relational FoundationsFree lesson

Adding and Modifying Schema

Adapt existing table structures over time using non-destructive migration strategies.

In this lesson, you will learn how to evolve your database structure safely by modifying existing tables without disrupting the data you’ve already worked so hard to store.

Evolving Your Schema

In real-world applications, your requirements change. You might start with a simple users table that only tracks username, but later realize you need to store an email address. Instead of deleting the table and starting over—which would destroy your existing data—you use schema modification commands to "grow" the table alongside your needs.

Adding New Columns

To add a new attribute, we use the ALTER TABLE command. When adding a column to a table that already contains rows, the database needs to know what to put in that new column for existing records. We usually provide a default value:

ALTER TABLE users 
ADD COLUMN email VARCHAR(255) DEFAULT 'pending@example.com';

By providing a DEFAULT, you ensure that every existing row stays valid and populated, preventing errors when your application code tries to read that new field.

Modifying Existing Columns

Sometimes, the initial design needs a tweak. Perhaps a column meant for short strings isn't large enough. You can modify the data type of an existing column, but you must be careful: if you try to shrink a column (like changing a VARCHAR(100) to a VARCHAR(10)), the database will reject the change if your data is too long to fit. Always ensure your "migration" matches the reality of the data currently sitting in your rows.

The Non-Destructive Mindset

The golden rule of database evolution is non-destructive migration. Never perform an operation that would require dropping a table if you can simply alter it. If you need to rename a column, many systems support a rename command that preserves the underlying data. If you need to change a column's structure entirely, consider adding the new column, migrating the data from the old column using an UPDATE statement, and only removing the old column once you are certain the new one is functioning as expected.

Key Takeaway: Evolving your schema is about extending functionality while preserving existing records; always use incremental ALTER commands to ensure your data stays intact as your design matures.