One ParasolOne Parasol

Database Design - Tables, Keys, and Normalization

Design efficient and organized database schemas

Database Design - Tables, Keys, and Normalization

📚 Resources for This Lesson

Primary Keys

Uniquely identifies each row in a table.

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100) UNIQUE
);

Foreign Keys

Creates relationships between tables.

CREATE TABLE departments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100)
);

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50),
    department_id INT,
    FOREIGN KEY (department_id) REFERENCES departments(id)
);

Data Types

Choose appropriate data types for efficiency.

-- Numeric types
INT, BIGINT, DECIMAL(10,2), FLOAT, DOUBLE

-- String types
VARCHAR(255), CHAR(10), TEXT

-- Date/Time types
DATE, TIME, DATETIME, TIMESTAMP

-- Other types
BOOLEAN, JSON, BLOB

Normalization

Organize data to reduce redundancy.

First Normal Form (1NF)

Second Normal Form (2NF)

Third Normal Form (3NF)

Creating Tables

CREATE TABLE orders (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT NOT NULL,
    order_date DATE NOT NULL,
    total_amount DECIMAL(10,2),
    status VARCHAR(20) DEFAULT 'pending',
    FOREIGN KEY (customer_id) REFERENCES customers(id),
    INDEX idx_customer_id (customer_id),
    INDEX idx_order_date (order_date)
);

Indexes

Speed up data retrieval.

-- Create index
CREATE INDEX idx_email ON employees(email);

-- Composite index
CREATE INDEX idx_name ON employees(last_name, first_name);

-- Unique index
CREATE UNIQUE INDEX idx_employee_id ON employees(id);

Constraints

Enforce data integrity.

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    email VARCHAR(100) NOT NULL UNIQUE,
    age INT CHECK (age >= 18),
    status VARCHAR(20) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

ER Diagram Relationships

One-to-One:   Customers 1:1 Addresses
One-to-Many:  Departments 1:N Employees
Many-to-Many: Students N:M Courses (via Enrollments)

Design Best Practices

← Back to All Lessons
Copyright © 2026. Made with ♥ by OneParasol Illustrations from