How to draw an ER diagram, from requirement to SQL tables

How to draw an ER diagram from a plain-language requirement: entities, keys, relationships, a table for many-to-many, columns and a check, each step in SQL.

Step 1: list the nouns in the requirement

The first step happens before you draw anything: write down what the database has to hold, in plain language. Here is the requirement for a makerspace’s booking system:

The makerspace runs workshops, such as laser cutting or basic welding. Each workshop is held in several sessions, each with a start time and a length. Members sign up with an email address and book a place in a session. A member can book many sessions, and a session takes many members, up to the workshop’s capacity. A booking records when it was made and the price the member paid.

Mark every noun. Each one is an entity, an attribute, or neither:

  • Workshop, session and member are entities. Each has many instances with facts of their own, and each becomes a table.
  • Start time, length, email address, capacity and price are attributes. Each describes one of those entities and has no facts of its own. They become columns.
  • The makerspace is neither. It’s the whole system; a table for it would hold one row.
  • Booking is a noun and a verb. Members book sessions, so for now it is a relationship between two entities. Step 4 turns it into a table.

The usual rule of thumb: nouns are entities, verbs are relationships. The steps below test that list, and each adds to one SQL Server script and the diagram drawn from it. What an ER diagram is has its own post; this one is about the order of the work.

Step 2: give each table a primary key

A primary key is a column, or a set of columns, that uniquely identifies each row of a table. Pick it before anything else, because every relationship you add later points at it.

A member already has something unique: the email address. It makes a poor primary key, though. People change addresses, and every booking that stores the key would have to change with it. So dbo.Member gets a number of its own, MemberId, and Email stays unique as an alternate key. Nothing in the requirement identifies a workshop or a session, since two workshops can share a title, so each gets a number too:

CREATE TABLE dbo.Member (
  MemberId int            IDENTITY NOT NULL
    CONSTRAINT PK_Member PRIMARY KEY,
  Email    nvarchar(254)  NOT NULL
    CONSTRAINT AK_Member_Email UNIQUE
);
CREATE TABLE dbo.Workshop (
  WorkshopId int IDENTITY NOT NULL
    CONSTRAINT PK_Workshop PRIMARY KEY
);
CREATE TABLE dbo.Session (
  SessionId int IDENTITY NOT NULL
    CONSTRAINT PK_Session PRIMARY KEY
);
dbo.Member with primary key MemberId and alternate key Email, dbo.Session with primary key SessionId and dbo.Workshop with primary key WorkshopId; no relationships.
Three tables, each with its primary key at the top of its box. dbo.Member keeps Email unique as an alternate key (AK). No lines yet.

IDENTITY lets SQL Server number the rows itself, so nothing outside the database has to supply a key. Naming each key after its table, {Table}Id, means a foreign key column later reads as the table it points at; the primary key column name post weighs it against a plain Id.

Step 3: add the relationships and their cardinality

Go back to the verbs. “Each workshop is held in several sessions” links workshop and session. “A member can book many sessions” links member and session. For each pair, ask the question from both sides: how many sessions can one workshop have, and how many workshops can one session belong to? The two answers are the relationship’s cardinality. A workshop has many sessions and a session belongs to one workshop, so the relationship is one-to-many, which in SQL is a foreign key on the “many” side:

ALTER TABLE dbo.Session ADD
  WorkshopId int NOT NULL
    CONSTRAINT FK_Session_Workshop_WorkshopId
    FOREIGN KEY REFERENCES dbo.Workshop (WorkshopId);
dbo.Workshop and dbo.Session with a dashed line between them, no mark at Workshop and a ring and a foot at Session; dbo.Member with no line.
One foreign key, one line from dbo.Workshop to dbo.Session. dbo.Member has no line yet.

Read the line from each end. At dbo.Workshop the line has no mark, and in this look a bare parent end says every session has exactly one workshop, because WorkshopId is NOT NULL; a nullable column would put a ring there. At dbo.Session, a ring and a foot say a workshop has zero or more sessions; a new workshop starts with none. The line is dashed because WorkshopId is not part of the session’s primary key.

