Major Keys, 6/100 Days of Code
In the little SQL experience I had prior to this course, I thought the primary key was just a way to keep track of rows in a table. They are far more important than I imagined.
Primary keys are at least one column in a table used to identify the meaning of its data. A single column primary key could be a student ID number in a table of students, and a multi-column primary key could be an athlete’s team and jersey number.
Simply put, you should be able to look at a primary key in a table and know what it represents.
The most important feature of a primary key is users cannot duplicate them when inserting information into a table. Let’s say an Android app prompts for a person’s favorite basketball player, team, and jersey number. Since each basketball player has a distinct team and jersey number, those columns would be set as the primary key of the database.
If a user enters LeBron James’ info (Cavaliers, #23, James), then accidentally enters Jeff Green with the same team and number (Cavaliers, #23, Green), the database will signal an error since the number 23 is a duplicate primary key. Fun fact, Jeff Green is #32 on the Cavaliers so this error is probably common in data entry. (see below)