Lesson 26 of 30
87%
ALTER TABLE
ALTER TABLE changes a table that already exists.
You can add a column, drop a column, or change a type.
Add a column
sql
Result
Query OK, 0 rows affected
Change a column
MODIFY changes the type. Old values must still fit.
sql
Result
Query OK, 0 rows affected
Rename a column
sql
Result
Query OK, 0 rows affected
| Field |
|---|
| id |
| name |
| age |
| hometown |
4 rows
Drop a column
The values in that column disappear.
sql
Result
Query OK, 0 rows affected
Be gentle
ALTER can lock a busy table for a moment.
On production, plan the change. On homework, just try it.
Tip: Add new columns as NULL first. Fill them. Then add NOT NULL if you need it.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.