Introduction to databases and persistence
In a Relational Database (RDBMS), data is organized into Tables. This structure helps avoid repeating data and keeps it accurate.
Imagine a spreadsheet for a simple "Students" list:
| id | first_name | last_name | age | |
|---|---|---|---|---|
| 1 | John | Doe | [email protected] | 25 |
| 2 | Jane | Smith | [email protected] | 22 |
Students or Products).first_name or age). Every row must have a value for this column (even if it is empty, or NULL).In JavaScript, variables can hold any type of data. In a database, **Columns** are strict. You must decide what type of data goes in a column.
INTEGER: Whole numbers (e.g., age, count).
TEXT: Text strings (e.g., name, email).
REAL: Decimal numbers (e.g., price, temperature).
BOOLEAN: True/False (often stored as 1 for True, 0 for False).
DATE: Dates and times.
Constraints are rules for your data. They ensure the data is good quality.
email must be different).The "Relational" part comes from how we link tables together. We use Keys for this.
A unique ID for a specific row.
id.A field in one table that links to the Primary Key of another table.
Example:
We have Students and Classes. A student can sign up for a class. We create an Enrollments table to link them.
Table: Students
| id (PK) | name |
|---|---|
| 1 | John |
| 2 | Jane |
Table: Classes
| id (PK) | title |
|---|---|
| 101 | Intro to SQL |
| 102 | Advanced JS |
Table: Enrollments
| id (PK) | student_id (FK) | class_id (FK) |
|---|---|---|
| 1 | 1 (John) | 101 (Intro to SQL) |
| 2 | 1 (John) | 102 (Advanced JS) |
| 3 | 2 (Jane) | 101 (Intro to SQL) |
In the Enrollments table, student_id is a Foreign Key. It points to the id in the Students table.
One record in Table A matches exactly one record in Table B.
One record in Table A matches many records in Table B.
Many records in Table A match many records in Table B.
Enrollments table above). You cannot just put a list of IDs in one cell.Why do we split data into many tables? Why not put everything in one big "Enrollments" spreadsheet? Answer: To avoid copying data and making mistakes.
Imagine we stored the student's name in every Enrollment row:
By keeping Students in one table and just using the ID 1 in Enrollments, we only update the name in one place. This is called Normalization.
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0

Found a mistake or have a suggestion? Let us know in the feedback form.