What is the difference between a surrogate key and a natural key?
A natural key uniquely identifies a row with data the world already uses: a doctor’s provider number, a patient’s email, an appointment’s doctor and start time. A surrogate key uniquely identifies a row with a value the database makes up, usually a number from IDENTITY, with no meaning outside it. Both can be candidate keys, so either could be the primary key. The choice is which one the other tables hold.
Here is a small clinic keyed by natural keys only:
CREATE TABLE dbo.Doctor ( Npi char(10) NOT NULL CONSTRAINT PK_Doctor PRIMARY KEY, FullName nvarchar(100) NOT NULL ); CREATE TABLE dbo.Patient ( Email nvarchar(254) NOT NULL CONSTRAINT PK_Patient PRIMARY KEY, FullName nvarchar(100) NOT NULL, BirthDate date NOT NULL ); CREATE TABLE dbo.Appointment ( Npi char(10) NOT NULL, StartsAt datetime2(0) NOT NULL, Email nvarchar(254) NOT NULL, CONSTRAINT PK_Appointment PRIMARY KEY (Npi, StartsAt), CONSTRAINT FK_Appointment_Doctor_Npi FOREIGN KEY (Npi) REFERENCES dbo.Doctor (Npi), CONSTRAINT FK_Appointment_Patient_Email FOREIGN KEY (Email) REFERENCES dbo.Patient (Email) );

Each key here means something. Npi is the doctor’s National Provider Identifier, a 10-digit number that carries no other information about the provider. Email names the patient. An appointment is a doctor and a start time, a composite key that also stops one doctor from being booked twice at the same minute.
Natural keys also travel. Every appointment stores a copy of the patient’s email, and any table that points at an appointment would store the doctor’s NPI and the start time as well.
What happens when a natural key value changes?
People change their email. Sam has two appointments when he does:
INSERT INTO dbo.Doctor (Npi, FullName) VALUES ('1234567893', N'Ana Ruiz'); INSERT INTO dbo.Patient (Email, FullName, BirthDate) VALUES (N'sam@example.com', N'Sam Lee', '1990-04-12'); INSERT INTO dbo.Appointment (Npi, StartsAt, Email) VALUES ('1234567893', '2026-03-02 09:00', N'sam@example.com'), ('1234567893', '2026-03-09 09:00', N'sam@example.com'); UPDATE dbo.Patient SET Email = N'sam.lee@example.com' WHERE Email = N'sam@example.com';
Msg 547, Level 16, State 1, Line 8
The UPDATE statement conflicted with the REFERENCE constraint "FK_Appointment_Patient_Email". The conflict occurred in database "Clinic", table "dbo.Appointment", column 'Email'.
The statement has been terminated.The inserts work and the update fails. Two appointments still hold sam@example.com, and the foreign key won’t let the patient’s key change under them. The message comes from the parent’s side, so it names the REFERENCE constraint and the child table.
There are two ways to get the change through:
- Declare the foreign key
ON UPDATE CASCADE. In a run with that foreign key, the same update succeeds and SQL Server rewrites both appointments inside the one statement, though it reports only the patient row as affected. Every table that copied the email is written too, and each of their foreign keys needs its own cascade, as ON DELETE CASCADE explains for deletes and updates alike. - Move the rows yourself: insert a patient row with the new email, point the appointments at it, delete the old row, all in one transaction.
Either way, nothing outside the database is updated. A reminder service or an export that stored the old email as the patient’s identity still holds it.
So a natural key makes a good primary key only when its value never changes. A patient’s email fails that test. The doctor’s NPI encodes no name, address or specialty, so it does better.
A surrogate key with IDENTITY
The same clinic with surrogate keys where the natural ones are weak. dbo.Patient gets PatientId, numbered by SQL Server, and keeps Email as a unique constraint. dbo.Appointment gets AppointmentId and keeps the doctor and start time unique the same way. The doctor keeps the NPI:
CREATE TABLE dbo.Doctor ( Npi char(10) NOT NULL CONSTRAINT PK_Doctor PRIMARY KEY, FullName nvarchar(100) NOT NULL ); CREATE TABLE dbo.Patient ( PatientId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Patient PRIMARY KEY, Email nvarchar(254) NOT NULL CONSTRAINT AK_Patient_Email UNIQUE, FullName nvarchar(100) NOT NULL, BirthDate date NOT NULL ); CREATE TABLE dbo.Appointment ( AppointmentId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Appointment PRIMARY KEY, Npi char(10) NOT NULL, StartsAt datetime2(0) NOT NULL, PatientId int NOT NULL, CONSTRAINT AK_Appointment_Npi_StartsAt UNIQUE (Npi, StartsAt), CONSTRAINT FK_Appointment_Doctor_Npi FOREIGN KEY (Npi) REFERENCES dbo.Doctor (Npi), CONSTRAINT FK_Appointment_Patient_PatientId FOREIGN KEY (PatientId) REFERENCES dbo.Patient (PatientId) );

