Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 50 ยท Modeling with UML

Data Flow and Entity-Relationship Diagrams

Learn data flow diagrams (levels, processes, stores, flows) and entity-relationship diagrams (entities, relationships, cardinality), plus how ER maps to tables.

UML is not the only diagramming family software engineers use. Two older notations remain very popular because they are simple and answer questions UML answers less directly. A data flow diagram (DFD) shows how data moves through a system: where it comes from, what transforms it, where it is kept and where it goes. An entity-relationship diagram (ERD) shows what data the system remembers and how the pieces of data relate, which is the starting point for a database design.

Both are drawings, so they are read-only here. The runnable examples are JavaScript stand-ins: one checks a DFD against the standard drawing rules, and the other derives relational table definitions from an ER model, the same idea a modeling tool uses when it generates a schema. They are teaching models; the SQL they print is a starting sketch, not a finished database design (see the SQL tutorial for the full story of tables, keys and normalization).

Data flow diagrams

A DFD ignores when things happen and who calls whom. It only follows data. It has four symbols:

  • External entity (a rectangle): a person, organization or system outside yours that sends or receives data, such as a customer or a bank. Same idea as an actor.
  • Process (a circle or rounded box): something that transforms data, named with a verb phrase ("Validate order") and usually numbered.
  • Data store (two parallel lines or an open box): data at rest, such as a file or database table ("D1 Orders").
  • Data flow (an arrow): data moving between the other three, labeled with what flows ("order details"), never with an action.

Levels of detail

DFDs are drawn in levels, each zooming into the one above:

  • Level 0 (context diagram): the whole system is a single process in the middle, with the external entities around it and the flows between them. It shows the scope in one picture.
  • Level 1: the single process is split into its 3 to 9 main processes, with the data stores they use.
  • Level 2 and below: a process from level 1 is broken down further, where more detail is useful.

The rule of balancing keeps levels consistent: the flows entering and leaving a process at one level must match the flows entering and leaving its breakdown at the next level.

Drawing rules

A DFD is easy to draw wrongly, so a few rules help:

  1. Every process needs at least one input and at least one output. A process with no input is a "miracle" (output from nothing); one with no output is a "black hole".
  2. Data cannot flow directly between two external entities, or between an external entity and a data store. A process has to handle it, or it is not part of your system.
  3. Data cannot flow directly between two data stores. A process must move it.
  4. Flows are labeled with data names, and a flow on a process's output should be derivable from its inputs.
flowchart LR
    cust["Customer"]
    bank["Bank"]
    p1(("1. Validate order"))
    p2(("2. Take payment"))
    p3(("3. Ship goods"))
    d1[("D1 Orders")]
    d2[("D2 Stock")]
    cust -- "order details" --> p1
    p1 -- "valid order" --> d1
    d2 -- "availability" --> p1
    d1 -- "order to pay" --> p2
    p2 -- "card request" --> bank
    bank -- "approval" --> p2
    p2 -- "paid order" --> p3
    p3 -- "shipment note" --> cust

A level 1 diagram of an online shop. Circles are processes, cylinders are stores, and every arrow is labeled with the data it carries.

Compare this with a sequence diagram: there, order of messages is the point. Here, there is no order at all, only which data goes where. That makes DFDs good for requirements analysis and for spotting missing data ("where does the shipping address come from?").

A stand-in that checks the DFD rules

The model below is the shop diagram as plain data: each node has a kind (external, process, store) and flows connect two nodes. The checker applies the rules above and reports problems. It is a simplified version of the validation some drawing tools offer; the rules about balancing between levels are not covered.

JS Validate a data flow diagram

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Three problems are reported. The last two flows break rule 2 and rule 3: a customer cannot write straight into the Orders store, and one store cannot copy into another. Both need a process in between ("Record address", "Reserve stock"). And "Ship goods" has an input but no output in this list, because the flow to the customer, the shipment note, was left out of the data; the checker found a black hole that the drawing above avoids. Small mistakes like these are common in hand-drawn DFDs.

Entity-relationship diagrams

