Before you start, you need
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.
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.
ALTER TABLE products
ADD COLUMN active INTEGER DEFAULT 1;
SELECT id, name, active
FROM products;| id | name | active |
|---|---|---|
| 1 | Mouse | 1 |
| 2 | Keyboard | 1 |
| 3 | Monitor 24 | 1 |
| 4 | Pro Laptop | 1 |
| 5 | Headphones | 1 |
| 7 | Monitor 27 | 1 |
| 8 | SSD Drive | 1 |
| 9 | Cable USB-C | 1 |
- ALTER TABLE adds active. The SELECT checks the new property on the products that already existed.
- In these SQLite-style exercises, the default 1 appears on existing rows and on new rows that leave out active.
- The number of products and their existing properties stay the same. The check is not finished just because the new column appears.
- 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
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.
Add a property with an initial value without deleting the existing products.
Declare the INTEGER type.
Add DEFAULT 1.
Solution explained
ALTER TABLE products ADD COLUMN active INTEGER DEFAULT 1;In these SQLite-style exercises, existing rows and new rows that leave out active both read the default 1.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Look at old and new rows
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.
The new property has a default and requires a value.
Leave reviewed out of the INSERT to check the DEFAULT.
The final SELECT should check all three rows.
Solution explained
ALTER TABLE notes ADD COLUMN reviewed INTEGER NOT NULL DEFAULT 0; INSERT INTO notes (id,text_value) VALUES (3,'New'); SELECT id,text_value,reviewed FROM notes ORDER BY id;The two existing rows and the new one all have reviewed 0. The original notes are kept.
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.
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.