
MySQL vs MongoDB - Visual Design with Real Data Models
When you’re starting learning databases, one of the first questions is: What are the differences between MySQL and MongoDB? They’re both interesting, but built for different purposes.
- MySQL is structured, relational, and based on SQL.
- MongoDB is flexible, document-based, and schema-less by default.
To really understand the difference, it helps to model something familiar, like how you store data in countries and cities.
Let’s see how you can design this model in both MySQL and MongoDB, and how you can do it visually - using the DbSchema tool.
DbSchema is not a database itself. It’s a visual tool that connects to your MySQL or MongoDB server and works directly with your database. Once connected, it shows you the real structure of your data - the tables or collections, the columns or fields, and even the relationships between them. You can explore the data, edit the schema, and understand your database more clearly, all in one place.
Key Differences: MySQL Tables vs MongoDB Documents
Feature | MySQL | MongoDB |
---|---|---|
Data Format | Tables (rows and columns) | Documents (JSON-like) |
Schema | Fixed and strict | Flexible (optional validation) |
Relationships | Foreign keys | Embedded or referenced |
Query Language | SQL | MongoDB Query Language |
Best For | Structured data (e.g., bank accounts) | Flexible data (e.g., user profiles) |
Relational Modeling: MySQL Example
In MySQL, you define the structure first: the tables and columns, and then insert the data to match that structure.
Relationships are defined using foreign keys.
Schema Design for Countries and Cities
We’ll define:
- A
Countries
table: each row is a country. - A
Cities
table: each city belongs to a country viacountry_id
.
SQL Example
|
This structure allows you to:
- Normalize your data. (Learn more about this here)
- Link each city to its country.
- Enforce data integrity using foreign keys.
When you create a table using code or the command line, it works - but you can’t actually see its structure. Most developers use tools like MySQL Shell or MongoDB Shell, which show data, not design.
In DbSchema
Method I:
With DbSchema, you get a visual diagram of your tables, so you can see how everything is built and connected.
- Write the code in the SQL Editor.
- See the table created in a visual diagram.
Method II:
If you’re not a developer, and you don’t know SQL Code, don’t worry! Instead of writing SQL, you can:
- Add two tables visually.
- Define columns using dropdowns for types.
- Draw a line between
Cities.country_id
andCountries.id
to create a foreign key. - DbSchema automatically generates the SQL script in the background and can deploy it to your database.
Document Modeling: MongoDB Example
MongoDB stores data in collections of documents, JSON-like objects that can contain nested structures such as arrays and objects.
In MongoDB, you insert documents that already contain data, and their shape defines the structure of the database.
When modeling countries and cities in MongoDB, you have two options:
Method I: Embedded Documents
Cities are stored inside each country document.
|
This method is useful when:
- Cities are always accessed together with their country.
- You don’t need to query cities on their own.
- You prefer a single document with all related data grouped.
Method II: Referenced Collections
Countries and cities are stored in separate collections, and cities reference the country via country_id
.
|
This method is better when:
- Cities are large or updated frequently.
- You want to query or manage cities independently.
- You want to avoid document size limitations.
In DbSchema
Method I:
With DbSchema, you get a visual diagram of your collections, so you can see how everything is built and connected.
- Write the code in the Mongo Query.
- See the collection created in a visual diagram.
Method II:
If you’re not a developer and don’t know MongoDB Shell or code, no problem! You can build everything visually in DbSchema:
- Create a
Countries
collection using the diagram editor. - Add fields like
name
,population
, or even an array of embeddedcities
. - Or create a separate
Cities
collection and link it toCountries
using acountry_id
reference field. - Define field types, add indexes, or set up validation rules - all without writing any JSON or code.
Note: In MongoDB, you don’t have to define a schema up front, but you can enforce structure using validation rules.
Read more about schema validation in this article.
Why Use a Visual Tool Like DbSchema?
If you want to learn more about how to work with MySQL or MongoDB, you can read this articles.
- Design visually - Create tables or collections just by clicking and dragging. No need to write code.
- See the structure clearly - The diagram shows how everything is connected, which makes it easier to understand.
- Skip the code - DbSchema writes the SQL or JSON for you, so you can focus on the logic, not the syntax.
- Work as a team - Built-in Git support helps you keep track of changes and collaborate safely.
- Create clean documentation - Export your diagrams as interactive HTML files to share with your team or clients.
What you should remember
Understanding the difference between MySQL and MongoDB really comes down to how they organize data.
In MySQL, you split information into separate tables and link them.
In MongoDB, you can keep everything together in one document or link collections loosely.
Each has its own advantages, the important thing is knowing which one fits your data and your project best.
DbSchema helps you explore both worlds without needing to write complex code. You can build your models visually, understand your data better, and even work offline if you want.
Try it now using something simple, like countries and cities - and you’ll see how powerful visual design can be.