Members and sessions are another matter. One member books many sessions and one session takes many members, so this relationship is many-to-many, and the same move fails. A SessionId column on dbo.Member holds one session per member. A MemberId on dbo.Session holds one member per session. A foreign key column holds one value per row, so it can put a foot at only one end of a line. That is why dbo.Member still stands alone in the picture.

Step 4: turn the many-to-many into a table

The relationship becomes a table of its own with a foreign key to each side: a junction table. The verb from step 1 names it, dbo.Booking, and each row is one member in one session:

CREATE TABLE dbo.Booking (
  SessionId int NOT NULL,
  MemberId  int NOT NULL,
  CONSTRAINT PK_Booking PRIMARY KEY (SessionId, MemberId),
  CONSTRAINT FK_Booking_Session_SessionId
    FOREIGN KEY (SessionId)
    REFERENCES dbo.Session (SessionId),
  CONSTRAINT FK_Booking_Member_MemberId FOREIGN KEY (MemberId)
    REFERENCES dbo.Member (MemberId)
);
dbo.Member, dbo.Workshop, dbo.Session and dbo.Booking; solid lines from Member and from Session to Booking, no mark at either parent and a ring and a foot at Booking, whose primary key is SessionId and MemberId; the dashed line from Workshop to Session.
The primary key of dbo.Booking is SessionId and MemberId, and both are foreign keys, so both of its lines are solid and its corners are round.

The many-to-many is now two one-to-many lines that meet in dbo.Booking. A session has zero or more bookings, and so does a member. The primary key is the pair, so a member books a given session once. Because the key’s columns are also the foreign keys, each line is solid and the box has rounded corners : a booking can’t exist without its session and its member.

Many first drafts skip this step. A diamond labeled “books”, or one line with a foot at each end, looks finished on a whiteboard, but no CREATE TABLE can store it.

Step 5: add the columns and their types

Now the attributes from step 1 go in, each on the table it describes, each with a SQL Server type:

ALTER TABLE dbo.Member ADD
  DisplayName nvarchar(100) NOT NULL,
  JoinedOn    date          NOT NULL;
ALTER TABLE dbo.Workshop ADD
  Title    nvarchar(80) NOT NULL,
  Capacity smallint     NOT NULL,
  Price    decimal(9,2) NOT NULL;
ALTER TABLE dbo.Session ADD
  StartsAt    datetime2(0) NOT NULL,
  DurationMin smallint     NOT NULL;
ALTER TABLE dbo.Booking ADD
  BookedAt  datetime2(3) NOT NULL,
  PricePaid decimal(9,2) NOT NULL;
dbo.Member with DisplayName and JoinedOn added, dbo.Workshop with Title, Capacity and Price, dbo.Session with StartsAt and DurationMin, dbo.Booking with BookedAt and PricePaid; the same three lines as before.
The requirement's attributes on their tables. PricePaid sits on dbo.Booking because it belongs to one member in one session.

Most of them have an obvious home. Title, capacity and list price describe a workshop; start time and length describe a session. The price paid needs a second look. A workshop’s price can change, and one member may get a discount, so the amount paid is a fact about the booking. It goes on dbo.Booking beside BookedAt. Put an attribute on the wrong table and you either repeat it on every row or lose its history.

For types, take the narrowest one that holds every real value: smallint for a capacity and a length in minutes, decimal(9,2) for money (never float, which stores an approximation), datetime2(0) for a start time that needs no fractions of a second, and nvarchar for names, so any alphabet fits.

Some phrases in the requirement become constraints. A capacity has to be a positive number, and say the largest room seats 40. A check constraint holds that:

ALTER TABLE dbo.Workshop ADD CONSTRAINT CK_Workshop_Capacity
  CHECK (Capacity > 0 AND Capacity <= 40);

The other half of “up to the workshop’s capacity”, no more bookings than seats, counts rows across a table. That rule goes in the code that writes bookings.

Step 6: check the design

