Database Design with MySQL
What is SQL?
SQL (Structured Query Language) is a standard language for storing, manipulating and retrieving data in databases. MySQL is a popular open‑source relational database management system that uses SQL. A well‑designed database is the backbone of any data‑driven application.
MySQL Setup
You can install MySQL locally (e.g., using XAMPP) or use a cloud service. Once installed, connect via command line or a GUI like phpMyAdmin.
Common Data Types
- INT – whole numbers
- VARCHAR(size) – variable‑length strings
- TEXT – long text
- DATE – date only
- DECIMAL – exact fixed‑point numbers
- BOOLEAN – true/false
Creating a Table
The CREATE TABLE statement defines a new table and its columns. Each column has a name, data type, and optional constraints.
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
hire_date DATE
);
Constraints like NOT NULL, UNIQUE, and PRIMARY KEY enforce data integrity.
Inserting Data
INSERT INTO employees (first_name, last_name, email, hire_date)
VALUES ('Siraw', 'Tadesse', 'siraw@sirawdev.com.et', '2025-01-10');
Selecting Data
SELECT id, first_name, email FROM employees WHERE last_name = 'Tadesse';
You can filter with WHERE, sort with ORDER BY, and limit results with LIMIT.
Updating Data
UPDATE employees SET email = 'siraw@example.com' WHERE id = 1;
Deleting Data
DELETE FROM employees WHERE id = 1;
Joins
Join tables to combine related data from multiple tables.
SELECT employees.first_name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Indexes
Indexes speed up queries. Create them on columns frequently used in WHERE, JOIN, and ORDER BY.
CREATE INDEX idx_lastname ON employees(last_name);
Normalization
Normalization reduces data redundancy. Aim for 3rd Normal Form (3NF): each table should have a primary key, each column depends on the whole key, and no non‑key columns depend on other non‑key columns.