Module 7 · Designing tables

Modify tables with ALTER TABLE

By the end you will be able to add a column while preserving the existing data.

About 12 min

0 of 52 lessons

Before you start, you need

Protect columns with constraints

Add a property without losing any rows

The shop needs a flag showing whether a product is active. The products are already stored: ALTER TABLE lets you change a table's structure without building another table from scratch.

This lesson uses ADD COLUMN to add a column. Give its name, its type and, if needed, its default value. active INTEGER DEFAULT 1 adds an integer flag whose initial value will be 1.

In these SQLite-style exercises, existing rows show the default 1, and new rows that leave out active get it too. Before adding a column, decide which value makes sense for the rows that already exist.

Then check the new column, the number of rows and the existing data. Adding a property must not delete products or change their names. If you require a value with NOT NULL, you also need to plan how the existing rows will meet that rule.

Minimal syntax
ALTER TABLE table_name
ADD COLUMN new_column type DEFAULT value;

See it in action

We want to add an active flag to the existing catalogue.

SQL
ALTER TABLE products
ADD COLUMN active INTEGER DEFAULT 1;

SELECT id, name, active
FROM products;
Example result: 8 of 9 rows
idnameactive
1Mouse1
2Keyboard1
3Monitor 241
4Pro Laptop1
5Headphones1
7Monitor 271
8SSD Drive1
9Cable USB-C1
  1. ALTER TABLE adds active. The SELECT checks the new property on the products that already existed.
  2. In these SQLite-style exercises, the default 1 appears on existing rows and on new rows that leave out active.
  3. The number of products and their existing properties stay the same. The check is not finished just because the new column appears.
  4. Adding a required column with no value for the existing rows can fail. Plan which initial value makes sense.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Add the active flag

Not started

Write the SQL statement that adds an INTEGER column called active to products, with a default value of 1.

We check the requested structure, the valid rows, the default values and that invalid rows are rejected.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Look at old and new rows

Not started

The prepared table notes has two rows. Add reviewed INTEGER NOT NULL DEFAULT 0, insert id 3 with text_value 'New' leaving out reviewed, then query id, text_value and reviewed sorted by id.

We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.

The table for this activity is already set up. Look at its data; you do not need to create it again.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Key takeaways

  • ALTER TABLE changes an existing structure.
  • ADD COLUMN adds a property.
  • Plan the value for the existing rows.
  • Check that the data has been kept.

Next: Add indexes for your lookups

Go deeper

Renaming or dropping columns and changing types depends on the engine and the existing constraints. Check dependencies and test the migration on a copy before running it on a real database.