relational algebr
DESCRIPTION
It include the topic Relational algebra, which is very useful for the beginners.TRANSCRIPT
![Page 1: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/1.jpg)
![Page 2: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/2.jpg)
![Page 3: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/3.jpg)
![Page 4: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/4.jpg)
•Two formal language for the relational model
(a)Relational Algebra
(b)Relational calculus
![Page 5: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/5.jpg)
RELATIONAL ALGEBRA
• Basic set of operation for the relational model.
• These operation enable a user to specify basic retrieval requests.
• The result of a retrieval is a new relation, which can be further manipulated using operations of same algebra.
•it is important for several reasons: provides formal foundation for relational
model used as basis for implementing and
optimizing queries in RDMS
![Page 6: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/6.jpg)
No commercial RDBMS in use today provides an interface for the relational queries.
•Relational algebra operations can be divided into
UNION INTERSECTION SET DIFFERENCECARTESIAN PRODUCT
SELECTPROJECTJOIN etc.
![Page 7: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/7.jpg)
Unary relational operations
•SELECT : s•PROJECT: p
:Operate on a single relation
![Page 8: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/8.jpg)
(a)SELECT : s
Syntax :
s <selection condition> (R)
<selection condition> is made up of number of clauses of the form
<attribute name > <comparison op> <attribute name> OR<attribute name > <comparison op> <constant value>
{ =,<,>,<=,>=,!= }
It select certain rows
![Page 9: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/9.jpg)
Ex : s(DNO=4 ) (EMPLOYEE)
Ex : s(SALARY>25000) (EMPLOYEE)
Lets try. . .
![Page 10: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/10.jpg)
Clauses can be connected by the Boolean operators and, or ,not to from a general selection condition.
s(DNO=4 AND SALARY>25000) OR (DNO=5 AND SALARY>30000) (EMPLOYEE)
Answer?????
![Page 11: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/11.jpg)
SELECT operation is commutative :
s <cond1> ( s <cond2> (R)) = s <cond2> ( s <cond1> (R))
Cascade of SELECT operation:
s<cond1> ( s <cond2> (… ( s <condn> (R)). . . ))
=( s <cond1> AND <cond2> AND . . . AND <condn> (R)
![Page 12: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/12.jpg)
(b)PROJECT :p
Syntax :
p <attribute list > (R)
It select certain columns
Duplicate elimination : it removes any duplicate tuples , and we get a valid relation .
![Page 13: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/13.jpg)
•pSEX, SALARY(EMPLOYEE)•p,Lname,Fname,SALARY(EMPLOYEE)
What is the resultant table ???
![Page 14: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/14.jpg)
Duplicate elimination
pFNAME, LNAME, SALARY(sDNO=5(EMPLOYEE))
I WANT ANSWERS. . NOT QUESTIONS.
![Page 15: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/15.jpg)
Sequence of operations &
RENAME operation
![Page 16: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/16.jpg)
We can write the operations as a single algebra expression by nesting the operation or we can apply one operation at a time and create intermediate result relations.
pFNAME, LNAME, SALARY(sDNO=5(EMPLOYEE))
Can replace with
DEPCS2 sDNO=5(EMPLOYEE)RESULT pFNAME, LNAME, SALARY (DEPCS2)
![Page 17: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/17.jpg)
Renaming of attribute TEMP sDNO=5(EMPLOYEE)R(FIRSTNAME,LASTNAME,SALARY) pFNAME, LNAME, SALARY (TEMP)
![Page 18: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/18.jpg)
A formal RENAME r(rho) operations:• rename either relation name, attribute name or both
r (B1,B2,…Bn) (R)r s (R)
r s(B1,B2,…Bn) (R)S: new table name;Bn: new attribute name
![Page 19: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/19.jpg)
UNION ,INTERSECTION & SET DIFFERENCE
UNION
INTERSECTION
SET DIFFERENCE
: R S: Include all tuples:Duplicates are eliminated
: R S: include all tuples that are in both R & S
: R - S: include all tuples that are in R but not in S
![Page 20: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/20.jpg)
Example:Union compatible• same degree• corresponding pair have same number
A BA B
A - B B - A
![Page 21: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/21.jpg)
Q. Retrieve the social security Number of all employees who either work in department 5 or directly supervise an employee who works department 5.
![Page 22: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/22.jpg)
•UNION and INTERSECTION are commutative but SET DIFFERENCE is not.
R S =R S – (R-S )-(S-R)
![Page 23: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/23.jpg)
![Page 24: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/24.jpg)
HELLOOOOOOO. .
![Page 25: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/25.jpg)
CARTESIAN PRODUCT
• Also known as CROSS PRODUCT or CROSS JOIN• Denoted by X• syntax :
• tab1 X tab2• also binary set operations.• if A has m tuples and B has n tuples then A X B has m * n tuples .• Cartesian product followed by SELECT is used to identify and select related tuples from two relation.
![Page 26: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/26.jpg)
EMP_ID DESIGNATION SALARY
101 PROFESSOR 1.25000
102 ASSOCIATE PROF 90000
103 ASSISTANT PROF 40000
E_ID F_NAME M_NAME
L_NAME AGE
102 SURESH KUMAR S 43
103 SUMI M MOL 23
101 SHREELEKSHMI R S 40
TAB2
TAB1
FIND salary ,name AND designation OF EACH EMPLOYEE?????
![Page 27: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/27.jpg)
DESIGNATION SALARY
EMP_ID E_ID F_NAME M_NAME L_NAME AGE
PROFESSOR 1.25000
101 102 SURESH KUMAR S 43
PROFESSOR 1.25000
101 103 SUMI M MOL 23
PROFESSOR 1.25000
101 101 SHREELEKSHMI
R S 40
ASSOCIATE PROF
90000 102 102 SURESH KUMAR S 43
ASSOCIATE PROF
90000 102 103 SUMI M MOL 23
ASSOCIATE PROF
90000 102 101 SHREELEKSHMI
R S 40
ASSISTANT PROF
40000 103 102 SURESH KUMAR S 43
ASSISTANT PROF
40000 103 103 SUMI M MOL 23
ASSISTANT PROF
40000 103 101 SHREELEKSHMI
R S 40
![Page 28: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/28.jpg)
DESIGNATION
SALARY EMP_ID E_ID F_NAME M_NAME L_NAME AGE
PROFESSOR
1.25000 101 101 SHREELEKSHMI
R S 40
ASSOCIATE PROF
90000 102 102 ANIL S GANESH 28
ASSISTANT PROF
40000 103 103 SUMI M MOL 23
SALARY F_NAME M_NAME L_NAME DESIGNATION
1.25000 RAMESH R VINOD PROFESSOR
90000 ANIL S GANESH ASSOCIATE PROF
40000 SUNIL A NARAYAN ASSISTANT PROF
![Page 29: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/29.jpg)
Write relational algebra expression Retrieve a list of names of each employee’s dependents.
![Page 30: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/30.jpg)
![Page 31: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/31.jpg)
• Combine related tuples from two relation into single tuples.• denoted by • R <JOIN CONDITION> S .• Consider previous example
JOIN
RESULT EMPLOYEE X DEPENDENTFINAL sSSN= ESSN (RESULT)
The above can be replaced with
FINAL EMPLOYEE SSN=ESSN DEPENDENT .
![Page 32: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/32.jpg)
Retrieve the name of manager of each department
![Page 33: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/33.jpg)
![Page 34: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/34.jpg)
• A general join condition is of the form <condition>AND<condition>AND. . . AND <condition>
Each condition of the form
Ai Biᶱ Comparison operator {=,!=,<,>,<=,>=}
•A JOIN operation with such a general join condition is called a THETA JOIN
![Page 35: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/35.jpg)
Variations of JOIN
•EQUIJOIN , =•NATURAL JOIN , *
![Page 36: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/36.jpg)
EQUIJOIN• The JOIN where the only comparison operator used is ‘ = ‘
NATURAL JOIN • It requires that two JOIN attributes have the same name in both relation .• if this is not the case then renaming operation done first.• This is basically an EQUIJOIN followed by removal of unnecessary attributes•Ex: DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS.
![Page 37: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/37.jpg)
Join attribute
![Page 38: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/38.jpg)
•A JOIN operation used to combine data from multiple relations so that related information can be presented in a single table. This operations are also known as inner joins.
![Page 39: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/39.jpg)
DIVISION •Denoted by
![Page 40: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/40.jpg)
R S
![Page 41: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/41.jpg)
YEAR TOUR_PART_EM_ID
2003 1001
2004 1001
2013 1001
2008 1004
2009 1003
2008 1003
2004 1004
2011 1004
2003 1004
2003 1002
2004 1002
2011 1002
2013 1002
2012 1002
EMP_id NAME ADDRESS
1001 SURESH AAAA
1002 LALAN BBBB
1003 JC CCCC
1004 ANVAR DDDD
Retrieve the name & address of employees who participated in all tour that ‘Suresh ‘sir attended .
faculty
tour
![Page 42: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/42.jpg)
Retrieve the names of employees who work on all the project that ‘John Smith’ works on
![Page 43: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/43.jpg)
![Page 44: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/44.jpg)
![Page 45: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/45.jpg)
Additional Relational operations
•GENERALIZED PROJECTION• allowing functions of attributes to be included in the projection•EX: •pSALARY-DEDUCTION, SALARY * 0.25(sDNO=5(EMPLOYEE))
![Page 46: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/46.jpg)
•AGGREGATE FUNCTIONS & GROUPING
• SUM•AVERAGE •MAXIMUM•MINIMUM•COUNT
•Aggregate function operation using the symbol
<grouping attribute> <function list>(R)
Calligraphic G
![Page 47: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/47.jpg)
.a r R(DNO,NO_OF_EMPLOYEES,AVERAGE_SAL) (Dno COUNT ssn, AVERAGE salary(EMPLOYEE))b. Dno COUNT ssn, AVERAGE salary(EMPLOYEE)c. COUNT ssn, AVERAGE salary(EMPLOYEE)
![Page 48: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/48.jpg)
•OUTER JOIN
• Used when we want to keep all the tuples in R• three main variation are • LEFT OUTER JOIN• RIGHT OUTER JOIN• FULL OUTER JOIN
![Page 49: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/49.jpg)
• Relation instructor1
• Relation teaches1ID course_id
101011212176766
CS-101APL-101BIO-101
Comp. Sci.Applied
PED
ID dept_name
101011212115151
name
SreejithSureshSreeja
![Page 50: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/50.jpg)
• Join
instructor teachesID dept_name
1010112121
Comp. Sci.Applied
course_id
CS-101 APL-201
name
SreejithSuresh
![Page 51: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/51.jpg)
Left Outer Join
instructor teaches
ID dept_name
101011212115151
Comp. Sci.Applied
PED
course_id
CS-101 APL-201 null
name
SreejithSureshSreeja
ID course_id
101011212176766
CS-101APL-101BIO-101
Comp. Sci.Applied
PED
ID dept_name
101011212115151
name
SreejithSureshSreeja
![Page 52: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/52.jpg)
Right Outer Join
instructor teaches
ID dept_name
101011212176766
Comp. Sci.Applied
null
course_id
CS-101 APL-201 BIO-101
name
SreejithSureshnull
ID course_id
101011212176766
CS-101APL-101BIO-101
Comp. Sci.Applied
PED
ID dept_name
101011212115151
name
SreejithSureshSreeja
![Page 53: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/53.jpg)
Full Outer Join
instructor teaches
ID dept_name
10101121211515176766
Comp. Sci.Applied
PED null
course_id
CS-101 APL-201 null BIO-101
name
SreejithSureshSreejanull
ID course_id
101011212176766
CS-101APL-101BIO-101
Comp. Sci.Applied
PED
ID dept_name
101011212115151
name
SreejithSureshSreeja
![Page 54: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/54.jpg)
Additional• semi join• left semi join.• Right semi join:
Rno Name AGE
1 Mahati 40
2 Manjusha 42
3 Meenu 38
R_no Phone
1 9865321245
3 9764316598
5 9598946532
cs2 Tour_cs2
![Page 55: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/55.jpg)
•left semi join.• Right semi join:
Rno Name AGE
1 Mahati 40
3 Meenu 38
R_no Phone
1 9865321245
3 9764316598
![Page 56: Relational algebr](https://reader034.vdocuments.mx/reader034/viewer/2022052619/556adb12d8b42a1d668b5002/html5/thumbnails/56.jpg)
Thank you