Available Tables

Next →

Understanding Database Relationships (Primary & Foreign Keys)

In a relational database, data is split across multiple tables that are linked together. These links are defined using primary keys and foreign keys.

Primary Keys

A primary key uniquely identifies each row in a table. Every table should have one column (or a combination of columns) that acts as its primary key — it must be unique and not null.

For example, the students table uses the id column as its primary key. You can view this in the Tables tab.

Foreign Keys

A foreign key is a column in one table that refers to the primary key of another table. It creates a relationship between two tables.

In this course, the enrollments table uses student_id and course_id to reference the students and courses tables, respectively. These are foreign keys. You can explore these relationships using the Tables tab on the right.

These relationships allow you to write queries that combine data from multiple tables — which you'll learn to do in the next levels.