DBMS NotesPYQs

Chapter 2

Data Models

Chapter Notes

Block-based notes with markdown, diagrams, code, and math.

Chapter 2 — Data Models

2.1 Data Model Comparison

ModelStructureUseExample DBMS
RelationalTables (relations)General-purpose OLTPMySQL, PostgreSQL
EREntities + relationshipsDesign phaseLucidchart, draw.io
Object-orientedObjects with methodsCAD/CAM, engineeringdb4o
HierarchicalTree recordsLegacyIBM IMS
NetworkGraph recordsLegacyIDMS
GraphNodes + edgesSocial, recommendationNeo4j

2.2 ER Model

Entity Types

TypeKeyNotationExample
Strong entityOwn primary keySingle rectangleStudent (sid)
Weak entityPartial key + owner PKDouble rectangleOrderItem (depends on Order)

Attribute Types

TypeNotationExample
Simple (atomic)Ellipseage
CompositeEllipse with sub-ellipsesname → {first, last}
MultivaluedDouble ellipsephone_numbers
DerivedDashed ellipseage ← birth_date

Key Types

KeyDefinitionExample
Super keyAny set of attributes that uniquely identifies a tuple{sid}, {sid, name}, {sid, name, dept}
Candidate keyMinimal super key — no proper subset is also a super key{sid}
Primary keyThe chosen candidate key (underlined in ER diagram)sid
Foreign keyReferences PK of another relationEnrollment.sid → Student.sid
Partial keyIdentifies a weak entity within its owner (dashed underline)dep_name in Dependent

Cardinality Constraints

NotationMeaningER notation
1:1One to oneSingle line both sides
1:NOne to manyArrow on the N side
M:NMany to manyNo arrow, plain line

Participation Constraints

  • Total (double line) — every entity MUST participate in the relationship.
  • Partial (single line) — some entities may not participate.

2.3 Specialization · Generalization · Aggregation

Specialization — top-down: start with general entity, split into subtypes that inherit all supertype attributes and add their own.

Generalization — bottom-up: observe multiple entity types with common attributes; merge common attributes into a new supertype.

Aggregation — treat a relationship set as an entity so it can participate in another relationship. Used when a relationship needs its own attributes or participates in a higher-level relationship.


2.4 Relational Model Basics

A relation is a table. A tuple is a row. An attribute is a column. The domain is the set of allowed values for an attribute.

Referential Integrity

A foreign key value in relation R must either:

  • Match an existing primary key value in the referenced relation S, OR
  • Be NULL (if allowed).

On violation, the DBMS enforces a rule:

RuleEffect
RESTRICT (NO ACTION)Block the update/delete
CASCADEPropagate the change to child rows
SET NULLSet FK column to NULL
SET DEFAULTSet FK column to its default value

2.5 ER → Relational Mapping Rules

-- Rule 1: Strong entity → own relation, PK = entity key
CREATE TABLE Department (
  dept_id   INT         PRIMARY KEY,
  dept_name VARCHAR(100) NOT NULL,
  location  VARCHAR(100)
);

-- Rule 2: 1:N relationship → FK on the N-side
CREATE TABLE Employee (
  emp_id  INT         PRIMARY KEY,
  name    VARCHAR(100) NOT NULL,
  salary  DECIMAL(10,2),
  dept_id INT REFERENCES Department(dept_id) ON DELETE SET NULL
);

-- Rule 3: M:N relationship → new relation, PK = combination of both FKs
CREATE TABLE Project (
  proj_id INT PRIMARY KEY,
  title   VARCHAR(200)
);

CREATE TABLE Works_On (
  emp_id  INT REFERENCES Employee(emp_id) ON DELETE CASCADE,
  proj_id INT REFERENCES Project(proj_id) ON DELETE CASCADE,
  hours   DECIMAL(5,2),
  PRIMARY KEY (emp_id, proj_id)
);

-- Rule 4: Weak entity → PK = owner PK + partial key
CREATE TABLE Dependent (
  emp_id       INT         REFERENCES Employee(emp_id) ON DELETE CASCADE,
  dep_name     VARCHAR(100),   -- partial key
  relationship VARCHAR(50),
  dob          DATE,
  PRIMARY KEY (emp_id, dep_name)  -- composite PK
);

-- Rule 5: Multivalued attribute → separate relation
CREATE TABLE Employee_Phone (
  emp_id INT  REFERENCES Employee(emp_id) ON DELETE CASCADE,
  phone  VARCHAR(20),
  PRIMARY KEY (emp_id, phone)
);

