You add a phone column to the database on your laptop, and the app runs fine. A teammate gets your latest code, and the app breaks on their laptop: there's no phone column. The code is the same, but your databases differ.
Adding a column, renaming one and making a new table are changes to a database's structure. Such a change needs to be written as a migration: ordered steps of change that are stored together with the code. That way every database gets the same changes in the same order and keeps its data: on your laptop, on your teammate's laptop, and on the production server (the server that real users use).
A migration is like renovating a house while people live in it. You don't destroy the house just to add one room. You add the room, and the family stays. Each renovation also goes into a logbook, so everyone knows which ones are done. In a database, the house is a table, the new room is a new column, and the family is the rows already there.
Why not DROP and CREATE again
On your laptop, the easiest way to change a table is DROP TABLE users; then a new CREATE TABLE users (...). But DROP TABLE deletes the table with every row in it. On the production server, that means all your users' accounts are gone. A migration changes the table's structure (its columns) while leaving its data (its rows) in place. The command for that is ALTER TABLE.
Add a column with ALTER TABLE
This is the users table from the exercise:
CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT);
INSERT INTO users VALUES (1, 'Budi', '[email protected]');
INSERT INTO users VALUES (2, 'Siti', '[email protected]');
Your app now needs phone numbers. Add the column, then insert a new user who has a phone number. Before you look at the result, guess: what will phone hold for Budi and Siti? An error, NULL, or empty text?
ALTER TABLE users ADD COLUMN phone TEXT;
INSERT INTO users (name, email, phone) VALUES ('Dewi', '[email protected]', '07700900123');
SELECT * FROM users;
| id | name | phone | |
|---|---|---|---|
| 1 | Budi | [email protected] | NULL |
| 2 | Siti | [email protected] | NULL |
| 3 | Dewi | [email protected] | 07700900123 |
ALTER TABLE usersmeans "change the structure of the users table".ADD COLUMN phone TEXTadds a text columnphoneat the end of the table.- The existing rows stay unchanged. In the new column, Budi and Siti get
NULL, the mark for "nothing here", not an error or empty text. - Order matters: an
INSERTthat namesphonebefore the column exists is refused with the errortable users has no column named phone.
What else ALTER TABLE can do
Other ALTER TABLE commands, with what they do in the comments:
ALTER TABLE users RENAME COLUMN phone TO phone_number; -- rename a column
ALTER TABLE users DROP COLUMN phone_number; -- delete a column and its contents
ALTER TABLE users RENAME TO members; -- rename the table
Each database's ALTER TABLE is a little different. What confuses people most is changing the type or rules of an existing column. A column's rules are its constraints, for example NOT NULL.
| Change | SQLite | MySQL | PostgreSQL |
|---|---|---|---|
| Add a column | ADD COLUMN |
ADD COLUMN |
ADD COLUMN |
| Rename a column | RENAME COLUMN a TO b |
RENAME COLUMN a TO b |
RENAME COLUMN a TO b |
| Delete a column | DROP COLUMN |
DROP COLUMN |
DROP COLUMN |
| Change a column's type or rules | not possible, rebuild the table | MODIFY COLUMN name VARCHAR(200) |
ALTER COLUMN name TYPE VARCHAR(200) |
Some limits in SQLite:
- SQLite has only supported
DROP COLUMNsince version 3.35 (2021), so older tutorials often say it can't. APRIMARY KEYcolumn, aUNIQUEcolumn and a column with an index still can't be dropped. ADD COLUMNrefuses aNOT NULLcolumn with noDEFAULT, with the errorCannot add a NOT NULL column with default value NULL. The reason: the old rows would holdNULL, andNOT NULLforbids that.ADD COLUMNalso refuses aUNIQUEcolumn:Cannot add a UNIQUE column.- MySQL's
MODIFY COLUMNis refused by SQLite withnear "MODIFY": syntax error.
Rebuilding a table in SQLite
Some changes can't be made with SQLite's ALTER TABLE, for example making email required. Instead, build a new table and move the data into it:
BEGIN TRANSACTION;
CREATE TABLE users_new (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL, phone TEXT);
INSERT INTO users_new (id, name, email, phone) SELECT id, name, email, phone FROM users;
DROP TABLE users;
ALTER TABLE users_new RENAME TO users;
COMMIT;
- Make a new table,
users_new, with the structure you want:emailis nowNOT NULL. - Copy every row from the old table with
INSERT INTO ... SELECT(anINSERTthat takes rows from the result of aSELECT). - Delete the old table.
- Rename the new table to the old name,
users.
The four steps are inside a transaction. So if one step fails, you can run ROLLBACK, and the old table comes back whole. DROP TABLE also deletes the old table's indexes and triggers (a trigger is a command that runs by itself when the table's contents change), so make both again afterwards. Laravel does these four steps for you when you change a column with ->change() on SQLite.
Migrations: changes that are recorded and in order
In a real project, each change is written as one migration file. The file's name starts with a date, so the order is clear. In Laravel, a migration file has two parts: up() to apply the change, down() to undo it. This is the migration file that adds the phone column:
// database/migrations/2026_10_02_000000_add_phone_to_users.php
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable(); // becomes ALTER TABLE users ADD COLUMN phone ...
});
}
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phone'); // becomes ALTER TABLE users DROP COLUMN phone
});
}
nullable() means the phone column may hold NULL.
php artisan migraterunsup()of every migration that hasn't run yet, in the order of their file names. The migrations that run together in one command are one batch (a group).- Laravel records every migration that has run in the
migrationstable: its file name and its batch number. Themigrationstable is the logbook from the house renovation above. A migration already recorded is never run twice. php artisan migrate:rollbackrunsdown()of the migrations in the last batch, then deletes their records from themigrationstable. Rollback here means undoing migrations, not a transaction'sROLLBACK.
⚠️ Steps in the wrong order, migrations edited later
INSERTbeforeALTER TABLE. The column doesn't exist yet, so theINSERTis refused. Change the structure first, then fill in the data.- Running
ADD COLUMNtwice. The result is the errorduplicate column name: phone(the name is already taken). That's why migrations are recorded in themigrationstable. - Forgetting that
down()can delete data. Rolling back theadd_phonemigration above runsDROP COLUMN phone, so every phone number goes with it. - Editing a migration file that already ran in production. A server that has recorded that migration will never run it again, so your edit never reaches the database on that server. Make a new migration file for the next change.
TL;DR: A migration changes a table's structure (its columns) without throwing its data away, with ALTER TABLE. ADD COLUMN phone TEXT adds a column, and the old rows get NULL. RENAME COLUMN renames a column, DROP COLUMN deletes a column and its contents. MySQL changes a column's type or rules with MODIFY COLUMN. PostgreSQL changes its type with ALTER COLUMN ... TYPE. SQLite can't change a column's type or rules with ALTER TABLE. It rebuilds the table inside a transaction: make a new table, copy the data, delete the old table, give the new one the old name. In a project, each change becomes an ordered migration file with up() (apply) and down() (undo), and the migrations table records which ones have run.
⚡ Don't DROP and CREATE
Meaning: DROP TABLE then CREATE TABLE again gives an empty table: every row is deleted. On a production server (the server that real users use), that means all your users' accounts are gone. A migration is a way to change a table's structure (its columns) while leaving its data where it is.
⚡ Add a column
Write:
ALTER TABLE users ADD COLUMN phone TEXT;
INSERT INTO users (name, email, phone) VALUES ('Dewi', '[email protected]', '07700900123');
Guess first: the users table already holds Budi and Siti. After the phone column is added, what does phone hold for them?
NULL. The existing rows stay unchanged, and the new column is filled with NULL, the sign for "nothing here".
Meaning: ALTER TABLE ... ADD COLUMN adds a column to an existing table. The order matters: change the structure first, then fill in the data. An INSERT that names phone before the column exists is refused.
⚡ Each database differs
Meaning: ADD COLUMN (add a column), RENAME COLUMN (rename a column) and DROP COLUMN (delete a column) work in SQLite, MySQL and PostgreSQL. Changing a column's type is where they differ. SQLite has no command for it. It rebuilds the table inside a transaction: make a new table, copy the data, delete the old table, then give the new one the old name.
⚡ Migrations are recorded and in order
Meaning: Each change is written as one migration file, whose name starts with a date so the order is clear. In Laravel (a PHP framework), that file has up() to apply the change and down() to undo it. The migrations table records which migrations have run, so none runs twice.