DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Composite Primary Keys: How Multi-Column Row Identity Works

A composite primary key identifies each row through a unique combination of two or more non-null columns. See a SQL example, its enforcement rules, and design trade-offs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A composite primary key uses two or more columns together to identify each row uniquely. The full combination cannot be repeated, and every column in the key must contain a non-null value; an individual value may recur when paired with a different value.

How a composite primary key identifies rows

PostgreSQL describes a primary key as a column or group of columns that can uniquely identify table rows. With a composite key, uniqueness applies to the group—not to each column on its own. For example, in a product-vendor table, a product can appear with several vendors and a vendor can supply several products, but the same product-vendor pair cannot appear twice. PostgreSQL documentation and Microsoft’s SQL Server documentation describe these primary-key rules and examples.

As an Amazon Associate I earn from qualifying purchases.

Example: a student-course enrollment table

If each student may enroll in many courses and each course may have many students, the pair of student and course IDs can identify each enrollment:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
CREATE TABLE enrollment (
    student_id integer NOT NULL,
    course_id integer NOT NULL,
    enrolled_on date,
    PRIMARY KEY (student_id, course_id)
);

This permits rows such as (12, 101) and (12, 205), because the student is enrolled in different courses. It also permits (27, 101), because a different student is taking course 101. It rejects another row with (12, 101), since that complete pair already identifies an enrollment. Neither student_id nor course_id has to be unique by itself.

How to declare the key in SQL

For a multi-column primary key, declare one table-level constraint listing all participating columns:

PRIMARY KEY (student_id, course_id)

This is one primary key made from both columns, not two separate primary keys. A table can have at most one primary key constraint, though it can have other unique constraints for additional uniqueness rules. A primary key also names the table’s designated row identifier and can serve as the default set of referenced columns when another table declares a foreign key to it. See PostgreSQL’s primary-key documentation and its foreign-key documentation.

What the database enforces

  • Every complete key combination is unique. The database rejects a second row with the same values across all key columns.
  • Every key column is non-null. A primary-key component cannot be omitted or set to NULL. PostgreSQL documents that declaring a primary key marks its participating columns NOT NULL.
  • The key is indexed for enforcement and lookup. PostgreSQL creates a unique B-tree index on the primary-key columns. Other database products may use different implementations or impose different limits; consult the documentation for the product and version in use.

These properties are described in PostgreSQL 16’s primary-key documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a composite key fits—and what to weigh

A composite key is a natural fit when the combination itself represents the row’s identity, such as a relationship table where each student-course or product-vendor pairing should occur only once. The enrollment example makes that rule explicit: the database prevents duplicate enrollments by preventing a repeated pair.

Rank #3

Before choosing one, consider whether the component values are stable and meaningful, how much key data referencing tables must carry in their foreign keys, and whether your database and application stack handle multi-column keys conveniently. Also check product-specific index and key-size restrictions. There is no universal rule that a composite key is better or worse than a single-column surrogate key; the choice depends on what defines a row and the practical requirements of the system.

For scale, Microsoft documents a SQL Server primary-key limit of 32 columns and 900 bytes of total key length. Those are SQL Server-specific limits, not general limits for all SQL databases; verify the documentation for the exact product and version you use. Microsoft’s SQL Server capacity specifications provide the product details.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Composite primary key versus a unique constraint

A primary key is the table’s designated row identifier and requires unique, non-null key values. A unique constraint can enforce a different candidate identifier—such as a username or external reference—without making it the primary key. A table may have multiple unique constraints but only one primary key. Because database products can differ in how unique constraints treat NULL, check the relevant product documentation before relying on null behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.