Unique Key Vs Primary Key

Dikshya SubediDikshya Subedi
1 min read

Primary Key:

a. It must contain unique values for each row, meaning no two rows can have the same value in the primary key column(s).

b. A primary key column cannot contain NULL values, as it must uniquely identify each row.

c. A table can have only one primary key.

d. The primary key is automatically indexed by the database management system for faster data retrieval.

e. The primary key is often used as a reference in other tables (foreign keys) to establish relationships between tables.

f. Ensures data integrity and is a crucial concept in relational database design.

Unique Key:

a. Unlike a primary key, a unique key column can contain NULL values, allowing for multiple NULL values in that column.

b. Unique keys are not automatically indexed by the database management system, but they can be indexed for performance optimization.

c. While unique keys also help ensure data integrity, they are not typically used as references in relationships between tables like primary keys.

Example:

CourseCode (PK)CourseNameInstructor(Unique Key)
CSC101Introduction to CSProf. Smith
MTH202Calculus IIProf. Johnson
ENG101English CompositionNULL
HST309LiteratureNULL
0
Subscribe to my newsletter

Read articles from Dikshya Subedi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dikshya Subedi
Dikshya Subedi