ER diagram to relational schema: 7 mapping rules in SQL

ER diagram to relational schema mapping turns entities, relationships and attributes into tables and keys. The seven rules, each with Chen, SQL and the tables.

How do you convert an ER diagram to tables?

Converting an ER diagram to a relational schema, or ER to relational mapping, turns each entity, attribute and relationship of a conceptual model into tables, columns, primary keys and foreign keys. Textbooks give it as a short algorithm with one rule per kind of ER construct, and the rules leave little to taste: the cardinality and participation on the diagram decide where each foreign key goes and whether it allows NULL.

A Chen diagram 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, which is what the mapping does.

The rules, in the order this post applies them:

  • A strong entity becomes a table, and its key attribute the primary key.
  • A 1:N relationship becomes a foreign key on the N side.
  • A 1:1 relationship becomes a unique foreign key, or a primary key that is also the foreign key (a shared key).
  • An M:N relationship becomes a table of its own, keyed by both sides.
  • A weak entity becomes a table whose primary key holds its owner’s key.
  • A multivalued attribute becomes a table of its own, keyed by the owner and the value.
  • A recursive 1:N relationship becomes a foreign key from a table to itself; a recursive M:N becomes a junction table with two foreign keys to it.

The example is a small HR database: departments, employees and their managers, badges, projects, dependents and skills. It shares departments, employees and managers with the employee management system template. Each step starts from the piece of the Chen diagram it maps and ends with the SQL Server script and the tables it makes. The scripts run in order on one database.

Step 1: each strong entity becomes a table

A strong entity has a key of its own. Chen draws it as a rectangle with its attributes in ovals and the key attribute underlined . The company has three:

Chen diagram of three entities: Department with the key DepartmentId and Name, Employee with the key EmployeeId, HiredOn and the composite attribute Name made of FirstName and LastName, and Project with the key ProjectId and Name.
Department, Employee and Project as Chen entities. Drawn for this post: ERDsmith draws tables, not Chen.

Each rectangle becomes a table, each oval a column, and the underlined attribute the primary key. A composite attribute such as an employee’s Name gets no column of its own; its parts, FirstName and LastName, do.

CREATE TABLE dbo.Department (
  DepartmentId int          NOT NULL
    CONSTRAINT PK_Department PRIMARY KEY,
  Name         nvarchar(50) NOT NULL
    CONSTRAINT AK_Department_Name UNIQUE
);
CREATE TABLE dbo.Employee (
  EmployeeId int          NOT NULL
    CONSTRAINT PK_Employee PRIMARY KEY,
  FirstName  nvarchar(50) NOT NULL,
  LastName   nvarchar(50) NOT NULL,
  HiredOn    date         NOT NULL
);
CREATE TABLE dbo.Project (
  ProjectId int           NOT NULL
    CONSTRAINT PK_Project PRIMARY KEY,
  Name      nvarchar(100) NOT NULL
);
dbo.Department with primary key DepartmentId and alternate key Name, dbo.Employee with primary key EmployeeId, dbo.Project with primary key ProjectId; no relationships.
Three tables and no lines yet. Department.Name is also unique, an alternate key the Chen diagram doesn't mark.

Every line from here on is a foreign key column added to one of these tables, or a new table.

Steps 2 and 3: 1:N and 1:1 relationships become foreign keys

Step 2: 1:N puts the foreign key on the N side

A department has many employees, and each employee works for one department. The double line on the employee’s side is total participation: every employee works for some department.

Chen diagram: Department, 1, the relationship works for, N, Employee, with a double line between works for and Employee.
Works for, a 1:N relationship. Drawn for this post (ERDsmith doesn't draw Chen).

An employee row can hold one department’s key, but a department row can’t hold a list of employees, so the key goes to the N side. Total participation makes it NOT NULL:

ALTER TABLE dbo.Employee ADD
  DepartmentId int NOT NULL
    CONSTRAINT FK_Employee_Department_DepartmentId
      REFERENCES dbo.Department (DepartmentId);
dbo.Department and dbo.Employee with the foreign key DepartmentId; a dashed line with no mark at Department and a ring and a foot at Employee.
DepartmentId on dbo.Employee is NOT NULL: no mark at Department, exactly one; a ring and a foot at Employee.
INSERT INTO dbo.Department (DepartmentId, Name)
VALUES (1, N'Engineering'), (2, N'Finance');
INSERT INTO dbo.Employee
  (EmployeeId, FirstName, LastName, HiredOn, DepartmentId)
VALUES (10, N'Maya', N'Chen', '2019-03-04', 1),
       (11, N'Leo', N'Park', '2022-08-15', 1),
       (12, N'Ana', N'Ruiz', '2021-01-11', 2);
INSERT INTO dbo.Employee
  (EmployeeId, FirstName, LastName, HiredOn)
VALUES (13, N'Tom', N'Berg', '2026-02-02');
Msg 515, Level 16, State 2, Line 8
Cannot insert the value NULL into column 'DepartmentId', table 'Company.dbo.Employee'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Three employees go in. Tom has no department, and Msg 515 stops him: the double line holds. A single line , partial participation, would make the column nullable instead. An attribute of a 1:N relationship, such as the date someone joined the department, goes on the same side as the foreign key.

Step 3: 1:1 puts a unique foreign key on the total side

Each employee holds at most one badge, and each badge belongs to exactly one employee:

Chen diagram: Employee, 1, the relationship holds, 1, Badge, with a double line between holds and Badge; Badge has the key BadgeId and IssuedOn.
Holds, a 1:1 relationship with total participation on the badge's side. Drawn for this post (ERDsmith doesn't draw Chen).

