Lesson 33 of 60 – PRIMARY KEY
55%

MySQL PRIMARY KEY

A PRIMARY KEY is a column or combination of columns that uniquely identifies each row in a MySQL table.

Note: A PRIMARY KEY must contain unique values and cannot contain NULL values.

1. What is a PRIMARY KEY?

A PRIMARY KEY is used to uniquely identify every record in a table.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

Here, student_id uniquely identifies each student.

2. Why Use a PRIMARY KEY?

A PRIMARY KEY helps MySQL identify individual records.

  • Identifies each row uniquely
  • Prevents duplicate key values
  • Does not allow NULL values
  • Helps establish relationships between tables
  • Can be used as a reference by foreign keys

3. PRIMARY KEY Syntax

The simplest syntax is:

column_name data_type PRIMARY KEY

Example:

student_id INT PRIMARY KEY

4. PRIMARY KEY Example

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100)
);

Each student must have a different student_id.

5. PRIMARY KEY Values Must Be Unique

Two rows cannot have the same PRIMARY KEY value.

INSERT INTO students
(student_id, name, course)
VALUES
(1, 'Amit', 'Python');

INSERT INTO students
(student_id, name, course)
VALUES
(1, 'Rahul', 'Java');

The second INSERT fails because student_id = 1 already exists.

6. PRIMARY KEY Cannot Be NULL

A PRIMARY KEY cannot contain NULL values.

INSERT INTO students
(student_id, name)
VALUES
(NULL, 'Priya');

This produces an error because the PRIMARY KEY must identify the row.

7. One PRIMARY KEY per Table

A table can have only one PRIMARY KEY constraint.

However, that PRIMARY KEY can contain more than one column. This is called a composite primary key.

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    PRIMARY KEY (student_id, course_id)
);

8. PRIMARY KEY with AUTO_INCREMENT

A common pattern is to use an integer PRIMARY KEY with AUTO_INCREMENT.

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    course VARCHAR(100)
);

MySQL automatically generates a new ID when a row is inserted without specifying the ID.

9. Insert Data with AUTO_INCREMENT

INSERT INTO students
(name, course)
VALUES
('Amit', 'Python');

INSERT INTO students
(name, course)
VALUES
('Priya', 'Java');

MySQL generates the student_id values automatically.

10. PRIMARY KEY at the End of CREATE TABLE

The PRIMARY KEY can also be defined separately inside CREATE TABLE.

CREATE TABLE students (
    student_id INT,
    name VARCHAR(100),
    course VARCHAR(100),
    PRIMARY KEY (student_id)
);

This is useful when defining a composite primary key or when keeping constraints together.

11. Add PRIMARY KEY to Existing Table

You can add a PRIMARY KEY to an existing table using ALTER TABLE.

ALTER TABLE students
ADD PRIMARY KEY (student_id);

The column must contain suitable unique, non-NULL values.

12. Removing a PRIMARY KEY

You can remove the PRIMARY KEY constraint using ALTER TABLE.

ALTER TABLE students
DROP PRIMARY KEY;

The table remains, but it no longer has a PRIMARY KEY.

13. PRIMARY KEY with INT

Integer columns are commonly used as PRIMARY KEY columns.

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(100),
    salary DECIMAL(10,2)
);

The employee_id uniquely identifies each employee.

14. PRIMARY KEY with VARCHAR

A VARCHAR column can also be used as a PRIMARY KEY if its values are unique and non-NULL.

CREATE TABLE countries (
    country_code VARCHAR(3) PRIMARY KEY,
    country_name VARCHAR(100)
);

Example values could be IND, USA, and GBR.

15. Viewing the PRIMARY KEY

You can use DESCRIBE to inspect a table.

DESCRIBE students;

The Key column shows PRI for a PRIMARY KEY column.

16. SHOW CREATE TABLE

SHOW CREATE TABLE displays the CREATE TABLE statement, including its constraints.

SHOW CREATE TABLE students;

This is useful for checking how the PRIMARY KEY has been defined.

17. Composite PRIMARY KEY

A composite PRIMARY KEY uses two or more columns together to uniquely identify a row.

CREATE TABLE student_courses (
    student_id INT,
    course_id INT,
    PRIMARY KEY (student_id, course_id)
);

The combination of student_id and course_id must be unique.

18. Composite Key Example

Consider these records:

student_id course_id
1 101
1 102
2 101

The same student can appear multiple times because the complete combination is different.

19. Duplicate Composite Key

This combination cannot be inserted twice:

