What are the types of keys in a DBMS?
A key is a set of columns that uniquely identifies each row of a table. The types differ in which set they mean and what it is for:
- Super key: any set of columns that uniquely identifies each row, spare columns allowed.
- Candidate key: a minimal super key. Drop any column and it stops being unique.
- Primary key: the candidate key you choose to name rows by. One per table, never NULL.
- Alternate key: every candidate key you didn’t choose. SQL Server declares it as a
UNIQUEconstraint and calls it a unique key in its messages. - Foreign key: columns that hold another table’s key values, so each row points at a row there.
- Composite key: any key of two or more columns.
- Natural key: a key made of data the world already uses, such as an airport code.
- Surrogate key: a key the database makes up, such as an
IDENTITYnumber, with no meaning outside it.
Other lists count 7 to 10 types, but the extra ones are other names for these eight: unique key is SQL Server’s name for an alternate key, a compound key is a composite key whose columns are all foreign keys, and secondary key is another name for an alternate key.
The first five are roles a set of columns plays. The last three describe the columns themselves: how many there are and where their values come from. So one key can be several types at once. A primary key can be composite and natural, and a foreign key can sit inside a primary key.
Here is a small airline with every type in it; the airline reservation system template is the full one. Airports, flights between them, passengers, and a ticket for each passenger on each flight:
CREATE TABLE dbo.Airport ( AirportCode char(3) NOT NULL CONSTRAINT PK_Airport PRIMARY KEY, Name nvarchar(100) NOT NULL ); CREATE TABLE dbo.Flight ( FlightId int IDENTITY NOT NULL CONSTRAINT PK_Flight PRIMARY KEY, FlightNo varchar(6) NOT NULL, DepartsOn date NOT NULL, FromAirportCode char(3) NOT NULL, ToAirportCode char(3) NOT NULL, CONSTRAINT AK_Flight_FlightNo_DepartsOn UNIQUE (FlightNo, DepartsOn), CONSTRAINT FK_Flight_Airport_FromAirportCode FOREIGN KEY (FromAirportCode) REFERENCES dbo.Airport (AirportCode), CONSTRAINT FK_Flight_Airport_ToAirportCode FOREIGN KEY (ToAirportCode) REFERENCES dbo.Airport (AirportCode) ); CREATE TABLE dbo.Passenger ( PassengerId int IDENTITY NOT NULL CONSTRAINT PK_Passenger PRIMARY KEY, PassportCountry char(2) NOT NULL, PassportNo varchar(9) NOT NULL, Email nvarchar(254) NOT NULL, FullName nvarchar(100) NOT NULL, CONSTRAINT AK_Passenger_PassportCountry_PassportNo UNIQUE (PassportCountry, PassportNo), CONSTRAINT AK_Passenger_Email UNIQUE (Email) ); CREATE TABLE dbo.Ticket ( FlightId int NOT NULL, PassengerId int NOT NULL, SeatNo varchar(3) NULL, CONSTRAINT PK_Ticket PRIMARY KEY (FlightId, PassengerId), CONSTRAINT AK_Ticket_FlightId_SeatNo UNIQUE (FlightId, SeatNo), CONSTRAINT FK_Ticket_Flight_FlightId FOREIGN KEY (FlightId) REFERENCES dbo.Flight (FlightId), CONSTRAINT FK_Ticket_Passenger_PassengerId FOREIGN KEY (PassengerId) REFERENCES dbo.Passenger (PassengerId) );

SQL Server has three ways to declare a key: PRIMARY KEY, UNIQUE and FOREIGN KEY. Super keys and candidate keys are words for the design. They exist in the data, declared or not, and SQL Server enforces only the ones you write down.
Super key and candidate key
Take dbo.Passenger. Three things pick out one passenger: the PassengerId the database hands out, the email address, and the passport, which is a country and a number. Nothing stops two countries from issuing the same number, so the number alone isn’t enough.
The super keys are each of those three, plus every set of columns that contains one of them: (PassengerId, FullName), (Email, PassportNo), the whole row. FullName alone is not a super key, since two people named Ana Lima can fly on the same day.
The candidate keys are the super keys with nothing spare: PassengerId, Email, and (PassportCountry, PassportNo). (PassengerId, FullName) is not one, because PassengerId is unique without the name.
A candidate key only works as a key once you declare it. One you know about but leave undeclared is data that happens to be unique today: SQL Server won’t keep it unique, and a foreign key can’t point at it, which is the cause of Msg 1776. In the diagram, the declared candidate keys are the primary key, above the line in each box, and the AK marks.
Primary key and alternate key (unique key in SQL Server)
From the candidates, one becomes the primary key. PassengerId is the usual choice: four bytes, never NULL, and it never changes, while people change email addresses and renew passports. The other candidates become alternate keys, each one a UNIQUE constraint. The two declarations differ in a few ways:
- A table has one primary key and any number of unique constraints.
- Primary key columns are NOT NULL. A unique constraint’s columns may allow NULL.
- A primary key builds a clustered index unless the table already has one. A unique constraint builds a unique nonclustered index.
- A foreign key can point at either.
NULL is where the two part ways in practice. A unique constraint treats NULL as a value like any other, so it allows one NULL per column, or one per combination of values. dbo.Ticket has an alternate key (FlightId, SeatNo) so that two passengers can’t hold the same seat on a flight. Seats are given at check-in, so SeatNo stays NULL until then. The first seatless ticket on flight 1 goes in; the second doesn’t:
INSERT INTO dbo.Airport (AirportCode, Name) VALUES ('SEA', N'Seattle-Tacoma International'), ('SFO', N'San Francisco International'); INSERT INTO dbo.Flight (FlightNo, DepartsOn, FromAirportCode, ToAirportCode) VALUES ('ZZ204', '2026-06-01', 'SEA', 'SFO'); INSERT INTO dbo.Passenger (PassportCountry, PassportNo, Email, FullName) VALUES ('US', '561234987', N'ana@example.com', N'Ana Lima'), ('CA', 'AB1234567', N'ben@example.com', N'Ben Okafor'); INSERT INTO dbo.Ticket (FlightId, PassengerId, SeatNo) VALUES (1, 1, NULL); INSERT INTO dbo.Ticket (FlightId, PassengerId, SeatNo) VALUES (1, 2, NULL);
Msg 2627, Level 14, State 1, Line 13
Violation of UNIQUE KEY constraint 'AK_Ticket_FlightId_SeatNo'. Cannot insert duplicate key in object 'dbo.Ticket'. The duplicate key value is (1, <NULL>).
The statement has been terminated.SQL Server saw (1, NULL) twice and called it a duplicate. The message also gives the alternate key its SQL Server name: a UNIQUE KEY constraint.
The fix is a unique index that leaves NULL out, with a WHERE filter:
ALTER TABLE dbo.Ticket DROP CONSTRAINT AK_Ticket_FlightId_SeatNo; CREATE UNIQUE INDEX IX_Ticket_FlightId_SeatNo ON dbo.Ticket (FlightId, SeatNo) WHERE SeatNo IS NOT NULL; INSERT INTO dbo.Ticket (FlightId, PassengerId, SeatNo) VALUES (1, 2, NULL);