The foreign key could sit on either side. On the side with total participation, the badge, it can be NOT NULL, and a unique constraint caps it at one badge per employee:

CREATE TABLE dbo.Badge (
  BadgeId    int  NOT NULL CONSTRAINT PK_Badge PRIMARY KEY,
  EmployeeId int  NOT NULL,
  IssuedOn   date NOT NULL,
  CONSTRAINT AK_Badge_EmployeeId UNIQUE (EmployeeId),
  CONSTRAINT FK_Badge_Employee_EmployeeId
    FOREIGN KEY (EmployeeId)
    REFERENCES dbo.Employee (EmployeeId)
);
dbo.Department, dbo.Employee and dbo.Badge; Badge.EmployeeId marked FK and AK; a dashed line from Employee to Badge with no mark at Employee and a ring and a bar at Badge.
EmployeeId on dbo.Badge is NOT NULL and unique, marked FK and AK: no mark at Employee, a ring and a bar at Badge.

On the employee’s side the column would be NULL for everyone without a badge, and SQL Server’s unique constraint lets only one NULL in. The one-to-one relationship post shows that catch and its fix. If a badge had no number of its own, EmployeeId could be its whole primary key instead: the shared key.

Step 4: an M:N relationship becomes a table of its own

An employee works on several projects, and a project has several employees. The hours belong to the pair, so Chen hangs them on the diamond :

