What is a unary relationship?
A unary relationship is a relationship in which an entity set is related to itself: a row of a table is related to other rows of that same table. It is also called a recursive or self-referencing relationship, and its degree is one, because only one entity set takes part. In SQL it becomes a self-referencing foreign key, a column that references the primary key of its own table. The primary key and the foreign key point at the same rows, but in two roles: one row plays the parent, another the child.
The examples come from a makerspace and are built on dbo.Member from the demo that the box at the end of this post opens. Experienced members mentor new ones. Each member has at most one mentor, and a mentor can take on several members, so the relationship is one-to-many, the common case. A unary relationship can have any cardinality a relationship between two tables can: one-to-one when a unique index filtered to skip NULLs caps each row at one partner (a plain unique constraint would let only one member go without), and many-to-many through a table of its own, as the vouching example further down shows.
A self-referencing foreign key for one-to-many
The foreign key goes on the many side, and here both sides are dbo.Member. So the table gets a MentorId column that points at its own MemberId:
CREATE TABLE dbo.Member ( MemberId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Member PRIMARY KEY, DisplayName nvarchar(100) NOT NULL, MentorId int NULL, CONSTRAINT FK_Member_Member_MentorId FOREIGN KEY (MentorId) REFERENCES dbo.Member (MemberId) ); CREATE INDEX IX_Member_MentorId ON dbo.Member (MentorId);

MentorId allows NULL, and it has to: somebody is first. Ines joins with no mentor, she takes on Hugo and Nadia, and Nadia takes on Tariq:
INSERT INTO dbo.Member (DisplayName, MentorId) VALUES (N'Ines Moreau', NULL); INSERT INTO dbo.Member (DisplayName, MentorId) VALUES (N'Hugo Brandt', 1), (N'Nadia Kerr', 1); INSERT INTO dbo.Member (DisplayName, MentorId) VALUES (N'Tariq Aziz', 3); INSERT INTO dbo.Member (DisplayName, MentorId) VALUES (N'Kai Moss', 99);
Msg 547, Level 16, State 1, Line 7
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Member_Member_MentorId". The conflict occurred in database "Makerspace", table "dbo.Member", column 'MemberId'.
The statement has been terminated.The last insert fails: no member has key 99, so Kai has no mentor to point at. For a key on its own table, Msg 547 names the constraint FOREIGN KEY SAME TABLE. Read the line in crow’s foot notation: the ring at the mentor’s end says a member has zero or one mentor, and the ring and the foot at the member’s end say a mentor has zero or more members. SQL Server doesn’t index a foreign key column on its own, so IX_Member_MentorId is there for the lookup every step of a chain makes: who does this member mentor?
The foreign key lets one wrong row through: a member who mentors themselves. MentorId = MemberId references a row that exists, so the key has no objection. A CHECK does:
ALTER TABLE dbo.Member ADD CONSTRAINT CK_Member_MentorId CHECK (MentorId <> MemberId);
UPDATE dbo.Member SET MentorId = MemberId WHERE MemberId = 2;
Msg 547, Level 16, State 1, Line 1
The UPDATE statement conflicted with the CHECK constraint "CK_Member_MentorId". The conflict occurred in database "Makerspace", table "dbo.Member".
The statement has been terminated.A CHECK sees one row at a time, so it stops a loop of one and nothing longer. Two members who mentor each other pass it, and so does a ring of three.
How to query a recursive relationship
A self join, dbo.Member joined to itself under two aliases, goes one level: each member beside their mentor. For the whole tree, SQL Server has the recursive common table expression. Its anchor member picks the rows at the top, those with no mentor. Its recursive member joins dbo.Member to the rows found by the pass before, one level per pass, and the query stops when a pass finds nothing:
WITH Chain AS ( SELECT MemberId, DisplayName, MentorId, 0 AS Depth FROM dbo.Member WHERE MentorId IS NULL UNION ALL SELECT m.MemberId, m.DisplayName, m.MentorId, c.Depth + 1 FROM dbo.Member AS m JOIN Chain AS c ON m.MentorId = c.MemberId ) SELECT MemberId, DisplayName, MentorId, Depth FROM Chain;
It returns all four members: Ines at depth 0, Hugo and Nadia at depth 1 under her, and Tariq at depth 2 under Nadia. Turn the join around, m.MemberId = c.MentorId, and start from one member instead of the top, and the same query walks up a chain to its root.
A ring the CHECK let through shows up on that walk. Make Ines a mentee of Tariq, and the chain from Tariq never reaches a member without a mentor:
BEGIN TRANSACTION; UPDATE dbo.Member SET MentorId = 4 WHERE MemberId = 1; WITH Up AS ( SELECT MemberId, MentorId, 0 AS Steps FROM dbo.Member WHERE MemberId = 4 UNION ALL SELECT m.MemberId, m.MentorId, u.Steps + 1 FROM dbo.Member AS m JOIN Up AS u ON m.MemberId = u.MentorId ) SELECT MemberId, MentorId, Steps FROM Up; ROLLBACK;
Msg 530, Level 16, State 1, Line 3
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.The walk goes Tariq, Nadia, Ines, Tariq again, and SQL Server returns 101 rows before it stops the statement at the default limit of 100 recursion levels. OPTION (MAXRECURSION n) on the query sets another limit, and 0 removes it, which turns a ring into a query that never ends. The transaction rolls Ines back. To keep rings out of the data, the check has to read other rows: before saving a new mentor, walk up from that mentor and refuse the change if the walk meets the member being changed. A trigger or the application can do that; a CHECK can’t.
Why SQL Server refuses a cascade on a self-reference
Nadia mentors Tariq, so the key refuses to let her go, as it would refuse any parent row that still has children:
DELETE FROM dbo.Member WHERE MemberId = 3;
Msg 547, Level 16, State 1, Line 1
The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "FK_Member_Member_MentorId". The conflict occurred in database "Makerspace", table "dbo.Member", column 'MentorId'.
The statement has been terminated.The usual answer for a parent with children is ON DELETE CASCADE. On a self-referencing foreign key SQL Server won’t create one. This is dbo.Member as above, its key declared with the cascade, run in a database of its own:
CREATE TABLE dbo.Member ( MemberId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Member PRIMARY KEY, DisplayName nvarchar(100) NOT NULL, MentorId int NULL, CONSTRAINT FK_Member_Member_MentorId FOREIGN KEY (MentorId) REFERENCES dbo.Member (MemberId) ON DELETE CASCADE );

Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'FK_Member_Member_MentorId' on table 'Member' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 1, Line 1
Could not create constraint or index. See previous errors.The key is part of CREATE TABLE, so the table isn’t created either. Msg 1785 comes from the key definitions alone, before any row exists: SQL Server refuses an action that could lead back to the table it started from, round a cycle or by two paths, and a self-reference is the shortest cycle there is. ON DELETE SET NULL gets the same message.
For mentoring, a cascade would be wrong anyway. When Nadia leaves, Tariq keeps his membership. So the key stays NO ACTION, and the delete does its own re-pointing first: Tariq moves to Nadia’s mentor, Ines.
SET XACT_ABORT ON; DECLARE @Leaving int = 3; BEGIN TRANSACTION; UPDATE dbo.Member SET MentorId = (SELECT MentorId FROM dbo.Member WHERE MemberId = @Leaving) WHERE MentorId = @Leaving; DELETE FROM dbo.Member WHERE MemberId = @Leaving; COMMIT;
Both statements run in one transaction, and with XACT_ABORT on, an error in the delete rolls back the update too, so a failed delete leaves nobody moved. Where the children really should go with the parent, as replies under a deleted comment do, the recursive CTE above can collect the whole subtree, and a single DELETE of those keys removes the parent and its children together.
Role names: what to call the column
dbo.Member already has a MemberId, so the foreign key column can’t take its parent column’s name, the usual choice in this schema (the primary key column name post covers why the key is MemberId and not Id). It takes a name for the part the parent row plays: MentorId. IDEF1X calls that a role name, “a name assigned to a foreign key attribute to represent the use of the foreign key in the entity” (FIPS 184, 2.58), and labels the column with the role name, a period and the name of the attribute it migrated from: MentorId.MemberId (3.14.3). The diagrams in this post write it that way too, so the box says both what the column means and which key it holds.
Role names aren’t only for self-references. The demo’s shop.Session has a HostId, the member who runs the session: a column named MemberId would hold the same value and say nothing about it. And a table with two foreign keys to one parent needs a role name for at least one of them, since it can’t hold two columns of one name. The standard’s own example is a part assembled from other parts, a many-to-many unary relationship.
A many-to-many unary relationship
New members need existing members to vouch for them. A member vouches for many people and is vouched for by several, and no single column on dbo.Member can hold that. A junction table can, with both its foreign keys pointing at dbo.Member:
CREATE TABLE dbo.Vouch ( MemberId int NOT NULL, VoucherId int NOT NULL, VouchedOn date NOT NULL, CONSTRAINT PK_Vouch PRIMARY KEY (MemberId, VoucherId), CONSTRAINT FK_Vouch_Member_MemberId FOREIGN KEY (MemberId) REFERENCES dbo.Member (MemberId), CONSTRAINT FK_Vouch_Member_VoucherId FOREIGN KEY (VoucherId) REFERENCES dbo.Member (MemberId), CONSTRAINT CK_Vouch_VoucherId CHECK (VoucherId <> MemberId) );

