Warming up the neural circuits...
The "R" in RDBMS stands for Relational. This is the most important concept in database engineering. By the end of this module, you'll know how to:
Imagine we are building a blog. If we store everything in one table, it looks like this:
| post_title | author_name | author_bio |
|---|---|---|
| Intro to | Alice | Senior Dev... |
| Advanced Joins | Alice | Senior Dev... |
| Node.js Guide | Alice | Senior Dev... |
If Alice changes her bio, we have to update 3 rows. If she has 1,000 posts, we have to update 1,000 rows. This is Redundancy, and it leads to bugs.
is the process of organizing data to minimize duplication. We split the "Big Table" into smaller, related tables.
zip_code, you don't need city because the zip code already tells you the city).To link our new tables, we use Keys.
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(200),
author_id INTEGER REFERENCES authors(id) -- This is the Foreign Key
);Referential Integrity: If you try to delete an author who still has posts, Postgres will block you. This prevents "orphan" posts with no author. This is the power of a Relational Database.
One record in Table A matches exactly one record in Table B.
User has one User_Profile.One record in Table A matches many records in Table B.
Author has many Posts. One Category has many Products.Many records in Table A match many records in Table B.
Student can take many Courses, and a Course has many Students.Rendering diagram…
Entity Relationship Diagram
-- The 'enrollments' table links students and courses
CREATE TABLE enrollments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
PRIMARY KEY (student_id, course_id)
);customers, orders, products).instructors and lessons.
lessons to instructors using a Foreign Key.instructor_id that doesn't exist. See what happens!Real-World Insight: Senior Backend Engineers are judged by how they design their database schemas. A bad schema can kill a project. A good, normalized schema makes the entire codebase easier to write and maintain.