The relational model, tables, and keys

Tables, rows and columns, entities and attributes, primary keys, foreign keys, one-to-one, one-to-many, and many-to-many, recursive relationships, cardinality, existence dependency, and strong vs weak entities.

Beginner 35 min read SQLRelational ModelTable
Lesson 3 / 7 0%
View path

The relational model, tables, and keys

Level: beginner · Reading time: about 30 to 35 minutes

Topics: relational model, table, row, column, entity, attribute, primary key, foreign key, relationship, recursive relationship, cardinality, existence dependency, strong entity, weak entity

What will we learn?

So far we know what a database is, why we use a DBMS, who works with databases, what a query is, and what a transaction means.

Now we look at the real structure: how data is organized inside a database.

By the end you should be able to:

  • recognize a table
  • tell a row from a column
  • explain entity and attribute
  • spot a primary key
  • understand a foreign key
  • see how tables relate
  • tell one-to-one, one-to-many, and many-to-many apart
  • recognize a recursive relationship
  • explain cardinality
  • explain existence dependency
  • tell strong entities from weak entities
  • sketch first tables for a simple problem
1. What is the relational model?

One of the most common ways to store data is the relational model.

In this model, data is kept mainly as tables.

A university that wants to store student data can have a Students table:

student_idnamemajor
101AliComputer
102SaraManagement
103RezaElectrical

The table holds three students. Each student is a row; their attributes sit in columns.

The relational model organizes data as tables that can relate to one another.
2. What is a table?

A table is a set of related data about one topic.

For example:

  • Students for students
  • Teachers for teachers
  • Courses for courses
  • Customers for customers
  • Products for products
  • Orders for orders

Each table should usually be about one main concept. Putting students, products, orders, and teachers in one table without a reason is a poor design.

3. What is a row?

Each table row is one record.

student_idnamemajor
101AliComputer

This row is one student.

Row = one instance or one record

A table of 1000 students usually has 1000 rows.

4. What is a column?

Columns describe attributes of the data.

In the students table:

  • student_id — student number
  • name — student name
  • major — field of study
student_idnamemajor
101AliComputer
102SaraManagement

The name column holds every student’s name.

Column = one specific attribute of the records
5. Row vs column

This difference matters. Take this table:

product_idtitleprice
1Laptop50000000
2Mouse800000
3Keyboard1500000

In this table:

  • Each row is one product.
  • Each column is one product attribute.

The second row 2 | Mouse | 800000 is one record. The price column holds every product’s price.

6. What is an entity?

An entity is something we want to store information about.

In a university:

  • Student
  • Teacher
  • Course
  • Department

In an online shop:

  • Customer
  • Product
  • Order
  • Payment

In a library:

  • Book
  • Member
  • Loan

If we store student data, Student is an entity.

7. What is an attribute?

Each entity has properties. Those properties are attributes.

A Student entity may have:

  • student_id
  • first_name
  • last_name
  • birth_date
  • major
  • phone
Entity = the thing we store data about
Attribute = a property of that entity
8. A simple entity and attribute example

In a shop, one entity is Product. Its attributes may be:

  • product_id
  • title
  • price
  • stock
product_idtitlepricestock
1Laptop500000008
2Mouse80000025

Product is the entity. product_id, title, price, and stock are its attributes.

9. The problem of identifying records

Suppose the students table looks like this:

namemajor
Ali AhmadiComputer
Sara RezaeiManagement
Ali AhmadiElectrical

Two students are named Ali Ahmadi. “Update Ali Ahmadi” is ambiguous.

Each record needs a unique identifier. That is where the primary key comes in.

10. What is a primary key?

A primary key is a column that uniquely identifies each record.

student_idnamemajor
101Ali AhmadiComputer
102Sara RezaeiManagement
103Ali AhmadiElectrical

Even with the same name, student_id = 101 is not student_id = 103. We can point at exactly one student.

11. Important primary key rules

A primary key must distinguish records from each other.

Values must be unique

Two records must not share the same primary key. This is wrong:

student_idname
101Ali
101Sara

A primary key must not be NULL

A record needs a definite identifier. This is not a good primary key:

student_idname
101Ali
NULLSara
12. Can a name be a primary key?

Technically it can work in some cases, but it is usually a poor choice.

