iExamWorld
Notes

DBMS and SQL: Complete Revision Notes for Placement Tests

English · Free on iExamWorld

DBMS and SQL in placement tests

Almost every service-company technical test includes DBMS questions. They fall into two groups: theory (keys, normal forms, ACID, transactions) and SQL output prediction on a small table. For output questions, write down the table, apply clauses in order and watch for NULLs.

DBMS versus file system

In a file-based system each application keeps its own files, so the same data is copied in many places. A Database Management System stores data centrally and gives controlled access to it.

IssueFile systemDBMS
Redundancy and inconsistencyHighControlled
Concurrent accessHard to manage safelyHandled by concurrency control
SecurityFile-level onlyUser-level privileges
Backup and recoveryManualBuilt-in recovery using logs
QueryingA program for each needA declarative language (SQL)

Three-schema architecture

  • Internal (physical) level: how data is stored, including files, indexes and record layout.
  • Conceptual (logical) level: the whole database as entities, attributes, relationships and constraints.
  • External (view) level: the part of the database each user group sees.

Physical data independence means the internal level can change without affecting the conceptual schema. Logical data independence means the conceptual schema can change without affecting external views; it is harder to achieve.

ER model

An entity is a real-world object (Student) and an entity set is a collection of similar entities. Attributes describe an entity:

  • Simple (Roll_No) versus composite (Name made of first and last name).
  • Single-valued versus multivalued (several phone numbers, drawn as a double ellipse).
  • Derived (Age from Date_of_Birth, drawn as a dashed ellipse).
  • Key attribute (underlined).

A relationship associates entities, such as a Student enrolling in a Course. A weak entity has no key of its own and depends on an owner entity; it is drawn with a double rectangle and its identifying relationship with a double diamond. Cardinality may be one-to-one, one-to-many, many-to-one or many-to-many. When mapping to tables, a one-to-many relationship puts a foreign key on the many side, and a many-to-many relationship needs a separate table holding both keys.

Keys

  • Super key: any set of attributes that uniquely identifies a row.
  • Candidate key: a minimal super key, with no attribute that can be removed.
  • Primary key: the candidate key chosen to identify rows; it cannot be NULL and must be unique.
  • Alternate key: a candidate key that was not chosen as the primary key.
  • Composite key: a key made of two or more attributes, such as (StudentID, CourseID).
  • Foreign key: an attribute that refers to the primary key (or a unique key) of another table. It may contain duplicates and, unless declared NOT NULL, NULLs.

If R(A, B, C) has A as its only candidate key, its super keys are A, AB, AC and ABC, which is four in all.

Functional dependencies and closure

X → Y means that rows with the same X value always have the same Y value. To find the closure X+, start with X and keep adding the right side of any dependency whose left side is already included. For R(A, B, C, D, E) with A → B, B → C and CD → E, the closure of {A, D} is {A, B, C, D, E}, so AD is a key. If X+ contains every attribute, X is a super key; if no proper subset of X does, X is a candidate key.

Normalisation

Normalisation removes redundancy and the insert, update and delete anomalies it causes.

  • 1NF: every column holds atomic values, with no repeating groups.
  • 2NF: 1NF and no partial dependency, meaning no non-prime attribute depends on only part of a composite candidate key.
  • 3NF: 2NF and no transitive dependency of a non-prime attribute on a key. Formally, for every X → Y, either X is a super key or Y is a prime attribute.
  • BCNF: for every non-trivial X → Y, X is a super key. Every BCNF relation is in 3NF, but not every 3NF relation is in BCNF.

A small example

Take Result(StudentID, CourseID, StudentName, Marks) with key (StudentID, CourseID). StudentName depends on StudentID alone, which is a partial dependency, so the table is only in 1NF. Split it into Student(StudentID, StudentName) and Result(StudentID, CourseID, Marks).

Next take Emp(EmpID, DeptID, DeptName). EmpID → DeptID and DeptID → DeptName form a transitive dependency, so the table is in 2NF but not 3NF. Split it into Emp(EmpID, DeptID) and Dept(DeptID, DeptName).

Finally, suppose each teacher teaches one subject and a student takes each subject from one teacher: (Student, Subject) → Teacher and Teacher → Subject. Teacher is not a super key, so this is in 3NF (Subject is prime) but not BCNF.

Transactions, ACID and concurrency

  • Atomicity: all or nothing; a failed transfer is rolled back.
  • Consistency: a transaction takes the database from one valid state to another.
  • Isolation: concurrent transactions do not see each other's partial work.
  • Durability: once committed, changes survive crashes, using logs.

A schedule is serializable if its effect equals some serial order. To test conflict serializability, draw a precedence graph with an edge Ti → Tj when an operation of Ti conflicts with a later operation of Tj (same item, at least one write). The schedule is conflict serializable if and only if the graph has no cycle. Two-phase locking (a growing phase, then a shrinking phase) guarantees conflict serializability.

A deadlock occurs when transactions wait for each other in a cycle; for example, T1 holds a lock on X and wants Y while T2 holds Y and wants X. The four conditions are mutual exclusion, hold and wait, no preemption and circular wait. Systems either prevent deadlock (for example with the timestamp-based wait-die and wound-wait schemes) or detect it with a wait-for graph and abort a victim.

Indexing

An index is an extra structure, usually a B+ tree or a hash, that speeds up searches on a column at the cost of storage and slower inserts, updates and deletes. A clustered index decides the physical order of rows, so a table has at most one. A table can have many non-clustered indexes.

SQL command groups

GroupCommands
DDL (definition)CREATE, ALTER, DROP, TRUNCATE, RENAME
DML (manipulation)INSERT, UPDATE, DELETE (SELECT is often listed here or as DQL)
DCL (control)GRANT, REVOKE
TCL (transaction)COMMIT, ROLLBACK, SAVEPOINT

SELECT, aggregates and NULLs

The logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. WHERE filters rows before grouping and cannot use aggregates; HAVING filters groups after grouping.

Aggregates ignore NULLs, except COUNT(*), which counts rows. If a bonus column holds 100, NULL, 300, NULL, 200, 400, then COUNT(*) = 6, COUNT(bonus) = 4, SUM(bonus) = 1000 and AVG(bonus) = 1000 / 4 = 250, not 1000 / 6. A comparison such as bonus = NULL is never true; use IS NULL instead.

Example: with Emp rows (HR, 30000), (HR, 40000), (IT, 50000), (IT, 60000), (IT, 45000), (Sales, 35000), the query SELECT dept FROM Emp GROUP BY dept HAVING AVG(salary) > 40000 returns only IT.

Joins

Let Students have id values 1, 2, 3, 4 and Enrol have sid values 1, 1, 3, 5, joined on id = sid.

JoinRowsReason
INNER3Matches only: id 1 twice and id 3 once
LEFT53 matches plus unmatched students 2 and 4
RIGHT43 matches plus unmatched sid 5
FULL OUTER63 matches plus 2 from the left plus 1 from the right
CROSS164 x 4, every pair

A self join joins a table with itself using aliases, for example pairing each employee with their manager from the same Employee table. Rows whose manager_id is NULL drop out of an inner self join.

Subqueries, set operators and views

A subquery is a query inside another, as in SELECT name FROM Staff WHERE salary > (SELECT AVG(salary) FROM Staff). A correlated subquery refers to the outer row and runs once for each outer row. IN, EXISTS, ANY and ALL work with subqueries that return several rows.

UNION combines results and removes duplicates, while UNION ALL keeps them and is faster. The values 1, 2, 2, 3 combined with 2, 3, 4 give 4 rows with UNION and 7 with UNION ALL. Both queries must have the same number of columns with compatible types.

A view is a stored query that behaves like a virtual table. It simplifies complex queries and limits what users can see.

DELETE, TRUNCATE and DROP

CommandTypeEffectWHERE allowed
DELETEDMLRemoves chosen rows; can be rolled backYes
TRUNCATEDDLRemoves all rows and keeps the structure; fasterNo
DROPDDLRemoves the table, its data and its structureNo

Whether TRUNCATE can be rolled back depends on the database product, so questions usually focus on the WHERE clause and on structure being kept or removed.

Constraints

NOT NULL, UNIQUE (one NULL or more may be allowed, depending on the product), PRIMARY KEY (UNIQUE plus NOT NULL), FOREIGN KEY (referential integrity, with options such as ON DELETE CASCADE), CHECK (a condition, for example that age must be at least 18) and DEFAULT.

Quick revision points

  • A DBMS reduces redundancy and handles concurrency, security and recovery.
  • The internal level covers storage, the conceptual level the whole logical design, and the external level user views.
  • Derived attributes use a dashed ellipse, multivalued attributes a double ellipse, and weak entities a double rectangle.
  • One-to-many: the foreign key goes on the many side; many-to-many needs a separate table.
  • A candidate key is a minimal super key; a primary key is unique and NOT NULL.
  • 2NF removes partial dependencies and 3NF removes transitive dependencies.
  • BCNF: every determinant is a super key.
  • ACID: atomicity, consistency, isolation, durability.
  • A cycle-free precedence graph means the schedule is conflict serializable.
  • Deadlock is a cycle of waiting transactions, found with a wait-for graph.
  • COUNT(*) counts all rows; COUNT(col) and AVG(col) skip NULLs.
  • WHERE filters rows before grouping; HAVING filters groups after grouping.
  • LEFT JOIN keeps every left row; FULL JOIN keeps unmatched rows from both sides.
  • UNION removes duplicates; UNION ALL keeps them.
  • DELETE can use WHERE, TRUNCATE empties the table, and DROP removes the table.

These notes were written with AI assistance for iExamWorld, based on the official syllabus. Spotted a mistake? Tell us.