-- Rule 6: 1:1 relationship → FK in either table (prefer total participation side)
CREATE TABLE Spouse (
  emp_id    INT PRIMARY KEY REFERENCES Employee(emp_id),
  spouse_name VARCHAR(100),
  wedding_date DATE
);

Exam Quick-Reference

  • Double rectangle = weak entity; double diamond = identifying relationship; double ellipse = multivalued attribute; dashed ellipse = derived attribute.
  • M:N → always a new relation. Never put FK on both sides without a bridge table.
  • Weak entity PK = owner's PK columns + partial key columns.
  • Specialization (top-down) ≠ Generalization (bottom-up) — the direction matters.
  • FK violation options: RESTRICT · CASCADE · SET NULL · SET DEFAULT.

Full Syllabus Outline

Every topic from the syllabus data is preserved here.

Solved PYQs

A few past questions with short answers.

Chapter 24 marksasked 1x2082 Kartik B

Explain different types of keys in a relational database along with example.

Solution

Key Types in the Relational Model

Key TypeDefinitionExample
Super KeyAny set of attributes that uniquely identifies every tuple in a relation{sid}, {sid, name}, {email}
Candidate KeyA minimal super key — no proper subset is also a super key{sid}, {email}
Primary KeyThe one candidate key chosen by the designer (underlined in schema)sid
Foreign KeyAn attribute in one relation that references the PK of another relationEnrollment.sid → Student.sid
Partial KeyAttribute(s) that partially identify a weak entity when combined with the owner's PKitem_no in Order_Item (combined with order_id)

Candidate Key vs Primary Key

  • A relation can have multiple candidate keys (e.g., both sid and email could uniquely identify a student).
  • The designer selects exactly one candidate key as the primary key.
  • All candidate keys share the same uniqueness and minimality properties; the PK is just the chosen representative.
  • The PK columns must not contain NULL values; other candidate keys may optionally be declared UNIQUE NOT NULL.

Referential Integrity (Foreign Key Constraint)

A foreign key value in a child relation must either match a primary key value in the referenced (parent) relation or be NULL. This prevents orphan records. On DELETE or UPDATE of a referenced row, the DBMS can: RESTRICT (block), CASCADE (propagate), SET NULL, or SET DEFAULT.

Chapter 28 marksasked 1x2082 Kartik B

Draw an ER-diagram for the Hospital Management System case: A hospital has multiple departments. Each department has a unique department ID and name. Doctors are identified by doctor ID with attributes name, specialization, and experience. A doctor belongs to one department but may treat multiple patients. Each patient has a patient ID, name, age, gender, and medical history. Each patient is assigned a room when admitted. A room has room number, type (ICU, General, Private), and cost per day. Nurses are identified by employee ID, name, and shift timings. The hospital has a pharmacy storing medicines with medicine ID, name, and expiry date. The hospital maintains records of appointments with date and time.

Solution

Cardinality Constraints:

  • A Department employs many Doctors and many Nurses (1:N each).
  • A Doctor treats many Patients; a Patient may see many Doctors (M:N → bridge via Appointment).
  • A Patient is assigned to at most one Room; a Room holds at most one Patient at a time (1:1).
  • An Appointment is between exactly one Doctor and one Patient on a given date and time.
Chapter 24 marksasked 1x2081 Chaitra R

Explain the various elements of the relational data model along with an example.

Solution

The Relational Data Model:

The relational data model, proposed by E.F. Codd in 1970, represents data as a collection of relations (tables). It is built on the mathematical foundation of set theory and predicate logic.

Core Terminology:

TermDefinitionExample
RelationA table with a fixed set of named columns and a set of rowsStudent table
TupleA single row; one complete record(1, 'Asha', 'CS', 3.8)
AttributeA named column with a specific domainname VARCHAR(100)
DomainThe set of allowed values for an attributeINT, VARCHAR(100), DATE
DegreeNumber of attributes (columns) in a relation4
CardinalityNumber of tuples (rows) in a relation500
SchemaThe structural definition: relation name + attributesStudent(sid, name, dept, gpa)

Integrity Constraints:

  • Entity Integrity: No attribute of the primary key may be NULL.
  • Referential Integrity: Every non-null foreign key value must match an existing primary key value in the referenced relation.
  • Domain Constraint: Each attribute value must lie within its defined domain.

Key Constraints:

  • Primary Key: Uniquely identifies each tuple; cannot be NULL.
  • Candidate Key: Any minimal set of attributes that uniquely identifies tuples.
  • Foreign Key: References the PK of another (or the same) relation, enforcing referential integrity.

More PYQs

Additional practice from this chapter.