student_id = 1
course_id = 101

For example:

INSERT INTO student_courses
(student_id, course_id)
VALUES
(1, 101);

INSERT INTO student_courses
(student_id, course_id)
VALUES
(1, 101);

The second record violates the PRIMARY KEY constraint.

20. PRIMARY KEY and Foreign Key

A PRIMARY KEY can be referenced by a FOREIGN KEY in another table.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE fees (
    fee_id INT PRIMARY KEY,
    student_id INT,
    FOREIGN KEY (student_id)
    REFERENCES students(student_id)
);

Here, students.student_id is the PRIMARY KEY and fees.student_id references it.

21. PRIMARY KEY and Index

MySQL automatically creates a unique index for a PRIMARY KEY.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

This supports efficient identification and lookup of rows.

22. PRIMARY KEY vs UNIQUE

PRIMARY KEY UNIQUE
Only one PRIMARY KEY constraint per table A table can have multiple UNIQUE constraints
Cannot contain NULL NULL handling is different and depends on the column/constraint
Identifies the row Enforces uniqueness

23. Choosing a PRIMARY KEY

A good PRIMARY KEY should generally be:

  • Unique
  • Stable
  • Not NULL
  • As simple as practical
  • Suitable for identifying one row

An integer AUTO_INCREMENT column is a common choice for many application tables.

24. Avoid Duplicate IDs

Do not manually insert duplicate PRIMARY KEY values.

INSERT INTO students
(student_id, name)
VALUES
(1, 'Amit');

INSERT INTO students
(student_id, name)
VALUES
(1, 'Rahul');

The second INSERT fails because 1 is already used.

25. PRIMARY KEY and NULL

A PRIMARY KEY column cannot contain NULL.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

The following is invalid:

INSERT INTO students
(student_id, name)
VALUES
(NULL, 'Amit');

26. Common PRIMARY KEY Mistakes

  • Trying to insert duplicate PRIMARY KEY values
  • Trying to insert NULL into a PRIMARY KEY
  • Choosing a value that is not stable
  • Defining more than one PRIMARY KEY constraint
  • Adding a PRIMARY KEY when existing data contains duplicates
  • Ignoring relationships with foreign keys
  • Using an unnecessarily large composite key

27. Adding PRIMARY KEY After Creating Table

Suppose a table already exists:

CREATE TABLE students (
    student_id INT,
    name VARCHAR(100),
    course VARCHAR(100)
);

You can add the PRIMARY KEY later:

ALTER TABLE students
ADD PRIMARY KEY (student_id);

28. Practical Student Table

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100),
    mobile VARCHAR(15)
);

INSERT INTO students
(name, course, mobile)
VALUES
('Amit Kumar', 'Python', '9876543210'),
('Priya Singh', 'Java', '9876543211'),
('Rahul Kumar', 'PHP', '9876543212');

SELECT *
FROM students;

Here, student_id uniquely identifies every student.

29. Practical Composite PRIMARY KEY

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    enrollment_date DATE,
    PRIMARY KEY (student_id, course_id)
);

This design allows one student to enroll in multiple courses while preventing the same student from being enrolled in the same course more than once.

30. Complete PRIMARY KEY Example

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100),
    fee DECIMAL(10,2)
);

INSERT INTO students
(name, course, fee)
VALUES
('Amit', 'Python', 15000.00),
('Priya', 'Java', 18000.00),
('Rahul', 'PHP', 12000.00);

SELECT *
FROM students;

DESCRIBE students;

SHOW CREATE TABLE students;

The student_id column uniquely identifies each student and automatically generates IDs for new records.

📌 Key Points

  • A PRIMARY KEY uniquely identifies each row.
  • A table can have only one PRIMARY KEY constraint.
  • A PRIMARY KEY cannot contain NULL values.
  • PRIMARY KEY values must be unique.
  • A PRIMARY KEY can contain one or multiple columns.
  • A multi-column PRIMARY KEY is called a composite PRIMARY KEY.
  • A PRIMARY KEY can be created with CREATE TABLE.
  • You can add a PRIMARY KEY using ALTER TABLE.
  • You can remove a PRIMARY KEY using DROP PRIMARY KEY.
  • A PRIMARY KEY is commonly used with AUTO_INCREMENT.
  • A PRIMARY KEY can be referenced by a FOREIGN KEY.
  • MySQL creates a unique index for a PRIMARY KEY.

🧠 Quick Quiz

Question: Which statement about a PRIMARY KEY is correct?