Professional Documents
Culture Documents
DBMS Third Chapter Part-2
DBMS Third Chapter Part-2
and
Relational Algebra
(part-2)
Chapter 3: Relational model and Relational
Algebra (T2: Page 173-200)
Resultant table: ?
• Comparison operator
• =, <, >, ≠, ≥,≤ for numeric value or date
• =, ≠ for strings of characters
• Constant value
– Value from attribute domain
Resultant table: ?
Resultant table: ?
Resultant table: ?
Resultant table: ?
Resultant table: ?
Resultant table: ?
Π empid (Employee))
Resultant table: ?
B.S.Shankar, Dept of MCA, VVIET 30
Project Operation: not Commutative
Employee
102 Empid
103 Π empid (Employee))
101
104 Employee 102
103
104
B.S.Shankar, Dept of MCA, VVIET 31
Sequence of operations Operation
Resultant table: ?
• Binary operation
• Both relational should be union compatible
• Union compatible has following characteristics:
– i.e. for R(A1, A2, ….An) & S(B1,B2,….Bn)
– dom(Ai) = dom(Bi) for 1 <= i <=n
– Degree of both relations should be same.
• UNION (RUS)
• INTERSECTION (R ∩S)
• MINUS (R-S)
• UNION (RUS)
– It includes all tuples that are either in R or S or in both
– Duplicate tuples are eliminated.
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 42
UNION
Student
Id Name Student U Instructor
101 Ram
Id Name
102 Shyam
103 Geeta 101 Ram
104 Rita 102 Shyam
103 Geeta
Instructor 104 Rita
Id Name 107 Smith
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 43
Set Theoretic Operation
• INTERSECTION (R∩S)
– It includes all tuples that are in R & S both
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 45
INTERSECTION
Student
Id Name
101 Ram Student ∩ Instructor
102 Shyam
Id Name
103 Geeta
104 Rita 102 Shyam
103 Geeta
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 46
Set Theoretic Operation
• (R-S)
– It includes all tuples that are in R but not in S
• (S-R)
– It includes all tuples that are in S but not in R
• (R-S) ≠ (S-R)
Id Name
101 Ram
102 Shyam
103 Geeta
104 Rita
Instructor – Student = ?
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 50
DIFFERENCE (S-R)
Student
Id Name
101 Ram
102 Shyam
Instructor - Student
103 Geeta
104 Rita Id Name
107 Smith
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 51
DIFFERENCE: not commutative
• Associative
• (R U (S U T) = (R U S) U T)
• (R ∩ (S ∩ T) = (R ∩ S) ∩ T)
Employee Dependant
Emp_id Name Dep_name Bdate
101 Ram
Meena 23-02-1988
102 Shyam
Raju 23-02-1990
103 Geeta
Employee х dependant = ?
employee х dependant
• JOIN
• DIVISION
• EQUI JOIN
– When comparison Operator used is =, is called an
EQUI JOIN
Employee
emp_id Name
Employee emp_id=eid Dependant
101 Ram
emp_id eid Name Dep_name
102 Shyam
101 101 Ram Meena
103 Geeta
102 102 Shyam Raju
Dependant
Dep_name eid
Meena 101
Raju 102
B.S.Shankar, Dept of MCA, VVIET 67
JOIN Operation
Dependant
Dep_name emp_id
Meena 101
Raju 102
B.S.Shankar, Dept of MCA, VVIET 69
JOIN Operation
Employee X Dependant = ?
R S R÷S
ESSN PNO PNO
12345 1
1
12345 2
2
23456 2
23456 1
35346 1
21336 2
B.S.Shankar, Dept of MCA, VVIET 77
Division
R S R÷S
ESSN PNO PNO ESSN
12345 1 12345
1
12345 2
2 23456
23456 2
23456 1
35346 1
21336 2
B.S.Shankar, Dept of MCA, VVIET 78
Notation for Query Trees
• ΠPnumber,Dnum,Lname,Bdate(Temp3)
• ΠPnumber,Dnum,Lname,Bdate(((σPlocation=‘Stafford’(PROJ
ECT))
⋈Dnum=Dnumber(DEPARTMENT))
⋈Mgr_SSN=SSN(EMPLOYEE))
(2) EMPLOYEE
⋈ E
P.Dnum=D.Dnumber
(1) D DEPARTMENT
σP.Plocation=‘Stafford’
P PROJECT
B.S.Shankar, Dept of MCA, VVIET 85
Additional Relational Operations
Relation Given
• EMPLOYEE (Ssn, Salary, Deduction, Years_service)
Result Expected
Report (Ssn, Net_salary, Bonus, Tax)
• Result:
R(DNO, COUNT_SSN, AVERAGE_SALARY)
• Result:
R(COUNT_SSN, AVERAGE_SALARY)
loan borrower
loan_number branch_name amount customer_name loan_number
loan borrower
loan_number branch_name amount customer_name loan_number
• FULL OUTER JOIN keeps all tuples present in both the left
and the right relations.
R S
Solution:
STAFFORD_PROJS<- σ PLOCATION=‘Stafford‘ (PROJECT)
Solution:
DEPT5_PROJS(PNO)<-ΠPNUMBER (σ DNUM=5(PROJECT))
Solution:
• SMITHS(ESSN) <- ΠSSN(σLNAME=‘Smith’(EMLPOYEE))
• SMITH_MGR_PROJS(PNO) <-ΠPNUMBER
(SMITH_MANAGED_DEPTS * PROJECT)
Solution:
• T1(SSN, NO_OF_DEPTS) <- ESSN ڻCOUNT DEPENDENT_NAME
(DEPENDENT)