DBMS 13
DBMS 13
The attribute domains (types of values accepted by attributes) of both the relations
must be compatible.
1. Union Operator (∪)-
Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two
tables (relations).
Let’s say we have two relations R1 and R2 both have same columns and we want to select
all the tuples (rows) from these relations then we can apply the union operator on these
relations.
Let R1 and R2 be two relations.
Then-
R1 ∪ R2 is the set of all tuples belonging to either R1 or R2 or both.
In R1 ∪ R2, duplicates are automatically removed.
Note: The rows (tuples) that are present in both the tables will only appear once in the
union set. In short you can say that there are no duplicates present after the union
operation.
Syntax of Union Operator (∪)
table_name1 ∪ table_name2
Union Operator (∪) Example:
Table 1: COURSE
Course_Id Student_Name Student_Id
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT
Student_Id Student_Name Student_Age
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
∏ Student_Name (COURSE) ∪ ∏ Student_Name (STUDENT)
Output:
Student_Name
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even though we
had few common names in both the tables, also in the COURSE table we had the duplicate
name itself.
Note: Only those rows that are present in both the tables will appear in the result set.
Syntax of Intersection Operator (∩)
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Let’s take the same example that we have taken above.
Table 1: COURSE
Course_Id Student_Name Student_Id
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT
Student_Id Student_Name Student_Age
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)
Output:
Student_Name
Aditya
Steve
Paul
Lucy
Relation R1 – R2
Table 2: S
Col_X Col_Y
XX 99
YY 11
ZZ 101
Query:
Let’s find the Cartesian product of table R and S.
RXS
Output:
Col_A Col_B Col_X Col_Y
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
Note: The number of rows in the output will always be the cross product of number of rows
in each table. In our example table 1 has 3 rows and table 2 has 3 rows so the output has
3×3 = 9 rows.
Rename (ρ):
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.
Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl