Skip to main content

Drawdb-app - Render your database model as png, markdown, mermaid, ...

ยท 6 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Drawdb-app - Render your database model

In my RSS feed recently, I came across another great application that lets you create a database model from scratch, i.e. create tables one by one and create the links between them.

However, there's another option that I find really nice: you can get a visual of the tables and relationships of any existing application in a few seconds from an .sql file that you import into drawdb-app.

Let's take a quick look at how it works.

By opening the https://www.drawdb.app/editor website, the first thing you'll be prompted to do is to choose your target database format. For instance, let's choice PostgreSQL (Situation: I'm coding a Laravel application using PostgreSQL and I want to relations of my DB in a nice pic).

I think the most common use I could personally make of it is to load an SQL file. To do this, just click on the File menu, then choose Import from SQL and, finally, retrieve a previously created .sql file from your hard drive.

Create and import a dummy sql fileโ€‹

If you don't have such file, I suggest to copy one from https://www.sqltutorial.org/sql-sample-database/. I put here below a PostgreSQL content found on that site (direct link) or to take any valid .sql file you can found on the Internet.

dummy.sql
CREATE TABLE regions (
region_id SERIAL PRIMARY KEY,
region_name CHARACTER VARYING (25)
);

CREATE TABLE countries (
country_id CHARACTER (2) PRIMARY KEY,
country_name CHARACTER VARYING (40),
region_id INTEGER NOT NULL,
FOREIGN KEY (region_id) REFERENCES regions (region_id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE locations (
location_id SERIAL PRIMARY KEY,
street_address CHARACTER VARYING (40),
postal_code CHARACTER VARYING (12),
city CHARACTER VARYING (30) NOT NULL,
state_province CHARACTER VARYING (25),
country_id CHARACTER (2) NOT NULL,
FOREIGN KEY (country_id) REFERENCES countries (country_id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE departments (
department_id SERIAL PRIMARY KEY,
department_name CHARACTER VARYING (30) NOT NULL,
location_id INTEGER,
FOREIGN KEY (location_id) REFERENCES locations (location_id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE jobs (
job_id SERIAL PRIMARY KEY,
job_title CHARACTER VARYING (35) NOT NULL,
min_salary NUMERIC (8, 2),
max_salary NUMERIC (8, 2)
);

CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name CHARACTER VARYING (20),
last_name CHARACTER VARYING (25) NOT NULL,
email CHARACTER VARYING (100) NOT NULL,
phone_number CHARACTER VARYING (20),
hire_date DATE NOT NULL,
job_id INTEGER NOT NULL,
salary NUMERIC (8, 2) NOT NULL,
manager_id INTEGER,
department_id INTEGER,
FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (department_id) REFERENCES departments (department_id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (manager_id) REFERENCES employees (employee_id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE dependents (
dependent_id SERIAL PRIMARY KEY,
first_name CHARACTER VARYING (50) NOT NULL,
last_name CHARACTER VARYING (50) NOT NULL,
relationship CHARACTER VARYING (25) NOT NULL,
employee_id INTEGER NOT NULL,
FOREIGN KEY (employee_id) REFERENCES employees (employee_id) ON DELETE CASCADE ON UPDATE CASCADE
);

So just copy/paste this SQL in Notepad; save it on your disk and go back to https://www.drawdb.app/editor; File -> Import from SQL.

And let's the magic happens:

The model

The image here above as been done using the File -> Export as feature.

You can self host the application thanks to Docker if you wantโ€‹

By visiting https://hub.docker.com/search?q=drawdb-io, you'll find a few Docker images for drawdb-app. For instance https://hub.docker.com/r/xinsodev/drawdb.

To run it, just start docker run --name some-drawdb -p 81:80 -d xinsodev/drawdb and access the interface on http://localhost:81.

Playing with exportation featureโ€‹

From now, take time to check export features; click on the File menu again and click on Export as.

Export

It's really cool to export a SQL file so easily to JSON but it's even cooler to export as markdown.

The rest of this article was generated by drawDB.app; a really useful tool to help developers document their database!

Summaryโ€‹

  • Introduction
  • Database Type
  • Table Structure
    • regions
    • countries
    • locations
    • departments
    • jobs
    • employees
    • dependents
  • Relationships
  • Database Diagram

Introductionโ€‹

Database typeโ€‹

  • Database system: PostgreSQL

Table structureโ€‹

regionsโ€‹

NameTypeSettingsReferencesNote
region_idSERIAL๐Ÿ”‘ PK, not null
region_nameBLOBnot null

countriesโ€‹

NameTypeSettingsReferencesNote
country_idBLOB๐Ÿ”‘ PK, not null
country_nameBLOBnot null
region_idINTEGERnot nullcountries_region_id_fk

locationsโ€‹

NameTypeSettingsReferencesNote
location_idSERIAL๐Ÿ”‘ PK, not null
street_addressBLOBnot null
postal_codeBLOBnot null
cityBLOBnot null
state_provinceBLOBnot null
country_idBLOBnot nulllocations_country_id_fk

departmentsโ€‹

NameTypeSettingsReferencesNote
department_idSERIAL๐Ÿ”‘ PK, not null
department_nameBLOBnot null
location_idINTEGERnot nulldepartments_location_id_fk

jobsโ€‹

NameTypeSettingsReferencesNote
job_idSERIAL๐Ÿ”‘ PK, not null
job_titleBLOBnot null
min_salaryNUMERIC(8,2)not null
max_salaryNUMERIC(8,2)not null

employeesโ€‹

NameTypeSettingsReferencesNote
employee_idSERIAL๐Ÿ”‘ PK, not null
first_nameBLOBnot null
last_nameBLOBnot null
emailBLOBnot null
phone_numberBLOBnot null
hire_dateDATEnot null
job_idINTEGERnot nullemployees_job_id_fk
salaryNUMERIC(8,2)not null
manager_idINTEGERnot null
department_idINTEGERnot nullemployees_department_id_fk

dependentsโ€‹

NameTypeSettingsReferencesNote
dependent_idSERIAL๐Ÿ”‘ PK, not null
first_nameBLOBnot null
last_nameBLOBnot null
relationshipBLOBnot null
employee_idINTEGERnot nulldependents_employee_id_fk

Relationshipsโ€‹

  • countries to regions: many_to_one
  • locations to countries: many_to_one
  • departments to locations: many_to_one
  • employees to jobs: many_to_one
  • employees to departments: many_to_one
  • dependents to employees: many_to_one

Database Diagramโ€‹