Composite key: a key of two or more columns in SQL Server

A composite key is a key of two or more columns, usually the primary key. How SQL Server enforces it, how foreign keys reference it, when a surrogate fits.

What is a composite key?

A composite key is a key made of two or more columns that together uniquely identify each row of a table, where no single column does. In SQL Server it is usually a primary key declared at the table level, PRIMARY KEY (StudentId, CourseId), and it is still the table’s one primary key. It is one of the types of keys, and the one most link tables use.

A school shows why it exists. A student takes many courses and a course has many students, so each enrollment gets a row of its own. A student appears once per course, a course once per student, and a pair at most once:

CREATE TABLE dbo.Student (
  StudentId int           NOT NULL
    CONSTRAINT PK_Student PRIMARY KEY,
  FullName  nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Course (
  CourseId int           NOT NULL
    CONSTRAINT PK_Course PRIMARY KEY,
  Title    nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Enrollment (
  StudentId  int  NOT NULL,
  CourseId   int  NOT NULL,
  EnrolledOn date NOT NULL,
  CONSTRAINT PK_Enrollment PRIMARY KEY (StudentId, CourseId),
  CONSTRAINT FK_Enrollment_Student_StudentId
    FOREIGN KEY (StudentId)
    REFERENCES dbo.Student (StudentId),
  CONSTRAINT FK_Enrollment_Course_CourseId
    FOREIGN KEY (CourseId)
    REFERENCES dbo.Course (CourseId)
);
dbo.Student with primary key StudentId and dbo.Course with primary key CourseId; dbo.Enrollment whose primary key StudentId and CourseId are also its foreign keys; solid lines from Student and from Course to Enrollment.
Both key columns of dbo.Enrollment are also foreign keys, so its lines are solid and its box is rounded: an enrollment can't be named without its student and its course.

Both columns of this key are also foreign keys, which makes it a compound key too: a compound key is a composite key whose columns are all foreign keys. That is why the crow’s foot lines are solid and the box is rounded . dbo.ExamResult further down adds a column of its own, ExamNo, so its key is composite but not compound.

Neither column is unique on its own. Maya (student 1) takes Algebra and Biology, and Algebra (course 10) has two students. What the key refuses is the same pair twice:

INSERT INTO dbo.Student (StudentId, FullName)
VALUES (1, N'Maya Chen'), (2, N'Leo Park');
INSERT INTO dbo.Course (CourseId, Title)
VALUES (10, N'Algebra'), (20, N'Biology');
INSERT INTO dbo.Enrollment (StudentId, CourseId, EnrolledOn)
VALUES (1, 10, '2026-09-01'), (1, 20, '2026-09-01'),
       (2, 10, '2026-09-02');
INSERT INTO dbo.Enrollment (StudentId, CourseId, EnrolledOn)
VALUES (1, 10, '2026-09-08');
Msg 2627, Level 14, State 1, Line 8
Violation of PRIMARY KEY constraint 'PK_Enrollment'. Cannot insert duplicate key in object 'dbo.Enrollment'. The duplicate key value is (1, 10).
The statement has been terminated.

The three enrollments go in. The fourth repeats (1, 10) and fails with Msg 2627, which prints the duplicate as a list of values in key order. SQL Server also made both columns NOT NULL when they joined the key, as it does for every primary key column, so half a pair is never stored. The same page sets the limits: 32 columns and 900 bytes of key.

A table that already exists gets its composite key with ALTER TABLE. Say dbo.Enrollment was first created as a plain table, the same three columns and no constraints:

ALTER TABLE dbo.Enrollment
  ADD CONSTRAINT PK_Enrollment
    PRIMARY KEY (StudentId, CourseId);
dbo.Enrollment with primary key StudentId and CourseId and the column EnrolledOn; no relationships.
Drawn from dbo.Enrollment created with no constraints and this ALTER TABLE: the pair is now its primary key.

Here SQL Server won’t make a column NOT NULL for you. A key column that allows NULL stops it with Msg 8111, “Cannot define PRIMARY KEY constraint on nullable column”, and a pair stored twice with Msg 1505, which prints the duplicate as Msg 2627 does. Both are followed by Msg 1750, “Could not create constraint or index”. Make the columns NOT NULL and remove the duplicates first.

How a foreign key references a composite key

A table that points at one enrollment has to carry the whole pair. Exam results belong to an enrollment, and each adds an exam number to its parent’s key:

CREATE TABLE dbo.ExamResult (
  StudentId int          NOT NULL,
  CourseId  int          NOT NULL,
  ExamNo    tinyint      NOT NULL,
  Score     decimal(5,2) NOT NULL,
  CONSTRAINT PK_ExamResult
    PRIMARY KEY (StudentId, CourseId, ExamNo),
  CONSTRAINT FK_ExamResult_Enrollment_StudentId_CourseId
    FOREIGN KEY (StudentId, CourseId)
    REFERENCES dbo.Enrollment (StudentId, CourseId)
);
dbo.Student, dbo.Course and dbo.Enrollment as before; dbo.ExamResult with primary key StudentId, CourseId and ExamNo, where StudentId and CourseId are a foreign key to dbo.Enrollment; a solid line from Enrollment to ExamResult.
The pair travels into dbo.ExamResult as one relationship, and with ExamNo it makes a three-column key.

The foreign key lists both columns on each side, and SQL Server checks them as one value, so a row’s pair has to match one enrollment:

INSERT INTO dbo.ExamResult (StudentId, CourseId, ExamNo, Score)
VALUES (1, 10, 1, 88.5);
INSERT INTO dbo.ExamResult (StudentId, CourseId, ExamNo, Score)
VALUES (2, 20, 1, 74);
Msg 547, Level 16, State 1, Line 3
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ExamResult_Enrollment_StudentId_CourseId". The conflict occurred in database "School", table "dbo.Enrollment".
The statement has been terminated.

Student 2 exists, and so does course 20, but Leo isn’t enrolled in Biology, so his exam result is refused. Two single-column foreign keys, one to dbo.Student and one to dbo.Course, would have let that row in. Unlike the Msg 547 of a single-column foreign key, which ends with the column’s name, this one names only the table.

The foreign key must list every column of the key. FOREIGN KEY (StudentId) REFERENCES dbo.Enrollment (StudentId) fails with Msg 1776, because StudentId alone isn’t a key of dbo.Enrollment. The post on Msg 1776 has that run and its fixes.

Does the column order of a composite key matter?

Yes, in three places, and only the first is refused when the script runs.

The referenced columns must be listed in the key’s order. Writing both sides as (CourseId, StudentId) fails with Msg 1776, though they are the same two columns.

The child’s columns pair with the parent’s by position, not by name. Attendance is kept per course, so this table puts CourseId first, and its foreign key was written in the table’s own column order:

CREATE TABLE dbo.Attendance (
  CourseId   int  NOT NULL,
  StudentId  int  NOT NULL,
  LessonDate date NOT NULL,
  CONSTRAINT PK_Attendance
    PRIMARY KEY (CourseId, StudentId, LessonDate),
  CONSTRAINT FK_Attendance_Enrollment_StudentId_CourseId
    FOREIGN KEY (CourseId, StudentId)
    REFERENCES dbo.Enrollment (StudentId, CourseId)
);
dbo.Enrollment with primary key StudentId and CourseId; dbo.Attendance with primary key columns CourseId.StudentId, StudentId.CourseId and LessonDate; a solid line from Enrollment to Attendance.
Each child column shows the parent column it holds: CourseId.StudentId is the CourseId column holding a StudentId. The pairs are crossed.

SQL Server accepts it, since all four columns are int. It now reads Attendance.CourseId as a student and Attendance.StudentId as a course, and the first real row fails:

INSERT INTO dbo.Attendance (CourseId, StudentId, LessonDate)
VALUES (10, 1, '2026-09-03');
Msg 547, Level 16, State 1, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Attendance_Enrollment_StudentId_CourseId". The conflict occurred in database "School", table "dbo.Enrollment".
The statement has been terminated.

Maya in Algebra is the enrollment (1, 10), and it exists. The foreign key looked for student 10 in course 1. The fix is to write the child’s list in the parent key’s order, FOREIGN KEY (StudentId, CourseId), whatever order the child table keeps its columns in. The table’s own key can stay course first.

The key’s order is also its index’s order. PK_Enrollment is a clustered index sorted by StudentId, then CourseId. A query for one student’s courses seeks into it; the plan for WHERE CourseId = 10 is a clustered index scan of the whole table. Put first the column you look up by most often, and give the other one an index of its own.

What a NULL does to a composite foreign key

A primary key column can’t be NULL, but a foreign key column can. A request to the school office may be about one enrollment or about none, so both columns allow NULL:

CREATE TABLE dbo.Request (
  RequestId int           NOT NULL
    CONSTRAINT PK_Request PRIMARY KEY,
  StudentId int           NULL,
  CourseId  int           NULL,
  Body      nvarchar(400) NOT NULL,
  CONSTRAINT FK_Request_Enrollment_StudentId_CourseId
    FOREIGN KEY (StudentId, CourseId)
    REFERENCES dbo.Enrollment (StudentId, CourseId)
);
dbo.Enrollment with primary key StudentId and CourseId; dbo.Request with primary key RequestId and foreign key columns StudentId and CourseId; a dashed line from Enrollment to Request.
Both columns allow NULL, so the relationship is optional: a dashed line with a ring at dbo.Enrollment's end.
INSERT INTO dbo.Request (RequestId, StudentId, CourseId, Body)
VALUES (1, 1, 10, N'Move my exam to Friday');
INSERT INTO dbo.Request (RequestId, StudentId, CourseId, Body)
VALUES (2, 1, 99, N'Add me to chemistry');
INSERT INTO dbo.Request (RequestId, StudentId, CourseId, Body)
VALUES (3, 7, NULL, N'Lost my locker key');
Msg 547, Level 16, State 1, Line 3
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Request_Enrollment_StudentId_CourseId". The conflict occurred in database "School", table "dbo.Enrollment".
The statement has been terminated.

Request 1 names an enrollment that exists. Request 2 names course 99, which Maya never took, and is refused. Request 3 goes in, although there is no student 7: its CourseId is NULL, and when any column of a composite foreign key is NULL, SQL Server checks none of them.

If a request names one part, it should name both. A check constraint says so: both columns NULL, or neither. With CHECK ((StudentId IS NULL AND CourseId IS NULL) OR (StudentId IS NOT NULL AND CourseId IS NOT NULL)) on dbo.Request, request 3 fails with Msg 547 against the check constraint instead.

Composite key or surrogate key?

A composite key fits a table whose rows are named by their parents and nothing else. An enrollment is the pair; change either value and it is another enrollment. There the key costs nothing, since the columns are needed anyway.

The cost shows further down. Every child copies the whole key, and each level adds a column: dbo.ExamResult has three, and a table under it would have four. Every join between them compares all of them. The usual alternative is a surrogate key, one identity column as the primary key, with the pair kept unique as an alternate key. This script builds the school that way, in place of the composite dbo.Enrollment and dbo.ExamResult; the student management system template does the same, with the school year added to its alternate key:

CREATE TABLE dbo.Enrollment (
  EnrollmentId int  NOT NULL IDENTITY
    CONSTRAINT PK_Enrollment PRIMARY KEY,
  StudentId    int  NOT NULL,
  CourseId     int  NOT NULL,
  EnrolledOn   date NOT NULL,
  CONSTRAINT AK_Enrollment_StudentId_CourseId
    UNIQUE (StudentId, CourseId),
  CONSTRAINT FK_Enrollment_Student_StudentId
    FOREIGN KEY (StudentId)
    REFERENCES dbo.Student (StudentId),
  CONSTRAINT FK_Enrollment_Course_CourseId
    FOREIGN KEY (CourseId)
    REFERENCES dbo.Course (CourseId)
);
CREATE TABLE dbo.ExamResult (
  EnrollmentId int          NOT NULL,
  ExamNo       tinyint      NOT NULL,
  Score        decimal(5,2) NOT NULL,
  CONSTRAINT PK_ExamResult PRIMARY KEY (EnrollmentId, ExamNo),
  CONSTRAINT FK_ExamResult_Enrollment_EnrollmentId
    FOREIGN KEY (EnrollmentId)
    REFERENCES dbo.Enrollment (EnrollmentId)
);
dbo.Student and dbo.Course; dbo.Enrollment with primary key EnrollmentId and alternate key StudentId and CourseId, dashed lines from Student and Course; dbo.ExamResult with primary key EnrollmentId and ExamNo, a solid line from Enrollment.
dbo.Enrollment keeps the pair as its alternate key (AK) and gets a key of its own; dbo.ExamResult points at EnrollmentId alone.

Leo’s Biology result still can’t go in: it needs an EnrollmentId, and his pair has none. That rests on the unique constraint: without it, the identity would let a student enroll in the same course twice. What you give up is the pair in the child. To list a student’s exam results you now join through dbo.Enrollment. And dbo.ExamResult still has a composite key, only a shorter one.

So keep the composite key on a link table that nothing references, or that one table references. Switch to a surrogate when the key would travel two levels down, when its values can change, or when it holds wide text. Surrogate key vs natural key weighs the two more generally.

How ERDsmith helps

The crossed pair in dbo.Attendance passed CREATE TABLE and broke on the first real row, and a short or reordered column list stops the script with Msg 1776. In ERDsmith a relationship takes its columns from the parent’s key when you draw it, and a list that stops matching the key is flagged:

  • The child gets one column per parent key column, in key order, with the parent column’s type.
  • A child column that holds a parent column under another name shows both, as in dbo.Attendance, so a crossed pair is visible on the box.
  • A relationship whose columns aren’t all its parent key’s columns, in its order, is marked as an error: the Msg 1776 case, before any script exists.
  • A relationship is identifying exactly when all its columns are in the child’s primary key, and its line and box show which: a solid line and rounded corners .