# Kumarapathirana K. P. S. D. Section 3.0. Introduction The basic set of operations for the relational model is the relational algebra User specifies basic.

## Presentation on theme: "Kumarapathirana K. P. S. D. Section 3.0. Introduction The basic set of operations for the relational model is the relational algebra User specifies basic."— Presentation transcript:

Kumarapathirana K. P. S. D. Section 3.0

Introduction The basic set of operations for the relational model is the relational algebra User specifies basic retrieval requests The result of a retrieval is a new relation The algebra operations thus produce new relations

Introduction A sequence of relational algebra operations forms a relational algebra expression Why …….. ? –I–It provides a formal foundation for relational model operations –I–It is used as a basis for implementing and optimizing queries –S–Some of its concepts are incorporated into the SQL

Two types –O–Operations developed specifically for relational databases Eg:SELECT, PROJECT and JOIN –S–Set operations from mathematical set theory Eg: UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT

Two types –U–Unary operations Operate on single relation Eg:SELECT and PROJECT –B–Binary operations Operate on two relations Eg: JOIN, UNION, INTERSECTION, MINUS ….

Unary Relational Operations SELECT –T–This operation is used to select a subset of the tuples from a relation that satisfy a selection condition –A–A filter –T–The SELECT operation is a horizontal partition of the relation into two sets of tuples Those tuples that satisfy the condition and are selected Those tuples that do not satisfy the condition and are discarded.

–R–Representation σ (R) Boolean Expression

The relation resulting from the SELECT operation has the same attributes as R is made up of a number of clauses of the form or Clauses can be arbitrarily connected by the Boolean operators AND, OR, and NOT to form a general selection condition

Example : σ FNAME = Nimal (R) SELECT * FROM EMPLOYEE WHERE FNAME = ‘Nimal’ EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing

EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 22LalithVithanaNimalasena45Finance To select the EMPLOYEE tuples whose department is ‘Finance’,

EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance To select the EMPLOYEE tuples whose age is greater than 30

The way the result is determined ……. –<– is applied independently to each tuple t in R –I–If the condition evaluates to TRUE, then tuple t is selected –T–The Boolean conditions AND, OR, and NOT have their normal interpretation (cond1 AND cond2) is TRUE if both (cond1 ) and (cond2) are TRUE; otherwise, it is FALSE (cond1 OR cond2) is TRUE if either (cond1 ) or (cond2) or both are TRUE; otherwise, it is FALSE (NOT cond) is TRUE if cond is FALSE; otherwise, it is FALSE.

The number of tuples in the resulting relation is always less than or equal to the number of tuples in R |σ c (R)| <= |R| The SELECT operation is commutative σ (R)) = σ (R)) Hence, a sequence of SELECTs can be applied in any order

