๐๏ธ Database & SQL
DBMS, SQL queries, joins, normalization, ACID.
๐๏ธ Organized Data
A Database is an organized collection of structured data. A DBMS (Database Management System) is software that manages databases: MySQL, PostgreSQL, Oracle, MS SQL Server, SQLite.
RDBMS (Relational DBMS) โ stores data in tables (relations) with rows and columns. Tables are related through keys. Examples: MySQL, PostgreSQL, Oracle, MS Access.
Key terms:
โข Table โ collection of rows (records) and columns (fields/attributes)
โข Primary Key โ unique identifier for each row (cannot be null/duplicate)
โข Foreign Key โ links to primary key of another table (establishes relationship)
โข SQL (Structured Query Language) โ language to interact with databases
ACID properties (transactions must follow):
Atomicity, Consistency, Isolation, Durability
DDL (Data Definition Language): CREATE TABLE, ALTER TABLE, DROP TABLE
DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
DCL (Data Control Language): GRANT, REVOKE
TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
SELECT * FROM Students WHERE Marks > 75 ORDER BY Name ASC;
INSERT INTO Students VALUES (1, 'Ravi', 85);
UPDATE Students SET Marks=90 WHERE ID=1;
DELETE FROM Students WHERE ID=1;
1NF โ no repeating groups, each cell has atomic value
2NF โ 1NF + no partial dependencies (all columns depend on full primary key)
3NF โ 2NF + no transitive dependencies
BCNF โ stronger version of 3NF
Goal: reduce redundancy and avoid update anomalies.
NoSQL databases โ MongoDB (document), Redis (key-value), Cassandra (column), Neo4j (graph). No fixed schema. Scales horizontally. Used for big data, real-time apps.
Database Tables and Relationships
AnimationRelational databases power everything โ bank transactions, e-commerce, social media, government records.
SQL Query Explorer
InteractiveINNER JOIN โ returns rows that have matching values in BOTH tables
SELECT * FROM A INNER JOIN B ON A.id = B.id;
Only rows where match exists in both tables.
LEFT JOIN (LEFT OUTER JOIN) โ returns ALL rows from left table, matched rows from right. NULL for non-matching right rows.
SELECT * FROM A LEFT JOIN B ON A.id = B.id;
RIGHT JOIN (RIGHT OUTER JOIN) โ returns ALL rows from right table, matched rows from left.
FULL OUTER JOIN โ returns ALL rows from both tables. NULL where no match.
CROSS JOIN โ returns cartesian product (every row of A ร every row of B). No ON clause needed.
SELF JOIN โ table joined with itself. Used for hierarchical data (employee-manager in same table).
Most common in practice: INNER JOIN and LEFT JOIN.
Example: Customers LEFT JOIN Orders would show all customers, even those with no orders.
โข DML command โ removes specific rows based on WHERE condition
โข DELETE FROM Students WHERE ID = 5;
โข Can be rolled back (undone) with ROLLBACK
โข Triggers fire (if any defined)
โข Table structure and other data remain
โข Slower for large data
TRUNCATE:
โข DDL command โ removes ALL rows from a table
โข TRUNCATE TABLE Students;
โข Cannot be rolled back (in most databases)
โข Faster than DELETE (doesn't log each row)
โข Table structure remains, only data is gone
โข Auto-increment resets to 1
DROP:
โข DDL command โ removes the ENTIRE TABLE (structure + data)
โข DROP TABLE Students;
โข Cannot be rolled back
โข Table no longer exists after DROP
Summary: DELETE = remove some rows, TRUNCATE = empty the table, DROP = destroy the table.
Mnemonic: Delete rows, Truncate table data, Drop entire table.