RDBMS - Unit 1 Notes
RDBMS - Unit 1 Notes
E.F. Codd introduced the Relational Model to organize data as relations or tables. After creating
the conceptual design of a database using an ER diagram, this design must be transformed into a
relational model which can then be implemented using relational database systems like Oracle
SQL or MySQL.
Example:
Let’s say we have a table called Student:
2. Tuple:
Example:
In the Student relation above:
3. Attribute:
Example:
In the Student relation:
2. Relational Schema:
A relation schema describes the structure of a relation, including its attributes and domains.
Notation: R(A1, A2, ..., An)
Example: Student(RollNo, Name, Age, Dept)
3. Keys:
Superkey: A set of one or more attributes that uniquely identifies a tuple.
Candidate key: A minimal superkey — no subset of it can uniquely identify a tuple.
Primary key: A chosen candidate key used to identify tuples uniquely.
Foreign key: An attribute in one relation that refers to the primary key of another relation —
establishes a relationship between tables.
4. Integrity Constraints:
Domain constraints: Each attribute's value must lie within a specified domain.
Key constraints: No two rows can have the same value for the primary key.
Entity integrity: Primary keys cannot be null.
Referential integrity: A foreign key must either be null or match a value in the referenced relation’s
primary key.
5. Operations:
Select (σ): Filters rows based on a condition.
Project (π): Retrieves specific columns (attributes).
Union (∪): Combines tuples from two relations (both must have the same schema).
Set difference (-): Returns tuples in one relation but not in another.
Cartesian product (×): Combines all tuples from two relations.
Join: Combines related tuples from two relations based on a condition.
6. Advantages of the Relational Model:
Simplicity: Easy-to-understand tabular structure.
Flexibility: Supports powerful query languages like SQL.
Data integrity: Ensures accuracy through constraints.
Scalability: Efficient for large datasets.
Notation:
σcondition(R)
σ is the selection operator.
R is the relation (table).
The condition is applied to each row.
Example:
Given the Student table:
σDept=’CSE’(Student)
Result:
2. Projection (π):
Notation:
Π attribute1, attribute2,…(R)
π is the projection operator.
It produces a new relation with only the specified columns.
Example:
To get only the names and departments of students:
Name Dept
Harry CSE
Hermione ECE
Ron CSE
3. Union (∪):
Notation:
R S
Works only if R and S have the same attributes (compatible schemas).
Example:
Student1:
RollNo Name
101 Harry
102 Hermione
Student2:
RollNo Name
102 Hermione
103 Ron
Student1 Student2
Result:
RollNo Name
101 Harry
RollNo Name
102 Hermione
103 Ron
Purpose: Returns rows that are in one relation but not in the other.
Notation:
R−S
Example:
Student1−Student2
Result:
RollNo Name
101 Harry
Purpose: Combines every row of one relation with every row of another.
Notation:
R×S
Example:
Student:
RollNo Name
101 Harry
102 Hermione
Course:
Course
Math
Physics
Student×Course
Result:
Extended operators are those operators which can be derived from basic operators.
There are mainly three types of extended operators in Relational Algebra:
Join
Intersection
Divide
Notation:
R∩S
R and S are two relations (tables) with the same set of attributes (they must have the
same schema for intersection to work).
Example:
Consider two relations:
Student1:
RollNo Name
101 Harry
102 Ron
103 Hermione
Student2:
RollNo Name
102 Ron
103 Hermione
104 Draco
Student1∩Student2
Result:
RollNo Name
102 Ron
103 Hermione
Notation:
R÷S
R (dividend relation): Contains extra attributes compared to S.
S (divisor relation): Contains a subset of attributes from R.
Result: The division returns all tuples from R that have corresponding matches for every tuple
in S.
Example:
Tables:
Student Course
Harry Math
Harry Physics
Ron Math
Hermione Math
Hermione Physics
Course
Math
Physics
Query:
Find students who are enrolled in all courses listed in the Courses table:
StudentCourse ÷ Courses
Result:
Student
Harry
Hermione
1. Identify matches:
o Look at each Student in StudentCourse and check if they are associated with
every Course in Courses.
2. Return qualified tuples:
o If a student has all courses from Courses, they are included in the result.
Join Operation
The Join operation in relational algebra combines related tuples from two
relations (tables) into a single result set.
It merges rows based on a common attribute or condition.
The general format is:
R S
Where R and S are relations (tables).
Types of Joins:
R⋈θS
Example:
Consider these tables:
Employee:
Department:
DeptID DeptName
101 HR
102 IT
Employee⋈Employee.DeptID = Department.DeptID
Result:
EmpID Name DeptID DeptName
1 Harry 101 HR
2 Ron 102 IT
2. Equi Join:
A special case of Theta Join where the condition is only equality (=).
Same example as above since it uses the condition:
Employee.DeptID = Department.DeptID
Notation:
R⋈S
Example:
Employee⋈Department
Result (duplicates merged):
4. Outer Joins:
Include rows even when there's no match — filling with nulls when needed.
Left Outer Join (⟕): All rows from R + matching rows from S
Right Outer Join (⟖): All rows from S + matching rows from R
Full Outer Join (⟗): All rows from R and S (null where no match)
Example:
1. Equi Join:
An Equi Join combines rows from two tables based on a specific condition
of equality (=) between attributes.
You explicitly specify the condition for matching.
Syntax:
R⋈conditionS
Example:
Employee Table:
Department Table:
DeptID DeptName
101 HR
102 IT
Result:
Key points:
✅ 2. Natural Join:
Syntax:
R⋈S
Example:
Employee ⋈ Department
Result:
Key points: