🔑 Primary Key (`PRIMARY KEY`)

🟢 Beginner

📖 Definition

A Primary Key is a column (or combination of columns) that uniquely identifies each individual record/row in a relational database table. A primary key automatically enforces two fundamental relational rules: uniqueness (no duplicate values) and non-nullability (no NULL values allowed).

🌍 Multilingual Explanations

English

A Primary Key uniquely identifies every row in a table. It strictly enforces UNIQUE and NOT NULL constraints. A table can have at most ONE Primary Key.

Hindi

Primary Key har row ko uniquely identify karti hai. Isme duplicate values nahi ho sakti aur ye kabhi NULL nahi ho sakti. Ek table mein sirf ek hi Primary Key ho sakti hai.

Marathi

Primary Key mhanje table madhil pratyek row chi unique olakh. Yatch NULL kiva duplicate values sangu shakat nahit.

Hinglish

Primary Key is like a Student Roll Number or Aadhaar Number — har record unique hota hai aur zero/NULL empty value allowed nahi hoti.

📝 Rules of Primary Key

  • Uniqueness: Every value in the primary key column must be distinct across all rows.
  • No NULL Values: Primary key columns CANNOT contain NULL or unassigned values.
  • Single Primary Key per Table: A database table can have ONLY ONE primary key constraint (though it can consist of multiple columns as a Composite Primary Key).
  • Automatic B-Tree Indexing: Most relational databases (PostgreSQL, MySQL, SQLite) automatically create a unique clustered/non-clustered B-Tree index on the primary key for fast row lookups.

⚡ SQL Syntax

-- 1. Column-level Primary Key constraint
CREATE TABLE users (
    user_id INT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE
);

-- 2. Table-level named constraint (Recommended Best Practice)
CREATE TABLE products (
    product_id INT,
    product_name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2),
    CONSTRAINT pk_products PRIMARY KEY (product_id)
);

-- 3. Adding Primary Key to an existing table
ALTER TABLE employees
ADD CONSTRAINT pk_employees PRIMARY KEY (employee_id);

💡 Production Example

-- Creating an e-commerce customers table with auto-incrementing primary key
CREATE TABLE customers (
    customer_id SERIAL CONSTRAINT pk_customers PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE
);

-- Inserting records
INSERT INTO customers (first_name, last_name, email)
VALUES
('Alex', 'Sharma', 'alex@example.com'),
('Priya', 'Patel', 'priya@example.com');

-- Fetching record by primary key (Fast B-Tree index lookup)
SELECT * FROM customers WHERE customer_id = 1;

⚠️ Common Pitfalls & Mistakes

  • Attempting to insert NULL into Primary Key: Generates a database constraint error (`ERROR 1048: Column 'id' cannot be null`).
  • Attempting duplicate Primary Key insertion: Throws a unique constraint violation error.
  • Using volatile data as Primary Key: Avoid changing fields like phone numbers or email addresses as primary keys because updating primary key values breaks foreign key references across tables.

🎯 Tiered Practice Exercises

  1. Beginner: Create a students table with a primary key student_id and insert 3 rows.
  2. Intermediate: Add a primary key constraint to an existing orders table using ALTER TABLE.
  3. Advanced: Demonstrate that inserting duplicate primary key values is rejected by your database engine.

🧭 Navigation

← Keys Module Overview | Next: Foreign Key →