Introduction to Databases and SQL: Concepts, RDBMS & SQL Commands
Study Deep: The ACID Properties
In any database transaction, four properties (ACID) must be maintained to ensure data reliability.
- A - Atomicity: "All or nothing." Either the entire transaction succeeds, or it is completely rolled back.
- C - Consistency: A transaction must transform the database from one valid state to another valid state.
- I - Isolation: Concurrent transactions should not interfere with each other.
- D - Durability: Once a transaction is committed, it remains committed even in the case of a system failure.
1. Core Database Terminology
Understanding databases begins with a clear vocabulary:
| Term | Definition |
|---|---|
| Data | Raw, unprocessed facts and figures (e.g., "Rohit", 25, "Delhi") |
| Information | Processed, meaningful data (e.g., "Rohit is a 25-year-old from Delhi") |
| Database (DB) | An organized, structured collection of data stored and accessed electronically |
| DBMS | Database Management System — software used to create, manage, and interact with databases (e.g., MySQL, Oracle) |
| RDBMS | Relational DBMS — stores data in structured tables with relationships between them |
| Table (Relation) | A collection of rows and columns, similar to a spreadsheet |
| Row (Tuple/Record) | A single entry in a table representing one complete set of related data |
| Column (Attribute/Field) | A specific characteristic or property; each column has a defined data type |
| Primary Key (PK) | A column (or combination) that uniquely identifies each row; cannot be NULL or duplicate |
| Foreign Key (FK) | A column that references the Primary Key of another table; establishes relationships |
| Schema | The overall structure/blueprint of a database — its tables, columns, and relationships |
2. What is MySQL?
MySQL is the most popular open-source Relational Database Management System (RDBMS). It organizes data into tables and uses SQL (Structured Query Language) to interact with the data.
Why MySQL with PHP?
- MySQL is free and open-source.
- Excellent PHP support via MySQLi and PDO extensions.
- Used by millions of websites (WordPress, Facebook originally).
- Fast, reliable, and easy to set up with XAMPP/WAMP.
3. RDBMS vs NoSQL Comparison
| Feature | RDBMS (e.g., MySQL) | NoSQL (e.g., MongoDB) |
|---|---|---|
| Data Model | Table-based (rows & columns) | Document, Key-Value, Graph, Column |
| Schema | Pre-defined, rigid schema | Dynamic, flexible schema |
| Language | SQL | Varies (JSON queries, etc.) |
| Relationships | Strongly supported (FK, JOINs) | Weak / not native |
| ACID Compliance | Yes (Atomicity, Consistency, etc.) | Often partially |
| Scaling | Vertical (add more power to one server) | Horizontal (add more servers) |
| Best For | Structured data, complex queries, transactions | Unstructured/semi-structured data, big data |
4. SQL Command Categories
SQL (Structured Query Language) is the standard language for interacting with relational databases. All SQL commands fall into one of four categories:
a) DDL — Data Definition Language Commands that define or modify the structure of the database (tables, databases, indexes).
| Command | Purpose | Example |
|---|---|---|
CREATE | Create a new database or table | CREATE DATABASE school; |
ALTER | Modify an existing table structure | ALTER TABLE students ADD age INT; |
DROP | Delete a database or table | DROP TABLE students; |
TRUNCATE | Delete all records (not table structure) | TRUNCATE TABLE students; |
b) DML — Data Manipulation Language Commands that manipulate the data stored in tables.
| Command | Purpose | Example |
|---|---|---|
SELECT | Retrieve/read data | SELECT * FROM students; |
INSERT | Add new records | INSERT INTO students VALUES (1,'Rohit','BCA'); |
UPDATE | Modify existing records | UPDATE students SET name='Rohit K' WHERE id=1; |
DELETE | Remove records | DELETE FROM students WHERE id=1; |
c) DCL — Data Control Language Commands that control user access and permissions.
| Command | Purpose | Example |
|---|---|---|
GRANT | Give a user permission | GRANT SELECT ON db.* TO 'user'@'localhost'; |
REVOKE | Remove a user's permission | REVOKE SELECT ON db.* FROM 'user'@'localhost'; |
d) TCL — Transaction Control Language Commands that manage database transactions (groups of operations that must all succeed or all fail).
| Command | Purpose | Example |
|---|---|---|
COMMIT | Permanently save all changes in a transaction | COMMIT; |
ROLLBACK | Undo changes back to the last COMMIT | ROLLBACK; |
SAVEPOINT | Create a checkpoint within a transaction | SAVEPOINT sp1; |
5. Important SQL Concepts
The WHERE Clause: Filters records to operate on specific rows only. Always use with UPDATE and DELETE to prevent modifying or deleting ALL records accidentally.
-- Update ONLY student with id=1
UPDATE students SET marks=90 WHERE id=1;
-- Delete ONLY students with course='BCA'
DELETE FROM students WHERE course='BCA';
-- Without WHERE: ALL records would be affected!
ORDER BY, LIMIT:
SELECT * FROM students ORDER BY name ASC; -- Sort A-Z
SELECT * FROM students ORDER BY marks DESC; -- Sort highest first
SELECT * FROM students LIMIT 10; -- Only first 10 records
6. Aggregate Functions
Definition: Aggregate functions perform a calculation on a set of rows and return a single summarized value. They are used with the SELECT statement, typically alongside GROUP BY.
| Function | Description | Example |
|---|---|---|
COUNT(*) | Number of rows (including NULLs) | SELECT COUNT(*) FROM students; |
COUNT(col) | Number of non-NULL values in column | SELECT COUNT(email) FROM students; |
SUM(col) | Sum of all values in a numeric column | SELECT SUM(marks) FROM students; |
AVG(col) | Average value of a numeric column | SELECT AVG(marks) FROM students; |
MAX(col) | Maximum value in a column | SELECT MAX(marks) FROM students; |
MIN(col) | Minimum value in a column | SELECT MIN(marks) FROM students; |
-- Get total number of students
SELECT COUNT(*) AS total_students FROM students;
-- Get average, max, and min marks
SELECT AVG(marks) AS avg_marks,
MAX(marks) AS highest,
MIN(marks) AS lowest
FROM students;
-- Get total marks scored by all students in BCA
SELECT SUM(marks) AS bca_total FROM students WHERE course = 'BCA';
7. GROUP BY and HAVING
GROUP BY: Groups rows that share the same value in a specified column, so aggregate functions can be applied per group (e.g., per course, per city).
HAVING: Filters groups after aggregation (similar to WHERE, but WHERE filters individual rows before grouping).
Rule: UseWHEREto filter rows. UseHAVINGto filter groups.
-- Count students in each course
SELECT course, COUNT(*) AS total
FROM students
GROUP BY course;
-- Average marks per course, sorted highest first
SELECT course,
AVG(marks) AS avg_marks,
COUNT(*) AS student_count
FROM students
GROUP BY course
ORDER BY avg_marks DESC;
-- Only show courses where average marks > 70 (HAVING filters groups)
SELECT course, AVG(marks) AS avg_marks
FROM students
GROUP BY course
HAVING AVG(marks) > 70;
-- WHERE + GROUP BY + HAVING together
SELECT course, COUNT(*) AS count
FROM students
WHERE is_active = 1 -- Filter rows first
GROUP BY course -- Then group
HAVING COUNT(*) >= 5 -- Then filter groups
ORDER BY count DESC;
Execution Order of a SELECT statement:
1. FROM → Identify source table
2. WHERE → Filter individual rows
3. GROUP BY → Group filtered rows
4. HAVING → Filter groups
5. SELECT → Choose columns / apply aggregates
6. ORDER BY → Sort results
7. LIMIT → Restrict number of output rows
8. SQL JOINs — Combining Multiple Tables
Definition: A JOIN combines rows from two or more tables based on a related column (usually a Primary Key – Foreign Key relationship). JOINs are fundamental to relational databases.
Setup for examples:
-- Table: departments
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
INSERT INTO departments VALUES (1,'BCA'), (2,'BBA'), (3,'MBA');
-- Table: students (has a foreign key to departments)
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
dept_id INT -- Foreign Key
);
INSERT INTO students VALUES
(1,'Rohit',1), (2,'Priya',1), (3,'Amit',2), (4,'Neha',NULL);
a) INNER JOIN — Returns rows where there is a match in BOTH tables.
SELECT s.name, d.dept_name
FROM students s
INNER JOIN departments d ON s.dept_id = d.dept_id;
-- Result: Rohit|BCA, Priya|BCA, Amit|BBA
-- Neha is excluded (NULL dept_id — no match)
b) LEFT JOIN (LEFT OUTER JOIN) — Returns ALL rows from the left table + matched rows from the right. Unmatched right-side values are NULL.
SELECT s.name, d.dept_name
FROM students s
LEFT JOIN departments d ON s.dept_id = d.dept_id;
-- Result: Rohit|BCA, Priya|BCA, Amit|BBA, Neha|NULL
-- Neha IS included (left table = students, all rows returned)
c) RIGHT JOIN (RIGHT OUTER JOIN) — Returns ALL rows from the right table + matched rows from the left. Unmatched left-side values are NULL.
SELECT s.name, d.dept_name
FROM students s
RIGHT JOIN departments d ON s.dept_id = d.dept_id;
-- Result: Rohit|BCA, Priya|BCA, Amit|BBA, NULL|MBA
-- MBA department IS included even though no student belongs to it
Visual Summary:
| JOIN Type | Returns |
|---|---|
INNER JOIN | Only rows with matching values in BOTH tables |
LEFT JOIN | All rows from left table + matching rows from right |
RIGHT JOIN | All rows from right table + matching rows from left |
-- Real example: Show each student with their course name and average marks
SELECT d.dept_name,
COUNT(s.id) AS student_count,
AVG(s.marks) AS avg_marks
FROM departments d
LEFT JOIN students s ON d.dept_id = s.dept_id
GROUP BY d.dept_name
ORDER BY avg_marks DESC;