0% found this document useful (0 votes)
4 views83 pages

UNIt - 2 Dbms (Relational Calculus)

This document covers the fundamentals of the Relational Model in Database Management Systems, including concepts such as tables, attributes, primary keys, and foreign keys. It discusses relational algebra and calculus, integrity constraints, advantages and disadvantages of the relational model, and types of relational query languages. Key topics include the 12 rules defined by E.F. Codd, set operations, and various operators used in relational algebra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views83 pages

UNIt - 2 Dbms (Relational Calculus)

This document covers the fundamentals of the Relational Model in Database Management Systems, including concepts such as tables, attributes, primary keys, and foreign keys. It discusses relational algebra and calculus, integrity constraints, advantages and disadvantages of the relational model, and types of relational query languages. Key topics include the 12 rules defined by E.F. Codd, set operations, and various operators used in relational algebra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

DBMS

UNIT 2

Relationship Algebra and Calculus

•Relationship Algebra Selection and Projection.

•Set Operations, Renaming, Joints, Division.

•Relation Calculus, Expressive Power of Algebra


and Calculus.
Topics

•Relational Model
•Relational Model Concepts
•Relational Integrity Constraints
•Adv. And Disadvantages of
Relational Model
Relational Model
• It was introduced by Dr. E.F. Codd in 1970 and forms the foundation of
relational database systems like MySQL, PostgreSQL, Oracle, and SQL
Server.
• The Relational Model in Database Management Systems (DBMS) organizes
data into tables (relations) with rows and columns.

• It is a method of organizing data into a collection of interconnected


tables(relations), where each table represents a specific entity with
its attributes (columns) and records (rows), and relationships
between entities are established using keys like primary and foreign
keys, allowing for structured and efficient data storage and retrieval.
• essentially, it's a way to represent data as a set of related tables,
making it easy to query and manipulate information using Structured
Query Language (SQL).
• E.F. Codd, the father of relational databases, introduced 12 rules (actually 13, including
Rule 0) to define what qualifies as a Relational Database Management System (RDBMS).
These rules ensure data integrity, consistency, and proper relational behavior.
E.F. Codd’s 12 Rules –
1. Information Rule – Data must be stored in tables with rows and columns.
2. Guaranteed Access Rule – Each data item must be uniquely accessible using a
combination of table, primary key, and column.
3. Systematic Treatment of NULLs – NULL values must be properly handled for missing or
unknown data.
4. Dynamic Online Catalog – Database metadata must be stored as tables and be
queryable like normal data.
5. Comprehensive Data Sub-language Rule – The database must support a complete
language like SQL for all operations.
6. View Updating Rule – Virtual tables (views) must be updatable if logically possible.
7. High-Level Insert, Update, and Delete – Operations should work on sets of rows, not
just single records.
8. Physical Data Independence – Changing storage methods should not affect data access.
9. Logical Data Independence – Changing table structures should not impact applications
or queries.
10. Integrity Independence – Constraints like keys and validations must be enforced by
the database itself.
11. Distribution Independence – The system should function the same way whether data
is centralized or spread across multiple locations.
12. No subversion Rule – No bypassing of integrity rules via low-level access methods.
Relational Model Concepts

• Tables – In the Relational model the, relations are saved in the table
format. It is stored along with its entities. A table has two properties rows
and columns. Rows represent records and columns represent attributes.

• Attribute: Each column in a Table. Attributes are the properties which


define a relation. e.g., customer_id, customer_na.me etc.

• Tuple – It is nothing but a single row of a table, which contains a single


record.

• Relation Schema: A relation schema represents the logical


representation of entire database.
Customer(customer_id, customer_name, status)
Relational Model Concepts
• Degree: The total number of attributes which in the
relation is called the degree of the relation.

• Cardinality: Total number of rows present in the Table.

• Column: The column represents the set of values for a


specific attribute.

• Relation instance – Relation instance is a finite set of tuples


in the RDBMS system. Relation instances never have
duplicate tuples.

• Attribute domain – Determines the type of data values that


are permitted for that attribute.
Relational Model Concepts
Employee Table
Relational Model Concepts
Primary key
Primary Key
A primary key is a column (or a set of columns) in a database table that
uniquely identifies each row in that table.
It ensures that no two rows can have the same value for the primary
key, enforcing uniqueness.
It cannot contain NULL values because it must always identify a row.
Example: In a table called "Students," a column like "Student_ID" could
be the primary key if every student has a unique ID.

Characteristics:
1. Uniqueness: No duplicate values allowed.
2. Non-null: Must always have a value.
foreign key
A foreign key is a column (or set of columns) in one table that refers to the
primary key of another table.
It establishes a relationship between two tables, enforcing referential
integrity (i.e., the foreign key value must match an existing primary key value
in the referenced table or be NULL if allowed).
It helps maintain consistency across related tables.

Characteristics:
1.Can contain duplicate values (unlike a primary key).
2.Can be NULL (if the relationship is optional).
3.Links to the primary key (or a unique key) of another table.
Primary key and Secondary key
Relational Integrity constraints
• Relational Integrity Constraints in a Relational Database
Management System (RDBMS) are rules that ensure the accuracy,
consistency, and reliability of data within the database.
• They are fundamental to maintaining the integrity of relationships
between tables and enforcing the structure of the relational model.
• These constraints are applied to the tables, columns, and
relationships to prevent invalid data from being inserted, updated,
or deleted.
Mainly 3 types of constraints
1. Domain constraints
2. Key constraints
3. Referential integrity constraints
1. Domain Constraints
Definition: Ensures that all values in a column fall within a
defined domain (i.e., a set of acceptable values).
How it’s Applied : Through data types, check constraints, and
default values.
Examples:
A column age might be restricted to integers between 0 and 150.
A column status might only allow values like 'Active', 'Inactive', or
'Pending’.

Age must be
integer
Emp id Ename Age
1 A 24
2 B x1 DOMAIN
CONSTRAINT
2. Key constraints
Definition: Ensures uniqueness of records in a table using keys like primary keys or
unique keys.

Types:
Primary Key: Uniquely identifies each row; no NULLs or duplicates.
Unique Key: Ensures all values are unique but allows NULLs (depending on the
RDBMS).
Example:
A table Users might have user_id as the primary key and email as a unique key.
No two users can have the same email, but email could be NULL (in some systems).

empid Ename
Empid=PK
1 A
2 B
1 C

KEY CONSTRAINT
MUST BE UNIQUE
3. Referential integrity constraints
Referential integrity refers to the accuracy and consistency of data within
a relationship.
Referential integrity is a key concept in relational databases that ensures
foreign key values in a related table must match primary key values in the
primary table. This prevents orphaned records and maintains data
consistency.

Definition: Ensures that relationships between tables remain consistent.


Specifically, a foreign key (FK) in one table must either match a primary key
value in another table or be NULL.
How it’s Applied : By defining foreign key constraints.
Rules: A foreign key value must exist in the referenced table’s primary key
column (or unique key column), or it must be NULL.
Prevents orphaned records (e.g., a row in a child table with no
corresponding row in the parent table).
Example:
Primary Table (Company Table) with CompanyId as the Primary Key.
Related Table (Product Table) with CompanyId as a Foreign Key referencing the Primary Table.
✅ Valid Record: If CompanyId = 1 exists in both tables, the relationship is maintained.
❌ Orphaned Record: If CompanyId = 15 exists in the Related Table but not in the Primary
Table, it violates referential integrity.
How to Applied - Use FOREIGN KEY constraints to ensure valid references.
Use ON DELETE CASCADE to automatically remove child records when a parent is deleted.
Always insert data into the Primary Table first before referencing it in the Related Table.
Maintaining referential integrity ensures data accuracy, consistency, and prevents broken
relationships in databases.
Advantages of Relational Model

