In the relational model, the Primary Key (PK) is the fundamental cornerstone of entity identity. A primary key is an attribute (or combination of attributes) that uniquely distinguishes every individual tuple in a table. It strictly enforces two mathematical guarantees: Uniqueness and Non-Nullability.
Figure 2.1: Referential integrity model showing Primary Key in parent table and Foreign Key linkage in child table.
• Super Key: Any set of one or more columns that taken together uniquely identify a row within a table.
• Candidate Key: A minimal super key (a super key with no redundant attributes). A table may have multiple candidate keys (e.g., student_id, aadhaar_number, email).
• Primary Key: The specific candidate key chosen by the database architect as the primary identifier for the relation.
• Alternate Key: Candidate keys that were not selected as the primary key.
-- Column-level definition
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);
-- Or using AUTO_INCREMENT surrogate key
CREATE TABLE courses (
course_id INT AUTO_INCREMENT,
course_name VARCHAR(100) NOT NULL,
CONSTRAINT pk_course PRIMARY KEY (course_id)
);
When an entity's uniqueness depends on the combination of two or more columns, define a Composite Primary Key at the table level. A classic example is a university course enrollment table:
CREATE TABLE course_enrollments (
student_id INT NOT NULL,
course_id INT NOT NULL,
enrollment_date DATE NOT NULL,
grade VARCHAR(2),
-- Composite Primary Key: A student can enroll in a specific course only once!
CONSTRAINT pk_enrollment PRIMARY KEY (student_id, course_id)
);
| Criteria | Natural Key (e.g., Email, SSN) | Surrogate Key (e.g., BIGINT AUTO_INCREMENT) |
|---|---|---|
| Real-World Meaning | Has business meaning in the real world. | Zero business meaning; artificial ID. |
| Join Performance | Joining long strings (VARCHAR) is slower. | Extremely fast 4-byte / 8-byte integer joins. |
| Stability Over Time | Can change (e.g., user changes their email). | Immutable (never changes once assigned). |
| Industry Recommendation | Enforce as a secondary UNIQUE constraint. |
Recommended primary key for foreign keys. |
Now let us see how primary keys link tables together via SQL Foreign Key!
No! A table can have only ONE primary key constraint. However, that single primary key can consist of multiple columns combined together, which is called a Composite Primary Key.
A Natural Key is an existing real-world attribute that is inherently unique (e.g., Passport number, Social Security Number). A Surrogate Key is an artificial, system-generated identifier with no real-world meaning (e.g., an AUTO_INCREMENT integer id or UUID) used purely for efficient indexing and joins.
Yes! In MySQL InnoDB, PostgreSQL, and SQL Server, defining a primary key automatically creates a unique Clustered Index (B+ Tree) that physically sorts the table rows on disk by that key.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 14: Foreign Key → Previous: ← SQL Constraints | Full Syllabus
Connecting primary keys to dependent child foreign keys.
Differences between PRIMARY KEY, UNIQUE, and CHECK.
How primary keys physically sort rows on disk via B-Trees.
Joining tables using primary and foreign key equalities.