The second seatless ticket now goes in, and two passengers given seat 12A on flight 1 still fail, with Msg 2601 from the index. A filtered index isn’t a candidate key, though, so a foreign key on (FlightId, SeatNo) fails with Msg 1776.
Foreign key
dbo.Ticket holds two foreign keys: FlightId takes its values from dbo.Flight, and PassengerId from dbo.Passenger. A ticket can’t name a flight that doesn’t exist, and a flight with tickets can’t be deleted: a foreign key constraint checks both.
dbo.Flight points at dbo.Airport twice, once for where it leaves from and once for where it lands. A table can’t hold two columns named AirportCode, so each one gets a role name: FromAirportCode and ToAirportCode. The post on the primary key column name shows where role names come in when keys are named after their table. The diagram writes the role name and then the parent’s column, FromAirportCode.AirportCode, so you can still see which key it holds.
Each foreign key is one line in the diagram, drawn in crow’s foot notation. The parent’s end carries no mark : the foreign key columns are NOT NULL, so every flight has exactly one airport of departure. A nullable foreign key would put a ring there, for zero or one. The ring and the foot at the child’s end say an airport has zero or more flights. A dashed line means the foreign key sits outside the child’s primary key. A solid one means it is part of that key, as in dbo.Ticket.
Composite key
The schema has three composite keys:
dbo.Ticket’s primary key,(FlightId, PassengerId): one ticket per passenger per flight.dbo.Flight’s alternate key,(FlightNo, DepartsOn): flight ZZ204 leaves every day, and every day has many flights, but the pair happens once.dbo.Passenger’s alternate key,(PassportCountry, PassportNo).
Each column of a composite key repeats on its own; only the combination is unique. A foreign key that points at one has to carry all of its columns, in the key’s order. Composite key covers that, and when a composite primary key is a better choice than a single column.
Natural key and surrogate key
dbo.Airport uses a natural key. SEA and SFO are the codes travelers and airlines already use. They are short and rarely change, so they make good keys and readable foreign key values.
dbo.Flight uses a surrogate key, FlightId, an IDENTITY column. Its natural key, (FlightNo, DepartsOn), is two columns wide, so every ticket would have to carry both, and an airline can renumber a flight. With a surrogate, a ticket holds four bytes that never need to change.
A surrogate key is unique by construction, so it never stops a duplicate. Keep the natural key as an alternate key, or the same flight can be stored twice under two ids. With AK_Flight_FlightNo_DepartsOn in place, inserting ZZ204 on June 1 a second time fails:
INSERT INTO dbo.Flight (FlightNo, DepartsOn, FromAirportCode, ToAirportCode) VALUES ('ZZ204', '2026-06-01', 'SEA', 'SFO');
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'AK_Flight_FlightNo_DepartsOn'. Cannot insert duplicate key in object 'dbo.Flight'. The duplicate key value is (ZZ204, 2026-06-01).
The statement has been terminated.Without that constraint the row would go in under a new FlightId, and tickets would split between two copies of one flight. Surrogate key vs natural key weighs the two choices further.
How ERDsmith helps
The filtered seat index that no foreign key can point at, and dbo.Flight’s second airport column, are where this schema goes wrong first. ERDsmith settles both on the diagram:
- A relationship onto a filtered unique index is an error, as SQL Server’s Msg 1776 would be.
- A second relationship from
dbo.Flighttodbo.Airportasks for a role name for its column, or you cancel it. - A relationship into
dbo.Ticketgives it one foreign key column per parent key column, in key order.