Reference Guide

Table of Contents

  1. Database Fundamentals

  2. MySQL Installation (Ubuntu 24)

  3. Data Types

  4. DDL - Data Definition Language

  5. DML - Data Manipulation Language

  6. DQL - Data Query Language

  7. JOINs

  8. Aggregate Functions

  9. Subqueries

  10. Indexes

  11. Database Design

  12. Security

  13. Backup & Recovery

  14. Best Practices


1. Database Fundamentals

What is a Database?

Organized collection of structured data stored electronically.

ACID Properties

  • Atomicity: All or nothing (transaction succeeds or fails completely)

  • Consistency: Database moves from one valid state to another

  • Isolation: Concurrent transactions don't interfere

  • Durability: Committed changes survive system failures

Relational Database Concepts

Table: Collection of related data (rows and columns) Row/Record: Single entry in a table Column/Field: Property of the data Primary Key: Unique identifier for each row Foreign Key: References primary key in another table

Relationships

One-to-Many: One user has many orders

Many-to-Many: Students ↔ Courses (requires junction table)

One-to-One: User ↔ UserProfile


2. MySQL Installation (Ubuntu 24)

Installation Steps

Security Script Recommendations

  • Validate password: Yes (MEDIUM level)

  • Remove anonymous users: Yes

  • Disable remote root: Yes

  • Remove test database: Yes

  • Reload privileges: Yes

Create User

Service Management

Login


3. Data Types

Numeric Types

String Types

Date/Time Types

Boolean

Common Patterns


4. DDL - Data Definition Language

CREATE DATABASE

CREATE TABLE

ALTER TABLE

DROP TABLE

TRUNCATE TABLE

View Table Structure


5. DML - Data Manipulation Language

INSERT

UPDATE

DELETE

Transactions


6. DQL - Data Query Language

SELECT Basics

WHERE Clause

ORDER BY

LIMIT

String Functions

Date Functions

Numeric Functions

Conditional Functions


7. JOINs

INNER JOIN

Returns only matching rows from both tables.

LEFT JOIN

Returns all rows from left table, matched rows from right table (NULL if no match).

RIGHT JOIN

Returns all rows from right table, matched rows from left table.

CROSS JOIN

Returns all possible combinations (Cartesian product).

Self JOIN

Join table to itself.


8. Aggregate Functions

Basic Aggregates

GROUP BY

HAVING

Filters grouped results (WHERE filters before grouping).


9. Subqueries

Scalar Subquery (Single Value)

Column Subquery (List of Values)

Table Subquery (Multiple Rows/Columns)

Correlated Subquery

EXISTS

Common Table Expressions (WITH)


10. Indexes

Creating Indexes

Viewing Indexes

Dropping Indexes

When to Index

Create Index:

  • Columns in WHERE clause

  • Foreign key columns

  • JOIN columns

  • ORDER BY columns

  • High selectivity (many unique values)

Don't Index:

  • Small tables (< 1000 rows)

  • Low selectivity (few unique values like gender)

  • Frequently updated columns

  • Rarely queried columns

Query Optimization


11. Database Design

Design Process

  1. Identify entities (users, products, orders)

  2. Identify attributes (name, price, date)

  3. Identify relationships (user has orders)

  4. Define primary keys

  5. Define foreign keys

  6. Normalize

Normalization

First Normal Form (1NF):

  • Atomic values (no arrays/lists)

  • Each row unique

  • No repeating groups

Second Normal Form (2NF):

  • Must be in 1NF

  • No partial dependencies (all non-key columns depend on entire primary key)

Third Normal Form (3NF):

  • Must be in 2NF

  • No transitive dependencies (non-key columns don't depend on other non-key columns)

Design Patterns

Audit Trail:

Soft Delete:


12. Security

User Management

Preventing SQL Injection

❌ Vulnerable:

✅ Secure (Prepared Statements):

Best Practices

  • ✅ Use prepared statements (prevent SQL injection)

  • ✅ Principle of least privilege (minimal permissions)

  • ✅ Strong passwords (12+ chars, mixed case, numbers, symbols)

  • ✅ Disable remote root login

  • ✅ Use SSL/TLS for connections

  • ✅ Regular backups

  • ✅ Keep MySQL updated

  • ✅ Enable audit logging

  • ✅ Encrypt sensitive data


13. Backup & Recovery

mysqldump (Logical Backup)

Automated Backup Script

Schedule with Cron

Point-in-Time Recovery


14. Best Practices

Database Design

  • Always use primary keys

  • Use foreign keys for relationships

  • Normalize to 3NF

  • Choose appropriate data types

  • Use constraints (NOT NULL, UNIQUE, CHECK)

  • Create ERD before implementation

SQL Queries

  • SELECT only needed columns (not *)

  • Use WHERE to filter early

  • Use LIMIT for pagination

  • Use prepared statements (prevent SQL injection)

  • Use transactions for related operations

  • Index foreign keys and WHERE columns

Performance

  • Index frequently queried columns

  • Don't over-index (slows writes)

  • Use EXPLAIN to analyze queries

  • Avoid functions in WHERE on indexed columns

  • Use covering indexes when possible

  • Regular OPTIMIZE TABLE

Security

  • Never use root in applications

  • Principle of least privilege

  • Strong passwords

  • Prepared statements (prevent SQL injection)

  • Regular backups

  • Keep MySQL updated

Common Mistakes

  • No primary key

  • Wrong data types (VARCHAR for numbers)

  • Not using foreign keys

  • SELECT * (returns unnecessary data)

  • Forgetting WHERE in UPDATE/DELETE

  • No indexes on foreign keys

  • SQL injection vulnerabilities


Quick Reference

MySQL Command Line

Essential SQL Commands

Data Types Quick Reference

Common Patterns


Common Query Patterns

Find Duplicates

Top N Per Group

Pagination

Running Total

Conditional Counting

Last updated