One to one relationship in SQL Server, and one-to-many

A one to one relationship links each row to at most one row of another table. Both ways to build it in SQL Server, one-to-many beside it, and when to merge.

What is a one-to-one relationship?

A one-to-one relationship links each row of one table to at most one row of another table, and back: each row of that other table links to at most one row of the first. A one-to-many relationship lets one row of the parent table match many rows of the child table, while each child row still matches one parent. Both are cardinalities: they say how many rows can sit at each end of a relationship.

One-to-many is the common case. At a bank, a customer opens several accounts and takes out several loans, and each account and each loan belongs to one customer. One-to-one is rarer: a customer has at most one online banking login, and a debit card serves one account. This post cuts the bank management system template down to a few tables and the columns that matter here, and adds two tables the template doesn’t have, dbo.OnlineLogin and dbo.CustomerTax.

One-to-many: a plain foreign key

The child table holds the foreign key. dbo.Account and dbo.Loan each carry a CustomerId that points at dbo.Customer:

CREATE TABLE dbo.Customer (
  CustomerId int NOT NULL CONSTRAINT PK_Customer PRIMARY KEY,
  FullName   nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Account (
  AccountId  int  NOT NULL CONSTRAINT PK_Account PRIMARY KEY,
  CustomerId int  NOT NULL,
  OpenedOn   date NOT NULL,
  CONSTRAINT FK_Account_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId)
);
CREATE TABLE dbo.Loan (
  LoanId     int NOT NULL CONSTRAINT PK_Loan PRIMARY KEY,
  CustomerId int NOT NULL,
  Amount     decimal(12, 2) NOT NULL,
  CONSTRAINT FK_Loan_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId)
);
dbo.Customer with primary key CustomerId; dbo.Account and dbo.Loan, each with a foreign key CustomerId; dashed lines from Customer to Account and to Loan, no mark at Customer, a ring and a foot at each child; a legend under the diagram.
Both foreign keys are NOT NULL and not unique: no mark at dbo.Customer, a ring and a foot at each child.

Nothing stops two accounts from holding the same CustomerId, so a customer can have many. Each account row has one CustomerId value, so an account has one customer, and because the column is NOT NULL and checked against dbo.Customer, it has to be a customer that exists:

INSERT INTO dbo.Customer (CustomerId, FullName)
VALUES (1, N'Maya Chen');
INSERT INTO dbo.Account (AccountId, CustomerId, OpenedOn)
VALUES (10, 1, '2026-03-02'), (11, 1, '2026-05-18');
INSERT INTO dbo.Loan (LoanId, CustomerId, Amount)
VALUES (700, 1, 25000.00);
INSERT INTO dbo.Account (AccountId, CustomerId, OpenedOn)
VALUES (12, 2, '2026-05-20');
Msg 547, Level 16, State 1, Line 7
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Account_Customer_CustomerId". The conflict occurred in database "Bank", table "dbo.Customer", column 'CustomerId'.
The statement has been terminated.

Maya’s two accounts and her loan go in. Account 12 names customer 2, who isn’t in dbo.Customer, and Msg 547 refuses it. In crow’s foot notation the line reads exactly one at the customer, which textbooks draw as two bars and these pictures leave bare , and zero or more at the account. The minimum is zero because SQL Server never requires a customer to have an account.

A joint account, held by two customers, is what one foreign key can’t store. That is many-to-many, and it takes a junction table, a link table with a foreign key to each side.

A child keyed by its parent’s key

To make a relationship one-to-one, start from the one-to-many and cap the child’s end at one row per parent. SQL Server does that with uniqueness on the foreign key, and there are two places to put it: the child’s primary key, or a unique constraint.

The online login exists only for its customer, so its primary key is the customer’s key. CustomerId is the login’s primary key and its foreign key at once:

CREATE TABLE dbo.OnlineLogin (
  CustomerId   int          NOT NULL,
  UserName     nvarchar(50) NOT NULL,
  LastSignInAt datetime2(0) NULL,
  CONSTRAINT PK_OnlineLogin PRIMARY KEY (CustomerId),
  CONSTRAINT FK_OnlineLogin_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId)
);
dbo.Customer, dbo.Account and dbo.Loan as before; dbo.OnlineLogin whose primary key CustomerId is also its foreign key; a solid line from Customer to OnlineLogin with no mark at Customer and a bar and a ring at OnlineLogin, whose box has rounded corners.
CustomerId is the whole primary key of dbo.OnlineLogin and its foreign key: a solid line, a bar and a ring at the login, and a rounded box.
INSERT INTO dbo.OnlineLogin (CustomerId, UserName)
VALUES (1, N'maya.chen');
INSERT INTO dbo.OnlineLogin (CustomerId, UserName)
VALUES (1, N'mchen');
Msg 2627, Level 14, State 1, Line 3
Violation of PRIMARY KEY constraint 'PK_OnlineLogin'. Cannot insert duplicate key in object 'dbo.OnlineLogin'. The duplicate key value is (1).
The statement has been terminated.

Maya’s first login goes in. A second login for customer 1 fails on PK_OnlineLogin with Msg 2627, so the login’s end is zero or one . Because the foreign key is the primary key, the relationship is identifying: a login can’t be named without its customer. That is why the line is solid and the box has rounded corners . And since primary key columns can’t be NULL, every login has a customer.

This shape fits a table that only extends its parent. It needs no key of its own, and a table that references it carries the customer’s key.

A unique foreign key

A card is a thing of its own, with a number and an expiry date, and it can be blocked and replaced. So dbo.Card keeps its own key, CardId, and a unique constraint on its foreign key makes the relationship one-to-one:

