Stage 1 · Lesson 2 of 8
Primary keys and foreign keys
A is a column, or a group of columns, that identifies exactly one row. Its value must be unique, and it can never be (empty). A is a column that holds another table's primary key.
You declare it as a , a rule the database enforces, so the database refuses values that do not exist in the other table. Together, keys are how tables connect. Every order points at one customer, and the database stops you creating an order for a customer who is not there.
Keys also help speed, because the database automatically builds an index (a lookup structure) on each primary key. You will use them constantly when you learn joins in the next stages.
Common trapchoosing a primary key that can change or repeat, such as an email address or a name, or leaving foreign key constraints off and ending up with rows that point at nothing.
A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CourseFreeCS50's Introduction to Databases with SQL
Harvard University · Beginner
Watch Lecture 0 (Querying) for the relational model; it uses SQLite, and the ideas carry straight across
- CourseFreeQuery and modify data with Transact-SQL
Microsoft · Beginner
Start with the Introduction to Transact-SQL module; it sets up a sample database in the browser or locally