Databases
Subject 4 of 10 Β· ER diagrams, the relational model, keys, relational algebra, SQL, normalization, transactions, concurrency control, and indexing.
1. ER Model
Entity sets and attributes
An entity is a real-world object (a student, a course); an entity set is a collection of similar entities (all students). An attribute is a property of an entity (name, age). Attributes come in flavors: simple (atomic, like age), composite (splittable, like name β first/last), derived (computed from another, like age from DOB), multivalued (several values, like phone numbers), and key attributes that uniquely identify an entity.
| ER component | Symbol | Meaning |
|---|---|---|
| Entity set | Rectangle | A collection of similar entities |
| Weak entity set | Double rectangle | Depends on another entity for identity |
| Attribute | Oval | A property of an entity |
| Key attribute | Underlined oval | Uniquely identifies the entity |
| Multivalued attribute | Double oval | Can hold multiple values |
| Relationship | Diamond | Association between entity sets |
Cardinality constraints (1:1, 1:N, M:N)
Cardinality (mapping cardinality) says how many entities on each side can participate in a relationship. One-to-one: each A links to at most one B and vice versa (person β passport). One-to-many: one A links to many B (department β employees). Many-to-many: many on both sides (students β courses). Total participation means every entity must participate (double line); partial participation means it may or may not (single line).
Weak entity sets
A weak entity set has no key of its own and depends on an identifying (owner) entity β e.g. a Building owns Rooms; room number alone is ambiguous, so the weak entity uses a partial key (room number) plus the owner's key. Its relationship to the owner is an identifying relationship (double diamond).
ER to relational mapping
| ER construct | Relational mapping rule |
|---|---|
| Strong entity set | Its own table; key attribute becomes primary key |
| Weak entity set | Its own table; add the owner's primary key, combine into a composite primary key |
| 1:1 relationship | Add the other's key as a foreign key on either side |
| 1:N relationship | Add the "one" side's key as a foreign key on the "many" side |
| M:N relationship | A new junction table with both keys; the pair is the primary key |
| Multivalued attribute | Its own table with the entity's key + the value |
2. Relational Model
Relation schema and instance
A relation (table) has a schema β the structure: relation name plus attribute names and domains β and an instance β the actual set of tuples (rows) at a moment in time. The degree is the number of attributes (columns); the cardinality is the number of tuples (rows). Tuples are unordered and no two tuples are identical; attribute values are atomic.
Integrity constraints
Domain constraints restrict each attribute to its allowed values. Entity integrity: no primary key attribute may be NULL. Referential integrity: a foreign key must reference an existing tuple or be NULL. Key constraints: each key value must be unique across the table.
3. Keys
Super, candidate, primary, alternate
| Key | Definition |
|---|---|
| Super key | Any set of attributes whose values uniquely identify each tuple; may contain extra attributes |
| Candidate key | A minimal super key β no proper subset is also a super key |
| Primary key | The candidate key chosen as the table's main identifier; never NULL |
| Alternate key | Candidate keys not chosen as primary |
| Foreign key | Attribute(s) in one table referencing the primary key of another table |
If a relation has n attributes and one candidate key of size k, the number of super keys is 2nβk β every superset of the candidate key works, and the remaining nβk attributes can each be in or out. With multiple candidate keys, count supersets of each and subtract the overlaps.
Referential integrity actions
When the referenced row is deleted or updated, the foreign key declares what happens:
| Action | Effect |
|---|---|
| ON DELETE CASCADE | Deletes the dependent rows too |
| ON DELETE SET NULL | Sets the foreign key to NULL in dependents |
| ON DELETE SET DEFAULT | Sets the foreign key to its default value |
| ON DELETE RESTRICT / NO ACTION | Blocks the delete if dependents exist |
4. Relational Algebra
Relational algebra is the procedural query language behind SQL. Each operator takes relations in and returns a relation out.
| Operator | Symbol | What it does |
|---|---|---|
| Select | Ο (sigma) | Keeps rows satisfying a condition: Οage>20(Students) |
| Project | Ο (pi) | Keeps listed columns, drops duplicates: Οname(Students) |
| Union | βͺ | Rows in either relation; duplicates removed |
| Intersection | β© | Rows present in both relations |
| Set difference | β | Rows in the first but not the second |
| Cartesian product | Γ | Every row of R paired with every row of S; degree adds, cardinality multiplies |
| Natural join | β | Joins on the common attributes, keeping one copy |
| Rename | Ο (rho) | Renames a relation or attribute: ΟS(R) |
Union compatibility: βͺ, β©, and β need both relations to have the same number of attributes with matching domains. Degree/cardinality: Ο keeps the degree, shrinks cardinality; Ο shrinks the degree; R Γ S has degree(R)+degree(S) and |R|Β·|S| tuples.
5. SQL Fundamentals
Types of SQL
| Type | Stands for | Commands |
|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE |
| DML | Data Manipulation Language | SELECT, INSERT, UPDATE, DELETE |
| DCL | Data Control Language | GRANT, REVOKE |
| TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT |
DDL: CREATE, ALTER, DROP
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT); defines a table. ALTER TABLE students ADD email VARCHAR(100); modifies structure after creation. DROP TABLE students; deletes the table and its data permanently; TRUNCATE TABLE students; deletes all rows but keeps the structure.
DML: INSERT, UPDATE, DELETE
INSERT INTO students VALUES (1, 'Asha', 20); β add one row; with a column list you can insert into specific columns. UPDATE students SET age = 21 WHERE id = 1; β always use WHERE, or every row changes. DELETE FROM students WHERE id = 1; β no WHERE means the whole table is emptied.
SELECT and WHERE
SELECT name, age FROM students; picks columns; SELECT * FROM students; picks all. SELECT DISTINCT city FROM students; removes duplicate values. Aliases rename output columns: SELECT name AS student_name FROM students;.
| WHERE construct | Example |
|---|---|
| Comparison | WHERE age > 20 (=, <>, <= β¦) |
| AND / OR / NOT | WHERE age > 18 AND city = 'Pune' |
| BETWEEN | WHERE marks BETWEEN 60 AND 90 (inclusive) |
| IN | WHERE city IN ('Pune','Mumbai') |
| LIKE | WHERE name LIKE 'A%' (% = any run, _ = one char) |
| IS NULL | WHERE email IS NULL |
Aggregate functions and GROUP BY
COUNT(*) counts rows (including NULLs); COUNT(col) counts non-NULL values. SUM, AVG, MIN, MAX ignore NULLs. GROUP BY dept splits rows into groups so each aggregate runs per group; HAVING filters groups afterward (WHERE filters rows before grouping).
SELECT dept, AVG(salary) FROM emp GROUP BY dept HAVING AVG(salary) > 50000;
ORDER BY
ORDER BY marks DESC; sorts results; default is ascending (ASC). Multiple columns sort by the first, then the second within ties: ORDER BY dept, salary DESC;.
6. Joins & Constraints in SQL
JOINs
Joins combine rows from two tables on a matching condition, usually a foreign key equality.
| Join | Keeps |
|---|---|
| INNER JOIN | Only rows with a match on both sides |
| LEFT JOIN | All rows from the left table; NULLs where the right has no match |
| RIGHT JOIN | All rows from the right table; NULLs where the left has no match |
| FULL OUTER JOIN | All rows from both tables; NULLs on the side with no match |
SELECT s.name, c.title FROM students s INNER JOIN enroll e ON s.id = e.sid INNER JOIN courses c ON e.cid = c.id;
Constraints
| Constraint | Rule |
|---|---|
| NOT NULL | Column must always have a value |
| UNIQUE | No two rows may share the value |
| PRIMARY KEY | NOT NULL + UNIQUE; one per table |
| FOREIGN KEY | Must reference an existing primary key value |
| CHECK | Value must satisfy a condition: CHECK (age >= 18) |
| DEFAULT | Value used when none is supplied: DEFAULT 'Pune' |
7. Normalization
Why normalize: the three anomalies
Bad design repeats data, and repeated data rots three ways. Insertion anomaly: you cannot add a fact without adding an unrelated one (can't add a department with no employees yet). Update anomaly: changing one fact means editing many rows, and missing one corrupts the data. Deletion anomaly: deleting a row destroys an unrelated fact (last employee of a department deletes the department info). Normalization restructures tables to kill redundancy while preserving information.
Goals of normalization
Eliminate data redundancy, ensure each piece of information is stored once, protect data integrity during inserts/updates/deletes, and keep the design flexible. The price: more tables, so queries need more joins β normalization trades query simplicity for update safety.
Functional dependencies
A functional dependency X β Y means: if two tuples agree on X, they must agree on Y β X determines Y. Trivial FD: Y β X (e.g. {A,B} β A) β always true, tells you nothing. Non-trivial FD: Y β X β the ones that matter. A full FD means no proper subset of X determines Y; otherwise it is partial.
Armstrong's axioms
The three axioms generate every FD implied by a set:
| Axiom | Rule |
|---|---|
| Reflexivity | If Y β X then X β Y |
| Augmentation | If X β Y then XZ β YZ for any Z |
| Transitivity | If X β Y and Y β Z then X β Z |
Derived rules you will actually use: union (X β Y and X β Z gives X β YZ), decomposition (X β YZ gives X β Y and X β Z), pseudotransitivity (X β Y and WY β Z gives WX β Z).
To find everything X determines: start with XβΊ = X, then repeatedly add the right side of every FD whose left side is already inside XβΊ, until nothing new appears. If XβΊ covers all attributes, X is a super key; if no subset of X does that, X is a candidate key.
Canonical cover (minimal cover)
The minimal equivalent set of FDs. Steps: (1) split right sides so each FD has a single attribute; (2) remove extraneous attributes from left sides β drop A from XY β Z if Z is still in (X)βΊ without A; (3) remove redundant FDs β drop X β Y if Y is in XβΊ computed from the other FDs. The result has no redundant FD and no FD with an extraneous attribute.
First Normal Form (1NF)
A table is in 1NF when every attribute holds an atomic, single value β no lists, no repeating groups, no composite values stuffed into one cell. 1NF is the entry ticket; every normalized design starts here.
8. Lossless Join Decomposition
A decomposition splits R into R1 and R2. It is lossless if joining the pieces gives back exactly the original table β no spurious tuples, no lost ones. The test: the decomposition is lossless iff the common attributes R1 β© R2 form a super key of R1 or of R2 β i.e. (R1 β© R2) β R1 or (R1 β© R2) β R2 holds in the FD set.
R(A,B,C) with A β B, decomposed into R1(A,B) and R2(A,C). Common attributes = {A}; A β B means A is a key of R1, so the join is lossless. If the common part determined nothing, the natural join would invent phantom rows.
Losslessness is about information preservation: the decomposition must not add or lose data. It says nothing about whether the FDs still hold β that is the next chapter's job.
9. Dependency Preserving Decomposition
A decomposition is dependency preserving if every FD of the original can be enforced by checking the decomposed tables individually β equivalently, the union of the FDs projected onto each piece implies all original FDs. To test: for each FD X β Y in the original set, check whether Y β XβΊ computed using only FDs whose attributes lie within a single decomposed relation; if every FD passes, dependencies are preserved.
A decomposition can be lossless but still lose an FD that spans the split β then enforcing that FD needs a join every time, which defeats the purpose. Good decompositions are both lossless and dependency preserving.
Example: R(A,B,C) with A β B, B β C decomposed into R1(A,B) and R2(B,C) is lossless (B is common and a key of R2) and dependency preserving (A β B lives in R1, B β C lives in R2, and A β C follows by transitivity).
10. Transactions
ACID properties
| Property | Guarantee |
|---|---|
| Atomicity | All or nothing β if any part fails, the whole transaction rolls back |
| Consistency | A transaction moves the database from one valid state to another; integrity constraints hold |
| Isolation | Concurrent transactions behave as if run one at a time; no interference |
| Durability | Once committed, the changes survive crashes β written to stable storage |
Classic example: transferring βΉ500 from account A to B. Atomicity means you never see the debit without the credit; durability means a crash right after commit loses nothing.
Transaction states
Active β executing; partially committed β last statement done, commit not yet recorded; committed β changes permanent; failed β something broke; aborted β rolled back to the pre-transaction state.
TCL commands
| Command | Effect |
|---|---|
| COMMIT | Makes all changes in the transaction permanent |
| ROLLBACK | Undoes all changes back to the last commit (or savepoint) |
| SAVEPOINT s1 | Sets a named checkpoint inside the transaction |
| ROLLBACK TO s1 | Undoes only back to the savepoint, keeping earlier work |
| SET TRANSACTION | Sets properties like isolation level for the transaction |
11. Concurrency Control
Schedules
A schedule is the interleaved order of operations from concurrent transactions. A serial schedule runs one transaction fully before the next β always correct, but slow. A concurrent schedule interleaves operations for throughput and better response time, but risks interference. A schedule is serializable if its effect equals some serial schedule β that is the correctness bar.
Conflict serializability
Two operations conflict if they are from different transactions, touch the same data item, and at least one is a write. A schedule is conflict serializable if conflicting operations can be reordered into a serial order without changing the outcome. The test: build a precedence graph β a node per transaction, an edge Ti β Tj for every conflict where Ti's operation came first. If the graph has no cycle, the schedule is conflict serializable.
readβread: no conflict. readβwrite, writeβread, writeβwrite: conflict. Only conflicting pairs constrain the order.
View serializability
A weaker test with three conditions against a serial schedule: (1) initial reads β each transaction reads the same initial values; (2) updated reads β every read sees the value written by the same transaction; (3) final writes β the last writer of each item is the same. Every conflict-serializable schedule is view serializable, but not vice versa β blind writes (writes with no preceding read) are the classic counterexample.
Recoverable schedules
| Schedule type | Rule |
|---|---|
| Recoverable | If Ti reads data written by Tj, Ti commits only after Tj commits |
| Cascadeless | A transaction reads only committed data β no cascading rollbacks |
| Strict | Neither reads nor writes uncommitted data β simplest recovery |
Locks
| Lock | Mode | Allows |
|---|---|---|
| Shared (S) | Read lock | Multiple transactions can hold S together; no one can write |
| Exclusive (X) | Write lock | Only one transaction; blocks both readers and writers |
Lock compatibility: SβS compatible; SβX, XβS, XβX all conflict. Lock granularity β how big a chunk you lock β trades concurrency against overhead: database β table β page β row β field. Finer granularity means more concurrency but more lock bookkeeping; coarser means less overhead but transactions block each other more.
Two-Phase Locking (2PL)
Growing phase: acquire locks, never release. Shrinking phase: release locks, never acquire. Any schedule obeying 2PL is conflict serializable. The moment between the last acquire and first release is the lock point. Upgrades (S β X) are allowed in the growing phase. 2PL guarantees serializability but not freedom from deadlock or cascading rollback.
| Variant | Rule | Buys you |
|---|---|---|
| Basic 2PL | Growing then shrinking | Conflict serializability |
| Strict 2PL | Hold all exclusive locks until commit/abort | Strict (recoverable, no cascading) schedules |
| Rigorous 2PL | Hold all locks until commit/abort | Easiest recovery; most blocking |
Timestamp ordering protocol
Each transaction gets a unique timestamp at start; older = smaller. For data item Q keep read_TS(Q) and write_TS(Q) β the largest timestamps that read/wrote it. A read by Ti succeeds only if TS(Ti) β₯ write_TS(Q); a write by Ti succeeds only if TS(Ti) β₯ read_TS(Q) and TS(Ti) β₯ write_TS(Q). Violations abort and restart the transaction with a new timestamp. No locks, no deadlocks β but aborts and restarts cost throughput.
12. Indexing & File Organization
Index types
| Index | Built on | Notes |
|---|---|---|
| Primary | The ordering key field of an ordered file | One per file; sparse or dense |
| Clustering | A non-key ordering field | Groups rows with equal values physically together |
| Secondary | Any non-ordering field | Always dense; needs an extra level of indirection via block pointers |
Dense vs sparse index
Dense: one index entry per data record β larger, but finds any record directly. Sparse: one entry per data block β smaller, but you may scan within the block after the lookup. Primary and clustering indexes can be sparse; secondary indexes must be dense.
Index math
Let BFR be the blocking factor (records per block) and the index blocking factor = entries per index block. Number of first-level index blocks = ceil(number of data blocks / index blocking factor). A multilevel index stacks sparse indexes: each level's entries point to blocks of the level below, shrinking by the blocking factor each time until one root block remains. Search cost = n + 1 block accesses, where n is the number of index levels (plus 1 for the data block). Example: 1000 data blocks with blocking factor 100 β 10 level-1 blocks β 1 root: 3 levels, 11 index blocks, 4 accesses per search.
B-tree vs B+ tree
| B-tree | B+ tree | |
|---|---|---|
| Data location | Keys and record pointers at every level | Records only at leaf nodes; internal nodes hold keys for navigation |
| Leaf linkage | None | Leaves linked in a chain β fast range scans and sequential access |
| Search | May finish at an internal node | Always descends to a leaf |
| Use | General purpose | The standard index structure in real DBMSs |
Node capacity: for an internal node of order p, pΒ·(block pointer size) + (pβ1)Β·(key size) β€ block size; for a leaf node of order pleaf, (pleafβ1)Β·(key size + record pointer size) + block pointer β€ block size β the extra pointer links to the next leaf.