Logo hdtoledo
hdtoledo.dev
Módulos_del_Sistema
MÓDULO_TEÓRICO

Building the Foundations (DDL)

Building DDL Foundations

Step Zero: Preparing our Workshop (Local Environments)

Before building our tables, we need a place to build! Just like Doña Pepa needed to buy a storefront before putting up shelves, we need to install a database "engine" on our computer.

Don't panic, nowadays installing these engines is as easy as installing any other program. There are several very popular all-in-one "environments" or packages:

  1. XAMPP / MAMP / LAMP: It's the friendliest environment to start with. It includes MySQL (the engine) and a visual interface in the browser called phpMyAdmin.
  2. PostgreSQL + pgAdmin: One of the most powerful databases in the world.
  3. SQL Server Express: Microsoft's official engine. Very robust.

💡 Recommendation: For this course, we recommend using XAMPP or simply installing MySQL Workbench. Both are excellent for learning without getting tangled in complex configurations.


Building the Foundations (DDL)

Now that we have our storefront (our installed environment), it's time to talk about DDL (Data Definition Language).

Think of DDL as the construction tools (hammers, drills). We don't put bread or milk into the store with DDL; with DDL we build the empty shelves and decide what size they will be.

1. Buying the Storefront: CREATE DATABASE

The first thing is to create the general space where our entire store will live. Open your MySQL console or visual tool and type this magic command:

sql
CREATE DATABASE dona_pepa_store; USE dona_pepa_store;
  • CREATE DATABASE: It's like buying the land and putting up a sign.
  • USE: Tells the system "Hey, I'm going to work inside this specific store, open the door".

2. Building the Shelves: CREATE TABLE

Now we will use CREATE TABLE to assemble our three main shelves (Entities) that we modeled in the previous lesson: Products, Customers, and Sales.

Before building, we must tell the database what kind of box will go on each level of the shelf:

  • INT: Only accepts whole numbers (1, 2, 3...).
  • VARCHAR(50): Accepts text, up to 50 characters (ideal for short names).
  • DECIMAL(10,2): Accepts numbers with 2 decimals (ideal for prices, e.g., $15.50).

Shelf 1: Products

sql
CREATE TABLE Products ( id_product INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, price DECIMAL(10, 2) NOT NULL, stock INT NOT NULL );

(Note: AUTO_INCREMENT makes MySQL assign the ID number automatically: 1, 2, 3...)

Shelf 2: Customers

sql
CREATE TABLE Customers ( id_customer INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, phone VARCHAR(15) );

Shelf 3: Sales (The Bridge)

This is where we use our famous Foreign Keys to connect everything.

sql
CREATE TABLE Sales ( id_sale INT PRIMARY KEY AUTO_INCREMENT, date DATETIME DEFAULT CURRENT_TIMESTAMP, -- The fields that will hold the "IDs" from the other tables id_customer INT, id_product INT, -- Creating the bridges (Foreign Keys) FOREIGN KEY (id_customer) REFERENCES Customers(id_customer), FOREIGN KEY (id_product) REFERENCES Products(id_product) );

And that's it! With these DDL statements, we have built the perfect foundations. Doña Pepa's store now has its empty and connected shelves, patiently waiting for us to fill them with inventory in the next lesson.