Data Modeling

Updated · 8 min read
mediumPostgreSQL 18sqldata-modelinger-diagramentitiesdesign

Data modeling in SQL is the process of turning a description of a business into a set of tables, columns, keys and relationships. It is used before any table is created, because the design decides what questions the database can answer easily, and which mistakes it can prevent. Every Fee Tracker table you've queried came from a model like the one in this lesson. In this lesson, we will start from a plain description of a tutoring program and build its model step by step: entities, attributes, keys, relationships, a junction table, and an entity-relationship diagram.

In One Minute

  • An entity is a kind of thing the business tracks: a student, a course, a payment. Each becomes a table.
  • An attribute is a fact about an entity: a student's name, a course's fee. Each becomes a column.
  • Every entity needs a primary key; relationships become foreign keys.
  • A many-to-many relationship needs a junction table (enrollments).
  • An ER diagram draws entities as boxes and relationships as lines.

Step 1: Start from the Story

A model begins with a description in plain words, like this one:

A family tutoring program teaches courses. Each student lives in a city. A student can enroll in many courses, and a course can have many students. Each enrollment has a date and a status. Students pay for their enrollments, sometimes in installments, by card, check or cash. Some courses require another course first. Older students mentor younger ones.

The nouns are candidate entities: student, course, city, enrollment, payment. The verbs and phrases between them are candidate relationships: a student lives in a city, enrolls in courses, pays for enrollments.

Step 2: Entities and Attributes

Each entity becomes a table, and each fact about it becomes a column.

EntityAttributes (columns)
Studentfirst name, last name, email, phone, city, state, grade, joined on
Coursetitle, technology, level, fee
Citycity, state, time zone
Enrollmentenrolled on, status
Paymentamount, due on, paid on, method

A fact belongs to the entity it describes. A course's fee is a fact about the course, so it goes in courses, not in each payment. That one rule prevents most design mistakes, as Normalization shows.

Step 3: Keys

Every table needs a primary key that identifies each row. The Fee Tracker uses made-up numbers for students, courses and payments, a composite key for enrollments, and the natural pair (city, state) for cities:

SQL▶ Try it
SELECT table_name, constraint_name
FROM information_schema.table_constraints
WHERE constraint_type = 'PRIMARY KEY' AND table_schema = 'public'
ORDER BY table_name;
Output
 table_name  | constraint_name
-------------+------------------
 cities      | cities_pkey
 courses     | courses_pkey
 enrollments | enrollments_pkey
 payments    | payments_pkey
 students    | students_pkey
(5 rows)

Explanation: Each of the five tables has exactly one primary key. Choosing Keys explains when to use a made-up number and when a natural value works.

Step 4: Relationships and the Junction Table

A relationship becomes a foreign key in the table on the "many" side. A student lives in one city, and a city has many students, so students gets (city, state) pointing at cities. A payment belongs to one student, so payments gets student_id.

A many-to-many relationship can't be stored in either table, because a student would need a list of courses and a course a list of students. It becomes a third table, a junction table, with one row per pair. That's enrollments, and it carries the facts about the pairing itself, the date and status:

SQL▶ Try it
SELECT s.first_name, c.title, e.enrolled_on, e.status
FROM enrollments AS e
JOIN students AS s USING (student_id)
JOIN courses AS c USING (course_id)
ORDER BY s.student_id, c.course_id;
Output
 first_name |       title       | enrolled_on |  status
------------+-------------------+-------------+-----------
 Srihitha   | Java Basics       | 2025-08-25  | completed
 Srihitha   | Java OOP          | 2025-10-27  | completed
 Srihitha   | Java Collections  | 2025-12-22  | active
 Srihitha   | SQL Basics        | 2026-01-02  | active
 Advitha    | Java Basics       | 2025-12-29  | active
 Advitha    | SQL Basics        | 2025-12-29  | active
 Navika     | JavaScript Basics | 2025-11-10  | active
(7 rows)

Explanation: Srihitha has four rows (four courses), and Java Basics appears twice (two students). Each row is one student-course pair, so neither students nor courses needs a list column.

students and courses connect through enrollments: one row per student-course pair

Step 5: Constraints from the Story

Rules in the story become constraints. "By card, check or cash" becomes CHECK (method IN ('card', 'check', 'cash')). "Pay for their enrollments" becomes a foreign key from payments (student_id, course_id) to enrollments, so nobody can pay for a course they aren't enrolled in. "Some courses require another course first" is a foreign key from courses.prerequisite_id to courses itself, and "older students mentor younger ones" another self-reference, students.mentor_id.

Step 6: The ER Diagram

An entity-relationship (ER) diagram draws the model: each table is a box listing its columns, and each foreign key is a line. In crow's foot notation, the line's ends show how many rows can be on each side:

Symbol at a line endMeans
A single bar ( | )Exactly one
A circle ( O )Zero, optional
A crow's foot (three prongs)Many
Circle with crow's footZero or many
Five tables with their keys; crow's feet mark the many side of each relationship

The diagram is read along each line in both directions: "a student has zero or many enrollments; an enrollment belongs to exactly one student."

From a Brief to Tables

Common Mistakes

A column holding a list. courses = 'Java Basics, SQL Basics' can't be joined or counted. Use a junction table.

Facts in the wrong table. A course fee copied into every payment drifts out of date.

No primary key. Without one, duplicate rows can't be told apart or referenced.

Skipping the story. Tables designed from screens or reports miss rules the business takes for granted.

Interview Corner

What are entities and attributes?

Entities are the kinds of things a system tracks, which become tables; attributes are the facts about them, which become columns.

How do you model a many-to-many relationship?

With a junction table holding a foreign key to each side, one row per pair, with a composite primary key, plus any facts about the pairing.

What is an ER diagram?

A drawing of entities (tables) and their relationships (foreign keys), with notation such as crow's feet showing how many rows can be on each side.

Practice

Challenge: The model says each student has a city. Show each city with its time zone and the number of students who live there, sorted by city.

Show solution
SQL▶ Try it
SELECT ci.city, ci.state, ci.time_zone, count(s.student_id) AS students
FROM cities AS ci
LEFT JOIN students AS s ON s.city = ci.city AND s.state = ci.state
GROUP BY ci.city, ci.state, ci.time_zone
ORDER BY ci.city;
Output
     city      | state |      time_zone      | students
---------------+-------+---------------------+----------
 Dallas        | TX    | America/Chicago     |        1
 San Francisco | CA    | America/Los_Angeles |        2
(2 rows)

The relationship from students to cities uses the two-column key (city, state), so the join matches both columns. Because each city's time zone is stored once, in cities, every student in San Francisco automatically shares the right one.

Quick check: Data modeling

3 questions
1In the story, "a student can enroll in many courses, and a course can have many students". How is this stored?
2Where does a course's fee belong?
3In crow's foot notation, what does a circle at a line end mean?

Key Takeaways

You want toModel it as
A kind of thingA table with a primary key
A fact about itA column in that table
One-to-manyA foreign key in the "many" table
Many-to-manyA junction table with two foreign keys

Was this tutorial helpful?

Lesson 70 of 115