Browser-based SQL tool · your scripts are read here, never run

SQL Schema Diff

Paste or drop two CREATE TABLE scripts, the old schema on the left and the new on the right. You get a side-by-side list of what differs and an ALTER script that turns the first into the second, with warnings before anything that loses data.

A: before

Drop the old schema .sql

B: after

Drop the new schema .sql

SQL Schema Diff and ALTER Migration Generator

Operator: Abdulla Al Fahad. How tools are built and checked

Two schema files, one from the main branch and one from a feature branch, and a question that a plain text diff answers badly: what changed in the database? Column order moves, a VARCHAR(255) becomes character varying(255) after a dump, a default gains a ::text cast, and the real change, a unique constraint on two columns, is one line in 400. This page reads both scripts as schemas rather than as text. It lines tables up, compares columns by name, type, nullability and default, compares primary keys, unique constraints, foreign keys and indexes, and lists only real differences. Then it drafts the ALTER statements that turn the first schema into the second. To see one schema drawn as boxes and lines instead, the SQL to ER diagram tool on this site does that for a single script.

Key features

  • Tables, columns, primary keys, unique constraints, foreign keys and plain or partial indexes compared
  • Types and defaults normalised first, so int and integer, varchar and character varying, now() and CURRENT_TIMESTAMP, and a default with a ::text cast do not show as changes
  • Side-by-side difference table with a CSV download
  • PostgreSQL and MySQL migration scripts, ordered so foreign keys are dropped before tables and added after them
  • Warnings for dropped tables and columns, shrinking types, new NOT NULL, unique and foreign key constraints, and unnamed constraints
  • Rename hints when a removed and an added column or table look like the same object

What is compared, and how the migration is ordered

Comparison happens on normalised values. Types are lower-cased with spaces tidied and common synonyms folded, including serial to integer, bigserial to bigint, decimal to numeric and timestamptz to timestamp with time zone; integer display widths such as int(11) are ignored, but tinyint(1) is kept because it usually means boolean. Defaults lose outer parentheses and ::casts, NULL equals no default, now() equals CURRENT_TIMESTAMP, and 0.00 equals 0. Unique constraints and indexes are matched by their column lists, so renaming an index alone is not a difference. Foreign keys are matched by column, referenced table and referenced column, and flagged as changed only when ON DELETE or ON UPDATE differs.

The migration is ordered to keep every statement valid: drop foreign keys that were removed or changed, drop indexes, unique constraints and changed primary keys, drop columns, drop tables, create new tables, add and alter columns, add keys and indexes, and finally add foreign keys. For a type change PostgreSQL gets ALTER COLUMN ... TYPE ... USING col::type, and serial or bigserial are written as their integer types because serial is only shorthand for a sequence. MySQL gets MODIFY COLUMN with the full new definition, and bigserial becomes BIGINT with AUTO_INCREMENT. Types are copied as written in the script, with no translation between dialects. MySQL has no partial indexes, so an index with a WHERE clause is created as a full index with a comment saying so.

Warnings are attached to the statements that need them. Dropped tables and columns are marked destructive. A column whose length shrinks (VARCHAR(100) to VARCHAR(50)) is marked destructive; other type changes are cautions because values must convert. A new NOT NULL on an existing column, a new NOT NULL column with no default, a new unique constraint and a new foreign key all fail on data that does not satisfy them, so each gets a caution naming the check to make first. Constraint names that were not in the script are guessed from the database's defaults for PostgreSQL and left as a comment for MySQL foreign keys, where a guess would be wrong.

How it works

The CREATE TABLE parser is the one behind the ER diagram tool, so it reads PostgreSQL, MySQL and MariaDB, SQLite and SQL Server syntax: quoted identifiers, inline REFERENCES, table-level constraints, ALTER TABLE ... ADD and CREATE UNIQUE INDEX. A second small pass over the same tokens picks up what that parser skips, namely plain indexes (CREATE INDEX and MySQL KEY or INDEX lines, including a PostgreSQL WHERE clause) and the names of primary key, unique and foreign key constraints, which are needed to write DROP statements.

Common use cases

  • A feature branch changes 6 tables and the reviewer reads the difference table instead of a 400-line schema.sql diff: two added columns, one shrunk VARCHAR and a new unique constraint stand out at once.
  • A staging dump from pg_dump is compared with the schema.sql in the repository, and the three indexes that someone created by hand on staging show up as extra.

How to use this tool

  1. Paste or drop the old schema as A and the new one as B; dumps from pg_dump or mysqldump work, as do hand-written scripts.
  2. Press Compare schemas and read the difference table, including any rename hints.
  3. Pick PostgreSQL or MySQL; the migration is regenerated for the dialect you choose.
  4. Read the warnings above the migration, edit the script, and test it on a copy of the database.

Example

A and B (excerpt)

A: users(id SERIAL PK, email VARCHAR(255), name VARCHAR(100), age INT); legacy_notes(...)
B: users(id BIGSERIAL PK, email VARCHAR(320), name VARCHAR(50) NOT NULL DEFAULT 'anon', full_name VARCHAR(100), UNIQUE(full_name)); tags(...)

Differences and the start of the PostgreSQL migration

added column users.full_name; removed column users.age; changed id (type), email (type), name (type, nullability, default); added unique (full_name); removed table legacy_notes; added table tags.
ALTER TABLE "users" DROP COLUMN "age";
DROP TABLE "legacy_notes";
ALTER TABLE "users" ALTER COLUMN "name" TYPE VARCHAR(50) USING "name"::VARCHAR(50);
Warnings: DROP COLUMN users.age deletes data; users.name shrinks from VARCHAR(100) to VARCHAR(50); users.name becomes NOT NULL.

Common pitfalls

A rename is a drop plus an add here, because two scripts do not say that a column was renamed. The migration would delete the column's data, so replace such pairs with RENAME by hand; the hints point at likely candidates.

Only the DDL listed above is compared. Views, functions, triggers, sequences, check constraints, comments, enums, partitions and column collations are not, so a clean report is not a guarantee that two databases are identical.

Foreign keys that point at a table missing from the script cannot be resolved and are left out of the comparison; a note says how many.

The migration is a draft. It does not wrap statements in a transaction, batch large changes or take locks into account; for big tables, plan online schema change tools separately.

  • Give constraints explicit names in your schema files. The migration can then drop them by name instead of by a guessed default.
  • Run the migration's data-dependent steps (NOT NULL, unique, foreign key) against a copy with production-like data; the warnings tell you which ones to try first.

Common errors and how to fix them

"No CREATE TABLE statements were found in script B"
The script may be INSERT statements or only ALTER TABLE changes. Compare full schema scripts, for example from pg_dump --schema-only or mysqldump --no-data.
"1 "(" never closed, the first on line 1"
A script is cut off, often a CREATE TABLE copied without its closing parenthesis and semicolon. The comparison stops with the message rather than guessing, and says which script it came from.

Best practices

  • Commit the schema script next to the migrations, so a diff between two git revisions is always one paste away.
  • Treat destructive warnings as a checklist: for each one, write down where the data goes or why it is safe to lose.

Frequently asked questions

Does it connect to my database?

No. It reads two scripts you provide. To compare a live database, dump its schema with pg_dump --schema-only or mysqldump --no-data and paste the result.

Why does a serial column change appear when both say serial?

It does not if both say serial. A change appears when the underlying type differs, for example serial (integer) against bigserial (bigint), because the column has to be widened.