- Lab
- Data

SQL Fundamentals: Working with Primary and Foreign Keys
In this hands-on lab, you will discover the essentials of SQL database management by working with primary and foreign keys. You’ll start by creating and populating tables, then progress to establishing relationships between them to ensure data integrity. Throughout the lab, you’ll practice using SQL queries to manipulate and retrieve data effectively, reinforcing your understanding of how databases operate. By the end of this lab, you’ll have a solid foundation in managing relational databases, equipping you with the skills to apply these concepts in real-world scenarios.

Path Info
Table of Contents
-
Challenge
Defining Primary Keys
SQL Guide
For each task, replace or update the existing SQL code in the top pane (main.sql) with your answer to the task and run it to see the results in the bottom pane.
Step 1: Defining Primary Keys
To review the concepts covered in this step, please refer to the course Primary Keys and Foreign Keys module of the Introduction to SQL Course.
In this step, you will deepen your understanding of primary keys and their critical role in relational databases. A primary key uniquely identifies each record in a table, ensuring data integrity by preventing duplicate entries. You’ll also explore how to effectively query and organize your data using SQL, ensuring that the correct data is retrieved in the specified order. This step builds a foundation for more advanced database operations, helping you master the essentials of data management.
📚 Industry Example
In any database system, uniquely identifying each record is crucial to ensure data integrity. For example, consider a simple online store that tracks orders. Each order needs a unique identifier—this is where a primary key, like `OrderID`, comes into play. The primary key ensures that each order is distinct and can be easily retrieved or referenced in the database. Without primary keys, managing orders would become chaotic, with potential mix-ups, duplicates, or lost data. This concept is fundamental in all database systems, from small applications to large-scale enterprise solutions. -
Challenge
Establishing Foreign Key Relationships
Step 2: Establishing Foreign Key Relationships
In this step, we'll continue building out our database by creating a table for our customers, establishing a foreign key relationship with the Orders table from Step 1, and exploring how to query data across these related tables. This process will help ensure that each order is correctly associated with a customer, maintaining the integrity and reliability of our data.
📚 Industry Example
As databases grow more complex, it’s important to manage relationships between different types of data. For instance, a database might have one table for customers (`Customers`) and another for their orders (`Orders`). A foreign key is a way to link these two tables together, ensuring that every order is tied to an actual customer. This relationship enforces data integrity, making sure that every piece of data in one table (like an order) is connected to relevant data in another table (like a customer). Without foreign keys, it would be easy for data to become disjointed or for errors to occur, such as orders being assigned to non-existent customers. -
Challenge
Referential Integrity
Step 3: Referential Integrity
Now that your database has tables with relationships established via foreign keys, this step will focus on enforcing referential integrity and managing data across related tables. You’ll explore what happens when you attempt to modify or delete data that’s involved in these relationships.
📚 Industry Example
Referential integrity is a key principle in database management, ensuring that relationships between tables remain consistent. For example, in a database used by a university, there might be a `Students` table and a `Courses` table. A foreign key in the `Courses` table could reference a student’s ID to indicate who is enrolled in each course. Referential integrity ensures that if a student is deleted from the `Students` table, the database either prevents the deletion (if they’re still enrolled in a course) or cascades the deletion to remove the student from the `Courses` table as well. This maintains the accuracy and consistency of the data across the database.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.