Can primary key be duplicate?
No, a primary key in SQL cannot contain duplicate values. The primary key is a fundamental concept in relational database design, serving as a unique identifier for each record (row) in a table. Ensuring that primary key values are unique and non-null is essential for maintaining data integrity, establishing relationships between tables, and optimizing database performance.
Why Primary Keys Must Be Unique
-
Unique Identification:
- The primary key's main purpose is to uniquely identify each record in a table. If primary keys could have duplicate values, it would be impossible to distinguish between different records, leading to ambiguity and data integrity issues.
-
Referential Integrity:
- Primary keys are often referenced by foreign keys in other tables to establish relationships. If primary keys were not unique, foreign key relationships would become unreliable, potentially causing orphaned records and inconsistent data across related tables.
-
Indexing and Performance:
- Most database systems automatically create an index on the primary key to enhance query performance. Unique and non-null values ensure that the index operates efficiently, allowing for faster data retrieval and manipulation.
Defining a Primary Key in SQL
When you define a primary key in SQL, the database system enforces two main constraints:
- Uniqueness: No two rows can have the same primary key value.
- Non-Nullability: Primary key columns cannot contain
NULL
values.
Example: Creating a Table with a Primary Key
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Email VARCHAR(100) UNIQUE );
In this example:
EmployeeID
is defined as the primary key.- The
PRIMARY KEY
constraint ensures that:- Each
EmployeeID
is unique. - No
EmployeeID
can beNULL
.
- Each
Attempting to Insert Duplicate Primary Keys
If you try to insert a duplicate value into a primary key column, the database will reject the operation and throw an error to maintain the integrity of the primary key constraint.
Example: Inserting a Duplicate Primary Key
-- Assuming EmployeeID 1 already exists INSERT INTO Employees (EmployeeID, FirstName, LastName, Email) VALUES (1, 'Alice', 'Williams', 'alice.williams@example.com');
Error Message:
ERROR: duplicate key value violates unique constraint "Employees_pkey"
DETAIL: Key (EmployeeID)=(1) already exists.
This error indicates that the EmployeeID
value 1
already exists in the Employees
table, and thus, cannot be inserted again.
Primary Key vs. Unique Key
While both primary keys and unique keys enforce uniqueness in a table, there are key differences:
Feature | Primary Key | Unique Key |
---|---|---|
Uniqueness | Enforces uniqueness | Enforces uniqueness |
Nullability | Cannot contain NULL values | Can contain NULL values (typically only one NULL depending on the DBMS) |
Number per Table | Only one primary key per table | Multiple unique keys per table |
Purpose | Uniquely identifies each row | Ensures distinctiveness of specific columns |
Composite Primary Keys and Uniqueness
A composite primary key consists of two or more columns used together to uniquely identify a record. Even with composite keys, none of the individual columns can contain NULL
values, and the combination of their values must be unique across all records.
Example: Composite Primary Key
CREATE TABLE OrderDetails ( OrderID INT, ProductID INT, Quantity INT, PRIMARY KEY (OrderID, ProductID) );
In this example:
- The combination of
OrderID
andProductID
serves as the primary key. - Neither
OrderID
norProductID
can beNULL
. - Each combination of
OrderID
andProductID
must be unique, ensuring that each product within an order is distinct.
Best Practices for Primary Keys
-
Use Surrogate Keys:
- Prefer using surrogate keys (e.g., auto-incrementing integers) over natural keys (e.g., Social Security Numbers) to avoid issues if natural key values change.
CREATE TABLE Products ( ProductID INT AUTO_INCREMENT PRIMARY KEY, ProductName VARCHAR(100), Price DECIMAL(10, 2) );
-
Keep Primary Keys Simple:
- Use a single column as the primary key when possible to simplify relationships and indexing.
-
Immutable Keys:
- Ensure that primary key values do not change over time to maintain referential integrity.
-
Choose Appropriate Data Types:
- Select data types that are efficient for indexing and storage. Integers are commonly used due to their performance benefits.
-
Avoid Using Mutable Columns:
- Columns that can change frequently should not be used as primary keys to prevent cascading changes and potential integrity issues.
-
Document Constraints:
- Clearly document the purpose and constraints of primary keys within your database schema for better maintainability and understanding.
Handling Primary Keys in Different SQL Dialects
Most SQL dialects enforce the non-nullability and uniqueness of primary keys similarly, but there might be slight variations in syntax or behavior.
MySQL Example:
CREATE TABLE Users ( UserID INT NOT NULL AUTO_INCREMENT, Username VARCHAR(50) NOT NULL, Email VARCHAR(100) NOT NULL, PRIMARY KEY (UserID) );
PostgreSQL Example:
CREATE TABLE Users ( UserID SERIAL PRIMARY KEY, Username VARCHAR(50) NOT NULL, Email VARCHAR(100) NOT NULL );
SQL Server Example:
CREATE TABLE Users ( UserID INT IDENTITY(1,1) PRIMARY KEY, Username VARCHAR(50) NOT NULL, Email VARCHAR(100) NOT NULL );
Conclusion
In SQL, a primary key is a critical component that ensures each record within a table can be uniquely identified. By enforcing both uniqueness and non-nullability, primary keys maintain data integrity and facilitate efficient data retrieval and relationship management. Properly defining and utilizing primary keys is essential for building robust, scalable, and reliable database systems.
Key Takeaways:
- Primary Keys Must Be Unique and Non-Null: They cannot contain duplicate or
NULL
values. - Only One Primary Key Per Table: A table can have only one primary key, which can consist of one or multiple columns (composite key).
- Used for Relationships: Primary keys are often referenced by foreign keys in other tables to establish relationships.
- Best Practices: Use surrogate keys, keep keys simple and immutable, and choose appropriate data types.
By adhering to these principles, you ensure that your database remains reliable, efficient, and well-organized.
GET YOUR FREE
Coding Questions Catalog