Practice Quiz: Keys & Relationships in Databases

 

๐Ÿงช Practice Quiz: Keys & Relationships in Databases

Level: Beginner
Purpose: Reinforce concepts from Part 4
Format: 5 multiple choice questions + answers at the end


๐Ÿ”ธ Quiz Questions

1. What is the primary purpose of a Primary Key in a database table?
A. To store large files
B. To create duplicate records
C. To uniquely identify each record
D. To format column headers


2. Which of the following can be a valid Primary Key?
A. A student’s name
B. A unique ID number like StudentID
C. A subject name
D. A course title


3. What is a Foreign Key used for?
A. To store passwords securely
B. To make a copy of the table
C. To connect two tables by referencing a Primary Key
D. To delete data from another table


4. In a relationship between Students and Results, which table usually contains the Foreign Key?
A. Students
B. Results
C. Teachers
D. Courses


5. True or False: A Foreign Key must always refer to a column that is a Primary Key in another table.


Answers

  1. C

  2. B

  3. C

  4. B

  5. True


๐Ÿ“ Mini Assignment: Create Your Own Tables and Define Keys

Objective: Apply what you've learned by designing two simple tables with a relationship.


๐Ÿ“Œ Instructions:

  1. Create a Students table with the following columns:

    • StudentID (Primary Key)

    • Name

    • Class

  2. Create a Marks table with the following columns:

    • MarkID (Primary Key)

    • StudentID (Foreign Key – linked to Students table)

    • Subject

    • Score

  3. Write the SQL commands to create both tables and define the keys.


๐Ÿ’ก Optional Challenge:

Insert sample data into both tables (at least 2 students and 3 marks) and write a query to display student names along with their subjects and scores.


Find solution to the assignment in next post.


Understanding Primary and Foreign Keys in Databases (With Examples for Beginners)

 

← Back to Home

๐Ÿ”ท Part 4: Primary Keys and Foreign Keys – Building Relationships in Databases


๐Ÿ“ Introduction

So far, we’ve learned how data is stored in tables and how we use SQL to interact with it. But what happens when your database has more than one table?

That’s where relationships come in — and they’re built using keys.

Keys help connect tables, maintain data integrity, and allow complex queries across multiple data sets. Today, we’ll explore Primary Keys and Foreign Keys, the foundation of relational database design.


๐Ÿ”‘ What is a Primary Key?

A Primary Key is a unique identifier for each record in a table. No two rows can have the same primary key, and it can’t be left empty (NULL).

๐Ÿ” Example – Students Table:

| StudentID | Name  | Course     |
|-----------|-------|------------|
| 1         | Aisha | Math       |
| 2         | Ravi  | Science    |
| 3         | Sara  | English    |

Here, StudentID is the Primary Key — each student has a unique ID.

๐Ÿ“˜ Think of it like a roll number in a classroom — no two students have the same roll number.


๐Ÿ”— What is a Foreign Key?

A Foreign Key is a column in one table that refers to the primary key in another table. It creates a link between two related tables.


๐Ÿงฉ Example: Students & Results Tables

๐Ÿงพ Students Table

| StudentID | Name  |
|-----------|-------|
| 1         | Aisha |
| 2         | Ravi  |

๐Ÿงพ Results Table

| ResultID | StudentID | Subject  | Marks |
|----------|-----------|----------|-------|
| 101      | 1         | Math     | 85    |
| 102      | 2         | Science  | 90    |

In this case:

  • StudentID is the Primary Key in the Students table.

  • StudentID in the Results table is a Foreign Key — it refers to the Students table.

๐Ÿ’ก This relationship ensures that every result belongs to a valid student.


๐Ÿ” Why Use Keys and Relationships?

  • Data Integrity: Prevents orphaned or mismatched records

  • Less Redundancy: No need to repeat data across tables

  • Scalability: Makes your database easier to maintain as it grows

  • Real-Life Modeling: Mimics real-world relationships (students have results, customers place orders, etc.)


๐Ÿ”ง SQL Example: Defining Primary and Foreign Keys

-- Create Students Table
CREATE TABLE Students (
  StudentID INT PRIMARY KEY,
  Name VARCHAR(100)
);

-- Create Results Table
CREATE TABLE Results (
  ResultID INT PRIMARY KEY,
  StudentID INT,
  Subject VARCHAR(50),
  Marks INT,
  FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);

๐Ÿ“š Real-Life Analogy

Imagine:

  • Primary Key = National ID Number

  • Foreign Key = Form asking for your National ID

You can’t submit the form unless your ID is valid — just like foreign keys rely on real, matching primary keys.


๐Ÿง  Recap

  • Primary Key: Uniquely identifies a row in a table (e.g., StudentID)

  • Foreign Key: Connects a row to another table using a reference

  • These keys help create relationships between tables and ensure the accuracy and reliability of your data.


✅ What’s Next?

In Part 5, we’ll explore NoSQL databases — a modern alternative to relational databases that works better for unstructured data, large-scale applications, and real-time needs.



Featured Post

NoSQL Databases Explained: Beginner's Guide to Flexible Data Storage (With Examples)

๐Ÿ”ท Part 5: Introduction to NoSQL Databases – A Flexible Alternative to Relational Models ๐Ÿ“ Introduction So far, we’ve learned how relat...

Popular Posts