- Define and explain Table Relationships in your own words
- Use key terms such as foreign key accurately
- Apply what you have learned to new examples and questions
- Avoid the common mistakes learners make with this topic
This lesson focuses on Table Relationships: link tables with foreign keys in one-to-many relationships.
Link tables with foreign keys in one-to-many relationships.
Key ideas
Keys stop data going missing
A primary key guarantees every row can be found exactly once — without one, two customers called 'Smith' become indistinguishable. Foreign keys then link tables: an order row stores the customer's ID rather than copying their whole address, so an address change is updated in one place only. This is the foundation of reliable data.
Relationships mirror the real world
One customer places many orders — a one-to-many relationship, built by putting the customer's ID as a foreign key in the Orders table. The foreign key must always point at a real primary key, a rule called referential integrity that stops orphaned records. Well-designed relationships mean no duplicated facts anywhere.
Key term — foreign key: A field in one table that stores the primary key of a row in another table, creating a link between them.
An Orders table has a CustomerID column. Is CustomerID a primary or foreign key here, and why?
A foreign key — it stores the primary key of a row in the Customers table, linking each order to its customer.
Answer: A foreign key — it stores the primary key of a row in the Customers table, linking each order to its customer.
- Storing the same fact in two tables Correction: store it once and link with a foreign key — duplicated data drifts out of sync when only one copy is updated.
- Forgetting the WHERE clause in an UPDATE or DELETE Correction: UPDATE Students SET Year = 13 with no WHERE changes every row — always write the WHERE first, or test with a SELECT using the same condition.
Practice
SELECT Title, Author FROM Books WHERE Year = 2010;.
One row: Leo — only Leo has Year 10.
It deletes every row in the table — add a WHERE clause, for example DELETE FROM Students WHERE ID = 4;, to remove only Leo.
It is the unique identifier for a row — duplicates or blanks would make it impossible to pinpoint exactly one record.
Quick check
Which of these best defines "foreign key"?
True or false: SQL tells the database exactly how to find the data, step by step.
- Table Relationships: link tables with foreign keys in one-to-many relationships.
- Keys stop data going missing: A primary key guarantees every row can be found exactly once — without one, two customers called 'Smith' become indistinguishable.
- table: A database structure of rows (records) and columns (fields), storing one type of thing — for example, one table per customer, one per order.
- Watch out for: storing the same fact in two tables