Computer Science
Grade 7
20 min
Data Relationships
Data Relationships
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a data relationship is in a database.
Identify and explain the purpose of a Primary Key and a Foreign Key.
Differentiate between a One-to-One and a One-to-Many relationship.
Diagram a simple One-to-Many relationship between two tables.
Explain how a Foreign Key is used to connect two tables.
Analyze a real-world scenario and determine the data relationship involved.
How does Netflix know which shows you've watched, or how does your school library know which books you've checked out? 📚 It's all about connecting pieces of information!
In this lesson, you'll learn how databases link different sets of data together, just like connecting dots to see the bigger picture. Understanding these connections, called 'data relatio...
2
Key Concepts & Vocabulary
TermDefinitionExample
Table (or Entity)A collection of related data organized in rows and columns, like a spreadsheet. Each table stores information about one specific type of thing.A `Students` table would store information only about students, with columns for Student ID, First Name, Last Name, and Grade.
Record (or Row)A single entry in a table, representing one complete item.In a `Students` table, one row would contain all the information for a single student, like: (101, 'Alice', 'Smith', 7).
Field (or Column)A single piece of information in a record. It's the title of a column in a table.In a `Students` table, `FirstName` is a field. For the student Alice Smith, the value in the `FirstName` field is 'Alice'.
Primary Key (PK)A special field in a tab...
3
Core Syntax & Patterns
The Primary Key Rule
Every table must have one field designated as its Primary Key, and the value in this field must be unique for every record.
Use this rule when designing any table. It guarantees you can find and identify any single record without confusion. Good choices are ID numbers, not names (since two people can have the same name).
The Foreign Key Linking Rule
To create a One-to-Many relationship, add the Primary Key from the 'One' side table into the 'Many' side table. This new field is the Foreign Key.
Use this when you need to connect two tables. For example, to link Authors and Books (one author can write many books), you put the `AuthorID` (the PK from the `Authors` table) into the `Books` table as a Foreign Key.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
In an online store's database, the `Products` table contains a Foreign Key field called `CategoryID`. The `Categories` table has `CategoryID` as its Primary Key. What can you infer about the relationship between products and categories?
A.Each product can belong to many categories.
B.Each category can have only one product.
C.Each product belongs to exactly one category, and a category can have many products.
D.Products and categories are not related; the field name is a coincidence.
Challenging
A database for a publisher has an `Authors` table (PK: `AuthorID`) and a `Books` table, with `AuthorID` as a Foreign Key in the `Books` table. This works for books with one author. What problem does this One-to-Many model create if a book is co-authored by two people?
A.The `Books` table would need two Primary Keys.
B.The `AuthorID` field in the `Books` table can only store the ID of one author.
C.The database would crash if you tried to enter two authors.
D.You would have to create a duplicate record for the book.
Challenging
Consider the social media example (`Users` to `Posts`). If you wanted to add a 'likes' feature, why does a simple One-to-Many relationship NOT work for tracking which users liked which posts?
A.Because a user can like their own post.
B.Because a post can have zero likes.
C.Because one user can like many posts, AND one post can be liked by many users.
D.Because the `Users` table would get too big.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free