Back to all posts

ERD Diagrams | MySQL Database Design and Docs with DbSchema

For developers and DBAs who change an existing MySQL database, and want to design each change on a diagram and read its SQL before it runs.

On this page

At the end of this guide, your MySQL schema is a DbSchema model: a diagram of its tables, saved as one file in Git. You change the design on the diagram, read the ALTER TABLE statements that DbSchema writes, and only then run them in MySQL. The same model exports HTML5 documentation of the schema.

Connecting and importing are free in the Community Edition. Saving the model and synchronizing it need DbSchema Pro. A new installation of DbSchema starts with a 15-day trial that includes them.

1. Connect DbSchema to MySQL and import the schema

To follow along on a scratch database, create two tables:

CREATE TABLE customers (
  customer_id INT PRIMARY KEY,
  name        VARCHAR(100) NOT NULL
);

CREATE TABLE orders (
  order_id    INT PRIMARY KEY,
  customer_id INT NOT NULL,
  order_date  DATE NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);

Then import them into DbSchema:

  1. Choose File → New Model, and keep Connect to Database selected.
  2. Click MySQL, choose New Connection, and press Connect to MySQL.
  3. Choose Standard as the Method. For a server on your own computer, choose This computer, default port. For any other server, choose Remote computer or custom port, and type the Server Host and Port.
  4. Type the Database User and Password, choose the Database, and press Connect. The first time, DbSchema downloads the MySQL JDBC driver.
  5. In the Reverse Engineer the Database dialog, tick the schema and press Load.

The DbSchema Connect to MySQL dialog with Method Standard, This computer, default port, the user root and the database shop

DbSchema draws each table as a box and each foreign key as a line, here from orders to customers. Connecting and importing only read from MySQL (Connect to Database). The MySQL ER diagram guide explains each line end, and why a MyISAM table has no lines.

The tables customers and orders that DbSchema imported, joined by the foreign key line from orders.customer_id to customers.customer_id

2. Save the model and commit it to Git

DbSchema keeps the imported schema in a model: its own copy of the tables, with the layout of the diagram. A change to the model reaches MySQL only when you synchronize the model with MySQL, in step 4.

  1. Choose File → Save Model to File, and save the .dbs file in the folder of a Git repository.
  2. Choose File → Git Collaboration.
  3. Under Uncommitted changes, tick the .dbs file, type a Commit message, and press Commit.
  4. Press Push to send the commit to the shared repository.

The Uncommitted changes panel of the DbSchema Git dialog, with shop.dbs ticked, a commit message and the Commit (1) button

If the folder holds no repository yet, the Git dialog says No repository found. Choose More → Clone in that dialog, then save the model into the cloned folder (Git Collaboration). GitHub and Bitbucket refuse your account password there, so type an access token instead.

3. Go offline and change the design

Go offline before you change anything. While DbSchema is connected, every change that you confirm with OK runs in MySQL at once.

  1. Choose Database → Offline (connect later). The connection button in the toolbar now reads Offline.
  2. Double-click the header of orders on the diagram.
  3. In the Columns tab, press Add. Name the column status, choose varchar, type 20 as the Length, and press OK.
  4. Press OK in the table dialog, then save the model.

The new column now exists in the model only. To rename a column, rename it in DbSchema rather than drop it and add another. DbSchema remembers the column's previous name in the model. Synchronization then renames the column in MySQL, and the column keeps its data (Schema Synchronization).

4. Compare the model with MySQL and run the script

  1. Click the connection button and choose your MySQL connection.
  2. DbSchema opens the Connected to the Database dialog. Choose Compare, then decide difference by difference.
  3. In the Synchronize with the Database dialog, press Compare.
  4. In the Synchronize Model and Database dialog, find the row of status. In its Action column, click the right-hand button, Create, which writes the column into the database.
  5. Select the row. The lower pane shows the ALTER TABLE statement that DbSchema will run.
  6. Press Commit in the database. DbSchema shows each statement in turn: press Execute to run it. To have someone review the script first, press Generate SQL script instead, and send the script to DbSchema's SQL Editor or to a file.
  7. Save the model, so that the file matches MySQL.

The DbSchema Synchronize Model and Database dialog with three differences, each set to the model or to the database, and the ALTER TABLE statement of the selected row in the lower pane

Read the script before it runs. MySQL can't roll back an ALTER TABLE (Statements That Cannot Be Rolled Back). On MySQL 9.1, a column added inside a transaction stays after the ROLLBACK:

START TRANSACTION;
ALTER TABLE orders ADD note VARCHAR(50);
ROLLBACK;
SHOW COLUMNS FROM orders LIKE 'note';
FieldTypeNullKeyDefaultExtra
notevarchar(50)YES NULL

5. Export the documentation and commit again

  1. Choose File → Export As... → HTML5/PDF Documentation, or press Ctrl+D.
  2. Type a Title, choose HTML5 under Format, type the File Path, and press Generate.
  3. In the Git dialog, commit the model with a message such as "Add orders.status", and press Push.

HTML5 documentation that DbSchema generated from a MySQL schema, open in a browser, with a column comment shown on mouse-over

The HTML5 file opens in any browser, without DbSchema. It holds the diagram, a search box, and a card for each table with its columns, indexes and foreign keys. The descriptions in the HTML5 file are the table and column comments that MySQL stores. The Community Edition documents a diagram of up to 12 tables, and DbSchema Pro any diagram. How to document a MySQL database shows how to write the comments.

The result

The model file in Git now matches MySQL, and so does the documentation. In the Git dialog, a teammate presses Model History to see which tables each commit changed. If someone changes MySQL outside DbSchema, choose Database → Refresh Model from Database before your next change. DbSchema shows how many differences it found, and Review Differences lists each one.

Download DbSchema, connect it to your MySQL server, and design your next change offline. Connecting, importing and the diagrams are free in the Community Edition. Saving the model, which Git needs, and synchronization are in DbSchema Pro, which the 15-day Architect trial includes.

DbSchema Database Designer