name
Ali Ahmadi
Sara Rezaei
Ali Ahmadi

Names can repeat. Prefer an identifier such as student_id, customer_id, or product_id.

13. Primary key examples
  • Students: student_id
  • Products: product_id
  • Customers: customer_id
  • Orders: order_id
  • Employees: employee_id

Names like id or <entity>_id are very common.

14. Why use several tables?

Why not put everything in one big table?

student_idstudent_namecourse_nameteacher_name
1AliSQLAhmadi
1AliJavaRezaei
2SaraSQLAhmadi

A lot of data repeats: Ali’s name, the SQL course, and teacher Ahmadi appear more than once.

Better to split:

Students

student_idname
1Ali
2Sara

Courses

course_idtitle
10SQL
20Java

Then we need to connect the tables. That is where relationships and foreign keys come in.

15. What is a foreign key?

A foreign key is a column that links two tables.

Students

student_idname
1Ali
2Sara

Orders

order_idstudent_idamount
10011500000
10021800000
10032300000

In Orders, student_id says which student owns each order. Order 1001 has student_id = 1; Students tells us student 1 is Ali. So order 1001 belongs to Ali.

16. Primary key vs foreign key

This distinction is essential.

A primary key uniquely identifies rows in the same table. Example: Students.student_id.

A foreign key links to another table. Example: Orders.student_id references Students.student_id.

17. A simple example

Customers

customer_idname
1Ali
2Maryam

Orders

order_idcustomer_idamount
5001200000
5011900000
5022300000

In Customers, customer_id is the primary key. In Orders, order_id is the primary key, and customer_id is a foreign key.

18. What is a relationship?

A relationship is a link between entities or tables.

  • Customer → Order: a customer places orders.
  • Student → Course: a student takes courses.
  • Teacher → Course: a teacher teaches courses.

Three important types:

  • One-to-one
  • One-to-many
  • Many-to-many
19. One-to-one

One-to-one means a row in A links to at most one row in B.

Example: Person ↔ Passport — each person has one passport, and each passport belongs to one person.

A simple view:

text
Person 1 ←→ 1 Passport
20. One-to-many

This type is very common: one row in A can link to many rows in B.

A customer can have many orders, but each order belongs to one customer.

text
Customer 1 → N Orders

Ali can have orders 101, 102, and 103. Customer and Order are one-to-many.

21. Another one-to-many example

Each department can have many students — Computer: Ali, Reza, Sara, Maryam — but in this model each student belongs to only one department.

text
Department 1 → N Students
22. Many-to-many

Sometimes many rows in the first table link to many rows in the second.

A student can take many courses, and a course can have many students.

So Student ↔ Course is many-to-many.

23. The many-to-many problem

Ali takes SQL, Java, and Database. Sara takes SQL and Python. Reza takes Java and Database.

Storing that directly inside Students or Courses does not give a clean structure.

The usual solution is a junction table.

24. What is a junction table?

For many-to-many we usually add a third table.

Students

student_idname
1Ali
2Sara

Courses

course_idtitle
10SQL
20Java

Enrollments

student_idcourse_id
110
120
210

This says student 1 takes course 10, student 1 takes course 20, and student 2 takes course 10.

25. A junction table can hold its own data

Besides linking Student and Course, we may also store the grade.

student_idcourse_idgrade
11018
12016
21019

Enrollments now stores the relationship and data about that relationship.

26. An online shop example

First entities can be:

Customer: customer_id, name, phone

Product: product_id, title, price

Order: order_id, customer_id, order_date

Customer and Order are one-to-many, because one customer can place many orders.

27. What is the relationship between product and order?

An order can contain many products, and a product can appear in many orders. Order ↔ Product is many-to-many, so we need a junction table.

OrderItems

order_idproduct_idquantity
10012
10051
10111

This table says which products are in each order, and in what quantity.

28. Why table design matters

If we store data without a decent design we may get:

  • lots of repetition
  • inconsistent data
  • hard updates
  • deletes that wipe related facts
  • painful queries
  • a system that is hard to maintain

Database structure is a core part of the work.

29. A first look at SQL

We can briefly see how these ideas show up in SQL. Creating Students:

sql
CREATE TABLE Students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    major VARCHAR(100)
);

