Unique Key Vs Primary Key
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) | CourseName | Instructor(Unique Key) |
CSC101 | Introduction to CS | Prof. Smith |
MTH202 | Calculus II | Prof. Johnson |
ENG101 | English Composition | NULL |
HST309 | Literature | NULL |
Subscribe to my newsletter
Read articles from Dikshya Subedi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by