Database Design Overview
The proyecto database is a product management system that demonstrates proper relational database design with multiple tables, foreign keys, and relationships.This schema is used throughout TEMA-04 exercises and represents a real-world inventory management system.
Database Schema
The proyecto database consists of four main tables:Creating the Database
Complete SQL Schema
Here’s the complete schema fromproyecto.sql:
Table Details
Productos (Products)
Stores product information with the following structure:| Column | Type | Constraints | Description |
|---|---|---|---|
id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique product identifier |
nombre | VARCHAR(200) | NOT NULL | Full product name |
nombre_corto | VARCHAR(50) | UNIQUE, NOT NULL | Short code for product |
descripcion | TEXT | NULL | Detailed product description |
pvp | DECIMAL(10,2) | NOT NULL | Retail price |
familia | VARCHAR(6) | FOREIGN KEY | Product category |
Familias (Categories)
Defines product categories:| Column | Type | Constraints | Description |
|---|---|---|---|
cod | VARCHAR(6) | PRIMARY KEY | Category code |
nombre | VARCHAR(200) | NOT NULL | Category name |
Tiendas (Stores)
Stores information about retail locations:| Column | Type | Constraints | Description |
|---|---|---|---|
id | INT | PRIMARY KEY, AUTO_INCREMENT | Store identifier |
nombre | VARCHAR(100) | NOT NULL | Store name |
tlf | VARCHAR(13) | NULL | Phone number |
Stocks (Inventory)
Tracks product quantities at each store:| Column | Type | Constraints | Description |
|---|---|---|---|
producto | INT | PRIMARY KEY, FOREIGN KEY | Product ID |
tienda | INT | PRIMARY KEY, FOREIGN KEY | Store ID |
unidades | INT UNSIGNED | NOT NULL | Available units |
Foreign Key Relationships
The schema implements referential integrity through foreign keys:Cascade Behavior
All foreign keys useON UPDATE CASCADE and ON DELETE CASCADE:
- ON UPDATE CASCADE: If a familia code changes, all related productos update automatically
- ON DELETE CASCADE: If a familia is deleted, all related productos are also deleted
Cascade operations maintain data integrity but should be used carefully. Deleting a familia will delete all products in that category!
Database User Setup
Creating a dedicated database user with minimal necessary privileges:Minimal Privileges
Grant only necessary privileges. In production, avoid
GRANT ALL and specify exact permissions (SELECT, INSERT, UPDATE, DELETE).Connecting to MySQL from PHP
Once the database is set up, connect using the PDO configuration:Data Types Best Practices
DECIMAL for Money
VARCHAR vs TEXT
- Use
VARCHARwith specific length for fields with known limits (names, codes) - Use
TEXTfor longer content without fixed length (descriptions)
Character Encoding
utf8mb4 supports all Unicode characters including emojis and special symbols.
Verifying the Schema
After creating the database, verify the structure:Common Schema Patterns
Auto-Increment Primary Keys
productos and tiendas tables. MySQL automatically assigns unique IDs.
Composite Primary Keys
stocks table to ensure each product-store combination is unique.
String Primary Keys
familias table. Useful for human-readable identifiers.
Next Steps
With your database schema in place, you’re ready to:- Implement CRUD Operations to manage data
- Learn Advanced Queries for complex data retrieval
- Review PDO Introduction for connection basics