DEV Community

Cover image for Relational Database Design with DBML
Ernesto Herrera Salinas
Ernesto Herrera Salinas

Posted on

Relational Database Design with DBML

Introduction

In today's data-driven world, databases are crucial in managing and storing data. As data becomes more complex and the demand for efficient data management increases, developers need tools to help them manage databases more efficiently. One such tool is DBML.

What is DBML?

DBML, or Database Markup Language, is an open-source language that simplifies database schema management. It provides a simple way to describe database schema using a human-readable syntax that resembles a programming language. Huy Nguyen created DBML, and it is available on GitHub for free.

With DBML, developers can define their database schema using a declarative syntax, which makes it easy to understand and manage. DBML supports various database engines, including MySQL, PostgreSQL, SQLite, and SQL Server.

One of the main benefits of using DBML is that it can save a lot of time and effort when managing database schema. Instead of manually creating and managing database schema files, developers can use DBML to create a single source of truth for their database schema.

DBML also provides various features that make it easy to work with databases, including automatically generating SQL scripts and importing and exporting schema files. DBML can also be integrated with tools and frameworks, including Visual Studio Code and Entity Framework.

To start with DBML, developers must create a file with the ".dbml" extension and define their database schema using the DBML syntax. DBML supports various database objects, including tables, views, indexes, and foreign keys.

Here's an example of a simple DBML schema:

Table users {
  id int [pk, increment]
  username varchar [unique]
  email varchar [unique]
  password varchar
}

Table posts {
  id int [pk, increment]
  title varchar
  content text
  created_at datetime [default: `now()`]
  updated_at datetime
  user_id int [ref: > users.id]
}

Table tags {
  id int [pk, increment]
  name varchar [unique]
}

Table post_tags {
  post_id int [ref: > posts.id]
  tag_id int [ref: > tags.id]
}

Table comments {
  id int [pk, increment]
  content text
  created_at datetime [default: `now()`]
  updated_at datetime
  user_id int [ref: > users.id]
  post_id int [ref: > posts.id]
}
Enter fullscreen mode Exit fullscreen mode

From this schema we can generate this diagram:

Diagram Generated with dbdiagram.io

dbdiagram.io

Just like that, it's even more cool, because we can generate this live, the first time I found out about DBML I found this site dbdiagra.io because I was looking for a tool to diagram relational databases. Here I recorded a quick demo of the site:

dbdiagram.io Demo

Top comments (0)