DBMS NotesPYQs

Chapter 2

Data Models

Full Syllabus Outline

Every topic from the syllabus data is preserved here.

Chapter Notes

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

Chapter 2 — Data Models

2.1 What is a Data Model?

A data model is a collection of conceptual tools for describing:

  1. Data structure — how data is organized
  2. Data relationships — how pieces of data relate to each other
  3. Data semantics — what the data means
  4. Constraints — rules the data must obey

Data Model Comparison

ModelCore StructureStrengthsWeaknessesExample DBMS
RelationalTables with rows and columnsGeneral-purpose, SQL, well-understoodNot ideal for complex nested dataMySQL, PostgreSQL, Oracle
ER (Entity-Relationship)Entities, attributes, relationshipsIntuitive design tool; maps to relational easilyConceptual only — not executableLucidchart, draw.io
Object-OrientedObjects with attributes and methodsModels complex real-world objects naturallyComplex querying; not widely adopteddb4o, Versant
HierarchicalTree of parent-child recordsFast for known access pathsRigid; no M:N without duplicationIBM IMS (legacy)
NetworkGraph of records connected by pointersM:N without duplicationComplex navigationIDMS (legacy)
GraphNodes (entities) + directed edges (relationships)Naturally models social/network dataLess efficient for tabular queriesNeo4j, Amazon Neptune

Exam tip: Know the key characteristic of each model and one example DBMS. Hierarchical and network models are legacy — know their disadvantages vs relational.


2.2 Entity-Relationship (ER) Model

The ER model is a conceptual design tool — it helps model real-world requirements before implementation. Proposed by Peter Chen in 1976.

2.2.1 Entities and Entity Sets

An entity is a distinguishable real-world object (a specific student, a specific course).
An entity set is the collection of all entities of the same type (all students, all courses).

Strong vs Weak Entities

Strong EntityWeak Entity
KeyHas its own primary keyHas only a partial key — depends on owner entity
ER notationSingle rectangleDouble rectangle
Identifying relationshipConnected to owner by double diamond
ExistenceExists independentlyCannot exist without its owner
ExampleEmployee(emp_id, name)Dependent(dep_name, dob) — partial key is dep_name, depends on Employee

2.2.2 Attributes and Their Types

Attribute TypeDefinitionER NotationExample
Simple (atomic)Cannot be divided furtherSingle ellipseage, salary
CompositeCan be divided into sub-attributesEllipse with child ellipsesname → {first_name, last_name}
MultivaluedCan hold multiple values for one entityDouble ellipsephone_numbers, skills
DerivedComputed from another attributeDashed ellipseage derived from birth_date

2.2.3 Keys in ER Model

Key TypeDefinitionExample
Super keyAny set of attributes that uniquely identifies an entity{sid}, {sid, name}, {sid, name, dept}
Candidate keyA minimal super key — no proper subset is also a super key{sid}
Primary keyThe chosen candidate key, underlined in ER diagramsid
Partial keyUniquely identifies a weak entity within its owner setdashed underlinedep_name in Dependent

2.2.4 Relationships and Mapping Cardinalities

Cardinality Constraints

CardinalityMeaningExampleER Notation
1:1Each entity in A relates to at most one in B, and vice versaOne person manages at most one departmentArrow on both sides
1:NOne entity in A relates to many in B; each B to at most one AOne department has many employeesArrow on the "one" side
M:NEntities in A can relate to many in B and vice versaA student enrolls in many courses; a course has many studentsNo arrows

Participation Constraints

TypeMeaningER NotationExample
Total participationEvery entity MUST participate in at least one relationshipDouble lineEvery employee MUST belong to a department
Partial participationSome entities may not participateSingle lineSome employees may not manage any project

Relationship attributes: Attributes that belong to the relationship itself, not either entity. Example: WORKS_ON between Employee and Project has attribute hours.

Exam tip: Always state both cardinality AND participation constraints when describing a relationship in an ER diagram.

2.2.5 Specialization, Generalization, and Aggregation

Specialization (Top-Down): Start with a general entity (supertype), define subtypes that inherit all attributes and add their own.

  • Symbol: ISA triangle (△) pointing from subtype to supertype
  • Disjoint: entity belongs to at most one subtype (marked "d")
  • Overlapping: entity can belong to multiple subtypes (marked "o")
  • Total: every supertype must belong to at least one subtype
  • Partial: some supertype entities may not belong to any subtype

Generalization (Bottom-Up): Start with multiple specific entity types, observe common attributes, and merge them into a new supertype.

  • Example: Car, Truck, Motorcycle all have vehicle_id, make, model, year → create a Vehicle supertype.
  • Same ER diagram result as specialization — difference is only the direction of thought.

Aggregation: Treat a relationship set as a higher-level entity so it can participate in another relationship.

  • Why needed? ER model cannot directly have a relationship between a relationship and an entity.
  • Example: An employee works on a project (WORKS_ON relationship). A manager supervises that specific employee-project combination. The supervisor relationship links to the aggregated WORKS_ON relationship.

2.3 Relational Model

The relational model organizes data as a collection of relations (tables). Proposed by E.F. Codd (IBM) in 1970.

Key Terminology

Relational TermSQL EquivalentDescription
RelationTableTwo-dimensional table of data
TupleRow / RecordA single row in the relation
AttributeColumn / FieldA named column
DomainData type + constraintsThe set of allowed values for an attribute
DegreeNumber of columnsNumber of attributes
CardinalityNumber of rowsNumber of tuples

Properties of a Relation (Formal)

  1. Each cell contains exactly one atomic value (1NF requirement)
  2. Each column has a unique name
  3. All values in a column are from the same domain
  4. No duplicate tuples (formally — SQL allows duplicates in tables)
  5. Order of rows is irrelevant (a relation is a set)
  6. Order of columns is irrelevant (attributes identified by name)

