dev.rean.me
database

05 - Database Design with Primary and Foreign Keys (Video in Khmer)

Learn how primary keys and foreign keys connect database tables, with a simple SQL example, practical tips, and a Khmer video lesson.

2026-08-25 ยท 2 min read

Share:

Hello my friend! In this lesson, we learn how primary keys and foreign keys help connect database tables. We also look at a simple SQL example. The video is in Khmer.

What is a primary key?

A primary key identifies each row in a table. Each value must be unique, and it cannot be NULL.

For example, each student can have a different student_id. This ID helps us find one student row.

What is a foreign key?

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

For example, a student can belong to a class. The class_id in the students table can refer to the class_id in the classes table.

CREATE TABLE classes (
  class_id INT PRIMARY KEY,
  class_name VARCHAR(100) NOT NULL
);

CREATE TABLE students (
  student_id INT PRIMARY KEY,
  student_name VARCHAR(100) NOT NULL,
  class_id INT,
  FOREIGN KEY (class_id) REFERENCES classes(class_id)
);

Here, student_id identifies each student. class_id in students links to a class. The foreign key helps prevent a student from referring to a class that does not exist.

Watch the Video

This lesson shows primary keys, foreign keys, and a database design example in SQL.

Tips

  • Choose a primary key that uniquely identifies every row.
  • Keep key columns the same data type in both related tables.
  • Add a foreign key when one table should refer to a row in another table.
  • Insert the referenced row first, such as a class before a student that uses its class_id.
  • Think about what should happen to related rows when a referenced row is updated or deleted.