Cardinality in DBMS: the types and the SQL that sets them

Cardinality in DBMS is how many rows of one table can relate to one row of another. The four types, minimum and maximum, and the SQL behind each.

What is cardinality in DBMS?

Cardinality in DBMS is the number of rows of one table that can relate to one row of another table. In ER terms, it is the number of times an entity of one entity set can take part in a relationship with an entity of the other. A relationship has two cardinalities, one for each direction, and the pair gives its type, the cardinality ratio:

  • One-to-one (1:1): a ticket has at most one refund, and a refund is for one ticket.
  • One-to-many (1:N): a train makes many runs, and each run belongs to one train.
  • Many-to-one (N:1): the same relationship read from the other side, runs to their train.
  • Many-to-many (M:N): a train stops at many stations, and a station serves many trains.

Each direction has a maximum and a minimum. The maximum is the cardinality proper: one or many. The minimum is the optionality (sometimes called modality): zero if a row may have no partner, one if it must have one. ER texts call a minimum of one total participation. Together they give four answers to “how many”: zero or one , exactly one , zero or more and one or more . Chen diagrams write 1, N and M on the lines instead; the ER diagram symbols post puts the notations side by side.

The examples come from a railway, cut down from the railway reservation system template with two changes: this post adds dbo.Refund, which the template doesn’t have, and lets Ticket.SeatId be NULL, where the template’s is NOT NULL. A train makes runs, at most one per date, and each run is a row of dbo.Journey:

CREATE TABLE dbo.Train (
  TrainId     int NOT NULL CONSTRAINT PK_Train PRIMARY KEY,
  TrainNumber varchar(10)   NOT NULL,
  Name        nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Journey (
  JourneyId int  NOT NULL CONSTRAINT PK_Journey PRIMARY KEY,
  TrainId   int  NOT NULL,
  DepartsOn date NOT NULL,
  CONSTRAINT AK_Journey_TrainId_DepartsOn
    UNIQUE (TrainId, DepartsOn),
  CONSTRAINT FK_Journey_Train_TrainId FOREIGN KEY (TrainId)
    REFERENCES dbo.Train (TrainId)
);
dbo.Train and dbo.Journey; a dashed line from Train to Journey with no mark at Train and a ring and a foot at Journey.
No mark at dbo.Train: Journey.TrainId is NOT NULL, so the end is exactly one. A ring and a foot at dbo.Journey because TrainId repeats there.

Read the line once in each direction. A run has exactly one train, and a train has zero or more runs . Textbook crow’s foot draws that exactly one as two bars at the parent; the pictures here leave the parent’s end bare when its foreign key is NOT NULL, as their legend says. The crow’s foot notation post covers how to read every end.

The word has other meanings in a DBMS. The cardinality of a table is its number of rows, and the cardinality of a column is how many distinct values it holds: high for a PNR, low for a travel class. SQL Server’s cardinality estimator guesses how many rows each step of a query returns, to pick a plan. Those describe data. The rest of this post is about relationships, which you decide when you design the tables.

How SQL sets the maximum

The maximum at the parent’s end is always one. A foreign key column holds one value per row, and that value matches one row of the parent’s key. So the question SQL answers is the maximum at the child’s end: how many child rows may repeat the same value.

A plain foreign key lets it repeat, so the child’s end is many. A booking holds tickets, and each ticket is for one run:

CREATE TABLE dbo.Booking (
  BookingId int      NOT NULL CONSTRAINT PK_Booking PRIMARY KEY,
  Pnr       char(10) NOT NULL CONSTRAINT AK_Booking_Pnr UNIQUE
);
CREATE TABLE dbo.Ticket (
  TicketId  int          NOT NULL
    CONSTRAINT PK_Ticket PRIMARY KEY,
  BookingId int          NOT NULL,
  JourneyId int          NOT NULL,
  Fare      decimal(8,2) NOT NULL,
  CONSTRAINT FK_Ticket_Booking_BookingId FOREIGN KEY (BookingId)
    REFERENCES dbo.Booking (BookingId),
  CONSTRAINT FK_Ticket_Journey_JourneyId FOREIGN KEY (JourneyId)
    REFERENCES dbo.Journey (JourneyId)
);
dbo.Train, dbo.Journey, dbo.Booking and dbo.Ticket; dashed lines from Train to Journey, from Booking to Ticket and from Journey to Ticket, each with no mark at the parent and a ring and a foot at the child.
Neither BookingId nor JourneyId is unique in dbo.Ticket, so both lines end in zero or more at the ticket.
INSERT INTO dbo.Train (TrainId, TrainNumber, Name)
VALUES (1, '10101', N'Morning Express');
INSERT INTO dbo.Journey (JourneyId, TrainId, DepartsOn)
VALUES (10, 1, '2026-03-02'), (11, 1, '2026-03-03');
INSERT INTO dbo.Booking (BookingId, Pnr)
VALUES (100, '4521873390');
INSERT INTO dbo.Ticket (TicketId, BookingId, JourneyId, Fare)
VALUES (1000, 100, 10, 845.00), (1001, 100, 10, 845.00);
INSERT INTO dbo.Ticket (TicketId, BookingId, JourneyId, Fare)
VALUES (1002, 100, NULL, 845.00);
Msg 515, Level 16, State 2, Line 9
Cannot insert the value NULL into column 'JourneyId', table 'Railway.dbo.Ticket'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Booking 100 takes two tickets on run 10, so both lines are many at the ticket’s end. Ticket 1002 has no run and fails with Msg 515, which belongs to the minimum, below.

To cap the child’s end at one, make the foreign key unique. A ticket is refunded once at most:

CREATE TABLE dbo.Refund (
  RefundId   int          NOT NULL
    CONSTRAINT PK_Refund PRIMARY KEY,
  TicketId   int          NOT NULL,
  Amount     decimal(8,2) NOT NULL,
  RefundedAt datetime2(0) NOT NULL,
  CONSTRAINT AK_Refund_TicketId UNIQUE (TicketId),
  CONSTRAINT FK_Refund_Ticket_TicketId FOREIGN KEY (TicketId)
    REFERENCES dbo.Ticket (TicketId)
);
dbo.Train, dbo.Journey, dbo.Booking, dbo.Ticket and dbo.Refund; a dashed line from Ticket to Refund with no mark at Ticket and a ring and a bar at Refund, whose TicketId is marked FK and AK.
AK_Refund_TicketId makes TicketId unique, so the end at dbo.Refund is zero or one: a one-to-one relationship.
INSERT INTO dbo.Refund (RefundId, TicketId, Amount, RefundedAt)
VALUES (1, 1001, 760.50, '2026-03-01 09:15');
INSERT INTO dbo.Refund (RefundId, TicketId, Amount, RefundedAt)
VALUES (2, 1001, 760.50, '2026-03-01 09:16');
Msg 2627, Level 14, State 1, Line 3
Violation of UNIQUE KEY constraint 'AK_Refund_TicketId'. Cannot insert duplicate key in object 'dbo.Refund'. The duplicate key value is (1001).
The statement has been terminated.

The second refund for ticket 1001 fails with Msg 2627. Making TicketId the primary key of dbo.Refund would cap it the same way; SQL Server enforces a primary key and a unique constraint each with a unique index.

A unique key caps the count only when it covers the foreign key and nothing more. dbo.Journey has a unique key on (TrainId, DepartsOn): a train runs once per date, but on many dates, so its end stays zero or more.

How SQL sets the minimum

At the parent’s end, the minimum is the foreign key column’s nullability. NOT NULL means every child row names a parent: exactly one. That was the Msg 515 above: JourneyId is NOT NULL, so every ticket has exactly one run. NULL means a row may name none: zero or one.

A seat works differently. A waitlisted ticket is sold before it has a seat, so the new column allows NULL:

CREATE TABLE dbo.Coach (
  CoachId   int        NOT NULL CONSTRAINT PK_Coach PRIMARY KEY,
  TrainId   int        NOT NULL,
  CoachCode varchar(5) NOT NULL,
  CONSTRAINT FK_Coach_Train_TrainId FOREIGN KEY (TrainId)
    REFERENCES dbo.Train (TrainId)
);
CREATE TABLE dbo.Seat (
  SeatId     int      NOT NULL CONSTRAINT PK_Seat PRIMARY KEY,
  CoachId    int      NOT NULL,
  SeatNumber smallint NOT NULL,
  CONSTRAINT FK_Seat_Coach_CoachId FOREIGN KEY (CoachId)
    REFERENCES dbo.Coach (CoachId)
);
ALTER TABLE dbo.Ticket ADD SeatId int NULL
  CONSTRAINT FK_Ticket_Seat_SeatId
    REFERENCES dbo.Seat (SeatId);
dbo.Train with dbo.Coach and dbo.Journey, dbo.Seat in a coach, dbo.Booking, and dbo.Ticket with BookingId, JourneyId and SeatId; a ring alone at dbo.Seat on its line to Ticket, no mark at any other parent, a ring and a foot at every child.
Ticket.SeatId allows NULL, so the end at dbo.Seat is zero or one, a ring; every other parent's end is exactly one, with no mark.
INSERT INTO dbo.Coach (CoachId, TrainId, CoachCode)
VALUES (1, 1, 'C1');
INSERT INTO dbo.Seat (SeatId, CoachId, SeatNumber)
VALUES (1, 1, 14);
UPDATE dbo.Ticket SET SeatId = 1 WHERE TicketId = 1000;
INSERT INTO dbo.Ticket (TicketId, BookingId, JourneyId, Fare)
VALUES (1003, 100, 11, 845.00);
INSERT INTO dbo.Ticket
  (TicketId, BookingId, JourneyId, Fare, SeatId)
VALUES (1004, 100, 11, 845.00, 99);
Msg 547, Level 16, State 1, Line 8
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Ticket_Seat_SeatId". The conflict occurred in database "Railway", table "dbo.Seat", column 'SeatId'.
The statement has been terminated.

Ticket 1000 gets seat 14 in coach C1. Ticket 1003 goes in with no seat, since SQL Server doesn’t check a NULL against dbo.Seat. Ticket 1004 names seat 99, which doesn’t exist, and Msg 547 stops it.

At the child’s end the minimum is always zero. Train 1 went in before any run, booking 100 before any ticket, and seat 14 before ticket 1000 named it. A foreign key constrains the child’s rows and never asks a parent for children.

Many-to-many needs a table of its own

One foreign key gives one-to-many at most, so a many-to-many between dbo.Train and dbo.Station becomes a junction table, also called a link table, with a foreign key to each. Every row is one stop:

CREATE TABLE dbo.Station (
  StationId int           NOT NULL
    CONSTRAINT PK_Station PRIMARY KEY,
  Code      varchar(5)    NOT NULL,
  Name      nvarchar(100) NOT NULL
);
CREATE TABLE dbo.TrainStop (
  TrainId    int      NOT NULL,
  StopNumber tinyint  NOT NULL,
  StationId  int      NOT NULL,
  DepartsAt  time(0)  NULL,
  CONSTRAINT PK_TrainStop PRIMARY KEY (TrainId, StopNumber),
  CONSTRAINT AK_TrainStop_TrainId_StationId
    UNIQUE (TrainId, StationId),
  CONSTRAINT FK_TrainStop_Train_TrainId FOREIGN KEY (TrainId)
    REFERENCES dbo.Train (TrainId),
  CONSTRAINT FK_TrainStop_Station_StationId
    FOREIGN KEY (StationId)
    REFERENCES dbo.Station (StationId)
);
dbo.Station, dbo.Train, dbo.Journey and dbo.TrainStop; a solid line from Train to TrainStop and a dashed line from Station to TrainStop, each with no mark at the parent and a ring and a foot at TrainStop, which has round corners.
Two one-to-many lines into dbo.TrainStop make the many-to-many. The line from dbo.Train is solid because TrainId is part of the primary key.
INSERT INTO dbo.Station (StationId, Code, Name)
VALUES (1, 'NDLS', N'New Delhi'), (2, 'AGC', N'Agra Cantt'),
       (3, 'GWL', N'Gwalior');
INSERT INTO dbo.Train (TrainId, TrainNumber, Name)
VALUES (2, '10202', N'Valley Mail');
INSERT INTO dbo.TrainStop (TrainId, StopNumber, StationId)
VALUES (1, 1, 1), (1, 2, 2), (1, 3, 3),
       (2, 1, 1), (2, 2, 2);
INSERT INTO dbo.TrainStop (TrainId, StopNumber, StationId)
VALUES (1, 4, 2);
Msg 2627, Level 14, State 1, Line 9
Violation of UNIQUE KEY constraint 'AK_TrainStop_TrainId_StationId'. Cannot insert duplicate key in object 'dbo.TrainStop'. The duplicate key value is (1, 2).
The statement has been terminated.

Train 1 stops at New Delhi, Agra Cantt and Gwalior, and train 2 at New Delhi and Agra Cantt. New Delhi serves two trains and each train calls at several stations: many on both sides. The last row would bring train 1 back to Agra Cantt as stop 4, and AK_TrainStop_TrainId_StationId refuses it, so a pair occurs once. The primary key is a composite key, the train and its stop number.

Why SQL can’t enforce one or more

Two ends are missing from every diagram above: one or more at a child, and exactly one at a child. Both say a parent row must have a child row. “Every booking holds at least one ticket” is a real rule of the railway, and SQL Server has no constraint for it.

INSERT INTO dbo.Booking (BookingId, Pnr)
VALUES (101, '4521873391');
INSERT INTO dbo.Ticket (TicketId, BookingId, JourneyId, Fare)
VALUES (1005, 102, 10, 845.00);
Msg 547, Level 16, State 1, Line 3
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Ticket_Booking_BookingId". The conflict occurred in database "Railway", table "dbo.Booking", column 'BookingId'.
The statement has been terminated.

Booking 101 goes in with no tickets, and nothing complains. The ticket for booking 102 fails, because that booking doesn’t exist. So the order is fixed: the parent first, then its children, and in between the parent has none. A constraint that demanded a child would refuse the parent.

The first thing to try is a check constraint with EXISTS over dbo.Ticket, and it stops at Msg 1046, “Subqueries are not allowed in this context”. Wrapped in a function, the same test compiles:

CREATE FUNCTION dbo.TicketCount (@BookingId int)
RETURNS int
AS
BEGIN
  RETURN (SELECT COUNT(*) FROM dbo.Ticket
          WHERE BookingId = @BookingId);
END;
GO
ALTER TABLE dbo.Booking WITH NOCHECK
  ADD CONSTRAINT CK_Booking_HasTicket
    CHECK (dbo.TicketCount(BookingId) >= 1);
INSERT INTO dbo.Booking (BookingId, Pnr)
VALUES (102, '4521873392');
Msg 547, Level 16, State 1, Line 1
The INSERT statement conflicted with the CHECK constraint "CK_Booking_HasTicket". The conflict occurred in database "Railway", table "dbo.Booking", column 'BookingId'.
The statement has been terminated.

WITH NOCHECK lets booking 101 stay. Booking 102 then fails on its own insert: at that moment it has no tickets, and with this constraint it never can.

So one or more lives outside the constraints. The application inserts a booking and its first ticket in one transaction, and a query for bookings without tickets finds whatever slipped through. A diagram read from the tables still shows zero or more at dbo.Ticket, whatever the rule says.

How ERDsmith helps

Each mark on these lines traces back to a column’s NULL or NOT NULL, a unique key or a primary key: the cardinality is fixed at design time, before any data goes in. In ERDsmith the parent’s end is read from the columns:

  • The parent’s end follows the foreign key’s columns: a ring , zero or one, when all of them allow NULL, as at dbo.Seat, and no mark , exactly one, when they don’t.
  • A relationship whose columns all sit in the child’s primary key, as from dbo.Train to dbo.TrainStop, is mandatory: its parent’s end never gets a ring , since no key column can be NULL.