An ER diagram models the persistent data of a system. Its vocabulary:

  • Entity: a type of thing the system keeps data about (CUSTOMER, ORDER, PRODUCT), which becomes a table.
  • Attribute: a property of an entity (name, email, price). One or more attributes form the key that identifies each instance (id).
  • Relationship: a named association between entities ("a customer places orders").
  • Cardinality: how many instances of one entity relate to how many of the other: one-to-one (1:1), one-to-many (1:N) or many-to-many (M:N). Participation says whether the link is mandatory or optional (can a customer exist with no orders?).

The most common drawing style today is crow's foot notation: a three-pronged "foot" means many, a single bar means one, a circle means zero (optional). Mermaid draws this style. Here, the symbols on each side of the line read "exactly one", "zero or more", "one or more":

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ ORDER_LINE : contains
    PRODUCT ||--o{ ORDER_LINE : "appears in"
    CUSTOMER {
        int id PK
        string name
        string email
    }
    ORDER {
        int id PK
        date placed_at
    }
    PRODUCT {
        int id PK
        string title
        decimal price
    }
    ORDER_LINE {
        int quantity
    }

A customer places zero or more orders; an order has one or more lines; a product appears in zero or more lines. Order line links orders to products.

Read each relationship in both directions: "a customer places zero or more orders" and "an order is placed by exactly one customer". If either sentence feels wrong, the cardinality is wrong. Note that the many-to-many relationship between orders and products has been resolved into ORDER_LINE, an entity of its own that carries the quantity: a pattern you will see in almost every real schema.

An ER diagram is close to a UML class diagram with multiplicities, but its focus is data to store, not behavior, so there are no operations.

From ER to tables

ER models translate into relational tables by simple rules:

  1. Each entity becomes a table; each attribute becomes a column; the key becomes the primary key.
  2. For a one-to-many relationship, put a foreign key on the "many" side (each order stores its customer_id).
  3. For a one-to-one relationship, put a foreign key on either side and make it unique.
  4. For a many-to-many relationship, create a junction table holding two foreign keys (usually also the primary key together), plus any attributes of the relationship.

The stand-in below applies rules 1 to 3 to a small model and prints the table definitions. Rule 4 is not generated here. It shows the translation as data transformation; real tools also handle names, types, constraints and indexes.

JS Derive tables from entities

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The foreign key customer_id lands in orders (many side), and in profile with UNIQUE, so each customer has at most one profile. The printed SQL is plain text; you could run it in the SQL tutorial's runner to try it. Notice that the generator needs the relationship's kind to decide where the key goes: the cardinality in the diagram directly drives the schema. This is why getting cardinality right on the ER diagram matters so much.

When to use which

Question Diagram
Where does data come from, how is it transformed and stored? Data flow diagram
What data do we keep, and how do the pieces relate? Entity-relationship diagram
What types and operations exist in the code? Class diagram

In practice teams often draw a level 0 DFD early to agree on scope, an ERD while designing the database, and class and sequence diagrams for the code. The next chapter, design principles, covers how to turn models like these into a well-structured system.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Level 0 data flow diagram (context diagram) show?

    The context diagram treats the system as a single process and shows its scope: who sends data in and who receives data out.
  2. 2. Which of these breaks the rules of data flow diagrams?

    External entities and data stores must be connected through a process; otherwise the flow is not part of the system's processing.
  3. 3. How is a many-to-many relationship usually represented in relational tables?

    A junction (link) table holds a foreign key to each entity, and any attributes of the relationship, such as a quantity.
  4. 4. In a one-to-many relationship between customer and order, where does the foreign key go?

    The foreign key goes on the 'many' side, so each order row stores which customer it belongs to.

Summary

  • A data flow diagram follows data: external entities, processes, stores and labeled flows, drawn in levels from a one-process context diagram down to detail.
  • DFD rules: every process has inputs and outputs, and external entities and stores connect only through processes; levels must balance.
  • An ER diagram models stored data with entities, attributes, relationships and cardinality; crow's foot notation is the common style.
  • ER models map to tables: entities become tables, 1:N puts a foreign key on the many side, M:N needs a junction table.
  • Cardinality chosen in the diagram decides the shape of the schema, so check each relationship in both directions.

Next: Design Principles: Abstraction, Cohesion and Coupling.