Relational model, relational algebra and calculus — Unit 2 Notes (Database Management Systems)

BCS402 · Unit 2

Relational model, relational algebra and calculus notes — Unit 2

Free unit-wise study notes on relational model, relational algebra and calculus for Database Management Systems, Semester 4 of B.Tech — Computer Science & Engineering — key concepts, examples, important questions and a revision checklist for semester exams.

The mathematical foundation of SQL. Covers Relational Model terminology (Relations, Tuples, Domains), Integrity Constraints (Primary Key, Foreign Key), converting ER diagrams to Tables, and the pure theoretical query languages: Relational Algebra and Relational Calculus.

Notebook — 15 pages

Page 1

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

1. The Relational Model Introduction

The Relational Model was introduced by E.F. Codd in 1970. It revolutionized database management by providing a solid mathematical foundation based on set theory and predicate logic. It is the basis for all modern relational databases (MySQL, PostgreSQL, Oracle).

The core concept is brilliantly simple: All data is represented as two-dimensional tables called Relations.

1.1 The Shift from Physical to Logical

In earlier models (Hierarchical, Network), the programmer had to navigate physical pointers to find related data. In the Relational Model, relationships are entirely logical. You relate two tables by ensuring they share a common column (e.g., finding the Department Name for an Employee by matching the `Dept_ID` column in both tables).

Next — Relational Model Terminology

1 of 15

Page 2

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

2. Relational Terminology

The Relational Model uses strict mathematical terminology. While SQL uses the terms 'Table', 'Row', and 'Column', the formal theory uses 'Relation', 'Tuple', and 'Attribute'.

  • Relation: A mathematical term for a two-dimensional Table. It is a set of tuples.
  • Tuple: A single Row in the table. It represents one specific record (e.g., all data regarding student John Doe).
  • Attribute: A single Column in the table. It represents a property (e.g., `Age` or `Salary`).
  • Degree (Arity): The number of attributes (columns) in a relation. A table with 5 columns has a degree of 5.
  • Cardinality: The number of tuples (rows) in a relation. A table with 1000 employee records has a cardinality of 1000.
  • Domain: The set of all permitted, valid values for an attribute. For example, the domain of an `Age` attribute might be integers between 0 and 150. A `Gender` domain might be strictly the set {'M', 'F', 'Other'}.

Next — Properties of a Relation

2 of 15

Page 3

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

3. Properties of a Relation

Not every table is a valid mathematical relation. To be a true relation, a table must adhere to strict mathematical rules based on Set Theory.

3.1 The Rules

  • Tuples are Unordered: The rows have no specific mathematical order. Moving row 10 to row 1 does not change the relation in any way. (This is why SQL output order is random unless you use `ORDER BY`).
  • Attributes are Unordered: The columns have no specific mathematical order. A table defined as (ID, Name) is logically identical to (Name, ID).
  • No Duplicate Tuples: A mathematical Set cannot contain duplicate elements. Therefore, a true relation cannot have two identical rows. Every row must be unique.
  • Atomic Values: Every cell at the intersection of a tuple and an attribute must contain exactly ONE atomic value. You cannot put a list or array inside a single cell. (This is known as First Normal Form).
  • Same Domain: All values in a specific column must come from the same domain. You cannot mix strings and integers in an `Age` column.

Next — Integrity Constraints

3 of 15

Page 4

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

4. Relational Integrity Constraints

A database must accurately reflect reality. Integrity constraints are strict mathematical rules enforced by the DBMS to prevent users from inserting garbage, invalid, or contradictory data.

4.1 Domain Constraints

Dictates that the value of each attribute must be an atomic value from the attribute's specific domain. If you try to insert "Apple" into an integer `Salary` column, the DBMS rejects it, enforcing domain integrity.

4.2 Entity Integrity Constraint

This rule states: No Primary Key attribute can be NULL.

The Primary Key is the sole mechanism used to uniquely identify individual tuples. If the Primary Key is NULL (unknown or missing), we cannot identify the record, defeating the entire purpose of the relational model. Therefore, the DBMS will instantly reject any `INSERT` operation that leaves the PK blank.

Next — Referential Integrity

4 of 15

Page 5

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

5. Referential Integrity (Foreign Keys)

This is the most complex and important constraint. It maintains consistency between two different tables.

5.1 The Foreign Key Concept

A Foreign Key (FK) is an attribute in one table (the Referencing Table) that perfectly matches the Primary Key of another table (the Referenced Table). It acts as the "glue" holding the database together.

Consider an `EMPLOYEE` table and a `DEPARTMENT` table. To indicate which department an employee works for, we add `Dept_ID` as a Foreign Key in the `EMPLOYEE` table, pointing to the Primary Key `Dept_ID` in the `DEPARTMENT` table.

5.2 The Referential Integrity Rule

The rule states: A Foreign Key value must either exist as a Primary Key value in the referenced table, or it must be completely NULL.

This prevents "Orphan Records". If an HR clerk tries to assign an employee to Department 99, but Department 99 does not exist in the `DEPARTMENT` table, the DBMS will aggressively block the operation, throwing a Foreign Key Violation error.