Now the same rows, plus two mistakes:
INSERT INTO dbo.Patient (Email, FullName, BirthDate) VALUES (N'sam@example.com', N'Sam Lee', '1990-04-12'); INSERT INTO dbo.Patient (Email, FullName, BirthDate) VALUES (N'sam@example.com', N'Samuel Lee', '1990-04-12'); INSERT INTO dbo.Patient (Email, FullName, BirthDate) VALUES (N'kim@example.com', N'Kim Park', '1985-11-30'); INSERT INTO dbo.Patient (PatientId, Email, FullName, BirthDate) VALUES (10, N'jo@example.com', N'Jo Diaz', '2001-07-08'); UPDATE dbo.Patient SET Email = N'sam.lee@example.com' WHERE PatientId = 1; SELECT PatientId, Email FROM dbo.Patient;
Msg 2627, Level 14, State 1, Line 3
Violation of UNIQUE KEY constraint 'AK_Patient_Email'. Cannot insert duplicate key in object 'dbo.Patient'. The duplicate key value is (sam@example.com).
The statement has been terminated.
Msg 544, Level 16, State 1, Line 7
Cannot insert explicit value for identity column in table 'Patient' when IDENTITY_INSERT is set to OFF.Sam goes in as patient 1. The second insert is the same person typed again, and AK_Patient_Email stops it. Kim goes in as patient 3, not 2: the failed insert used up 2, and IDENTITY never reuses a value. A rollback or a restart can leave gaps as well, so don’t read an identity value as a count of rows.
Jo’s insert gives its own PatientId, and SQL Server refuses it with Msg 544: an identity column takes an explicit value only under SET IDENTITY_INSERT, as SQL Server identity shows.
The last update is the email change that failed before. It writes one row. Every appointment holds PatientId 1, so none of them notice.
IDENTITY hands a new number to every insert, including the same patient entered twice at two front desks. Without AK_Patient_Email, the second Sam would have become patient 2, with his appointments split across two records. The unique constraint is where the natural key’s meaning goes when it stops being the primary key:
- It keeps one row per real thing: one patient per email, one appointment per doctor and start time, the double-booking rule the composite key used to enforce.
- It is still a key. A table fed by a system that only knows patients by email can reference it with a foreign key, since SQL Server accepts a unique constraint as a foreign key’s target.
- Its index also serves lookups by email.
The price is one more index per table. Every insert writes to both indexes. The primary key clusters by default, so PK_Appointment is the clustered index and AK_Appointment_Npi_StartsAt a nonclustered one. The table is ordered by AppointmentId, a number that only grows. New rows go at the end.
IDENTITY, SEQUENCE or uniqueidentifier?
IDENTITY is a column property, one per table, and its value exists only once the row is inserted. SQL Server has two other ways to make a surrogate key. Here invoices take theirs from a sequence and lab results from a GUID:
CREATE SEQUENCE dbo.InvoiceIdSeq AS int START WITH 1; CREATE TABLE dbo.Invoice ( InvoiceId int NOT NULL CONSTRAINT DF_Invoice_InvoiceId DEFAULT (NEXT VALUE FOR dbo.InvoiceIdSeq) CONSTRAINT PK_Invoice PRIMARY KEY, AppointmentId int NOT NULL, Amount decimal(9, 2) NOT NULL, CONSTRAINT FK_Invoice_Appointment_AppointmentId FOREIGN KEY (AppointmentId) REFERENCES dbo.Appointment (AppointmentId) ); CREATE TABLE dbo.LabResult ( LabResultId uniqueidentifier NOT NULL CONSTRAINT DF_LabResult_LabResultId DEFAULT (NEWSEQUENTIALID()) CONSTRAINT PK_LabResult PRIMARY KEY, AppointmentId int NOT NULL, Test nvarchar(100) NOT NULL, CONSTRAINT FK_LabResult_Appointment_AppointmentId FOREIGN KEY (AppointmentId) REFERENCES dbo.Appointment (AppointmentId) );