You do not need to memorize every detail yet. Notice PRIMARY KEY: student_id is the table’s primary key.

30. Foreign keys in SQL

Now create Orders:

sql
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    student_id INT,
    amount DECIMAL(10,2),
    FOREIGN KEY (student_id)
        REFERENCES Students(student_id)
);

FOREIGN KEY (student_id) marks the column as a foreign key. REFERENCES Students(student_id) says it points at Students.

31. A common mistake

Suppose Orders has:

order_idcustomer_id
110
250

But Customers only has:

customer_id
10
20

Customer 50 does not exist. A correctly defined foreign key lets the DBMS reject that invalid link. That protects data integrity.

32. How this session connects to the last one

Last session covered integrity and the DBMS. Here is a concrete case.

A foreign key stops us from storing a row that points at a missing record — for example an order for a customer who does not exist.

The sessions start to connect.

33. Recursive relationship

In ER modeling, sometimes an entity relates to itself. That is a recursive relationship.

Example: the Employee entity.

Each employee may have a manager, and the manager is also an employee.

So we have:

Employee → manages → Employee

For example:

  • Ali manages Reza.
  • Reza manages Maryam.

There is only one entity, but the link runs between instances of the same entity.

In an Employees table we usually add a column such as manager_id that points to employee_id in the same table.

employee_idnamemanager_id
1Maryam2
2Reza3
3AliNULL

To remember: recursive relationship = one entity relates to itself.

34. Cardinality

Cardinality says how many instances on one side of a relationship can link to instances on the other side — minimum and maximum.

Three common forms:

  • 1:1 — one-to-one
  • 1:N — one-to-many
  • M:N — many-to-many

Example:

Department 1 ---- N Employee

One department can have many employees, but in this model each employee belongs to one department.

Cardinality can be more precise than the relationship name alone, for example:

  • 0..1 — zero or one
  • 1..N — at least one, many allowed
  • 0..N — zero or many

To remember: cardinality = how many instances can participate on each side?

35. Existence dependency

In ER modeling, existence usually means existence dependency.

One entity can exist only if another entity exists.

Example:

Order → OrderItem

An order item without an order makes no sense. If there is no order, the item cannot stand on its own.

So OrderItem has an existence dependency on Order.

Another example:

Employee → Dependent

A dependent person in the system only makes sense when a specific employee exists.

This idea is closely tied to a weak entity, because a weak entity usually cannot exist without its owner entity.

To remember:

Existence dependency = can this entity exist without the other one?
  • A student can exist without taking a course → no existence dependency.
  • An order item cannot exist without an order → existence dependency.
36. Strong entity vs weak entity

Strong entity

An entity that can be identified on its own and usually has its own primary key.

Example: Student

student_idname
101Ali
102Sara

student_id alone identifies each student. Student is a strong entity.

Weak entity

An entity that cannot be fully identified on its own and depends on another entity.

Example: OrderItem

order_iditem_noproduct
10011Mouse
10012Keyboard
10021Laptop

item_no = 1 is not enough, because it can repeat in different orders. Full identity needs order_id + item_no, for example 1001 + 1.

OrderItem depends on Order and can be modeled as a weak entity.

Strong entityWeak entity
IndependentDependent
Has its own primary keyKey is usually completed with the owner entity’s key
Identifiable without another entityNot fully identifiable without the owner entity
Example: StudentExample: OrderItem
Strong = it introduces itself.
Weak = it needs another entity to introduce itself.

Weak does not mean unimportant — it only means the model needs an owner for identity or existence.

Session summary
  • Table — related records
  • Row — one record
  • Column — one attribute of the records
  • Entity — something we store data about
  • Attribute — a property of an entity
  • Primary key — uniquely identifies a row
  • Foreign key — links two tables

Three important relationship types: one-to-one, one-to-many, and many-to-many. Many-to-many usually needs a junction table.

In ER we also covered:

  • Recursive relationship — one entity relates to itself (Employee → manages → Employee).
  • Cardinality — how many instances can link on each side.
  • Existence dependency — can one entity exist without another?
  • Strong entity — independent, identified by its own primary key.
  • Weak entity — depends on an owner entity for identity or existence (OrderItem).

Session 3 exercises

Work these after the reading. A sample answer appears after you submit.