Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Uniqueness and Keys - Intro to Relational Databases

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. In a lot of databases, we're talking
    about unique entities out in the world,
  2. like individual people or locations, or
    email accounts, cities, or gorillas.
  3. If something is unique,
    there can't be two of them.
  4. So for instance, names aren't unique,
    not even full names.
  5. There are a lot of people in
    the world named Jennifer Smith.
  6. But a particular person named Jennifer
    Smith is still a unique individual.
  7. You wouldn't want to give one
    person another person's grades, or
  8. their parking tickets,
    just because they share the same name.
  9. Whenever we want to unambiguously relate
    a row of one table to a row of another,
  10. to see they are about the same person or
    thing out in the world.
  11. We have to have a unique value
    to talk about that thing.
  12. In the cutest animals database, we used
    a numerical ID for each animal picture,
  13. which we used to relate
    the votes table to it.
  14. That's a pretty common choice.
  15. Just make up a unique number for
    your database.
  16. It's so common that most database
    systems can do it for you.
  17. User IDs, comment IDs on forums and
    so on, are all examples of this.
  18. In database terminology, a column and
  19. a table that uniquely identifies rows on
    that table, can be called a primary key.
  20. Sometimes the world gives us
    a natural primary key for a table.
  21. For instance, if you have a table of
    countries and their capital cities,
  22. you can be confident that there
    aren't two countries named France.
  23. So in that case, you can safely use
    the name of the country as a key.
  24. You don't have to call
    it country number 29.
  25. But you have to make sure
    that a key really is unique.
  26. For instance, in the US,
  27. there are several states with
    a city named Springfield.
  28. There's Springfield,
    Massachusetts, Springfield,
  29. Illinois, and a whole bunch of others.
  30. So it's obvious that the name
    Springfield isn't unique by itself.
  31. Now, you might be tempted to think
    that city plus state is unique, but
  32. it turns out that
    that's not true either.
  33. In the state of Wisconsin, there are
    five different towns named Springfield,
  34. and there are three in New Jersey,
    and two in Texas.
  35. Doh!
  36. And that's why most countries
    use postal codes, or
  37. what we call zip codes in the US.
  38. Just like saying that somebody
    is player number three or
  39. user 1,723, instead of using their name.
  40. Using a made up number like 54028,
    or 54659,
  41. instead of Springfield,
    Wisconsin, guarantees uniqueness.