Primary Key Constraints and Unique Key Constraints are both important in database management systems to maintain data integrity and ensure the accuracy of the data stored. They both ensure uniqueness within a column or set of columns but with some key differences.
Primary Key Constraints
- Uniqueness: A primary key constraint ensures that all values in the primary key column are unique. A table can only have one primary key constraint.
- Not Null: A primary key column cannot contain NULL values. Each row must have a valid value for the primary key.
- Single Column or Composite: A primary key can consist of a single column or multiple columns (composite primary key).
- Default Indexing: A clustered index is automatically created on the primary key columns. This index physically organizes the table data based on the primary key, improving performance for queries that involve the primary key.
- Foreign Keys: The primary key of one table can be referenced as a foreign key in another table to establish relationships between tables.
- Only One Primary Key: A table can have only one primary key constraint, though it can be composed of multiple columns.
Unique Key Constraints
- Uniqueness: A unique key constraint also ensures that all values in the unique key columns are unique. However, a table can have multiple unique constraints.
- Nullable: Unlike primary key columns, unique key columns can contain NULL values, but each NULL value is considered unique.
- Single Column or Composite: Similar to primary keys, unique keys can be defined on a single column or multiple columns (composite unique key).
- Index: A non-clustered index is typically created on a unique key column. This index helps speed up queries that involve searching or filtering based on the unique key.
- Foreign Keys: Unique keys can also be referenced as foreign keys in other tables.
- Multiple Unique Keys: A table can have multiple unique key constraints.
Use a primary key when you need a guaranteed unique identifier for each row in the table. This is often the case for the main entity represented by the table.
Use a unique key when you need to ensure a column or set of columns has unique values, but is separate from the main identifier of the table. You can have multiple unique keys to enforce uniqueness on different sets of columns.
Example
User Table
- Primary Key: user_Id (guaranteed unique identifier for each user).
- Unique Key: user_Email (ensures no duplicate email addresses).
Examples
Primary Key Table Structure.
Primary Key and Unique Key Table Structure.
employee_id is the primary key and email, and phone_number is the unique key.
The primary key has a clustered index and the unique key has non clustered index.
Composite Keys
It is used as a primary key or a unique key, involving combining multiple columns to uniquely identify a row in a database table.
Example
Composite Primary Key: A primary key can consist of a single column or multiple columns.
student_id, and course_id are used to create composite primary key.
Composite Unique Key: A table can have multiple unique key constraints.
product_id and customer_id are used to create a composite unique key.
The query file is attached.