DBMS
Interview essentials · SQL · normalization · transactions
0 / 12 revised
DBMS vs RDBMS 🎯
DBMS
▾
Quick Recall
RDBMS = DBMS + relational model + ACID + SQL
| Feature | DBMS | RDBMS |
|---|---|---|
| Model | File-based, hierarchical, network | Relational (tables) |
| ACID | Not guaranteed | Guaranteed |
| Normalization | Not enforced | Enforced |
| Relationships | No formal support | Foreign keys |
| Examples | XML stores, flat files | MySQL, PostgreSQL, Oracle |
Keys 🎯
DBMS
▾
Quick Recall
Super ⊇ Candidate ⊇ Primary; Foreign links tables
| Key | Definition |
|---|---|
| Super Key | Any set of attributes that uniquely identifies a row |
| Candidate Key | Minimal super key (no redundant attributes) |
| Primary Key | Chosen candidate key; NOT NULL + UNIQUE |
| Foreign Key | References PK of another table; enforces referential integrity |
| Composite Key | PK made of 2+ columns together |
| Alternate Key | Candidate keys not chosen as PK |
ER Model & Mapping
DBMS
▾
Quick Recall
Entity = table, Attribute = column, Relationship = FK
ER Components: Entity (rectangle), Attribute (oval), Relationship (diamond).
Cardinality: 1:1, 1:N, M:N.
Mapping rules:
Cardinality: 1:1, 1:N, M:N.
Mapping rules:
- Strong entity → table with PK
- Weak entity → table with partial key + owner FK (composite PK)
- 1:1 → FK on either side (prefer total participation side)
- 1:N → FK on N-side
- M:N → new junction table with both FKs as composite PK
- Multivalued attribute → separate table
Normalization 🎯
DBMS
▾
Quick Recall
1NF→2NF→3NF→BCNF: remove partial, transitive, and overlapping candidate key dependencies
| Form | Rule | Removes |
|---|---|---|
| 1NF | Atomic values, no repeating groups | Multi-valued cells |
| 2NF | 1NF + no partial dependency on composite PK | Partial dependencies |
| 3NF | 2NF + no transitive dependency (non-prime → non-prime) | Transitive dependencies |
| BCNF | For every FD X→Y, X must be a super key | Overlapping candidate keys |
FD: sid→sname,dept; dept→dept_head (transitive!)
Decompose: Student(sid,sname,dept) + Dept(dept,dept_head) → 3NF
Functional Dependencies
DBMS
▾
Quick Recall
X→Y means X uniquely determines Y; use Armstrong's axioms
Armstrong's Axioms:
Closure X⁺: All attributes determinable from X. Algorithm: start with X, repeatedly add RHS of any FD whose LHS ⊆ current set.
Canonical Cover: Minimal equivalent FD set — no redundant FDs or attributes.
- Reflexivity: If Y⊆X then X→Y
- Augmentation: If X→Y then XZ→YZ
- Transitivity: If X→Y and Y→Z then X→Z
Closure X⁺: All attributes determinable from X. Algorithm: start with X, repeatedly add RHS of any FD whose LHS ⊆ current set.
Canonical Cover: Minimal equivalent FD set — no redundant FDs or attributes.
SQL Quick Reference 🎯
DBMS
▾
Quick Recall
SELECT→FROM→JOIN→WHERE→GROUP BY→HAVING→ORDER BY→LIMIT
| Category | Commands |
|---|---|
| DDL | CREATE, ALTER, DROP, TRUNCATE |
| DML | SELECT, INSERT, UPDATE, DELETE |
| DCL | GRANT, REVOKE |
| TCL | COMMIT, ROLLBACK, SAVEPOINT |
| Join | Returns |
|---|---|
| INNER | Matching rows in both tables |
| LEFT OUTER | All left + matched right (NULL if no match) |
| RIGHT OUTER | All right + matched left |
| FULL OUTER | All rows from both (NULLs where no match) |
| CROSS | Cartesian product |
| SELF | Table joined with itself |
HAVING filters groups (WHERE filters rows before grouping).
Window: ROW_NUMBER() / RANK() / DENSE_RANK() OVER (PARTITION BY col ORDER BY col)
Indexing 🎯
DBMS
▾
Quick Recall
B+ tree = balanced, range-friendly; clustered changes row order, non-clustered doesn't
| Type | Clustered | Non-Clustered |
|---|---|---|
| Row order | Data physically sorted by index | Separate structure points to data |
| Count per table | Only 1 | Multiple allowed |
| Speed | Faster range queries | Extra lookup (pointer → row) |
| Leaf nodes | Actual data rows | Pointers to data |
When to index: High-read, low-write columns used in WHERE/JOIN. Avoid on small tables or frequently updated columns.
Transactions & ACID 🎯
DBMS
▾
Quick Recall
Atomicity=all-or-nothing, Consistency=valid state, Isolation=no interference, Durability=survives crash
| Property | Meaning | Ensured By |
|---|---|---|
| Atomicity | Transaction fully completes or fully rolls back | Undo log |
| Consistency | DB moves from one valid state to another | Integrity constraints |
| Isolation | Concurrent txns don't interfere | Concurrency control (locks/MVCC) |
| Durability | Committed data survives crashes | Redo log, WAL |
Concurrency Control 🎯
DBMS
▾
Quick Recall
2PL: grow locks then shrink; strict 2PL holds till commit
Problems: Lost update, dirty read, unrepeatable read, phantom read.
Lock types: Shared (read) — multiple allowed; Exclusive (write) — one only.
2-Phase Locking (2PL):
Strict 2PL: Hold all exclusive locks until commit/abort → prevents cascading rollbacks.
Deadlock handling: Wait-die / Wound-wait (age-based), timeout, or detect cycle in wait-for graph and abort youngest.
Lock types: Shared (read) — multiple allowed; Exclusive (write) — one only.
2-Phase Locking (2PL):
- Growing phase: Acquire locks, no releases
- Shrinking phase: Release locks, no new acquires
Strict 2PL: Hold all exclusive locks until commit/abort → prevents cascading rollbacks.
Deadlock handling: Wait-die / Wound-wait (age-based), timeout, or detect cycle in wait-for graph and abort youngest.
Isolation Levels & Anomalies 🎯
DBMS
▾
Quick Recall
Read Uncommitted→Read Committed→Repeatable Read→Serializable (strictness increases)
| Level | Dirty Read | Non-Repeatable | Phantom |
|---|---|---|---|
| Read Uncommitted | Yes | Yes | Yes |
| Read Committed | No | Yes | Yes |
| Repeatable Read | No | No | Yes |
| Serializable | No | No | No |
- Serial: Txns execute one after another
- Serializable: Equivalent result to some serial schedule (conflict/view)
- Recoverable: If T2 reads from T1, T1 commits before T2
- Cascadeless: Only read committed data
Query Optimization
DBMS
▾
Quick Recall
Query plan: parse→optimize→execute; indexes + join order matter most
Optimization techniques:
- Push selections/projections down (reduce rows early)
- Join ordering: smaller result sets first
- Use indexes on WHERE/JOIN columns
- Avoid SELECT * — fetch only needed columns
- Subquery vs JOIN: JOINs often faster (optimizer can rewrite)
- EXPLAIN/EXPLAIN ANALYZE to inspect query plans
CAP Theorem & SQL vs NoSQL
DBMS
▾
Quick Recall
Pick 2 of 3: Consistency, Availability, Partition tolerance
CAP Theorem: In a distributed system, you can guarantee at most 2 of:
- Consistency: All nodes see same data
- Availability: Every request gets a response
- Partition Tolerance: System works despite network failures
| SQL | NoSQL | |
|---|---|---|
| Schema | Fixed, predefined | Dynamic, flexible |
| Scaling | Vertical | Horizontal |
| ACID | Full | BASE (eventual consistency) |
| Best for | Complex queries, transactions | Big data, real-time, unstructured |