DBMS (LONG 12pm)
DBMS (LONG 12pm)
Q1) Discuss different operations in relational algebra? Explain each operation by giving
suitable example.
Ans) Relational Algebra Operations
Relational algebra provides a set of theoretical operations to manipulate data in relational
databases. Here's a breakdown of some fundamental operations:
1. Selection (σ): This operation filters rows based on a specific condition. It's denoted
by σ followed by a predicate (p) and the relation (r). The predicate defines the
selection criteria using comparison operators (<, >, =, etc.) and logical operators
(AND, OR, NOT).
Example:
Consider a table Students(id, name, major) and you want to find students majoring in
Computer Science (CS).
σ(major = 'CS')(Students)
This expression selects all rows from Students where the major attribute equals 'CS'.
2. Projection (π): This operation selects specific columns from a relation, discarding the
rest. It's denoted by π followed by a comma-separated list of attributes (A) to be
included in the result.
Example:
Continuing with the Students table, suppose you only need student names.
π(name)(Students)
This expression projects only the name column, resulting in a table with just student names.
3. Union (U): This operation combines rows from two compatible relations (having the
same schema). It includes duplicates and eliminates no data.
Example:
Imagine tables Enrolled_CS(student_id, course_name) and Enrolled_Math(student_id,
course_name). You want all courses offered (CS and Math).
Enrolled_CS ∪ Enrolled_Math
This expression combines courses from both tables, potentially containing duplicates if
students are enrolled in courses from both departments.
4. Set Difference (-): This operation finds rows present in one relation (r) but not in
another (s), both having the same schema.
Example:
Say you want CS courses offered but not taken by any student yet (assuming an empty
Enrolled_CS table initially).
Courses_Offered - Enrolled_CS
This expression identifies courses offered in a hypothetical Courses_Offered table that are not
yet enrolled in by any student.
5. Cartesian Product (x): This operation creates a new relation by combining all rows
from one relation with all rows from another. The resulting relation has all columns
from both input relations.
Example:
Consider tables Students(id, name) and Courses(course_id, name). You might want to create a
temporary relation listing potential student-course combinations.
Students x Courses
This expression creates a new relation with every student paired with every course, which
might be useful for further processing like enrollment planning.
These are just a few core operations in relational algebra. Remember, relational algebra
expressions can be combined to achieve complex data manipulation tasks.