1. Data Integrity and Consistency: The use of keys (primary and foreign) ensures
relationships between tables are maintained, reducing redundancy and enforcing
referential integrity.

2. Simplicity: Data is organized into tables with rows and columns, making it intuitive
and easy to understand, even for non-technical users.

3. Flexibility: Queries can be written (e.g., using SQL) to retrieve data in various
ways without altering the underlying structure.

4. Scalability: The model supports large datasets and can be extended by adding
more tables or relationships as needed.

5. Standardization: It’s widely adopted, and tools like SQL provide a universal
language for interacting with relational databases.

6. Security: Access can be controlled at the table, row, or column level, ensuring
data protection.
Disadvantages of Relational Model

1. Complexity in Design: Setting up a relational database requires careful


planning (e.g., normalization), which can be time-consuming and complex for
large systems.

2. Performance Issues: For very large datasets or complex queries with multiple
joins, performance can degrade without optimization (e.g., indexing).

3. Rigid Structure: The fixed schema (tables and columns) can make it harder to
adapt to changing requirements compared to NoSQL models.

4. Scalability Limits: While scalable vertically (better hardware), horizontal


scaling (across multiple servers) can be challenging compared to non-relational
databases.

5. Data Redundancy: Although normalization reduces redundancy,


denormalization (sometimes used for performance) can reintroduce it.
Relational Query Language in DBMS
• Relational Database systems are expected to be equipped with a
query language that assists users to query the database.
• Relational Query Language is used by the user to communicate with
the database user requests for the information from the database.
• Relational algebra breaks the user requests and instructs the DBMS
to execute the requests. It is the language by which the user
communicates with the database.

• Types of Relational Query Language

1. Procedural Query Language

2. Non-Procedural Language
1. Procedural Query Language
• In Procedural Language, the user instructs the system to perform a
series of operations on the database to produce the desired results.
Users tell what data to be retrieved from the database and how to
retrieve it.
Relational Algebra is a Procedural Query Language.

2. Non-Procedural Language
• In Non Procedural Language user outlines the desired information
without giving a specific procedure or without telling the steps by step
process for attaining the information. It only gives a single Query on
one or more tables to get .The user tells what is to be retrieved from
the database but does not tell how to accomplish it.
Relational Calculus is a Non Procedural Language .
Relational Algebra
Relational Algebra is a procedural query language or formal query
language, which takes instances of one or more relation as an input and
generates a new relation as an output. It uses a different set of operators
(like unary or binary operators) and operands to perform queries.

• Relational Algebra is a theoretical query language used in relational


databases. It provides a set of mathematical operations to manipulate and
retrieve data stored in tables (relations).
• Key Characteristics of Relational Algebra
✔ Procedural Query Language – Specifies how to retrieve data step-by-step.
✔ Uses Set Theory & Logic – Operations are based on sets, unions,
intersections, etc.
✔ Foundation for SQL – SQL queries internally use relational algebra concepts.
✔ Works with Relations (Tables) – Each operation takes one or more tables as
input and produces a new table as output.
Relational Algebra Operators
Unary Operators – Relational Algebra

• In Relational Algebra, unary operators are


operations that work on a single relation (table) at a
time.
Some of the key unary operators in relational algebra
are:
1. Selection Operator(σ)
2. Projection Operator(ℼ)
3. Rename Operator (ρ)
1. Selection Operator(σ)
• The Selection operator (σ - Sigma) in Relational Algebra is used to filter rows
from a relation (table) based on a given condition. It selects only those tuples
(rows) that satisfy the specified condition.
Predicate in Relational Algebra
• A predicate in relational algebra is a logical condition that is used to filter
tuples (rows) from a relation (table). It is commonly used in the selection (σ)
operation to retrieve only those tuples that satisfy a given condition.
Projection (π) Operation
Rename operation- ρ (rho)

