Developers

ER Diagram Guide: How to Draw an Entity Relationship Diagram for Your Database

A practical ER diagram guide: entities, attributes, keys, one-to-many and many-to-many relationships, crow's foot notation, a worked example for an online shop, common mistakes, and turning the diagram into SQL.

ER Diagram Guide: How to Draw an Entity Relationship Diagram for Your Database
On this page
  1. The building blocks
  2. The three kinds of relationship
  3. One-to-one
  4. One-to-many
  5. Many-to-many
  6. Reading crow's foot notation
  7. Step by step: design a database with an ER diagram
  8. A worked example: a small online shop
  9. Common mistakes the diagram catches
  10. From diagram to SQL
  11. Keep the diagram alive

The database is the part of an application that's hardest to change later. Rename a page or redesign a screen and nobody notices; restructure the tables after a year of real data and you're writing migration scripts for days. An ER diagram, short for entity relationship diagram, lets you design that structure on screen first, spot problems while they're cheap to fix, and give everyone on the project the same picture of how the data fits together.

The building blocks#

  • Entities are the things you store data about: customers, orders, products, invoices. Each becomes a table.
  • Attributes are the details of each entity: a customer's name, phone and email. Each becomes a column.
  • Primary keys uniquely identify each row, usually an id column.
  • Foreign keys point from one table to another's primary key, creating the relationship: orders.customer_id points to customers.id.
  • Relationships describe how many of one entity relate to another.

The entity relationship model was introduced by Peter Chen in 1976 and is still the standard way to plan relational databases.

The three kinds of relationship#

One-to-one#

Each record on one side matches at most one on the other: a user and their profile settings. Often these can live in the same table; split them only when one side is optional or rarely used.

One-to-many#

The most common. One customer has many orders; each order belongs to one customer. Put the foreign key on the "many" side: orders.customer_id.

Many-to-many#

An order contains many products, and a product appears in many orders. Relational databases can't store this directly, so you add a junction table, order_items, with a foreign key to each side and any details about the link, such as quantity and the price at the time of sale.

Reading crow's foot notation#

Most ER diagram tools use crow's foot symbols at the ends of each relationship line:

  • A single bar (|): one.
  • A crow's foot (three prongs): many.
  • A circle (o): zero, meaning optional.

So a line from customers to orders with a bar at the customer end and a circle-plus-crow's-foot at the order end reads: "each order has exactly one customer; each customer has zero or more orders."

Step by step: design a database with an ER diagram#

  1. List the nouns in how you describe the business: "Customers place orders for products, pay with invoices." Those nouns are candidate entities.
  2. Add attributes to each. Keep only what you'll actually use.
  3. Pick primary keys. A numeric or UUID id is simplest; avoid using phone numbers or emails as keys, because they change.
  4. Draw relationships and decide one-to-many or many-to-many for each.
  5. Add junction tables for every many-to-many.
  6. Check with real examples: walk through a real order and make sure every piece of information has exactly one home.

A worked example: a small online shop#

Imagine [Shop name] needs a database for online orders. The entities:

  • customers: id, name, phone, email, created_at.
  • products: id, name, price, stock, category_id.
  • categories: id, name.
  • orders: id, customer_id, status, total, created_at.
  • order_items: id, order_id, product_id, quantity, unit_price.
  • payments: id, order_id, method, amount, paid_at.

Relationships: one customer to many orders; one order to many order_items; one product to many order_items; one category to many products; one order to many payments (deposits and balances). Note that unit_price is stored on order_items as well as on products: when prices change next month, old orders must still show what the customer paid.

Common mistakes the diagram catches#

  • Repeating groups: columns like product1, product2, product3 on orders. That's a missing many-to-many.
  • Duplicated data: the customer's phone copied onto every order. Store it once and link.
  • Missing history: current prices or addresses overwriting what was true at the time of an order.
  • Vague names: "data", "info", "type2". Name tables and columns so anyone can read the diagram.
  • No foreign key constraints: without them, orders can point to customers that no longer exist. The PostgreSQL documentation on constraints explains how databases enforce them.

Our database design guide covers normalisation, indexes and naming in more depth.

From diagram to SQL#

Once the diagram looks right, each entity becomes a CREATE TABLE statement and each relationship a FOREIGN KEY constraint. You can write these by hand, or let a tool generate them. The Tekservix database designer lets you start from a template, paste existing SQL or describe your app, then edit tables in a designer, see the ER diagram update, and export SQL for MySQL, PostgreSQL, SQLite or SQL Server. When the tables exist, our SQL query builder guide helps with writing the queries that read them.

Keep the diagram alive#

A diagram drawn once and never updated becomes misleading. Update it whenever you add a table or relationship, keep it in the project's documentation, and use it when onboarding new developers. It's the fastest way for someone new to understand how the data fits together, and the first thing to check when a report's numbers look wrong.

Frequently asked questions

What is an ER diagram?

An entity relationship (ER) diagram is a picture of a database's structure. It shows the things you store data about (entities, which become tables), the details of each (attributes, which become columns), and how they connect (relationships, implemented with keys).

What is crow's foot notation?

It's the most common way to show relationships in ER diagrams. Symbols at each end of a line show how many records can be on that side: a single line means one, a three-pronged crow's foot means many, and a circle means the relationship is optional.

How do you show a many-to-many relationship in an ER diagram?

In the final design, add a junction table between the two entities. For example, orders and products are linked through an order_items table that holds one row per product in each order, with a foreign key to each side plus details like quantity and price.

Share this article
Written by

The team behind Tekservix

The Tekservix Team builds and supports Tekservix: WhatsApp and SMS APIs, AI tools, invoicing, PDF and SEO tools for growing businesses. We write practical, tested guides based on what we see our customers do every day.

More articles from the Tekservix Team

Get new guides in your inbox

One short email when we publish something useful: WhatsApp and SMS marketing, AI tools, SEO and running a business online. No spam, unsubscribe anytime.