Primary Key

SQL · Working with Tables

Theory

A PRIMARY KEY is the column, or set of columns, that uniquely identifies each row. The database enforces two rules on it: values must be unique and they can never be NULL. An employee id of 1 can exist only once, and every employee must have an id. A table can have only one primary key, but it may be composite, such as (order_id, line_no) for order lines.

Most engines automatically build an index on the primary key, so lookups and joins by key are fast. Other tables point to it with foreign keys. Keys can be natural (a real-world value like PAN number) or surrogate (a meaningless number from an identity column); surrogate keys are popular because they never change. Note that SQLite, for historical reasons, allows NULL in non-integer primary keys unless you also add NOT NULL.

Why we use it

Without a reliable identifier you cannot safely update, delete or join a specific row, and duplicates creep in. The primary key guarantees that every row can be found and referenced exactly.

When to use it

Give almost every table a primary key. Use a surrogate identity key when no natural value is stable and unique; use a composite key for link tables. For other columns that must be unique, such as email, add a UNIQUE constraint.

How it works

  1. INSERT or UPDATE a row
  2. Is the key value NULL? Yes: Reject the row. No: Check uniqueness.
  3. Does another row already have this key? Yes: Reject: duplicate key. No: Accept the row.
  4. Update the primary key index
  5. Row is stored and can be referenced

Syntax

column datatype PRIMARY KEY

Example

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic