LearnViz CompSci
Home / Learn / Databases / Primary Keys

Beginner Topic 5 of 51 Databases

Primary Keys

Column that uniquely identifies each row.

How it works

A primary key is a column or set of columns whose values uniquely identify each row in a table; it cannot be NULL and cannot repeat. The database builds a unique index on it to enforce uniqueness and speed up lookup. Keys can be natural, drawn from real-world data such as an email address, or surrogate, such as an auto-increment integer or UUID with no business meaning. Composite keys use multiple columns.

Step-by-step walkthrough

  1. A primary key column identifies each row.
  2. Values must be unique.
  3. Inserting a second id 3 is rejected.
  4. The key can never be NULL.
  5. Other tables point to a row through its key.

Where it is used

Every table should have one so rows can be referenced by other tables through foreign keys and updated or deleted precisely. ORMs and replication tools depend on them.

Common pitfall

Natural keys can change, such as a renamed username, forcing cascading updates; surrogate keys avoid this. Sequential IDs expose counts and are guessable, while random UUIDs fragment indexes. Choosing very wide keys inflates every index and foreign key. Never reuse a key for a different entity.

Where it fits in the course

Primary Keys is topic 5 of 51 in Databases, rated Beginner. It follows Tables, Rows and Columns. The next topic is Foreign Keys.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Databases quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Primary Keys?
Column that uniquely identifies each row. It belongs to Databases and is a beginner-level topic.
Where do people use Primary Keys?
Every table should have one so rows can be referenced by other tables through foreign keys and updated or deleted precisely.
What is the most common mistake with Primary Keys?
Natural keys can change, such as a renamed username, forcing cascading updates; surrogate keys avoid this.
What should I learn before this topic?
Start with Tables, Rows and Columns, the topic just before it in Databases.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Databases quiz.
Is Primary Keys hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Primary Keys?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Primary Keys?
Tables, Rows and Columns, Foreign Keys, Relational Model are closely connected. The full list is in the related topics section of this page.
What should I study after Primary Keys?
Continue with Foreign Keys, the next topic in Databases.

Related topics