What Is a Schema Migration? A Beginner's Explanation
Once you move from practice databases to a real project, you will hear about "migrations". The word sounds like moving house, but here it usually means something smaller: a recorded, repeatable change to the structure of your database. This post explains the idea for beginners.
The problem migrations solve
Imagine you build an app with a members table. A month later you need a phone column. On your laptop, you type:
ALTER TABLE members ADD COLUMN phone TEXT;
It works. But your classmate's copy of the database does not have the column, nor does the test server, nor the live app. Nobody remembers exactly what you typed. Soon every copy of the database is slightly different.
The idea
A schema migration is a small file that describes one change to the database structure. The files are numbered or dated, kept alongside your code, and applied in order. Every copy of the database runs the same files, so they all end up the same.
migrations/
001_create_members.sql
002_add_phone_to_members.sql
003_create_meetings.sql
The database usually keeps a small table recording which migrations have already been applied, so each runs only once.
A tiny example
002_add_phone_to_members.sql:
ALTER TABLE members ADD COLUMN phone TEXT;
Some tools also ask for a matching "down" step that undoes the change, useful if you need to go back.
Tools that do this for you
You do not need to write the bookkeeping yourself. Many frameworks include migration tools:
| Framework or tool | Language |
|---|---|
| Django migrations | Python |
| Alembic (with SQLAlchemy) | Python |
| Rails Active Record migrations | Ruby |
| Entity Framework Core migrations | C# |
| Flyway, Liquibase | Java and others |
Your framework's tutorial will show you its commands, typically something like "make a migration" and "apply migrations".
Good beginner habits
- Never edit a migration that others have already run. Write a new one instead.
- Keep migrations small. One change per file is easier to understand and to undo.
- Commit migrations with the code that needs them. Then code and database stay in step.
- Try them on a copy first. Run new migrations on your practice database before anywhere else.
Migrations versus "moving data"
"Migration" is also used for moving a whole database from one system to another, for example from MySQL to PostgreSQL. That is a much bigger job with its own planning. For a beginner, schema migrations are the everyday meaning.
Why it is worth learning early
Migrations turn database changes into something you can review, repeat and share, just like code. Learning them on a small project makes team projects much smoother later. If you are just starting with tables, read designing your first relational database first.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Get new pages by email