Chen diagram: Employee, M, the relationship works on, N, Project; the attribute HoursPerWeek on the works on diamond.
Works on, an M:N relationship with an attribute of its own. Drawn for this post (ERDsmith doesn't draw Chen).

No foreign key column can hold this. Put ProjectId on dbo.Employee and an employee gets one project; put EmployeeId on dbo.Project and a project gets one employee. So the relationship becomes a junction table: one row for each pair, a foreign key to each side, and the pair as its primary key. The diamond’s attribute becomes a column of that table:

CREATE TABLE dbo.EmployeeProject (
  EmployeeId   int          NOT NULL,
  ProjectId    int          NOT NULL,
  HoursPerWeek decimal(4,1) NOT NULL,
  CONSTRAINT PK_EmployeeProject
    PRIMARY KEY (EmployeeId, ProjectId),
  CONSTRAINT FK_EmployeeProject_Employee_EmployeeId
    FOREIGN KEY (EmployeeId)
    REFERENCES dbo.Employee (EmployeeId),
  CONSTRAINT FK_EmployeeProject_Project_ProjectId
    FOREIGN KEY (ProjectId)
    REFERENCES dbo.Project (ProjectId)
);
dbo.Department, dbo.Employee, dbo.Project and dbo.EmployeeProject, whose primary key EmployeeId and ProjectId are also foreign keys; solid lines from Employee and from Project to EmployeeProject, which has rounded corners.
dbo.EmployeeProject keys on both foreign keys: two solid lines and a rounded box. HoursPerWeek is a plain column.

The M:N is now two one-to-many lines: an employee has zero or more rows here, and so does a project. The primary key keeps a pair from going in twice.

Steps 5 and 6: tables keyed by their owner

Step 5: a weak entity takes its owner’s key

A dependent, a child or spouse on an employee’s benefits plan, has no key of its own: “Sofia” means nothing until you say whose dependent she is. Chen draws a double rectangle , a double diamond to the owner, and a dashed underline under the partial key:

Chen diagram: Employee, 1, the double diamond has, N, the double rectangle Dependent, with a double line between has and Dependent; Dependent has Name with a dashed underline, Relation and BornOn.
Dependent, a weak entity owned by Employee. Drawn for this post (ERDsmith doesn't draw Chen).

The table’s primary key is the owner’s key plus the partial key, (EmployeeId, Name), and EmployeeId is also the foreign key, so the relationship is identifying. The weak entity post goes further into that bond.

CREATE TABLE dbo.Dependent (
  EmployeeId int          NOT NULL,
  Name       nvarchar(50) NOT NULL,
  Relation   nvarchar(20) NOT NULL,
  BornOn     date         NOT NULL,
  CONSTRAINT PK_Dependent PRIMARY KEY (EmployeeId, Name),
  CONSTRAINT FK_Dependent_Employee_EmployeeId
    FOREIGN KEY (EmployeeId)
    REFERENCES dbo.Employee (EmployeeId)
    ON DELETE CASCADE
);
dbo.Department, dbo.Employee and dbo.Dependent, whose primary key is EmployeeId and Name, EmployeeId also a foreign key; a solid line from Employee to Dependent, which has rounded corners.
EmployeeId is in the primary key of dbo.Dependent and points at dbo.Employee: a solid line, a rounded box. ON DELETE CASCADE isn't drawn.

With ON DELETE CASCADE, deleting an employee deletes their dependents in the same statement: a dependent can’t outlive its owner.

Step 6: a multivalued attribute gets its own table

An employee has several skills. A double oval marks an attribute that holds a set of values:

Chen diagram: Employee with the key attribute EmployeeId and the multivalued attribute Skill in a double oval.
Skill, a multivalued attribute of Employee. Drawn for this post (ERDsmith doesn't draw Chen).

A column holds one value, so the set moves into a table of its own: one row per employee and skill, the pair as the primary key, the employee’s key also the foreign key.

CREATE TABLE dbo.EmployeeSkill (
  EmployeeId int          NOT NULL,
  Skill      nvarchar(50) NOT NULL,
  CONSTRAINT PK_EmployeeSkill PRIMARY KEY (EmployeeId, Skill),
  CONSTRAINT FK_EmployeeSkill_Employee_EmployeeId
    FOREIGN KEY (EmployeeId)
    REFERENCES dbo.Employee (EmployeeId)
);
dbo.Department, dbo.Employee and dbo.EmployeeSkill, whose primary key is EmployeeId and Skill, EmployeeId also a foreign key; a solid line from Employee to EmployeeSkill, which has rounded corners.
dbo.EmployeeSkill keys on EmployeeId and Skill: a solid line and a rounded box, the same shape as a dependent.
INSERT INTO dbo.EmployeeSkill (EmployeeId, Skill)
VALUES (11, N'SQL'), (11, N'Python'), (12, N'SQL');
INSERT INTO dbo.EmployeeSkill (EmployeeId, Skill)
VALUES (11, N'SQL');
Msg 2627, Level 14, State 1, Line 3
Violation of PRIMARY KEY constraint 'PK_EmployeeSkill'. Cannot insert duplicate key in object 'dbo.EmployeeSkill'. The duplicate key value is (11, SQL).
The statement has been terminated.

Leo’s two skills and Ana’s one go in. Adding the skill SQL for Leo a second time fails on PK_EmployeeSkill with Msg 2627. Columns Skill1, Skill2 and Skill3 on dbo.Employee would cap the count at three and let the same skill in twice. Once a skill needs facts of its own, a description or a level, it has become an entity, and this table turns into step 4’s M:N: a dbo.Skill with its own key, and dbo.EmployeeSkill pointing at it.

Step 7: a recursive relationship points back at its own table

A manager is an employee too. Chen draws this recursive relationship as one diamond with both lines on the same rectangle , and role names on the lines say which end is which:

Chen diagram: the relationship supervises with both lines on Employee, one marked 1 with the role manager, the other N with the role report.
Supervises, a recursive 1:N relationship on Employee. Drawn for this post (ERDsmith doesn't draw Chen).

It is 1:N, so step 2’s rule applies: the foreign key goes on the N side, the report, which is the same table. The column takes the role’s name, ManagerId, since the table’s own key already has EmployeeId, and it allows NULL because someone sits at the top:

ALTER TABLE dbo.Employee ADD
  ManagerId int NULL
    CONSTRAINT FK_Employee_Employee_ManagerId
      REFERENCES dbo.Employee (EmployeeId);
All seven tables: dbo.Department, dbo.Employee, dbo.Project, dbo.Badge, dbo.EmployeeProject, dbo.Dependent and dbo.EmployeeSkill; a dashed line loops from dbo.Employee back to itself for ManagerId, with a ring at the parent end and a ring and a foot at the child end.
The whole company. ManagerId loops from dbo.Employee back to itself, with a ring at the parent end because it allows NULL.

A recursive M:N, such as employees who review several colleagues and are reviewed by several, maps by step 4’s rule: a junction table whose two foreign keys both point at dbo.Employee, one of them under a role name. What SQL Server checks and refuses on a key like ManagerId, the cascade included, is in the unary relationship post.

How ERDsmith helps

Each step ended on the same few questions about a column: which table holds it, whether it allows NULL, whether it is unique, whether it sits in the primary key. A Chen diagram leaves them open; a physical model has to answer them. ERDsmith works at that level. It draws tables and keys in crow’s foot or IDEF1X, not Chen’s rectangles and diamonds , and every line follows from the columns behind it:

  • Steps 4 to 6 put a parent’s key inside the new table’s primary key, and that alone makes the relationship identifying: dbo.EmployeeProject, dbo.Dependent and dbo.EmployeeSkill hang on solid lines .
  • A non-identifying relationship is optional exactly when its columns allow NULL: ManagerId gets a ring at its parent end, DepartmentId doesn’t.
  • A self-reference stays non-identifying, and its column keeps a role name, as ManagerId does for EmployeeId.