• The rename operation in relational algebra is denoted by ρ (rho) and


is used to rename a relation (table) or its attributes (columns).

Types of RENAME
• Renaming can be used by three methods, which are as follows −
• Changing name of the relation.
• Changing name of the attribute.
• Changing both.
Question – Cosidering this table find out these.

1. Find students who are in the CSE branch


2. Get only the Name and Age of students.
3. Find the names of CSE students.
Binary Operator - binary operators are operations
that take two relations as input and produce a new
relation as output.

1. Union
2. Cartesian
3. Set difference
1. Union Operator (∪)
• The Union (∪) operator is used to combine the result sets of two
relations while removing duplicate tuples.
Conditions for UNION:
• Both relations must have the same number of attributes
(columns).
• The attributes must have the same domain (data type).
Symbol: R∪S

Example:
If R = { (1, A), (2, B) } and S = { (2, B), (3, C) }, then:
• R∪S={(1,A),(2,B),(3,C)}
Query - Students_2024 ∪ Students_2025

Result – here duplicate tuple is removed


Cross Product/ Cartesian Product(×)
The Cross Product (Cartesian Product, ×) operation combines
every tuple from the first relation with every tuple from the
second relation.
Symbol: R×S
Key Points:
•It produces a new relation with all possible combinations of
tuples from both tables.
•The number of resulting tuples = (rows in first table) × (rows in
second table).
•It increases the number of rows significantly, so it’s usually
followed by a Selection (σ) or Join (⨝) operation to filter
relevant data.
Query - Students × Courses
Result
3. Set Difference Operator (−)
The Set Difference (−) operator is used to find tuples that are in
one relation but not in another. It helps in queries like finding
students who haven't enrolled in a course or employees who are
not in a project.
Symbol: R−S
Condition: Both relations must be union-compatible.
Example: If R = { (1, A), (2, B) } and S = { (2, B), (3, C) }, then:
R−S={(1,A)}
Students − Enrolled_Students
Extended Operators - Extended (or derived)
operators in relational algebra are built using
the basic operators (selection, projection,
Cartesian product, union, etc.)

1.Join Operator
2.Intersection Operator
3.Division Operator
1. Join Operator
A join is an operation that combines the rows of two or
more tables based on related columns. This operation is
used for retrieving the data from multiple tables
simultaneously using common columns of tables.

The main purpose of Join is to retrieve the data from


multiple tables in other words Join is used to perform
multi-table queries. It is denoted by ⨝.

Symbol: 𝑅⋈𝑆
Inner Joins
In relational algebra, an inner join combines two relations
based on a condition, returning only the matching tuples
(rows) where the condition is true, effectively creating a
new relation with only the common data.

In Relational Algebra, an Inner Join is represented using the ⋈


(Join) operator and is defined as a combination of Cartesian
Product (×) followed by Selection (σ).
Types of Inner Joins
1. Theta Join
2. EQUI Join
3. Natural Join
1. Theta Join (⋈θ)

Definition:
Theta Join (⋈₍θ₎) is a type of inner join where two relations
(tables) are joined based on a condition (θ) that can use any
comparison operator, such as:
= (equal)
≠ (not equal)
> (greater than)
< (less than)
≥ (greater than or equal to)
≤ (less than or equal to)

Syntax - R1 ⋈θ R2
where θ is the join condition/predicate.
Find students whose age is greater than or equal to the minimum age for
eligibility.

STUDENTS ⋈₍STUDENTS.Age ≥ ELIGIBILITY.Min_Age₎ ELIGIBILITY


