

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Creating a Table
incomplete
2: Create Table Practice
incomplete
3: Altering Tables
incomplete
4: Intro to Migrations
incomplete
5: Up Migration
incomplete
6: Down Migration
incomplete
7: Migration Review
incomplete
8: SQL Data Types
incomplete
9: Practice – Posts Table
incomplete
10: Practice – Posts Table Migration
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
We often need to alter our database schema without deleting it and re-creating it. Imagine if Twitter deleted its database each time it needed to add a feature, that would be a disaster! Your account and all your tweets would be wiped out on a daily basis.
Instead, we can use the ALTER TABLE statement to make changes in place without deleting any data.
With SQLite an ALTER TABLE statement allows you to:
ALTER TABLE employees
RENAME TO contractors;
ALTER TABLE contractors
RENAME COLUMN salary TO invoice;
ALTER TABLE contractors
ADD COLUMN job_title TEXT;
ALTER TABLE contractors
DROP COLUMN is_manager;
Unlike some SQL databases, SQLite does not support performing multiple operations (like adding multiple columns) in a single ALTER TABLE statement. Each change must be made in a separate ALTER TABLE command.
We need to make some changes to the people table! At the moment, we have these six columns (shown as rows, so we can display datatypes):
| CID | NAME | TYPE | NOTNULL | DFLT VALUE | PK |
|---|---|---|---|---|---|
| 0 | id | INTEGER | 0 | 0 | |
| 1 | tag | TEXT | 0 | 0 | |
| 2 | name | TEXT | 0 | 0 | |
| 3 | age | INTEGER | 0 | 0 | |
| 4 | balance | REAL | 0 | 0 | |
| 5 | is_admin | BOOLEAN | 0 | 0 |