GK SOLUTIONS
AI • IoT • Arduino • Projects & Tutorials
DEFEAT THE FEAR

🗃️ Lesson 2: Databases & Tables

⏱️ Estimated time: 15 minutes | Difficulty: Beginner

Organizing Information

A database is a collection of related data. Data is organized into tables, which consist of columns (fields) and rows (records).

Creating a Database

CREATE DATABASE my_app;
USE my_app;

Creating Tables

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) UNIQUE,
    age INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Common Data Types

Type Description Example
INT Whole numbers 42
VARCHAR(n) Text up to n characters "Hello"
TEXT Long text Articles, descriptions
FLOAT / DOUBLE Decimal numbers 3.14
BOOLEAN True/False TRUE
DATE Date '2025-01-15'
TIMESTAMP Date + Time '2025-01-15 14:30:00'

Constraints

PRIMARY KEY    -- Unique identifier for each row
NOT NULL       -- Field cannot be empty
UNIQUE         -- No duplicate values
DEFAULT value  -- Default value if none given
AUTO_INCREMENT -- Automatically increases (1, 2, 3...)
FOREIGN KEY    -- Links to another table

Modifying Tables

ALTER TABLE users ADD phone VARCHAR(20);
ALTER TABLE users DROP COLUMN phone;
DROP TABLE users;  -- Deletes the whole table!

✅ Quick Quiz

❓ What does PRIMARY KEY do?

❓ What data type stores text up to a specific length?

❓ What does NOT NULL mean?

← Previous Next: SELECT Queries →