What is an ER diagram? Parts, notations and an example

What is an ER diagram? A picture of the entities a database stores, their keys and how they relate. The parts, three notations and the SQL behind one.

What is an ER diagram?

An ER diagram, or entity relationship diagram (ERD), is a visual representation of the data a system stores and how the pieces relate to each other. It shows three things: what you store (entities), the facts you keep about each (attributes), and how they connect (relationships). Peter Chen introduced it in 1976, in the paper The Entity-Relationship Model: Toward a Unified View of Data, as a tool for designing databases.

In a relational database those three become tables, columns and foreign keys. An ER diagram of a SQL Server database is therefore a map of its tables: a box per table, a line per foreign key, and marks on each line that say how many rows can sit at each end.

The example here is a small library, cut down from the library management system template. The library has books, owns one or more copies of each, and lends copies to its members. A loan records which copy went to which member, the day it went out and the day it is due back.

Three ways to draw it: Chen, crow’s foot and IDEF1X

The same library can be drawn in several notations, and three of them cover most of what you will meet in books and tools. Each one draws the library below; what every symbol means is in ER diagram symbols.

Chen

Chen’s notation is for talking a design through with people who will never read SQL, before anyone has decided on tables. Each diamond reads as a sentence: a book has N copies, a member takes N loans, each loan covers one copy, and authors write books, M to N.

Chen diagram of the library: rectangles Author, Book, Member, BookCopy and Loan; diamonds writes between Author and Book (M and N), has between Book and BookCopy (1 and N), covers between BookCopy and Loan (1 and N), takes between Member and Loan (1 and N); double lines from BookCopy to has and from Loan to covers and takes; ovals for the attributes, the keys underlined.
The library in Chen notation, with a few attributes on each entity. Drawn for this post: ERDsmith draws tables, not Chen.

Crow’s foot

Crow’s foot is the notation for the tables you will create: a box per table with its columns, a line per foreign key, and marks at the line’s ends for how many rows meet there. Here is the library as four SQL Server tables; the rest of this post reads crow’s foot:

CREATE TABLE dbo.Book (
  BookId int NOT NULL CONSTRAINT PK_Book PRIMARY KEY,
  Isbn   char(13) NOT NULL CONSTRAINT AK_Book_Isbn UNIQUE,
  Title  nvarchar(300) NOT NULL
);
CREATE TABLE dbo.BookCopy (
  BookCopyId int NOT NULL CONSTRAINT PK_BookCopy PRIMARY KEY,
  BookId     int NOT NULL,
  Barcode    varchar(20) NOT NULL,
  CONSTRAINT FK_BookCopy_Book_BookId FOREIGN KEY (BookId)
    REFERENCES dbo.Book (BookId)
);
CREATE TABLE dbo.Member (
  MemberId   int NOT NULL CONSTRAINT PK_Member PRIMARY KEY,
  CardNumber varchar(20) NOT NULL,
  FullName   nvarchar(200) NOT NULL
);
CREATE TABLE dbo.Loan (
  LoanId     int  NOT NULL CONSTRAINT PK_Loan PRIMARY KEY,
  BookCopyId int  NOT NULL,
  MemberId   int  NOT NULL,
  LoanedOn   date NOT NULL,
  DueOn      date NOT NULL,
  ReturnedOn date NULL,
  CONSTRAINT FK_Loan_BookCopy_BookCopyId
    FOREIGN KEY (BookCopyId)
    REFERENCES dbo.BookCopy (BookCopyId),
  CONSTRAINT FK_Loan_Member_MemberId FOREIGN KEY (MemberId)
    REFERENCES dbo.Member (MemberId)
);
dbo.Book, dbo.BookCopy, dbo.Member and dbo.Loan; dashed lines from Book to BookCopy, from BookCopy to Loan and from Member to Loan, each with no mark at the parent and a ring and a foot at the child; a legend under the diagram.
One dashed line per foreign key, from the table that holds it to the table it points to; the legend under it reads the ends.

IDEF1X

IDEF1X came out of US Air Force data modeling and became the federal standard FIPS 184, for models that have to follow a written standard. It draws the same tables with less at the line ends: a filled dot at the child’s end for zero, one or more, and nothing at the parent’s :

IDEF1X diagram of dbo.Book, dbo.BookCopy, dbo.Member and dbo.Loan: dashed lines from Book to BookCopy, from BookCopy to Loan and from Member to Loan with a filled dot at each child end; (FK) after BookId, BookCopyId and MemberId, (AK) after Isbn; a legend under the diagram.
The same four tables in IDEF1X, drawn from the same script.

What does an ER diagram show?