INSERT INTO dbo.Doctor (Npi, FullName) VALUES ('1234567893', N'Ana Ruiz'); INSERT INTO dbo.Appointment (Npi, StartsAt, PatientId) VALUES ('1234567893', '2026-03-02 09:00', 1); INSERT INTO dbo.Invoice (AppointmentId, Amount) VALUES (1, 120.00); DECLARE @InvoiceId int = NEXT VALUE FOR dbo.InvoiceIdSeq; INSERT INTO dbo.Invoice (InvoiceId, AppointmentId, Amount) VALUES (@InvoiceId, 1, 35.00); INSERT INTO dbo.LabResult (AppointmentId, Test) VALUES (1, N'Blood count');
The first invoice takes its number from the column’s default, 1. The second asks the sequence first, holds the value in a variable and inserts it: 2. That is what a sequence gives you over IDENTITY: the number before the insert, one series shared by several tables, explicit values without IDENTITY_INSERT. Write the START WITH. Without it, an int sequence starts at its lowest value, and in a run its first number was -2147483648.
A uniqueidentifier key is 16 bytes, against 4 for an int, and every foreign key and every index on the child carries those bytes too. In return, any server or application can make one without asking the database, which helps when rows are created in several places.
NEWID() makes random values, so new rows land all over the clustered index. NEWSEQUENTIALID() makes each value greater than the last on that computer since Windows started, so new rows go at the end, as with IDENTITY. Microsoft warns that its next value can be guessed, so don’t use it where a key in a URL is all that protects a record. It works only as a column default:
SELECT NEWID(); SELECT NEWSEQUENTIALID();
Msg 302, Level 16, State 1, Line 2
The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type 'uniqueidentifier' in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression.Msg 302 is a compile error, so nothing in the batch runs, not even the SELECT NEWID() on line 1.
Surrogate keys in a data warehouse
A data warehouse keeps the history the clinic’s tables overwrite. When Sam changes his email, dbo.Patient keeps only the new one. A patient history dimension of type 2, in slowly changing dimension (SCD) terms, adds a new row for every change instead. One patient then has several rows, so PatientId can’t be the dimension’s primary key, and each version gets a surrogate key of its own:
CREATE TABLE dbo.PatientHistory ( PatientHistoryId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_PatientHistory PRIMARY KEY, PatientId int NOT NULL, Email nvarchar(254) NOT NULL, ValidFrom date NOT NULL, ValidTo date NULL, CONSTRAINT AK_PatientHistory_PatientId_ValidFrom UNIQUE (PatientId, ValidFrom) ); CREATE TABLE dbo.Visit ( VisitId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Visit PRIMARY KEY, PatientHistoryId int NOT NULL, StartsAt datetime2(0) NOT NULL, CONSTRAINT FK_Visit_PatientHistory_PatientHistoryId FOREIGN KEY (PatientHistoryId) REFERENCES dbo.PatientHistory (PatientHistoryId) );

INSERT INTO dbo.PatientHistory (PatientId, Email, ValidFrom) VALUES (1, N'sam@example.com', '2026-01-05'); INSERT INTO dbo.Visit (PatientHistoryId, StartsAt) VALUES (1, '2026-03-02 09:00'); UPDATE dbo.PatientHistory SET ValidTo = '2026-03-05' WHERE PatientId = 1 AND ValidTo IS NULL; INSERT INTO dbo.PatientHistory (PatientId, Email, ValidFrom) VALUES (1, N'sam.lee@example.com', '2026-03-05'); INSERT INTO dbo.Visit (PatientHistoryId, StartsAt) VALUES (2, '2026-03-09 09:00'); SELECT h.PatientId, h.Email, v.StartsAt FROM dbo.Visit AS v JOIN dbo.PatientHistory AS h ON h.PatientHistoryId = v.PatientHistoryId;
PatientId comes from the clinic and stays as the business key. To the warehouse it is a natural key, though the clinic made it up. AK_PatientHistory_PatientId_ValidFrom allows one version per patient per day. Each visit points at the version current when it happened, so the query returns the March 2 visit with sam@example.com and the March 9 visit with sam.lee@example.com, both under patient 1. Group by PatientId to count a patient’s visits across versions; join on PatientHistoryId to see the patient as they were.
How to choose between a surrogate key and a natural key
A natural key can be the primary key when all of these hold:
- the value exists before the row does;
- it never changes for the row’s life;
- it is short, one or two narrow columns;
- few tables reference it, or none.
The doctor’s char(10) NPI passes. So does a short code in a lookup table that nothing renames. Use a surrogate key when the natural one changes (an email, a name, a phone number), is wide or composite and gets referenced (Npi and StartsAt would be copied into every invoice and lab result), or doesn’t exist: no set of a patient’s columns is certain to be unique, since names and birth dates repeat.
Then pick the surrogate’s type: int IDENTITY by default, bigint when the numbers can pass 2,147,483,647, the largest int, a sequence when you need the number first or across tables, and uniqueidentifier when rows are made in several places.
Whichever key you pick, keep every natural key the business relies on declared, as the primary key or a unique constraint.
How ERDsmith helps
Sam’s email update failed because two appointments held copies of his old key, and those copies were settled the moment Email became the primary key. Swapping that key later means rewriting every table that holds it, so the choice is cheapest while the tables are still being drawn:
- The first key column you add to a table is an
intidentity named after the table, such asPatientId. - A unique constraint is drawn as AK, so the natural key stays in sight after a surrogate takes its place.
- Change a parent’s primary key and the children’s foreign key columns follow it: one per key column, with its type.
- A natural key carried into a child’s primary key draws a solid line , so you see where it has spread.