Next — Mapping ER to Relational Schema

5 of 15

Page 6

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

6. Mapping ER Diagrams to Relational Tables

Once an ER Diagram is drawn, it must be mechanically converted into Relational Tables. There is a strict algorithmic process for this.

6.1 Mapping Regular Entities

For each strong entity type, create a table. Include all simple attributes. Choose one of the candidate keys as the Primary Key.

6.2 Mapping Composite Attributes

Because relations must be atomic, composite attributes are "flattened". If an entity has a composite `Address` attribute (Street, City, Zip), you create three separate columns in the table: `Street`, `City`, and `Zip`. The parent attribute name `Address` is discarded.

6.3 Mapping Multi-Valued Attributes

A cell cannot hold multiple values. If `EMPLOYEE` has a multi-valued `Phone_Number` attribute, you must create an entirely New Table. This new table has two columns: the Phone Number itself, and a Foreign Key pointing back to the Employee's Primary Key. The PK of this new table is the combination of both columns.

Next — Mapping Relationships

6 of 15

Page 7

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

7. Mapping Relationships to Tables

How relationships are mapped depends entirely on their Cardinality Ratio.

7.1 Mapping 1:N (One-to-Many)

Identify the "Many" side of the relationship. Take the Primary Key of the "One" side, and insert it as a Foreign Key into the table of the "Many" side.

Example: Department (1) to Employee (N). Place the `Dept_ID` inside the `EMPLOYEE` table.

7.2 Mapping M:N (Many-to-Many)

You cannot simply place a Foreign Key in either table, because one row would need to point to multiple rows, violating atomic values.

You MUST create an entirely New Table (a Junction Table / Linking Table).
Example: Student (M) to Course (N). Create an `ENROLLMENT` table. Its columns will be the Primary Key of Student (`Student_ID`) and the Primary Key of Course (`Course_ID`). The Primary Key of this new table is the combination of both.

Next — Relational Algebra Introduction

7 of 15

Page 8

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

8. Relational Algebra

Before SQL was invented, E.F. Codd designed a pure mathematical query language: Relational Algebra. It is a procedural language; it specifies exactly how to perform operations step-by-step using mathematical symbols.

It forms the absolute backbone of query processing. When you write an SQL query, the DBMS compiler translates it into a Relational Algebra expression tree to execute it.

8.1 The Closure Property

A fundamental property of Relational Algebra: Every operation takes one or two relations as input, and always produces a new relation as output. Because the output is always a relation, operations can be nested indefinitely (e.g., joining two tables, then filtering the result, then selecting columns from that result).

Next — Basic Algebra Operators: Select and Project

8 of 15

Page 9

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

9. Relational Algebra: Select and Project

9.1 SELECT Operation (σ\sigma - Sigma)

Filters the rows (tuples) of a relation based on a specific boolean condition. It slices the table horizontally.

Syntax: σcondition(Relation)\sigma_{condition}(Relation)

Example: To find all employees earning more than 50k:50k: \sigma_{Salary > 50000}(EMPLOYEE)$

(Equivalent SQL: `SELECT FROM EMPLOYEE WHERE Salary > 50000`)*

9.2 PROJECT Operation (π\pi - Pi)

Selects specific columns (attributes) from a relation and discards the rest. It slices the table vertically. Crucially, because the output must be a mathematical set, the PROJECT operation automatically eliminates any duplicate rows.

Syntax: πattribute_list(Relation)\pi_{attribute\_list}(Relation)

Example: To get a list of all employee names:
πName(EMPLOYEE)\pi_{Name}(EMPLOYEE)

Nesting: To get the names of employees earning > 50k:50k: \pi_{Name}(\sigma_{Salary > 50000}(EMPLOYEE))$

Next — Set Operations in Relational Algebra

9 of 15

Page 10

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

10. Relational Algebra: Set Operations

Because relations are sets, standard set theory operations apply. However, to use them, the two relations must be Union Compatible (Type Compatible): they must have the exact same number of columns, and corresponding columns must have the same data types.

10.1 UNION (\cup)

Combines all tuples from relation R and relation S, removing duplicates.
RSR \cup S

10.2 INTERSECTION (\cap)

Returns only the tuples that exist in BOTH relation R and relation S.
RSR \cap S

10.3 SET DIFFERENCE (-)

Returns all tuples that exist in R, but do NOT exist in S. (Order matters!).
RSR - S

10.4 CARTESIAN PRODUCT (×\times)

Does not require Union Compatibility. It combines every single row of R with every single row of S. If R has 10 rows and S has 5 rows, the output will be a massive table of 50 rows. Rarely used alone; it is the mathematical foundation for the JOIN operation.

Next — The JOIN Operation

10 of 15

Page 11

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

11. Relational Algebra: The JOIN Operation

The JOIN operator (\bowtie) is the most critical operation for combining related data from different tables.

Mathematically, a JOIN is a Cartesian Product followed immediately by a SELECT operation to filter out the nonsense combinations.

11.1 Theta Join (θ\bowtie_\theta)

