Modeling the Store (Keys)

Understanding Data Modeling
Before writing code or installing programs, we need to plan how we are going to save the information. Imagine you are going to build a house: first, you need the blueprints. In databases, we call this Modeling.
In Doña Pepa's store, we need to organize the information into separate "boxes." We will call these boxes Entities (which will later become Tables in our database).
For the mini-market, we have identified three main entities:
- 📦 Products: What we sell (bread, milk, rice).
- 👥 Customers: Who buys from us (Juan, Doña María).
- 🛒 Sales: The record that someone bought something.
The Master Key: Primary Key (PK)
Imagine that in Doña Pepa's neighborhood there are three people named "Juan." If Pepa writes in her notebook "Juan owes me $5", how does she know exactly which Juan to charge?
To solve this in real life, governments invented the ID Card (DNI/SSN). It is a unique and non-transferable number for each person.
In databases, we do exactly the same thing. We assign a unique and unrepeatable identifier to every single thing we register. We call this a Primary Key (PK).
- The Product "Milk" will have the unique identifier
id_product = 1. - The Customer "Juan García" will have the unique identifier
id_customer = 5.
💡 Golden Rule: Two products can never have the same
id_product. If milk is product number 1, no other product in the entire store will be number 1. It is sacred.
The Connection Bridge: Foreign Key (FK)
This is where the magic of "Relational" databases comes in: Relationships.
Imagine Doña Pepa sold milk to Juan García. In her old notebook, she would write the whole sentence: "On October 10th, Juan García, who lives on 5th street, bought XYZ brand Milk". That's writing too much and prone to errors!
In a Relational Database, we use the keys we created earlier to connect the information boxes. Instead of writing long texts, we simply use the ID numbers to point to the information.
We will write the following in the Sales table: "Customer 5 bought product 1".
That number 5 and that 1 we wrote inside the sales record act as Foreign Keys (FK).
They are called "foreign" because they are numbers that do not originally belong to that table, but come "visiting" from another table (Customers and Products) to create a connection bridge between them.
Quick Summary:
- 🔑 Primary Key: The row's own ID. It tells us "who I am."
- 🔗 Foreign Key: A number pointing to the ID of another table. It tells us "who I relate to."
With these mental blueprints ready, in the next lesson we will create our first tables using real SQL code!
