


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















1. Introduction to DBMS
Component Function Query Processor Parses, optimizes, and executes SQL queries Storage Manager Manages disk space, buffers, and file organization Transaction Manager Ensures ACID properties and concurrency control Catalog / Data Dictionary Stores metadata (schema, constraints, users) Authorization Manager Controls user access and permissions
NoSQL (Key-Value) Simple key → value pairs Redis, DynamoDB NoSQL (Column-family) Tables with flexible columns Cassandra, HBase NoSQL (Graph) Nodes and edges for relationships Neo4j Object-Oriented Objects with attributes and methods db4o Hierarchical Tree structure (parent-child)
Network Graph structure with pointers
2.2 Relational vs. NoSQL Feature Relational (SQL) NoSQL Schema Fixed, predefined Flexible, dynamic Consistency Strong (ACID) Eventual (BASE) Scalability Vertical (bigger server) Horizontal (more servers) Query Language SQL Varies (often JSON-based) Best For Structured data, complex queries Unstructured data, high throughput
2.3 DBMS vs. File System Aspect File System DBMS Data redundancy High Low (normalized) Concurrent access Poor Excellent Query capability None (must write code) SQL (declarative) Recovery Manual Automatic (WAL, logs) Security File-level Row/column-level Integrity Application-enforced DBMS-enforced
3. The Relational Model
3.1 Key Terminology Term Definition Example Relation A table Students Tuple A row (1, "Alice", 20) Attribute A column name, age
sql CREATE TABLE Students ( student_id INT PRIMARY KEY, -- primary key email VARCHAR(100) UNIQUE, -- alternate/candidate key name VARCHAR(50) NOT NULL, department_id INT, FOREIGN KEY (department_id) REFERENCES Departments(department_id) ); 3.3 Integrity Constraints Constraint Description Entity Integrity Primary key cannot be NULL Referential Integrity Foreign key must reference an existing row (or be NULL) Domain Integrity Values must belong to the attribute's domain Key Integrity Primary key values must be unique User-defined Custom business rules (e.g., salary > 0)
4. SQL Fundamentals
Sub-language Purpose Commands DDL (Data Definition) Define/modify schema CREATE, ALTER, DROP, TRUNCATE DML (Data Manipulation) Modify data INSERT, UPDATE, DELETE DQL (Data Query) Retrieve data SELECT DCL (Data Control) Manage permissions GRANT, REVOKE TCL (Transaction Control) Manage transactions COMMIT, ROLLBACK, SAVEPOINT 4.1 DDL — Creating Tables sql CREATE TABLE Employees ( emp_id INT PRIMARY KEY AUTO_INCREMENT, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, salary DECIMAL(10,2) CHECK (salary > 0), hire_date DATE DEFAULT CURRENT_DATE, dept_id INT, FOREIGN KEY (dept_id) REFERENCES Departments(dept_id) ); ALTER TABLE Employees ADD COLUMN email VARCHAR(100); ALTER TABLE Employees DROP COLUMN email; DROP TABLE Employees;
sql SELECT dept_id, COUNT(*) AS num_employees, AVG(salary) AS avg_salary FROM Employees GROUP BY dept_id HAVING AVG(salary) > 60000;
Join Type Description Returns INNER JOIN (^) Matches in both tables Only matching rows LEFT JOIN All rows from left + matches NULLs for non-matches on right RIGHT JOIN (^) All rows from right + matches NULLs for non-matches on left FULL OUTER JOIN (^) All rows from both NULLs where no match CROSS JOIN (^) Cartesian product All combinations SELF JOIN Join a table to itself E.g., employee → manager sql -- INNER JOIN example SELECT e.first_name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id = d.dept_id; -- LEFT JOIN example (include employees with no department) SELECT e.first_name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.dept_id;
sql -- Employees earning more than the average SELECT first_name, salary FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees); -- Employees in departments located in 'New York' SELECT first_name FROM Employees WHERE dept_id IN ( SELECT dept_id FROM Departments WHERE location = 'New York' );
Operation Description UNION Combine results, remove duplicates UNION ALL Combine results, keep duplicates INTERSECT (^) Rows in both results EXCEPT / MINUS Rows in first but not second
sql -- Rank employees by salary within each department SELECT first_name, dept_id, salary, RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rank FROM Employees;
5.3 Normalization Example
StudentID StudentName Courses Advisor 1 Alice DB, OS Dr. Smith 2 Bob DB Dr. Jones
StudentID StudentName Course Advisor 1 Alice DB Dr. Smith 1 Alice OS Dr. Smith 2 Bob DB Dr. Jones
5.4 Denormalization
6. ACID Properties & Transactions
6.1 ACID Properties Property Definition Example
6.3 Transaction Control (SQL) sql BEGIN TRANSACTION; UPDATE Accounts SET balance = balance - 500 WHERE account_id = 1; UPDATE Accounts SET balance = balance + 500 WHERE account_id = 2; -- If both succeed: COMMIT; -- If something went wrong: ROLLBACK; -- Partial rollback: SAVEPOINT before_update; -- ... more operations ... ROLLBACK TO before_update; 6.4 BASE (NoSQL Counterpart) Property Meaning Basically Available System is available even during partial failures Soft State State may change over time without input Eventual Consistency Data will eventually be consistent across nodes
7. Concurrency Control
Anomaly Description Example Dirty Read Reading uncommitted data T1 updates a row; T2 reads it; T1 rolls back → T2 has invalid data Non-repeatable Read Same query returns different results T1 reads a row; T2 updates it; T1 reads again → different value Phantom Read New rows appear between reads T1 reads all rows matching a condition; T2 inserts a new matching row; T reads again → extra row Lost Update Two transactions update the same data; one overwrites the other T1 and T2 both read value 100; both add 10; final = 110 instead of 120 7.1 Locking Lock Type Description Shared (S) Multiple transactions can read (but not write) Exclusive (X) Only one transaction can read or write
7.3 Deadlock Handling Strategy Description Prevention Timestamp ordering (wait-die, wound-wait) Detection Wait-for graph; find cycles Recovery Abort one transaction in the cycle (victim selection)
8. Indexing & Query Optimization 8.1 Indexes
Index Type Description Use Case B+ Tree Balanced tree; all data in leaves; linked leaves for range queries Default in most DBMS Hash Hash table; O(1) for equality lookups Exact match queries Bitmap Bit per distinct value Low-cardinality columns (e.g., gender)
Full-text Inverted index for text search Searching documents Clustered Data rows physically ordered by index key Primary key (one per table) Non-clustered Separate structure pointing to data rows Secondary indexes
sql CREATE INDEX idx_employee_dept ON Employees(dept_id); CREATE UNIQUE INDEX idx_employee_email ON Employees(email); DROP INDEX idx_employee_dept;
8.2 Query Execution Pipeline text SQL Query → Parser → Logical Plan → Optimizer → Physical Plan → Execution Engine → Results