E-Learning Platform
0 of 139 builtAlways freeSign in
← Database Foundations I
1.04

The ID Column

On this stop
  • Primary keys
  • Unique IDs

A new student enrolls today: Sara Ali. Lovely — except the drawer already holds a Sara Ali. Different person, same name. Next week Marcus decides he'd rather go by his middle name. If names are how you find cards in the drawer, both events are small emergencies: one name now points at two people, and one person no longer matches their card.

Names, it turns out, are terrible labels. They repeat, they change, they get misspelled. Facts deserve a label that never wobbles.

The primary key

That's what the id column has been doing in every grid so far. Each record gets a value that is unique — no two rows in the table may share it — and permanent — assigned once, never changed, never reused, even if the record it labeled is long gone.

A column that plays this role is called the table's primary key. Think of a library card number: two members can share a name, a birthday, even an address, but never a card number. When the librarian needs to be certain — this Sara, not that one — the card number settles it.

Up closeText
students
id | name       | age
---+------------+----
1  | Sara Ali   | 14
2  | Marcus Lee | 15
3  | Sara Ali   | 13

Two Saras, zero confusion. Ask for student 3 and there is exactly one answer, today and forever. Marcus can change his name tomorrow; he'll still be student 2, and everything that ever pointed at student 2 still points at the same human.

Why not use something real?

A fair objection: why invent a number when people already have unique-ish things — email addresses, phone numbers, national ID numbers? Because "unique-ish" is doing quiet work in that sentence. Emails get abandoned and re-registered by strangers. Phone numbers get recycled by carriers. Families share an email. Real-world identifiers belong in the record, as ordinary facts that may change; the primary key stands apart from all of them — a value that means nothing except "this exact record," which is precisely why it can be trusted to mean it forever.

The common mistake here is picking a "meaningful" key because it feels tidier, then paying for it the day the meaningful thing changes. Database designers learned this the hard way, decades ago, and settled on a habit you'll now recognize everywhere: an id column, plain numbers, counting up from 1, assigned by the database itself so nobody ever hands out the same number twice.

IDs are for pointing at

Here's the quiet superpower, and the setup for the rest of this unit: once every record has one true label, other records can refer to it by that label. A loan card doesn't need to copy a member's whole life story — it can say "member 2 borrowed book 7" and be exact.

Before we can enjoy that trick, though, you should watch what happens without it. Next lesson we try to cram an entire school into one giant table — and watch it rot in real time.