🎯 Learning Objectives
- Understand database concepts (tables, rows, columns)
- Learn SQL basics (CREATE, INSERT, SELECT)
- Use phpMyAdmin to manage databases
- Create database schemas
- Understand data types and primary keys
🗄️ What is a Database?
Database: An organized collection of data stored electronically. Think of it like an Excel file, but much more powerful!
- Table: Like a spreadsheet (e.g., students table)
- Row: A single record (e.g., one student)
- Column: A field/attribute (e.g., name, email)
- Primary Key: Unique identifier for each row (e.g., id)
📊 Database vs File Storage
| Feature | Files (Week 10) | Database (Week 11) |
|---|---|---|
| Search | Slow, read entire file | Fast, indexed search |
| Update | Rewrite entire file | Update specific record |
| Structure | Unstructured text | Structured tables |
| Multiple Users | Difficult | Handles concurrency |
| Security | Basic | Advanced permissions |
🚀 Setting Up MySQL in XAMPP
- Open XAMPP Control Panel
- Start Apache and MySQL modules
- Click "Admin" next to MySQL to open phpMyAdmin
- You should see the phpMyAdmin interface
phpMyAdmin URL: http://localhost/phpmyadmin
📝 MySQL Data Types
| Data Type | Description | Example |
|---|---|---|
| INT | Integer numbers | 25, 100, -5 |
| VARCHAR(n) | Variable length string (max n) | VARCHAR(100) - "Ahmad" |
| TEXT | Long text content | Blog post content |
| DATE | Date (YYYY-MM-DD) | 2025-01-15 |
| DATETIME | Date and time | 2025-01-15 14:30:00 |
| DECIMAL(m,d) | Decimal numbers | DECIMAL(10,2) - 99.99 |
| BOOLEAN | True/False (0 or 1) | 1 (true), 0 (false) |
🏗️ Creating a Database
Using phpMyAdmin Interface:
- Click "New" in the left sidebar
- Enter database name:
school_db - Choose collation:
utf8mb4_general_ci - Click "Create"
Using SQL Query:
-- Create database
CREATE DATABASE school_db;
-- Select the database to use
USE school_db;
📋 Creating Tables
Students Table:
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
age INT NOT NULL,
course VARCHAR(50),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Table Structure Explained:
id INT AUTO_INCREMENT PRIMARY KEY- Unique ID, automatically increasesVARCHAR(100) NOT NULL- Text field, cannot be emptyUNIQUE- Value must be unique (no duplicates)DEFAULT CURRENT_TIMESTAMP- Automatically set current date/time
More Example Tables:
-- Products table
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(200) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Blog posts table
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
content TEXT NOT NULL,
author VARCHAR(100),
views INT DEFAULT 0,
published BOOLEAN DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
➕ Inserting Data (INSERT)
Insert Single Record:
-- Insert one student
INSERT INTO students (name, email, age, course)
VALUES ('Ahmad Ibrahim', 'ahmad@email.com', 20, 'Web Development');
-- Insert with fewer fields (optional fields can be skipped)
INSERT INTO students (name, email, age)
VALUES ('Fatima Yusuf', 'fatima@email.com', 22);
Insert Multiple Records:
INSERT INTO students (name, email, age, course)
VALUES
('Ahmad Ibrahim', 'ahmad@email.com', 20, 'Web Development'),
('Fatima Yusuf', 'fatima@email.com', 22, 'Data Science'),
('Ibrahim Musa', 'ibrahim@email.com', 21, 'Web Development'),
('Aisha Hassan', 'aisha@email.com', 19, 'UI/UX Design'),
('Yusuf Ahmed', 'yusuf@email.com', 23, 'Mobile Development');
📖 Selecting Data (SELECT)
Select All Records:
-- Get all students
SELECT * FROM students;
Select Specific Columns:
-- Get only names and emails
SELECT name, email FROM students;
Select with WHERE Clause:
-- Get students in Web Development course
SELECT * FROM students
WHERE course = 'Web Development';
-- Get students older than 20
SELECT * FROM students
WHERE age > 20;
-- Get specific student by email
SELECT * FROM students
WHERE email = 'ahmad@email.com';
Select with ORDER BY:
-- Order by name (A-Z)
SELECT * FROM students
ORDER BY name ASC;
-- Order by age (oldest first)
SELECT * FROM students
ORDER BY age DESC;
Select with LIMIT:
-- Get first 5 students
SELECT * FROM students
LIMIT 5;
-- Get students 6-10
SELECT * FROM students
LIMIT 5 OFFSET 5;
💻 Complete Example: School Database
-- 1. Create database
CREATE DATABASE school_db;
USE school_db;
-- 2. Create students table
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
age INT NOT NULL,
course VARCHAR(50),
grade CHAR(1),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 3. Insert sample data
INSERT INTO students (name, email, age, course, grade)
VALUES
('Ahmad Ibrahim', 'ahmad@email.com', 20, 'Web Development', 'A'),
('Fatima Yusuf', 'fatima@email.com', 22, 'Data Science', 'B'),
('Ibrahim Musa', 'ibrahim@email.com', 21, 'Web Development', 'A'),
('Aisha Hassan', 'aisha@email.com', 19, 'UI/UX Design', 'B'),
('Yusuf Ahmed', 'yusuf@email.com', 23, 'Mobile Development', 'A'),
('Zainab Ali', 'zainab@email.com', 20, 'Web Development', 'C'),
('Usman Bello', 'usman@email.com', 24, 'Data Science', 'B'),
('Halima Sani', 'halima@email.com', 21, 'UI/UX Design', 'A');
-- 4. Query examples
-- Get all students
SELECT * FROM students;
-- Get Web Development students only
SELECT * FROM students
WHERE course = 'Web Development';
-- Get students with grade A
SELECT * FROM students
WHERE grade = 'A';
-- Count students per course
SELECT course, COUNT(*) as total
FROM students
GROUP BY course;
-- Get youngest to oldest
SELECT name, age FROM students
ORDER BY age ASC;
✏️ Practice Exercises
- Create a database called
library_db - Create a
bookstable with: id, title, author, isbn, year, price - Insert at least 5 books
- Write queries to:
- Get all books
- Find books by a specific author
- Get books published after 2020
- Order books by price
🔑 Key SQL Commands Summary
| Command | Purpose | Example |
|---|---|---|
| CREATE DATABASE | Create a database | CREATE DATABASE mydb; |
| CREATE TABLE | Create a table | CREATE TABLE users (...); |
| INSERT INTO | Add data | INSERT INTO users VALUES (...); |
| SELECT | Retrieve data | SELECT * FROM users; |
| WHERE | Filter results | WHERE age > 20 |
| ORDER BY | Sort results | ORDER BY name ASC |
| LIMIT | Limit results | LIMIT 10 |
✅ Today's Deliverable
- ✓ School database created with students table
- ✓ At least 5 students inserted
- ✓ Screenshots of phpMyAdmin showing your database
- ✓ SQL queries tested and working
- ✓ Practice exercises completed