App
A table can have only one primary key, though it can consist of a single column (simple primary key) or multiple columns (composite primary key). Primary keys uniquely identify each record and enforce entity integrity in SQL databases like MySQL, PostgreSQL, or SQL Server.
A table can only have one primary key because SQL databases enforce strict entity integrity—this means each record must be uniquely identifiable without ambiguity. 🔥 While you can design it as a single column (like an auto-increment ID) or combine multiple columns (like a composite key), the database treats it as one logical key.
I’ve seen developers accidentally create multiple candidate keys, but only one can officially serve as the primary key to maintain data consistency across relationships.
For example, in an orders table, you might use a single orderid as the primary key, while a composite key combining customerid and order_date could work for a junction table.
The key rule? Never let two columns both claim to be the primary key—SQL won’t allow it. This design choice prevents duplicates and ensures every row has a distinct identity.
💡 In This Article
- SQL Primary Key Rules: Single vs Composite Keys
- Database Design Best Practices for Primary Keys
SQL primary key rules: single vs composite keys
Here's what's actually happening under the hood: SQL databases enforce a strict one-primary-key-per-table rule because it's the foundation of entity integrity. Think of it like a unique fingerprint for each record - if you allowed multiple primary keys, you'd create ambiguity about which one truly identifies the row.
The database engine needs this single identifier to maintain relationships between tables, enforce constraints, and prevent duplicate records from slipping through.
When we talk about single-column primary keys, we're referring to simple identifiers like an auto-incrementing integer (e.g., userid) or a natural key (e.g., email). These work well when you have a clear, stable attribute that can uniquely identify each row.
For example, in a users table, a single userid column with values like 1, 2, 3... serves perfectly. The database stores this as a clustered index by default in many systems, making lookups extremely fast - typically under 5 milliseconds for properly sized tables.
Composite primary keys come into play when no single column can uniquely identify a record. Imagine an enrollment table tracking students in courses - you might combine studentid and courseid to create a unique pair.
This works because the combination (studentid=101, courseid=CS101) is guaranteed unique, even if individual columns aren't. The challenge?
Performance degrades slightly as the key grows - a composite key with 3 columns might increase index size by 30-50% compared to a single-column key, though modern databases handle this efficiently.
What most people don't realize is how this design affects joins. When you create a foreign key relationship, the database must match the primary key structure exactly. Trying to reference a composite key with a single column or vice versa creates referential integrity violations.
For instance, if your orders table has a composite primary key of (customerid, orderdate), your orderitems table must reference both columns together - not just one of them. This precision prevents the classic "orphan record" problem where relationships become broken.
Let's look at real-world examples where each approach shines. Single-column keys excel in tables with simple, stable entities like products or users, where you can assign sequential IDs. Composite keys become essential for junction tables (like studentcourses) or when modeling many-to-many relationships where natural keys don't exist.
The choice often comes down to whether you need to preserve business meaning (composite) or optimize for simplicity (single-column).
Interestingly, some databases like PostgreSQL allow you to define multiple unique constraints that could serve as candidate keys, but only one can be designated as the primary key. This reflects the SQL standard's emphasis on having exactly one canonical identifier per table.
The database doesn't prevent you from creating additional unique indexes, but they're not primary keys - they're just alternative ways to enforce uniqueness on different columns.
