UNIt - 2 Dbms (Relational Calculus)
UNIt - 2 Dbms (Relational Calculus)
UNIT 2
•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.
• 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.
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.
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
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.
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.
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. 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
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.
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.
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.