2. Equi Join
Equi Join is a type of Inner Join where the condition for matching rows is based
solely on equality. meaning the operator used is the equals sign (=). It combines
rows from two tables based on matching values in specified columns.
3. Natural Joins
A Natural Join is a type of Inner Join where the join condition is implicitly based on the
common attribute names in both relations. It automatically removes duplicate
columns.
It Combines two relations based on all columns with the same names. It automatically
eliminates duplicate columns from the result.
Applying Natural join
Comparison
Outer Joins
In relational algebra, an outer join is an extension of the join
operation that ensures all tuples (rows) from one or both relations
(tables) are included in the result, even if there are no matching
tuples in the other relation.
This is particularly useful when you want to preserve data that
might otherwise be excluded due to missing matches in a
standard (inner) join. There are three types of outer joins: left
outer join, right outer join, and full outer join.

Types of Outer Joins:


1.Left Outer Join (⟕)
2.Right Outer Join (⟖)
3.Full Outer Join (⟗)
Definitions
1.Left Outer Join (⟕): Includes all tuples from the left relation, and the matched
tuples from the right relation. If no match exists, NULL values are appended for
attributes from the right relation.
2.Right Outer Join (⟖): Includes all tuples from the right relation, and the matched
tuples from the left relation. If no match exists, NULL values are appended for
attributes from the left relation.
3.Full Outer Join (⟗): Includes all tuples from both relations, with NULLs in places
where there is no match.
Here, the StudentID in R is a primary key, and StudentID in S is a foreign key linking students to
their enrolled courses.
Note that Carol (StudentID 3) has no courses, and there’s no student with a course that
doesn’t exist in R
2. Intersection Operator
The Intersection (∩) operation retrieves only the
common tuples from two relations.
It is useful in scenarios like finding students who are
enrolled in two different courses, employees who work in
multiple departments, etc.
Symbol: 𝑅∩𝑆
Description: Retrieves common tuples between two
relations.
Condition: Both relations must be union-compatible.
Example: If R = { (1, A), (2, B) } and S = { (2, B), (3, C) },
then:𝑅∩𝑆={(2,𝐵)}
3. Division Operator
In relational algebra, the division operator (denoted by ÷) is used to
find tuples in one relation that are associated with all tuples in
another relation.
It’s a less commonly used operator compared to joins or projections,
but it’s powerful for specific types of queries, such as identifying "for
all" relationships.
1. Find student names along with their course names using Natural Join.
2. Find students who are taking the "DBMS" course.
3. Find students who are not enrolled in any course.
4. Find students who are taking courses taught by "Dr. Smith".
Find student names along with their course
names using Natural Join
Find students who are taking the "DBMS" course.
Find students who are not enrolled in any course.
Find students who are taking courses taught by "Dr.
Smith".
S.NO. Training Domain Technologies Used Subject Knowledge Required
HTML, CSS, JavaScript, React, Web Technologies, DBMS, Basic
1 Full Stack Web Development
Node.js, MongoDB Programming
Python Programming with
2 Python, Tkinter, Flask Basic Programming
Projects
3 Java with DSA Java, Collections Framework OOPs, Data Structures
C++ with Competitive
4 C++, STL Data Structures, Algorithms
Programming
5 Mobile App Development Flutter, Dart, React Native OOPs, Mobile Basics
6 AI/ML Python, scikit-learn, TensorFlow Maths, Linear Algebra, Statistics
7 Data Science Python, Pandas, Matplotlib Statistics, Data Handling
Arduino, Raspberry Pi, C/C++, Basic Electronics,
8 IoT
Sensors Microcontrollers
Cryptography, Distributed
9 Blockchain Development Solidity, Ethereum, Web3.js
Systems
10 Cloud & DevOps AWS, Docker, Jenkins Computer Networks, OS
Kali Linux, Wireshark,
11 Cybersecurity Computer Networks, OS
Metasploit
12 UI/UX Design Figma, Adobe XD Human-Computer Interaction
13 Software Testing Selenium, JUnit Software Engineering
Data Handling, Business
14 Power BI & Visualization Power BI, Excel
Understanding

You might also like