2.3.1 Keys in the Relational Model

Key TypeDefinitionExample
Super keySet of attributes that uniquely identifies every tuple{sid}, {sid, name}
Candidate keyMinimal super key{sid}
Primary key (PK)Chosen candidate key; must be unique + NOT NULLsid INT PRIMARY KEY
Foreign key (FK)Attribute(s) in R that reference the PK of another relation SEnrollment.sid references Student.sid
Composite keyPK made of multiple attributes(sid, cid) in Enrollment

2.3.2 Referential Integrity Constraint

A foreign key constraint requires that every non-null FK value must match an existing PK value in the referenced relation.

EventRESTRICT (NO ACTION)CASCADESET NULLSET DEFAULT
Delete referenced row❌ Block the delete✓ Delete child rows tooSet FK to NULLSet FK to default
Update referenced PK❌ Block the update✓ Update FK values tooSet FK to NULLSet FK to default

Exam example: Delete a department with employees → RESTRICT: error; CASCADE: employees also deleted; SET NULL: employees' dept_id becomes NULL.


2.4 ER Diagram → Relational Schema Mapping Rules

These rules are very frequently tested. Memorize all 6 rules.

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

-- RULE 2: 1:N binary relationship → FK on the N-side (many 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
  -- FK on Employee (N side), references Department (1 side)
);

-- RULE 3: M:N relationship → new bridge/junction relation
-- PK = combination of both entity PKs; NEVER put FK on either entity alone for M:N
CREATE TABLE Project (
  proj_id INT PRIMARY KEY,
  title   VARCHAR(200) NOT NULL,
  budget  DECIMAL(12,2)
);
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),          -- relationship attribute
  PRIMARY KEY (emp_id, proj_id)  -- composite PK
);

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

-- 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)  -- composite PK
);

-- RULE 6: 1:1 relationship → FK in either table (prefer total participation side)
ALTER TABLE Department ADD COLUMN manager_id INT REFERENCES Employee(emp_id);

-- RULE 7: Specialization/Generalization → per-level tables (most common approach)
CREATE TABLE Person (pid INT PRIMARY KEY, name VARCHAR(100), age INT);
CREATE TABLE Student_Spec (
  pid     INT PRIMARY KEY REFERENCES Person(pid),  -- PK is also FK
  gpa     DECIMAL(3,2),
  major   VARCHAR(50)
);
CREATE TABLE Employee_Spec (
  pid     INT PRIMARY KEY REFERENCES Person(pid),
  salary  DECIMAL(10,2),
  emp_id  VARCHAR(20)
);

Summary of ER → Relational Mapping Rules

ER ConstructRelational Mapping Rule
Strong entityOwn table; PK = entity PK
Weak entityOwn table; PK = owner PK + partial key; FK to owner with CASCADE delete
1:1 relationshipFK in either table (prefer total participation side)
1:N relationshipFK on the N-side (many side)
M:N relationshipNew bridge/junction table; PK = combination of both entity PKs
Multivalued attributeNew separate table; PK = entity PK + attribute value
Derived attributeUsually not stored (computed when needed)
Specialization (ISA)3 options: per-level tables (most common), per-subtype tables, or single table with discriminator

Exam trap — M:N: Never store M:N by putting a multivalued FK in one entity. You MUST create a separate junction table with a composite PK.


Exam Quick-Reference — Chapter 2

ER Notation Summary:

  • Rectangle = strong entity; Double rectangle = weak entity
  • Diamond = relationship; Double diamond = identifying relationship (for weak entity)
  • Ellipse = attribute; Double ellipse = multivalued; Dashed ellipse = derived
  • Primary key attribute = underlined; Partial key = dashed underline
  • Double line = total participation; Single line = partial participation

Key mapping rules (memorize):

  • M:N → always a new bridge table
  • Weak entity PK = owner's PK + partial key
  • 1:N → FK goes on the N (many) side
  • Multivalued attribute → separate table
  • FK violation actions: RESTRICT · CASCADE · SET NULL · SET DEFAULT

Solved PYQs

A few past questions with short answers.

CT30102004Chapter 28 marksasked 2x2081 Chaitra R, 2079 Chaitra R

Construct an ER diagram for a medium-sized Company delivering products to customers, employing various experts, consultants and supporting staff, with inter-disciplinary project teams selected from different departments, each project having a Project Manager. Assume necessary attributes, relationship sets and mapping constraints.

Solution

Cardinality: Dept→Emp (1:N). Emp↔Proj (M:N via Works_On). Emp→Proj (manages, 1:N). Client→Proj (1:N). M:N relationships require bridge tables with composite PKs.

CT30102009Chapter 28 marksasked 2x2080 Chaitra R

Consider the database of departmental store. There are various departments; one department sells many items, some items may be sold by more than one department. A department has many employees; an employee belongs to at most one department. A manager is an employee who may look after more than one department but a department may be looked after by only one manager. A supplier may supply more than one item; every item is supplied by only one supplier at a time. Construct ER diagram.

Solution

Cardinality: Dept→Emp (1:N). Dept↔Item (M:N). Dept→Manager (1:1 or N:1). Supplier→Item (1:N). Total participation where applicable (e.g., every Emp belongs to a Dept).

CT30102008Chapter 24 marksasked 2x2080 Chaitra R

Define generalization and specialization with its notation and examples.

Solution

Generalization: Bottom-up. Combine subtypes into supertype (Employee+Student → Person). Specialization: Top-down. Split supertype into subtypes (Person → Employee, Student).

Constraints: Disjoint/Overlapping, Total/Partial. Mapped via table-per-type, table-per-hierarchy, or table-per-concrete-class.

More PYQs

Additional practice from this chapter.