A join based on a general boolean condition (θ\theta).
EMPLOYEEEMPLOYEE.Dept_ID=DEPARTMENT.Dept_IDDEPARTMENTEMPLOYEE \bowtie_{EMPLOYEE.Dept\_ID = DEPARTMENT.Dept\_ID} DEPARTMENT

11.2 Equi-Join and Natural Join

  • Equi-Join: A Theta join where the condition only uses equality operators (`=`). The resulting table will have two identical columns (one from each table).
  • Natural Join (\bowtie): The ultimate, cleanest join. It automatically finds columns in both tables that have the exact same name, performs an Equi-Join on them, and then automatically strips out the duplicate column in the output. This is what we intuitively mean when we "join" tables.

Next — Outer Joins and Division

11 of 15

Page 12

Wink Notes

B.Tech CSE — 4th Semester

Database Management Systems

Unit - 2

12. Outer Joins and Division

12.1 Outer Joins

A standard Natural Join drops any tuples that don't find a match in the other table. If an Employee hasn't been assigned a Department yet, they disappear from the output.

Outer Joins preserve these unmatched tuples, filling the missing data with NULLs.

  • Left Outer Join (=\mathrel{=\kern{-0.5em}\bowtie}): Keeps every row from the Left table, regardless of a match.
  • Right Outer Join (=\mathrel{\bowtie\kern{-0.5em}=}): Keeps every row from the Right table.
  • Full Outer Join (==\mathrel{=\kern{-0.5em}\bowtie\kern{-0.5em}=}): Keeps every row from both tables.

    12.2 Division (÷\div)

    Used for queries involving the phrase "for all".
    Example: Find the students who have taken
    all courses offered by the CS department. The division operator STUDENT_COURSES÷CS_COURSESSTUDENT\_COURSES \div CS\_COURSES handles this mathematically complex requirement.

    Next — Relational Calculus Introduction

    12 of 15

    Page 13

    Wink Notes

    B.Tech CSE — 4th Semester

    Database Management Systems

    Unit - 2

    13. Relational Calculus

    While Relational Algebra is procedural (how to get data), Relational Calculus is Declarative (what data to get). You describe the properties the desired result should have, without giving the step-by-step procedure to compute it. It is based heavily on First-Order Predicate Logic.

    SQL is primarily based on Tuple Relational Calculus.

    13.1 Tuple Relational Calculus (TRC)

    Variables range over tuples.
    Syntax:
    {tP(t)}\{t \mid P(t)\}
    "Find all tuples
    tt such that predicate P(t)P(t) is true."

    Example: Find names of employees earning more than 50k.50k. \{t.Name \mid t \in EMPLOYEE \land t.Salary > 50000\}$

    TRC uses powerful mathematical quantifiers:
    *
    \forall (For All): Universal Quantifier.
    *
    \exists (There Exists): Existential Quantifier.

    Next — Domain Relational Calculus

    13 of 15

    Page 14

    Wink Notes

    B.Tech CSE — 4th Semester

    Database Management Systems

    Unit - 2

    14. Domain Relational Calculus (DRC)

    Unlike TRC where variables represent entire rows, in Domain Relational Calculus, variables represent individual column values (domains).

    14.1 DRC Syntax

    Syntax: {x1,x2,...,xnP(x1,x2,...,xn)}\{x_1, x_2, ..., x_n \mid P(x_1, x_2, ..., x_n)\}
    Where
    xix_i are domain variables.

    Example: Find names of employees earning more than 50k.50k. \{n \mid \exists i, a, s ( \langle i, n, a, s \rangle \in EMPLOYEE \land s > 50000)\}$

    Here, i,n,a,si, n, a, s represent the attributes ID, Name, Age, Salary. The query asks for nn (Name) where there exists some combination of values that form a valid row in the EMPLOYEE table, and the salary ss is greater than 50000.

    While highly theoretical, Codd proved that Relational Algebra, TRC, and DRC are all mathematically equivalent in their expressive power (Codd's Theorem). Anything you can query in one, you can query in the others.

    Next — Summary Checklist

    14 of 15

    Page 15

    Wink Notes

    B.Tech CSE — 4th Semester

    Database Management Systems

    Unit - 2

    15. Summary Checklist

    Unit 2 requires absolute mastery of mathematical notation and constraint theory.

    15.1 University Exam Checklist

    • Define the terms Relation, Tuple, Attribute, Degree, and Domain.
    • List the essential properties that distinguish a mathematical Relation from a simple table.
    • Explain Domain, Entity, and Referential Integrity Constraints in detail.
    • What is a Foreign Key? Why is it necessary?
    • Given an ER Diagram containing a Many-to-Many relationship and a Multi-Valued attribute, convert it into standard Relational Tables.
    • Explain the difference between the SELECT (σ\sigma) and PROJECT (π\pi) operations in Relational Algebra.
    • Write the Relational Algebra queries for finding specific data using Natural Join, Select, and Project.
    • Explain the difference between a Theta Join, Equi-Join, and Natural Join.
    • What is the difference between Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC)?

    15 of 15

    Continue in this subject