CREATE TABLE dbo.Card (
  CardId     int      NOT NULL CONSTRAINT PK_Card PRIMARY KEY,
  AccountId  int      NOT NULL,
  CardNumber char(16) NOT NULL,
  ExpiresOn  date     NOT NULL,
  CONSTRAINT AK_Card_AccountId UNIQUE (AccountId),
  CONSTRAINT FK_Card_Account_AccountId
    FOREIGN KEY (AccountId)
    REFERENCES dbo.Account (AccountId)
);
dbo.Customer, dbo.Account and dbo.Loan as before; dbo.Card with primary key CardId and AccountId marked FK and AK; a dashed line from Account to Card with no mark at Account and a bar and a ring at Card.
AccountId is NOT NULL and unique, marked FK and AK: a dashed line, no mark at dbo.Account, a bar and a ring at dbo.Card.
INSERT INTO dbo.Card (CardId, AccountId, CardNumber, ExpiresOn)
VALUES (500, 10, '4000123412341234', '2030-08-31'),
       (501, 11, '4000123455556666', '2030-11-30');
INSERT INTO dbo.Card (CardId, AccountId, CardNumber, ExpiresOn)
VALUES (502, 10, '4000123498769876', '2031-02-28');
Msg 2627, Level 14, State 1, Line 4
Violation of UNIQUE KEY constraint 'AK_Card_AccountId'. Cannot insert duplicate key in object 'dbo.Card'. The duplicate key value is (10).
The statement has been terminated.

Each account gets one card. A second card for account 10 fails on AK_Card_AccountId, again Msg 2627, this time for a UNIQUE KEY constraint. Drop the constraint and the same table is one-to-many, with nothing else to change, which makes this the shape to pick when the rule may loosen later. Banks replace lost cards, and once the old card’s row has to stay, one card per account stops being true. The bank template keeps every card and allows one unblocked card of each type per account, with a filtered unique index on (AccountId, CardType) WHERE BlockedOn IS NULL.

A nullable unique foreign key has a catch in SQL Server: the unique constraint treats NULLs as equal. With AccountId nullable, a first card with no account goes in and a second fails with Msg 2627, the duplicate key value (<NULL>). To allow any number of unlinked cards and still one card per account, use a filtered unique index with WHERE AccountId IS NOT NULL in place of the constraint.

When should a one-to-one be one table?

SQL Server enforces the child’s end of both designs, never the parent’s: a customer row goes in with no login, and nothing complains. Every one-to-one a schema can declare is one to zero-or-one. For the login that is right, since plenty of customers never bank online. It is wrong when the second row is required.

Say the bank had put each customer’s tax ID in a table of its own:

CREATE TABLE dbo.CustomerTax (
  CustomerId int     NOT NULL,
  TaxId      char(9) NOT NULL,
  CONSTRAINT PK_CustomerTax PRIMARY KEY (CustomerId),
  CONSTRAINT FK_CustomerTax_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId)
);
dbo.Customer, dbo.Account and dbo.Loan as before; dbo.CustomerTax whose primary key CustomerId is also its foreign key; a solid line from Customer to CustomerTax with a bar and a ring at CustomerTax.
dbo.CustomerTax is keyed by CustomerId like the login: a bar and a ring at its end, so a customer with no tax row is valid.
INSERT INTO dbo.CustomerTax (CustomerId, TaxId)
VALUES (1, '123456789');
INSERT INTO dbo.Customer (CustomerId, FullName)
VALUES (2, N'Leo Park');

Maya gets her tax ID. Leo goes into dbo.Customer with none, and both statements succeed: no constraint can say that every customer needs a dbo.CustomerTax row. A trigger or the application could check it, but the column belongs in dbo.Customer, where NOT NULL does the job. This dbo.Customer replaces the one above, and dbo.CustomerTax goes:

CREATE TABLE dbo.Customer (
  CustomerId int           NOT NULL
    CONSTRAINT PK_Customer PRIMARY KEY,
  FullName   nvarchar(100) NOT NULL,
  TaxId      char(9)       NOT NULL
);
dbo.Customer with primary key CustomerId and the columns FullName and TaxId; no relationships.
TaxId is a NOT NULL column of dbo.Customer, so there is no second table and no line to draw.
INSERT INTO dbo.Customer (CustomerId, FullName, TaxId)
VALUES (1, N'Maya Chen', '123456789');
INSERT INTO dbo.Customer (CustomerId, FullName)
VALUES (2, N'Leo Park');
Msg 515, Level 16, State 2, Line 3
Cannot insert the value NULL into column 'TaxId', table 'Bank.dbo.Customer'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Now Leo without a tax ID fails with Msg 515. So merge the two tables when every parent must have the child row and the two are read together. Keep the one-to-one when one of these holds:

  • Most parents have no child row, like the login: one table would carry a block of NULL columns for every customer who never signs up.
  • The child holds data with access rules of its own, such as sign-in details, which you can grant on one table and deny on the other.
  • The child describes a subtype: only some loans are mortgages, and the property’s address and value belong to those loans alone, in a table keyed by LoanId.

The primary key vs foreign key post covers what SQL Server checks on each side of a relationship.

How ERDsmith helps

Whether a relationship is one-to-one or one-to-many, and whether it should exist at all, is decided by keys and NULLs: a primary key on the foreign key, a unique constraint beside it, a column that could have been NOT NULL in the parent. In ERDsmith those keys are on the boxes and the lines follow them, so the choice is visible while the tables are still being designed:

  • A column in a unique key carries AK on its row, beside FK when it is a foreign key too, as dbo.Card.AccountId does above: the mark that tells this one-to-one from a one-to-many on the same columns.
  • A unique constraint with a filter is marked as an error, with its fix: write it as a unique index. So the WHERE AccountId IS NOT NULL rule for unlinked cards ends up a filtered index, as above.