Introduction
A SQL dump is a backup of a database written as SQL statements in a text file. Run against an empty database, the statements create the tables again and insert every row. Because it is plain text, a dump can be read, searched, compared and moved between servers and versions, which the database’s own files cannot.
Dumps are made by tools such as mysqldump, pg_dump and the
.dump command of SQLite. Each database has its own dialect of SQL, so a
dump from one system usually needs changes before another will load it. The same
.sql extension is used for any file of SQL, such as hand-written queries
and migration scripts.
Structure
The file is text, usually UTF-8, made of statements that each end with a semicolon.
Comments start with -- or sit between /* and
*/. A typical dump has, in order:
- settings for the session, such as the character set, and a comment naming the tool and server version
- for each table,
CREATE TABLEwith its columns and keys - the rows, as
INSERT INTO … VALUES (…), (…);statements, or in PostgreSQL as aCOPY … FROM stdin;block of tab-separated lines ended by\. - indexes, constraints, views, triggers and other objects
Large dumps are often compressed, for example as .sql.gz. There is more
on this in What a dump is,
Statements and
Ending statements.
Dialects
The file has no header that names its database. A reader tells them apart by the comments at the top of the file and by how the SQL is written.
| Dialect | Written by | Names | Rows | Marks |
|---|---|---|---|---|
| MySQL, MariaDB | mysqldump, phpMyAdmin | `name` | INSERT, many rows to a statement | -- MySQL dump, /*!40101 … */, DELIMITER |
| PostgreSQL | pg_dump | "name" if needed | COPY blocks | -- PostgreSQL database dump, COPY … FROM stdin; |
| SQLite | sqlite3 .dump | as written | INSERT, a row to a statement | PRAGMA foreign_keys=OFF;, BEGIN TRANSACTION; |
| SQL Server | Management Studio | [name] | INSERT, a row to a statement | GO, SET ANSI_NULLS, [dbo] |
Hand-written scripts and migrations use the same .sql extension and follow
no fixed layout. The parts of a statement are described in
CREATE TABLE,
INSERT,
Literals,
Names and
Comments.
The viewer colours the text with the string rules of the dialect it finds. In MySQL and
MariaDB a backslash escapes in '…' and "…" strings, so
'a\'b' is one string; in PostgreSQL a backslash escapes only inside
E'…' strings (and everywhere if the file sets
standard_conforming_strings off); in standard SQL and SQLite it does not
escape at all, and only a doubled quote does. It reads the hints in the file: a
-- MySQL dump header, /*!40101 … */ comments,
ENGINE= or DELIMITER mean MySQL; a -- PostgreSQL database dump
header, COPY … FROM stdin;, plpgsql, E'…' strings or
dollar quotes mean PostgreSQL; NO_BACKSLASH_ESCAPES switches the escape off. With no
hint it uses whichever reading leaves every quote closed, and the standard one when both do. A
COPY … FROM stdin; block, up to its \. line, is data and is not coloured
as SQL, and a $$ … $$ or $tag$ … $tag$ body is one string colour, not SQL
(in a MySQL file $$ is a DELIMITER, not a quote).
Not described
Known, not yet covered
- Reference: the pages describe the SQL that dumps are made of (settings,
CREATE TABLE,INSERT,COPY, terminators, literals, names, comments). They do not describe SQL as a query language:SELECTwith joins, grouping, window functions and common table expressions,UPDATE,DELETEandMERGE, and the access statementsGRANTandREVOKE. The ISO SQL standard and each vendor’s manual describe them. - Reference: the procedural languages in routine and trigger bodies (PL/pgSQL, Transact-SQL, MySQL’s stored program syntax) are not described beyond how a dump ends their statements, and only four dialects are covered: MySQL and MariaDB, PostgreSQL, SQLite and SQL Server. Oracle and other databases (Db2, Firebird) have no page.
- Viewer: the dialect is guessed from hints in the file, so a file with none and a backslash string that closes either way is read as standard SQL. A dollar-quoted body is one string colour: the language named after
LANGUAGE(PL/pgSQL, SQL, Python) is not coloured inside it. The data ofCOPYis not split into columns, and aCOPYthat reads from a file or program has no data block to skip. - Viewer:
DELIMITERlines andGOare plain,U&'…'prefixes are plain, and placeholders (@param,:named,$1) and the::cast are not marked. The same words are coloured as keywords for every dialect. The file is not parsed and nothing is checked or run: there is no outline of tables or list of problems, and only the first 1 MiB is read and coloured.
Links
Software
- mysqldump – makes dumps of MySQL and MariaDB databases
- pg_dump – makes dumps of PostgreSQL databases, loaded again with psql
- phpMyAdmin – exports and imports MySQL dumps in a web browser (web)
- DBeaver – database tool that runs SQL scripts against many databases (Windows, macOS, Linux)
- Visual Studio Code – free code editor, for reading and editing dumps (Windows, macOS, Linux)