Vault Notes

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 componentSymbolMeaning
Entity setRectangleA collection of similar entities
Weak entity setDouble rectangleDepends on another entity for identity
AttributeOvalA property of an entity
Key attributeUnderlined ovalUniquely identifies the entity
Multivalued attributeDouble ovalCan hold multiple values
RelationshipDiamondAssociation 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 constructRelational mapping rule
Strong entity setIts own table; key attribute becomes primary key
Weak entity setIts own table; add the owner's primary key, combine into a composite primary key
1:1 relationshipAdd the other's key as a foreign key on either side
1:N relationshipAdd the "one" side's key as a foreign key on the "many" side
M:N relationshipA new junction table with both keys; the pair is the primary key
Multivalued attributeIts 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

KeyDefinition
Super keyAny set of attributes whose values uniquely identify each tuple; may contain extra attributes
Candidate keyA minimal super key β€” no proper subset is also a super key
Primary keyThe candidate key chosen as the table's main identifier; never NULL
Alternate keyCandidate keys not chosen as primary
Foreign keyAttribute(s) in one table referencing the primary key of another table
Counting super keys

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:

ActionEffect
ON DELETE CASCADEDeletes the dependent rows too
ON DELETE SET NULLSets the foreign key to NULL in dependents
ON DELETE SET DEFAULTSets the foreign key to its default value
ON DELETE RESTRICT / NO ACTIONBlocks 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.

OperatorSymbolWhat 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

TypeStands forCommands
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATE
DMLData Manipulation LanguageSELECT, INSERT, UPDATE, DELETE
DCLData Control LanguageGRANT, REVOKE
TCLTransaction Control LanguageCOMMIT, 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 constructExample
ComparisonWHERE age > 20 (=, <>, <= …)
AND / OR / NOTWHERE age > 18 AND city = 'Pune'
BETWEENWHERE marks BETWEEN 60 AND 90 (inclusive)
INWHERE city IN ('Pune','Mumbai')
LIKEWHERE name LIKE 'A%' (% = any run, _ = one char)
IS NULLWHERE 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.

JoinKeeps
INNER JOINOnly rows with a match on both sides
LEFT JOINAll rows from the left table; NULLs where the right has no match
RIGHT JOINAll rows from the right table; NULLs where the left has no match
FULL OUTER JOINAll 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

ConstraintRule
NOT NULLColumn must always have a value
UNIQUENo two rows may share the value
PRIMARY KEYNOT NULL + UNIQUE; one per table
FOREIGN KEYMust reference an existing primary key value
CHECKValue must satisfy a condition: CHECK (age >= 18)
DEFAULTValue 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:

AxiomRule
ReflexivityIf Y βŠ† X then X β†’ Y
AugmentationIf X β†’ Y then XZ β†’ YZ for any Z
TransitivityIf 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).

Attribute closure X⁺

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.

Worked check

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.

Why it matters

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

PropertyGuarantee
AtomicityAll or nothing β€” if any part fails, the whole transaction rolls back
ConsistencyA transaction moves the database from one valid state to another; integrity constraints hold
IsolationConcurrent transactions behave as if run one at a time; no interference
DurabilityOnce 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

CommandEffect
COMMITMakes all changes in the transaction permanent
ROLLBACKUndoes all changes back to the last commit (or savepoint)
SAVEPOINT s1Sets a named checkpoint inside the transaction
ROLLBACK TO s1Undoes only back to the savepoint, keeping earlier work
SET TRANSACTIONSets 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.

Conflict matrix

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 typeRule
RecoverableIf Ti reads data written by Tj, Ti commits only after Tj commits
CascadelessA transaction reads only committed data β€” no cascading rollbacks
StrictNeither reads nor writes uncommitted data β€” simplest recovery

Locks

LockModeAllows
Shared (S)Read lockMultiple transactions can hold S together; no one can write
Exclusive (X)Write lockOnly 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.

VariantRuleBuys you
Basic 2PLGrowing then shrinkingConflict serializability
Strict 2PLHold all exclusive locks until commit/abortStrict (recoverable, no cascading) schedules
Rigorous 2PLHold all locks until commit/abortEasiest 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

IndexBuilt onNotes
PrimaryThe ordering key field of an ordered fileOne per file; sparse or dense
ClusteringA non-key ordering fieldGroups rows with equal values physically together
SecondaryAny non-ordering fieldAlways 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-treeB+ tree
Data locationKeys and record pointers at every levelRecords only at leaf nodes; internal nodes hold keys for navigation
Leaf linkageNoneLeaves linked in a chain β€” fast range scans and sequential access
SearchMay finish at an internal nodeAlways descends to a leaf
UseGeneral purposeThe 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.