đ The PRIMARY KEY constraint in SQL is used to uniquely identify each row in a table. A primary key ensures that every record has a unique identifier, making it one of the most important constraints in relational databases.
đ What is a PRIMARY KEY?
A PRIMARY KEY is a column or a combination of columns whose values uniquely identify every row in a table. A primary key cannot contain duplicate values and cannot contain NULL values.
Information
đ¯ Why Use PRIMARY KEY?
Primary keys provide a reliable way to identify records and maintain relationships between tables.
- đ Ensure every row is uniquely identifiable.
- đ Prevent duplicate records.
- đ Disallow NULL values.
- đ Support relationships using foreign keys.
- đ Improve query performance through indexing.
đ Basic Syntax
PRIMARY KEY Syntax
CREATE TABLE table_name
(
column_name data_type PRIMARY KEY
);đĄ Create a Table with a PRIMARY KEY
Create a Students table where StudentID uniquely identifies every student.
Single-Column PRIMARY KEY
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(100),
Age INT
);In this table, every student must have a unique StudentID.
â Valid INSERT
This statement succeeds because the primary key value is unique.
Valid INSERT
INSERT INTO Students
(StudentID, Name, Department, Age)
VALUES
(101, 'Alice', 'Computer Science', 20);â Duplicate PRIMARY KEY
Attempting to insert another row with the same primary key causes an error.
Duplicate PRIMARY KEY
INSERT INTO Students
(StudentID, Name, Department, Age)
VALUES
(101, 'Bob', 'Mathematics', 21);Error
â NULL PRIMARY KEY
A primary key cannot contain NULL.
NULL PRIMARY KEY
INSERT INTO Students
(StudentID, Name)
VALUES
(NULL, 'Charlie');Error
đ Composite PRIMARY KEY
A primary key can consist of multiple columns. This is known as a composite primary key.
Composite PRIMARY KEY
CREATE TABLE Enrollments
(
StudentID INT,
CourseID INT,
Semester VARCHAR(20),
PRIMARY KEY (StudentID, CourseID)
);In this example, the combination of StudentID and CourseID must be unique.
â Add a PRIMARY KEY to an Existing Table
A primary key can also be added after a table has been created.
Add PRIMARY KEY
ALTER TABLE Students
ADD CONSTRAINT PK_Students
PRIMARY KEY (StudentID);Warning
â Drop a PRIMARY KEY
The syntax for removing a primary key depends on the database system.
Drop PRIMARY KEY (MySQL Example)
ALTER TABLE Students
DROP PRIMARY KEY;Drop PRIMARY KEY (SQL Server Example)
ALTER TABLE Students
DROP CONSTRAINT PK_Students;đ Example Table
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
| 103 | Charlie | Physics |
âī¸ PRIMARY KEY vs UNIQUE
| Feature | PRIMARY KEY | UNIQUE |
|---|---|---|
| Duplicate Values Allowed | â No | â No |
| NULL Allowed | â No | Depends on the database system. |
| Number Allowed Per Table | One | Multiple |
| Main Purpose | Identify each row. | Prevent duplicate values. |
đŧ Real-World Example
In an online banking system, every customer receives a unique customer ID. This identifier is used throughout the database to link accounts, transactions, loans, and other records.
Customers Table
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY,
FullName VARCHAR(100),
Email VARCHAR(150),
Phone VARCHAR(20)
);â ī¸ Common Mistakes
- â Choosing a column whose values may change frequently.
- â Attempting to insert duplicate primary key values.
- â Allowing business rules to rely on non-unique columns.
- â Trying to create a primary key on columns containing duplicate or NULL values.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ A PRIMARY KEY uniquely identifies each row.
- đ Duplicate values are not allowed.
- đ NULL values are not allowed.
- đ Each table can have only one primary key, but it may contain multiple columns.
- đ Primary keys are commonly referenced by foreign keys.
- đ Choosing the right primary key improves database integrity and performance.