A cascade of SELECT operations can be combined into a single SELECT operation with a conjunctive (AND) condition σ (σ (….(σ (R)) = σ AND AND …. AND (R)

Exercise…. Find the students of IIT & CST degree program Find the 4 th year students whose degree program is SCT SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

PROJECT –S–Selects certain columns from the table and discards the other columns –v–vertical partition of the relation into two relations: One has the needed columns (attributes) and contains the result of the operation The other contains the discarded columns

–R–Representation ∏ (R)

To list each employee's employee id, first and last name EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage46Finance 34SunimalKeerthiPerera33Production 22LalithVithanaNimalasena45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAME 12NimalKumara 34SunimalKeerthi 22LalithVithana 35ChathuraShanaka 11WicramaSapumal

If the attribute list includes only non-key attributes of R –D–Duplicate tuples are likely to occur –P–PROJECT operation removes any duplicate tuples Duplicate elimination –E–Eg: ∏ LNAME,AGE,DEPARTMENT (Employee) EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 34SunimalKeerthiPerera33Production 22LalithVithanaLiyanage45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing LNAMEAGEDEPARTMENT Liyanage45Finance Perera33Production Liyanage45Finance Silva20Sales Amarathunga23Marketing LNAMEAGEDEPARTMENT Liyanage45Finance Perera33Production Silva20Sales Amarathunga23Marketing

The number of tuples in a relation resulting from a PROJECT operation is always less than or equal to the number of tuples in R If the projection list is a superkey of R, the resulting relation has the same number of tuples as R ∏ list1 (∏ list2 R) = ∏ list1 (R) If list 2 contains list 1

Exercise…. Find the names of students of all the students Find id and the degree program of all the students SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

Sequences of Operations Several relational algebra operations can be applied one after the other –W–We can write the operations as a single relational algebra expression –W–We can apply one operation at a time and create intermediate result relations We must give names to the relations that hold the intermediate results.

Eg: –R–Retrieve the first name, last name, and age of all employees who work in finance department ∏ FNAME,LNAME,AGE ( σ DEPARTMENT = FINANCE (Employee)) EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 34SunimalKeerthiPerera33Production 22LalithVithanaLiyanage45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing FNAMELNAMEAGE NimalLiyanage45 LalithLiyanage45

Eg: –R–Retrieve the first name, last name, and age of all employees who work in finance department FinDept_Emp  σ (Employee) ∏ FNAME,LNAME,AGE (FinDept_Emp) EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 34SunimalKeerthiPerera33Production 22LalithVithanaLiyanage45Finance 35ChathuraShanakaSilva20Sales 11WicramaSapumalAmarathunga23Marketing EIDFNAMEMNAMELNAMEAGEDEPARTMENT 12NimalKumaraLiyanage45Finance 22LalithVithanaLiyanage45Finance FNAMELNAMEAGE NimalLiyanage45 LalithLiyanage45

Exercise…. Find the names of 2 nd year students of IIT & CST degree program Find the first name and the degree program of students with sid 12, 50 and 34 SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

It is often simpler to break down a complex sequence of operations We can also use this technique to rename the attributes in the intermediate and result relations FinDept_Emp  σ (Employee) Emp(FirstName, Name, Age)  ∏ FNAME,LNAME,AGE (FinDept_Emp)

RENAME –R–Rename either the relation name or the attribute names, or both –R–Repesentation ρ S(B1, B2,.... Bn) (R) ρ S (R) ρ (B1,B2,...,Bn) (R)

Eg: FinDept_Emp  σ (Employee) Emp(FirstName, Name, Age)  ∏ FNAME,LNAME,AGE (FinDept_Emp)

Exercise…. Find the names of 2 nd year students of IIT & CST degree program name them as FIRSTNAME, MIDDLENAME and LASTNAME Find the ids and the degree program of students with sid 12, 50 and 34 name the new relation as ENROLLMENTS with the column names ID and ENROLLED_DEGREE_PROGRAM SIDFNAMEMNAMELNAMEDEGPROYEAR 12SahanNilankaPereraCST2 23NuwanLasanthaSilvaIIT3 45ChamaliNadeeKumariSCT4 50VipuliSamadhiPereraCST2 03NimaliPubuduPereraSCT4 11VishvaLakmalFernandoCST3 34LasithPiumalSilvaIIT2

Relational Algebra Operations From Set Theory UNION Result1 = ∏ (EMPLOYEE) Result2(EID) = ∏ (EMPLOYEE) EIDFNAMEMNAMELNAMEAGEDEPARTMENTSUPERVISOR 12NimalKumaraLiyanage45Finance03 34SunimalKeerthiPerera33Production04 22LalithVithanaLiyanage45Finance12 35ChathuraShanakaSilva20Production34 11WicramaSapumalAmarathunga23Marketing01

RESULT = RESULT1 υ RESULT2 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 12 34 22 35 11 03 04 01 RESULT Duplicate tuples are eliminated

–T–The UNION operation produces the tuples that are in either RESULT1 or RESULT2 or both –T–Two relations R(A1, A2,..., An) and S(B1,B2,…, Bn) are said to be union compatible If they have the same degree n If dom(Ai) = dom(Bi) for 1< i < n –c–commutative operations R υ S = S υ R

Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who works on project ‘345’ or ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

INTERSECTION Result1 = ∏ (EMPLOYEE) Result2(EID) = ∏ (EMPLOYEE) EIDFNAMEMNAMELNAMEAGEDEPARTMENTSUPERVISOR 12NimalKumaraLiyanage45Finance03 34SunimalKeerthiPerera33Production04 22LalithVithanaLiyanage45Finance12 35ChathuraShanakaSilva20Production34 11WicramaSapumalAmarathunga23Marketing01

RESULT= RESULT1 RESULT2 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 12 34 RESULT υ

Result is a relation that includes all tuples that are in both relations commutative operations R S = S R associative operations (R S) T = R (S T) υ υ υυυυ

Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who works on project ‘345’ & ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

MINUS / SET DIFFERENCE Result1 = ∏ (EMPLOYEE) Result2(EID) = ∏ (EMPLOYEE) EIDFNAMEMNAMELNAMEAGEDEPARTMENTSUPERVISOR 12NimalKumaraLiyanage45Finance03 34SunimalKeerthiPerera33Production04 22LalithVithanaLiyanage45Finance12 35ChathuraShanakaSilva20Production34 11WicramaSapumalAmarathunga23Marketing01

RESULTX = RESULT1 – RESULT2 RESULTY = RESULT2 – RESULT1 EID 12 34 22 35 11 EID 03 04 12 34 01 RESULT1RESULT2 EID 03 04 01 RESULTY EID 22 35 11 RESULTX

The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S not commutative R - S ≠ S - R

Exercise…. Find the employees who works on project ‘345’ Find the employees who works on project ‘678’ Find the employees who is working on project ‘345’ and not working on project ‘678’ with the results of the above two answers. EIDFNAMEMNAMELNAME 12SahanNilankaPerera 23NuwanLasanthaSilva 45ChamaliNadeeKumari EIDPROJECTID 12345 12678 12463 23678 23345 45463 45345

The CARTESIAN PRODUCT Operation –K–Known as CROSS PRODUCT or CROSS JOIN –B–Binary set operation –T–The relations do not have to be union compatible –C–Combines tuples from two relations in a combinatorial fashion –d–denoted by X –T–The result of R(A 1, A 2,..., A n ) X S(B 1, B 2,..., B m ) will be Q(A 1, A 2,..., A n, B 1, B 2,..., B m ) –I–If R has n R tuples, and S has n S tuples, then R x S will have n R * n S tuples

SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg :

SIDNameAgeDegreeProgramDegreeProgramCodeDegreeProgramName 112Sahan21CST Computer Science & Technology 212Achini21IIT CSTComputer Science & Technology 234Nalaka22SCT CSTComputer Science & Technology 112Sahan21CST IITIndustrial Information Technology 212Achini21IIT Industrial Information Technology 234Nalaka22SCT IITIndustrial Information Technology 112Sahan21CST SCTScience & Technology 212Achini21IIT SCTScience & Technology 234Nalaka22SCT Science & Technology STUDENT X DEGREEPROGRAMINTERMEDIATE_RESULT

∏ SID,NAME,AGE,DEGREEPROGRAMNAME ( σ DegreeProgramCode = DegreeProgram (INTERMEDIATE_RESULT)) SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology

BINARY RELATIONAL OPERATIONS JOIN –D–Denoted by –C–Combine related tuples from two relations into single tuples. –A–Allows us to process relationships among relations –R–Representation S R

SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : INTERMEDIATERESULT SIDNameAgeDegreeProgramDegreeProgramCodeDegreeProgramName 112Sahan21CST Computer Science & Technology 212Achini21IIT Industrial Information Technology 234Nalaka22SCT Science & Technology INTERMEDIATERESULT  STUDENT DEGREEPROGRAM RESULT  ∏ SID,NAME,AGE,DEGREEPROGRAMNAME (INTERMEDIATERESULT) SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology RESULT

SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : RESULT  ∏ SID,Name,Age,DegreeProgramName ( STUDENT DegreeProgram = DegreeProgramCode DEGREEPROGRAM) RESULT

A general join condition is of the form AND AND... AND

Equi Join –W–Where only equality comparisons is used

Natural Join –T–The two join attributes have the same name in both relations –I–If not, a renaming operation is applied first –D–Denoted by * –R–Representation R * S R * ρ (S)

SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : RESULT  STUDENT * ρ (DegreeProgram, Name) DEGREEPRGRAM RESULT

SIDNameAgeDegreeProgramName 112Sahan21 Computer Science & Technology 212Achini21 Industrial Information Technology 234Nalaka22 Science & Technology SIDNameAgeDegreeProgram 112Sahan21CST 212Achini21IIT 234Nalaka22SCT DegreeProgram Code DegreeProgram Name CSTComputer Science & Technology IITIndustrial Information Technology SCTScience & Technology STUDENT DEGREEPROGRAM Eg : DEGREE_PRO  ρ (DegreeProgram, Name) DEGREEPRGRAM RESULT  STUDENT * DEGREE_PRO RESULT

OUTER JOIN –W–When we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the JOIN –3–3 types LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

–A–An extension of the join operation that avoids loss of information. –U–Uses null values: Null signifies that the value is unknown or does not exist

Relation loan Relation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 loan-numberamount L-170 L-230 L-260 branch-name Downtown Redwood Perryridge

Inner Join loan Borrower loan-numberamount L-170 L-230 3000 4000 customer-name Jones Smith branch-name Downtown Redwood Jones Smith null loan-numberamount L-170 L-230 L-260 3000 4000 1700 customer-namebranch-name Downtown Redwood Perryridge Left Outer Join loan Borrower

Right Outer Join loan borrower Full Outer Join loan-numberamount L-170 L-230 L-155 3000 4000 null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-numberamount L-170 L-230 L-260 L-155 3000 4000 1700 null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null

Aggregation function – Takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra – Representation G1, G2, …, Gn g F1( A1 ), F2( A2 ),…, Fn( An ) (E)

LOANNOBRANCHAMOUNT 233123122121Colombo23,322 324234234233Badulla34,211 434234232323Kandy12,213 231231312312Colombo23,221 342324234223Kandy12,332 g sum(AMOUNT) (ACCOUNT) AMOUNT 105,299 BRANCH g sum(AMOUNT) (ACCOUNT) BRANCHAMOUNT Colombo 46543 Badulla34,211 Kandy24545

Aggregate Functions (Cont.) Result of aggregation does not have a name – Can use rename operation to give it a name – For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account)

Download ppt "Kumarapathirana K. P. S. D. Section 3.0. Introduction The basic set of operations for the relational model is the relational algebra User specifies basic."

Similar presentations