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.
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_id | name | major |
|---|---|---|
| 101 | Ali | Computer |
| 102 | Sara | Management |
| 103 | Reza | Electrical |
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:
Studentsfor studentsTeachersfor teachersCoursesfor coursesCustomersfor customersProductsfor productsOrdersfor 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_id | name | major |
|---|---|---|
| 101 | Ali | Computer |
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 numbername— student namemajor— field of study
| student_id | name | major |
|---|---|---|
| 101 | Ali | Computer |
| 102 | Sara | Management |
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_id | title | price |
|---|---|---|
| 1 | Laptop | 50000000 |
| 2 | Mouse | 800000 |
| 3 | Keyboard | 1500000 |
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_idfirst_namelast_namebirth_datemajorphone
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_idtitlepricestock
| product_id | title | price | stock |
|---|---|---|---|
| 1 | Laptop | 50000000 | 8 |
| 2 | Mouse | 800000 | 25 |
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:
| name | major |
|---|---|
| Ali Ahmadi | Computer |
| Sara Rezaei | Management |
| Ali Ahmadi | Electrical |
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_id | name | major |
|---|---|---|
| 101 | Ali Ahmadi | Computer |
| 102 | Sara Rezaei | Management |
| 103 | Ali Ahmadi | Electrical |
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_id | name |
|---|---|
| 101 | Ali |
| 101 | Sara |
A primary key must not be NULL
A record needs a definite identifier. This is not a good primary key:
| student_id | name |
|---|---|
| 101 | Ali |
| NULL | Sara |
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_id | student_name | course_name | teacher_name |
|---|---|---|---|
| 1 | Ali | SQL | Ahmadi |
| 1 | Ali | Java | Rezaei |
| 2 | Sara | SQL | Ahmadi |
A lot of data repeats: Ali’s name, the SQL course, and teacher Ahmadi appear more than once.
Better to split:
Students
| student_id | name |
|---|---|
| 1 | Ali |
| 2 | Sara |
Courses
| course_id | title |
|---|---|
| 10 | SQL |
| 20 | Java |
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_id | name |
|---|---|
| 1 | Ali |
| 2 | Sara |
Orders
| order_id | student_id | amount |
|---|---|---|
| 1001 | 1 | 500000 |
| 1002 | 1 | 800000 |
| 1003 | 2 | 300000 |
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_id | name |
|---|---|
| 1 | Ali |
| 2 | Maryam |
Orders
| order_id | customer_id | amount |
|---|---|---|
| 500 | 1 | 200000 |
| 501 | 1 | 900000 |
| 502 | 2 | 300000 |
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:
Person 1 ←→ 1 Passport20. 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.
Customer 1 → N OrdersAli 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.
Department 1 → N Students22. 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_id | name |
|---|---|
| 1 | Ali |
| 2 | Sara |
Courses
| course_id | title |
|---|---|
| 10 | SQL |
| 20 | Java |
Enrollments
| student_id | course_id |
|---|---|
| 1 | 10 |
| 1 | 20 |
| 2 | 10 |
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_id | course_id | grade |
|---|---|---|
| 1 | 10 | 18 |
| 1 | 20 | 16 |
| 2 | 10 | 19 |
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_id | product_id | quantity |
|---|---|---|
| 100 | 1 | 2 |
| 100 | 5 | 1 |
| 101 | 1 | 1 |
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:
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:
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_id | customer_id |
|---|---|
| 1 | 10 |
| 2 | 50 |
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_id | name | manager_id |
|---|---|---|
| 1 | Maryam | 2 |
| 2 | Reza | 3 |
| 3 | Ali | NULL |
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 one1..N— at least one, many allowed0..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_id | name |
|---|---|
| 101 | Ali |
| 102 | Sara |
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_id | item_no | product |
|---|---|---|
| 1001 | 1 | Mouse |
| 1001 | 2 | Keyboard |
| 1002 | 1 | Laptop |
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 entity | Weak entity |
|---|---|
| Independent | Dependent |
| Has its own primary key | Key is usually completed with the owner entity’s key |
| Identifiable without another entity | Not fully identifiable without the owner entity |
| Example: Student | Example: 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.