Whatever the notation, an ER diagram of a database answers five questions. In the library’s pictures:

  • Entities: the things you keep rows about. Book, BookCopy, Member and Loan are entities, and each becomes a table: Book is dbo.Book. Chen calls the type an entity set and each book in it an entity, which in SQL is the table and its rows.
  • Attributes: the facts kept about each entity, which become its columns. A book has an ISBN and a title; a loan has the day it went out, the day it is due and the day it came back.
  • Keys: the attribute that uniquely identifies each row. Chen underlines it ; the crow’s foot and IDEF1X pictures put it at the top of the box, above a line. BookId is the primary key of dbo.Book; Isbn is an alternate key (AK), unique as well but not the one other tables point at. A foreign key (FK) is a column that holds another table’s key, as Loan.MemberId holds a member’s.
  • Relationships: how two entities connect, a diamond in Chen and a line in the other two. Every line in the library is one-to-many: a book has many copies, a copy goes out on many loans over the years, and a member takes many loans.
  • Cardinality: the number of rows of one entity that can relate to one row of the other, read once in each direction. A loan covers exactly one copy, which the line shows by leaving the copy’s end bare ; a copy has zero or more loans, the ring and the foot at the loan’s end . Chen writes 1, N or M on the line, crow’s foot draws the ends.

An ER diagram doesn’t show the rows themselves, and it doesn’t show queries or the order in which things happen.

What is an ER diagram used for?

An ER diagram has two jobs, at the two ends of a database’s life.

The first is designing before you build, the job how to draw an ER diagram walks through step by step. Moving a box or a line on a diagram costs nothing, while changing a table that already holds years of loans and feeds other applications costs a migration. Drawing the library first is where you notice that a member borrows a particular copy, so dbo.Loan points at dbo.BookCopy, or that a book with two authors can’t keep its author in one column. It also gives the people who run the library and the people who build the database one picture to argue over.

The second is reading a database that already exists. A diagram drawn from the tables of a database you inherit puts on one screen what dozens of CREATE TABLE statements spread over files: which tables sit in the middle and which hang off them.

Designs are often drawn at three levels of detail. The Chen picture is a conceptual-level diagram: entities, attributes and relationships, with many-to-many left unresolved and no foreign keys. A logical model resolves each many-to-many and adds the foreign keys, still for no particular database. A physical model is the tables of one database engine, with data types, NULL or NOT NULL and the constraints. The crow’s foot and IDEF1X pictures are physical models: they were drawn from a SQL Server script.

From ER diagram to database

Each entity becomes a CREATE TABLE, each attribute a column, and each one-to-many relationship a foreign key on the many side, as Loan.MemberId points at dbo.Member. A many-to-many can’t be a foreign key, since a column holds one value per row, so Chen’s M:N diamond between Author and Book becomes a table of its own, dbo.BookAuthor, keyed by both sides:

CREATE TABLE dbo.Author (
  AuthorId int NOT NULL CONSTRAINT PK_Author PRIMARY KEY,
  FullName nvarchar(200) NOT NULL
);
CREATE TABLE dbo.BookAuthor (
  BookId   int NOT NULL,
  AuthorId int NOT NULL,
  CONSTRAINT PK_BookAuthor PRIMARY KEY (BookId, AuthorId),
  CONSTRAINT FK_BookAuthor_Book_BookId FOREIGN KEY (BookId)
    REFERENCES dbo.Book (BookId),
  CONSTRAINT FK_BookAuthor_Author_AuthorId
    FOREIGN KEY (AuthorId) REFERENCES dbo.Author (AuthorId)
);
dbo.Author and dbo.BookAuthor added to dbo.Book, dbo.BookCopy, dbo.Member and dbo.Loan; solid lines from Author and from Book to BookAuthor, no mark at either parent and a ring and a foot at BookAuthor, which has rounded corners; dashed lines among Book, BookCopy, Member and Loan; a legend under the diagram.
The M:N diamond is a table between two one-to-many lines. Both lines are solid because BookId and AuthorId together are the primary key of dbo.BookAuthor.

ER diagram to relational schema maps every kind of Chen construct this way, one rule per step.

How ERDsmith helps

Every decision in the library was made on the diagram first: loans point at copies, authors meet books in a table of their own, a copy can’t exist without its book. Each one became a column, a key or a constraint.

ERDsmith works at the third level only, the physical one: you edit the library’s tables, and the lines come from their columns.

  • Drawing the line from dbo.Member to dbo.Loan adds MemberId to the loan, with the type of the member’s key.
  • A line is solid exactly when all its columns are in the child’s primary key, as on dbo.BookAuthor, and dashed otherwise.
  • A non-identifying line is optional exactly when its columns allow NULL, so the end at the parent follows the column.