INSERT INTO dbo.Vouch (MemberId, VoucherId, VouchedOn) VALUES (4, 1, '2026-03-02'), (4, 2, '2026-03-05'), (2, 1, '2026-01-12'); SELECT m.DisplayName AS Member, v.DisplayName AS VouchedBy FROM dbo.Vouch AS x JOIN dbo.Member AS m ON m.MemberId = x.MemberId JOIN dbo.Member AS v ON v.MemberId = x.VoucherId;
Ines vouches for Hugo, and Ines and Hugo both vouch for Tariq. Reading the rows back takes dbo.Member twice, under two aliases, one per role. MemberId keeps its parent column’s name. IDEF1X asks for more: where the two occurrences can hold different values, each one gets a role name of its own, so its own example names both. The CHECK stops a member from vouching for themselves.
Cascades meet the same rule here. One of the two keys may cascade, so that deleting a member removes the vouches for them. With ON DELETE CASCADE on both, a delete in dbo.Member would reach dbo.Vouch by two paths, and the second key fails with Msg 1785 too.
How ERDsmith helps
Every problem above is there in the design before a row exists: a foreign key column that can’t take its parent column’s name, a second key to the same parent, a cascade that comes back round to its own table. In ERDsmith they show up while you draw:
- The mentor loop on
dbo.Memberis always non-identifying, and its column starts asParentMemberId, never onMemberIditself; renaming itMentorIdmakes that the role name. - The second line from
dbo.Membertodbo.Vouchasks for a role name for its column,MemberId2by default, which is whereVoucherIdcomes in. - A cascade on
FK_Member_Member_MentorId, or on both ofdbo.Vouch’s keys, marks the table with the error SQL Server would raise as Msg 1785.