Run three checks on the diagram before any data goes in:

  • Every table has a primary key. Without one nothing names a row for good, and other tables have no key to point at unless you add a unique constraint.
  • Every foreign key points at a key of its parent: the primary key or a unique constraint. SQL Server enforces this one. A foreign key from a new Session.HostName column to Member.DisplayName fails with Msg 1776, “There are no primary or candidate keys in the referenced table ‘dbo.Member’ that match the referencing column list”.
  • No repeating groups: no Session1Id, Session2Id and Session3Id on a member, and no list of ids in one column. A repeating group is a many-to-many that skipped step 4, and removing it is the first step of database normalization.

On the database, two catalog queries confirm the first two checks:

SELECT t.name
FROM sys.tables AS t
WHERE NOT EXISTS (SELECT * FROM sys.key_constraints AS k
                  WHERE k.parent_object_id = t.object_id
                    AND k.type = 'PK');
SELECT fk.name, i.name AS referenced_key
FROM sys.foreign_keys AS fk
JOIN sys.indexes AS i
  ON i.object_id = fk.referenced_object_id
 AND i.index_id = fk.key_index_id;

The first returns no rows, so every table has a primary key. The second lists the three foreign keys, each with the key it references: PK_Workshop, PK_Session and PK_Member.

Last, put in a few rows the requirement allows and two it forbids:

INSERT INTO dbo.Member (Email, DisplayName, JoinedOn)
VALUES (N'ana@example.com', N'Ana Ruiz', '2026-01-10'),
       (N'tom@example.com', N'Tom Berg', '2026-02-03');
INSERT INTO dbo.Workshop (Title, Capacity, Price)
VALUES (N'Laser cutting basics', 8, 45.00);
INSERT INTO dbo.Session (WorkshopId, StartsAt, DurationMin)
VALUES (1, '2026-03-07 10:00', 180),
       (1, '2026-03-14 10:00', 180);
INSERT INTO dbo.Booking
  (SessionId, MemberId, BookedAt, PricePaid)
VALUES (1, 1, '2026-02-20 18:02', 45.00),
       (2, 1, '2026-02-20 18:03', 45.00),
       (1, 2, '2026-02-21 09:40', 40.50);
INSERT INTO dbo.Booking
  (SessionId, MemberId, BookedAt, PricePaid)
VALUES (1, 1, '2026-02-22 12:00', 45.00);
INSERT INTO dbo.Booking
  (SessionId, MemberId, BookedAt, PricePaid)
VALUES (3, 2, '2026-02-22 12:05', 45.00);
Msg 2627, Level 14, State 1, Line 14
Violation of PRIMARY KEY constraint 'PK_Booking'. Cannot insert duplicate key in object 'dbo.Booking'. The duplicate key value is (1, 1).
The statement has been terminated.
Msg 547, Level 16, State 1, Line 17
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Booking_Session_SessionId". The conflict occurred in database "Makerspace", table "dbo.Session", column 'SessionId'.
The statement has been terminated.

Ana books both sessions and Tom books the first one at a discount, so a member holds two sessions and a session holds two members. Ana booking session 1 again breaks PK_Booking, and the message prints the pair. A booking for session 3, which doesn’t exist, breaks the foreign key to dbo.Session. When the rows the requirement allows go in and the ones it forbids are refused, the tables hold the requirement.

How ERDsmith helps

Each step above is a change to keys and columns, and the mistakes the steps avoid show in a picture before any data exists: a table with no line, a line with a foot at both ends that no column could store. ERDsmith draws each line and its ends from the columns and keys behind it, so each step’s effect shows on the boxes as you make it:

  • A new table starts with an empty primary key, and a key with no columns is flagged until you give it one.
  • Every relationship targets the parent’s primary key or a unique index you choose, so a foreign key to a plain column like DisplayName can’t be drawn.
  • Drawing a relationship adds a child column for each column of the parent’s key, with its type, and the ends of the line follow from those columns: no mark at the parent for NOT NULL, a ring for nullable columns, a solid line when the columns sit in the child’s primary key.
  • Change the type of dbo.Member.MemberId and dbo.Booking.MemberId follows it.