basics04Chapter 4 of 6

ER diagrams

An entity relationship diagram, ER diagram for short, is the map of a database: every table a box, every link a line.

How to read it

  • Each box is a table (an entity). Its name is at the top.
  • Below it come the columns (attributes) with their types.
  • PK marks the primary key, FK a foreign key.
  • A line joins a foreign key to the primary key it points at. The ends of the line show the cardinality.

The Byte Shop

Click a line or its label to read it as a sentence.

Notation
byte-shop.dbPKprimary keyFKforeign key
Throughorders.customer_idcustomers.id

A customer has no, one or many orders; each order belongs to exactly one customer.

ER diagram of byte-shop.db with the tables customers, orders and products. orders.customer_id points to customers.id and orders.product_id points to products.id. Both links are one to many: one customer or product, any number of orders.

The hidden n:m

Customers and products have no line of their own. They are linked through orders: one customer, many orders, each with one product. That is the n:m from the last chapter, drawn the way a database stores it.