Algebra relacional

Post on 13-Jul-2015

198 views 4 download

Transcript of Algebra relacional

1

Relational AlgebraRelational Algebra

2

database Modulesdatabase ModulesModule 1: Database SystemsModule 1: Database Systems

Module 2: Relational ModelModule 2: Relational ModelModule 3: Entity Relationship ModelModule 3: Entity Relationship Model

Module 4: ER to Relat ional MappingModule 4: ER to Relat ional MappingModule 5: FDs and NormalizationModule 5: FDs and Normalization

Module 6: Relational AlgebraModule 6: Relational AlgebraModule 7: SQLModule 7: SQL

Module 8: Database Systems Module 8: Database Systems ArchitectureArchitecture

FundamentalFundamentalss

√√

Data Data ModelingModeling√√

Data Data DesignDesign√√

Data Data AccessAccess

ArchitectureArchitecture

3

MotivationMotivation The relational data model The relational data model

provides a means of provides a means of defining the database defining the database structure and constraintsstructure and constraints

A data model must also A data model must also provide a set of provide a set of operations to manipulate operations to manipulate the datathe data

NAME SALARY ADDRESS DEPT

Smith 50k St. Lucia Printing

Dilbert 40k Taringa Printing

Jones 60k Kenmore Printing

Trump 65k Auchenflower Head Office

Harrison 78k St. Lucia Head Office

Find the names and departments of allFind the names and departments of allemployees who earn more than 55Kemployees who earn more than 55K

Increment the salary of all employeesIncrement the salary of all employeesin the printing department by 10%in the printing department by 10%

What is the address of employeeWhat is the address of employeeJonesJones

The basic set of relational model operationsThe basic set of relational model operationsconstitute the Relational Algebraconstitute the Relational Algebra

4

ContentsContents

Relational AlgebraRelational Algebra• What is a Relational QueryWhat is a Relational Query• Relational Query LanguagesRelational Query Languages• Relational Algebra OperationsRelational Algebra Operations• Query Formulation in Relational AlgebraQuery Formulation in Relational Algebra• Exercises in Relational AlgebraExercises in Relational Algebra

5

What is a Relational QueryWhat is a Relational Query• Data in a relat ional database can be Data in a relat ional database can be

manipulated in the fol lowing ways:manipulated in the fol lowing ways:– – INSERTINSERT : New tuples may be inserted : New tuples may be inserted– – DELETEDELETE : Existing tuples may be deleted : Existing tuples may be deleted– – UPDATEUPDATE : Values of attr ibutes in existing : Values of attr ibutes in existing tuplestuples may be changedmay be changed– – RETRIEVERETRIEVE : Attr ibutes of specif ic tuples, : Attr ibutes of specif ic tuples, entireentire tuples, or even entire relations may be tuples, or even entire relations may be retrievedretrieved

• Relational Query Languages should provide all Relational Query Languages should provide all of the aboveof the above

6

Relational Query LanguagesRelational Query LanguagesRelational Queries are formulated in RelationalRelational Queries are formulated in Relational

Query LanguagesQuery Languages• Relational Algebra (RA)Relational Algebra (RA)

– – Formal query language for a relational databaseFormal query language for a relational database• Structured Query Language (SQL)Structured Query Language (SQL)

– – Comprehensive, commercial query language withComprehensive, commercial query language with widely accepted international standardwidely accepted international standard

• Query by Example (QBE)Query by Example (QBE)– – Commercial, graphical query language withCommercial, graphical query language with minimum syntaxminimum syntax

7

SQL and Relational AlgebraSQL and Relational AlgebraSQLSQL

• Declarative languageDeclarative language– – Users specify what the Users specify what the result of the query should result of the query should be, DBMS decides be, DBMS decides operations and order of operations and order of executionexecution

• OperationsOperations– – Provides commands to Provides commands to create and modify create and modify database structure and database structure and constraints (DDL)constraints (DDL)– – Provides commands to Provides commands to insert, delete, update and insert, delete, update and retrieve (DML)retrieve (DML)

RARA• Procedural languageProcedural language

– – Algebraic expressions Algebraic expressions specify an order of specify an order of operations ie. How the operations ie. How the query wil l be processedquery wil l be processed

• OperationsOperations– – Provides operators, that Provides operators, that enable a user to specify enable a user to specify retr ieval requests onlyretrieval requests only

8

ContentsContents

Relational AlgebraRelational Algebra• What is a Relational QueryWhat is a Relational Query• Relational Query LanguagesRelational Query Languages• Relational Algebra OperationsRelational Algebra Operations• Query Formulation in Relational AlgebraQuery Formulation in Relational Algebra• Exercises in Relational AlgebraExercises in Relational Algebra

9

Relational Algebra Relational Algebra OperationsOperations

• Relational algebra operations are applied onRelational algebra operations are applied onrelationsrelations

• Result of relational algebra operations are alsoResult of relational algebra operations are alsorelations, i.e the algebra operations producerelations, i.e the algebra operations producenew relations from oldnew relations from old

• A sequence of relational algebra operationsA sequence of relational algebra operationsforms a relational algebra expression, whoseforms a relational algebra expression, whoseresult will also be a relationresult will also be a relation

10

Types of RA OperationsTypes of RA Operations• Set operations from mathematical set theory Set operations from mathematical set theory

(Applicable because each relat ion is also a (Applicable because each relat ion is also a set set of tuples)of tuples)

– – UNIONUNION– – INTERSECTIONINTERSECTION– – DIFFERENCEDIFFERENCE– – CARTESIAN PRODUCTCARTESIAN PRODUCT

• Operations developed specif ically for RDBsOperations developed specif ically for RDBs– – SELECTSELECT– – PROJECTPROJECT– – JOINJOIN– – DIVISIONDIVISION

11

Operators and NotationOperators and Notation

Traditional SetTraditional SetOperatorsOperators

• Intersection Intersection ∩∩• Union Union ∪∪• Difference Difference ——• Cartesian Product Cartesian Product ××

Specific DatabaseSpecific DatabaseOperatorsOperators

• Select Select σσ• Project Project ΠΠ• Join Join ⋈⋈• Division Division ÷÷

12

Understanding RA Understanding RA OperationsOperations

• SELECTSELECT• PROJECTPROJECT• Assignment and NamingAssignment and Naming• UNIONUNION• INTERSECTIONINTERSECTION• DIFFERENCEDIFFERENCE• Properties of operatorsProperties of operators• CARTESIAN PRODUCTCARTESIAN PRODUCT• JOINJOIN• DIVISIONDIVISION

Discuss FirstDiscuss First

Discuss SecondDiscuss Second

Discuss Third Discuss Third

13

SelectSelect

σσ < < se l ec t i on c ond i t i on >se l ec t i on cond i t i on > ( < relation name > )( < relation name > )

Select those rows which satisfy a given condition This Select those rows which satisfy a given condition This operation is also called “restriction”operation is also called “restriction”

NAME SALARY

ADDRESS DEPT

Smith 50k St. Lucia PrintingDilbert 40k Taringa PrintingJones 60k Kenmore PrintingTrump 65k Auchenflowe

rHead Office

Harrison

78k St. Lucia Head Office

Selected Selected TuplesTuples

14

Select ExampleSelect Example1. List all details of employees working in1. List all details of employees working in

department 4?department 4?

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, DOB, Address, Sex, , DOB, Address, Sex, Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

σσ D n o = 4D n o = 4 (EMPLOYEE)(EMPLOYEE)

15

Select ExampleSelect Example

2. List all details of employees earning more than 2. List all details of employees earning more than $30000?$30000?

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, DOB, Address, Sex, , DOB, Address, Sex, Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

σσ Sa l ar y > 30000Sa l a r y > 30000 (EMPLOYEE)(EMPLOYEE)

16

Select ExampleSelect Example3. List all details about employees who work in3. List all details about employees who work in

department 4 and earn over $25000, or workdepartment 4 and earn over $25000, or workin department 5 and earn over $30000?in department 5 and earn over $30000?

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, DOB, Address, Sex, , DOB, Address, Sex, Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

σσ (Dno = 4 (Dno = 4 ∧ ∧ Sa la r y > 25000) Sa l a r y > 25000) ∨ ∨

( Dno = 5 ∧ S a la ry > 30000 )( Dno = 5 ∧ S a la ry > 30000 ) (EMPLOYEE)(EMPLOYEE)

17

ProjectProject

ΠΠ< a t r i bu t e co l umn name´s< a t r i bu t e co l umn name´s > > ( < relation name > )( < relation name > )

Select those columns specified in the listSelect those columns specified in the list

NAME SALARY

ADDRESS DEPT

Smith 50k St. Lucia PrintingDilbert 40k Taringa PrintingJones 60k Kenmore PrintingTrump 65k Auchenflowe

rHead Office

Harrison

78k St. Lucia Head OfficeSelected Selected attributesattributes

18

Project ExampleProject Example

4. For each employee, list their name, date of birth 4. For each employee, list their name, date of birth and salary. and salary.

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, Bdate, Address, Sex, , Bdate, Address, Sex,

Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

ΠΠ Ename,B da te , Sa l a r yEname, B da te , Sa la r y (EMPLOYEE)(EMPLOYEE)

19

Project ExampleProject Example

5. List the salaries paid to employees in each 5. List the salaries paid to employees in each department and the department number.department and the department number.

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, DOB, Address, Sex, , DOB, Address, Sex,

Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

ΠΠ D n o , S a l a r yD n o , S a l a r y (EMPLOYEE) (EMPLOYEE)

20

Handling Complex QueriesHandling Complex QueriesFormulation of complex queries may requireFormulation of complex queries may require

several relational algebra operations one afterseveral relational algebra operations one afterthe otherthe other– – Operations can be written as a single relationalOperations can be written as a single relational algebra algebra expressionexpression by nesting the operationsby nesting the operations– – Operations can be applied one at a time byOperations can be applied one at a time by creating creating intermediateintermediate result relationsresult relationsIntermediate Results have to be Intermediate Results have to be “assigned”“assigned” to totemporary relations which must be temporary relations which must be “named”“named”

21

Relation Assignment and NamingRelation Assignment and Naming

• Relation AssignmentRelation Assignment– – Result Relation Result Relation ←← Relational Expression Relational Expression

• Relation NamingRelation Naming– – TEMP TEMP ←←

• Attribute (re)NamingAttribute (re)Naming– – TEMP (dept, emp-salary) TEMP (dept, emp-salary) ←←

22

Assignment ExampleAssignment Example6. Create a new relation named RESULT, 6. Create a new relation named RESULT,

containing each employee and their date of birth. containing each employee and their date of birth. Label the resulting columns with ‘Employee’ and Label the resulting columns with ‘Employee’ and ‘DOB’.‘DOB’.

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, Bdate, Address, Sex, , Bdate, Address, Sex,

Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

RESULT(Employee,DOB )RESULT(Employee,DOB ) ← ← ΠΠ Ename,BdateEname,Bdate (EMPLOYEE)(EMPLOYEE)

23

Assignment ExampleAssignment Example7. List the names and salaries of all employees who 7. List the names and salaries of all employees who

work for department 5work for department 5 EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, Bdate, Address, , Bdate, Address,

Sex, Sex, Salary, SuperSSN, Dno]Salary, SuperSSN, Dno]

ΠΠ E na m e , S a l a ryE na me , S a l a ry ( ( σσ Dn o = 5Dn o = 5 (EMPLOYEE )(EMPLOYEE ) ) )

EMPS - DEP5EMPS - DEP5 ← ← σσ Dno = 5Dno = 5 (EMPLOYEE )(EMPLOYEE )

RESULT RESULT ← ← ΠΠ Ename,Salary Ename,Salary (EMPS_ DEP5(EMPS_ DEP5))

Query with Query with Expression Expression

Query withQuery withIntermediateIntermediate

RelationsRelations

24

Understanding RA Understanding RA OperationsOperations

• SELECTSELECT• PROJECTPROJECT• Assignment and NamingAssignment and Naming• UNIONUNION• INTERSECTIONINTERSECTION• DIFFERENCEDIFFERENCE• Propert ies of operatorsPropert ies of operators• CARTESIAN PRODUCTCARTESIAN PRODUCT• JOINJOIN• DIVISIONDIVISION

25

Basic Set OperatorsBasic Set Operators• Relation is a Relation is a set set of of

tuples (no duplicates)tuples (no duplicates)• Set theory, and Set theory, and

hence elementary sethence elementary setoperators also applyoperators also applyto relationsto relations– – UNIONUNION– – INTERSECTIONINTERSECTION– – DIFFERENCEDIFFERENCE– – CARTESIANCARTESIAN PRODUCTPRODUCT

UnionA∪B

IntersectionA∩B

DifferenceA - B

A B

A B

A B

26

Union Compatibility in RelationsUnion Compatibility in Relations

Two relations R(A1, A2, ..., An) and S(B1, B2,Two relations R(A1, A2, ..., An) and S(B1, B2,..., Bn) are ..., Bn) are union compatibleunion compatible iffiff– – They have the They have the same degree nsame degree n, (number of , (number of

columns)columns)– – Their columns Their columns have corresponding domainshave corresponding domains, i.e, i.e dom(Ai) = dom(Bi) for 1 dom(Ai) = dom(Bi) for 1 ≤≤ i i ≤≤ n n

Applies to union, intersection and differenceApplies to union, intersection and difference

27

… … Union CompatibilityUnion CompatibilityAlthough domains need to correspond they doAlthough domains need to correspond they do

not have to have the same namenot have to have the same name

WORKS_ONWORKS_ON [ESSN, Pno, Hours] [ESSN, Pno, Hours]WORKED_ONWORKED_ON [Employee, Project, Duration] [Employee, Project, Duration]

wherewhere dom (ESSN) = dom (Employee) dom (ESSN) = dom (Employee) dom (Pno) = dom (project)dom (Pno) = dom (project) dom (Hours) = dom (Duration)dom (Hours) = dom (Duration)

28

UnionUnionR1 R1 ∪∪ R2 R2

Produces a relation that includes all tuples Produces a relation that includes all tuples that appear only in R1, or only in R2, or in that appear only in R1, or only in R2, or in both R1 and R2both R1 and R2

Duplicate Tuples are eliminatedDuplicate Tuples are eliminated

R1 and R2 must be union compatibleR1 and R2 must be union compatible

29

Union ExampleUnion Example8.Identify the employees who either work on 8.Identify the employees who either work on

projects or have dependents. projects or have dependents.

WORKS_ON WORKS_ON [ [ESSN, PNoESSN, PNo, Hours], Hours] DEPENDENTDEPENDENT [ [ESSN, Dep_NameESSN, Dep_Name, Sex, DOB, , Sex, DOB, Relationship]Relationship]

WORKS_ON WORKS_ON ∪∪ DEPENDENTDEPENDENTThe relations are not UNION compatible !The relations are not UNION compatible !

30

Union ExampleUnion Example

9.List the ESSN’s of employees who either have 9.List the ESSN’s of employees who either have dependents or work on projects.dependents or work on projects.

WORKS_ONWORKS_ON [ [ESSN, PNoESSN, PNo, Hours], Hours] DEPENDENTDEPENDENT [ [ESSN, Dep_NameESSN, Dep_Name, Sex, DOB, , Sex, DOB, Relationship]Relationship]

ΠΠ E SS NE SS N ( ( DEPENDENT )DEPENDENT ) ∪∪ ΠΠ ESS NE SS N (WORKS_ON )(WORKS_ON )

31

IntersectionIntersection

R1R1 ∩∩ R2R2 Produces a relation that includes the tuples Produces a relation that includes the tuples

that appearthat appearin both R1 and R2.in both R1 and R2.

R1 and R2 must be union compatible.R1 and R2 must be union compatible.

32

Intersection ExampleIntersection Example

11. List the ESSN’s of employees who have11. List the ESSN’s of employees who havedependents and are managersdependents and are managers..

DEPENDENTDEPENDENT [ [ESSN, Dep_NameESSN, Dep_Name, Sex, DOB, , Sex, DOB, Relationship]Relationship]

DEPARTMENTDEPARTMENT [DName, [DName, DNumberDNumber, MgrSSN, MgrStart], MgrSSN, MgrStart]

ΠΠ E S S NE S S N ( ( DEPENDENT )DEPENDENT ) ∩ ∩ ΠΠ M G RS S NM G RS S N (DEPARTMENT )(DEPARTMENT )

33

DifferenceDifference

R1 R1 -- R2 R2Produces a relation that includes all the Produces a relation that includes all the

tuples thattuples thatappear in R1, but do not appear in R2.appear in R1, but do not appear in R2.

R1 and R2 must be union compatible.R1 and R2 must be union compatible.

34

Difference ExampleDifference Example

11. List the ESSN’s of employees who have11. List the ESSN’s of employees who havedependents but do not work on projectsdependents but do not work on projects..

WORKS_ONWORKS_ON [ [ESSN, PNoESSN, PNo, Hours], Hours]DEPENDENTDEPENDENT [ [ESSN, Dep_NameESSN, Dep_Name, Sex, , Sex,

DOB, DOB, Relationship]Relationship]

ΠΠ ESS NE SS N ( ( DEPENDENT )DEPENDENT ) —— ΠΠ E SS NE SS N (WORKS_ON )(WORKS_ON )

35

Properties of OperatorsProperties of Operators

• Commutative and Associative OperatorsCommutative and Associative Operators• Precedence among operators in relationalPrecedence among operators in relational

algebra expressionsalgebra expressions• De Morgan’s LawsDe Morgan’s Laws

36

Commutative and AssociativeCommutative and Associative

A ∪ B Commutative A ∪ B = B ∪ Aassociative (A ∪ B) ∪ C = A ∪ ( B ∪ C )

A∩ B commutative A ∩ B = B ∩ Aassociative (A ∩ B) ∩ C = A ∩ ( B ∩ C )

A — B

not commutative A — B ≠ B — A

not commutative (A — B) — C ≠ A — (B — C )

37

Operator PrecedenceOperator Precedence == , ≠, , ≠, <, >, ≤, ≥<, >, ≤, ≥

notnot andand oror σσ , , ΠΠ ∩∩ , ∪, —, ×, +, ⋈, ∪, —, ×, +, ⋈

• Operators performed left to r ight in the Operators performed left to r ight in the expressionexpression

• ( ) can be used to alter operator precedence, ( ) can be used to alter operator precedence, that is operations in ( ) wil l be performed before that is operations in ( ) wil l be performed before even if they have a lower precedence ordereven if they have a lower precedence order

HigherHigher

LowerLower

38

Precedence ExamplePrecedence Example12.12. List all employees who are male, and either List all employees who are male, and either earn less than $40000 or work for deptment 5.earn less than $40000 or work for deptment 5.

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN , DOB, Address, Sex, , DOB, Address, Sex, Salary, SuperSSN, Dnum]Salary, SuperSSN, Dnum]

ΠΠ E na m eE na m e ( ( σσ ( S ex = ‘M ’ a n d ( Sa la r y < 4 00 0 0 o r Dn u m = 5 ) )( S ex = ‘M ’ a n d ( Sa la r y < 4 00 0 0 o r Dn u m = 5 ) ) (EMPLOYEE )(EMPLOYEE ) ))

How does the above solution differ from the following?How does the above solution differ from the following?

ΠΠ En am eEn am e ( ( σσ ( Se x = ‘M ’ an d Sa la r y < 4 00 0 0 o r Dn u m = 5 )( Se x = ‘ M ’ an d Sa l a r y < 4 00 0 0 o r Dn u m = 5 ) (EMPLOYEE )(EMPLOYEE ) ))

39

De Morgan’s LawsDe Morgan’s Laws¬ ¬ ( p( p ∧ ∧ q )q ) ≡ ¬ ≡ ¬ pp ∨ ∨ ¬ ¬ qq¬ ¬ ( p( p ∨ ∨ q )q ) ≡ ¬ ≡ ¬ pp ∧∧ ¬ ¬ qq

where p and q are predicates, e.g Age>20,where p and q are predicates, e.g Age>20,Dept=Research, e.gDept=Research, e.g¬¬ (Salary (Salary >> 40000) 40000) ∧∧ (Dept (Dept == Research) ) Research) )

≡≡¬¬ (Salary (Salary >> 40000) 40000) ∨∨ ¬¬ (Dept(Dept = = Research)Research)

40

DeMorgan’s Law ExampleDeMorgan’s Law Example13. List all projects which are neither located in 13. List all projects which are neither located in Brisbane, nor controlled by department 4.Brisbane, nor controlled by department 4.

PROJECTPROJECT [PName, [PName, PNoPNo, Plocation, Dnum], Plocation, Dnum]

ΠΠ P n am eP n am e (( σσ no t ( P lo c a t i on = ‘ B r i s b ane ’ ) a n d n o t ( Dn um =4 ) no t ( P lo c a t i on = ‘ B r i s b ane ’ ) an d n o t ( Dn um =4 ) (PROJECT)(PROJECT) ))

≡≡ΠΠ P n am eP n am e ( ( σσ no t ( P l oc a t i on = ‘ B r i s b ane ’ o r D nu m = 4)no t ( P l oc a t i on = ‘ B r i s b a ne ’ o r D nu m = 4 ) (PROJECT)(PROJECT) ))

≡≡ΠΠ P n am eP n am e ( ( σσ n o t ( P l o c a t i o n < > ‘ B r i s b an e ’ an d D nu m < > 4 )no t ( P l oc a t i o n < > ‘ B r i s b an e ’ a n d D n um < > 4 ) (PROJECT)(PROJECT) ))

41

Cartesian ProductCartesian ProductR1 R1 ×× R2 R2

• Also known as a cross-product or cross-join R1 Also known as a cross-product or cross-join R1 and R2 need and R2 need NOT be union compatibleNOT be union compatible

• The result of R1 (A1, A2, … An) x R2 (B1, B2, … The result of R1 (A1, A2, … An) x R2 (B1, B2, … Bm) is a relation Q with Bm) is a relation Q with n + m attr ibutesn + m attr ibutes Q Q (A1, A2, … An, B1, B2, … Bm) in that order(A1, A2, … An, B1, B2, … Bm) in that order

• Q has one tuple for each combination of tuples Q has one tuple for each combination of tuples from R1 and R2, thus if R1 has r tuples and R2 from R1 and R2, thus if R1 has r tuples and R2 has t tuples, then has t tuples, then Q will have Q will have r * t tuplesr * t tuples

42

Cartesian Product ExampleCartesian Product Example

Subject

CS114

CS115

CS180

Subject Student Degree

CS114 Anna BIT

CS114 Fred BSc

CS115 Anna BIT

CS115 Fred BSc

CS180 Anna BIT

CS180 Fred BSc

Student Degree

Anna BIT

Fred BSc=×

43

Cartesian Product ExampleCartesian Product Example14. For each female employee, l ist the names14. For each female employee, l ist the names

of all of her dependents.of all of her dependents.

EMPLOYEE EMPLOYEE [Ename,[Ename,SSNSSN,DOB,Address,Sex,Salary,SuperSSN, Dno],DOB,Address,Sex,Salary,SuperSSN, Dno]DEPENDENT DEPENDENT [[ESSN, DepNameESSN, DepName, Sex, DOB, Relationship], Sex, DOB, Relationship]

FEMALE_EMPS FEMALE_EMPS ← ← σσ S ex = ‘ F ’S e x = ‘ F ’ (EMPLOYEE)(EMPLOYEE)EMP_NAMES EMP_NAMES ← ← ΠΠ E n am e , S SNE n am e , S S N (FEMALE_EMPS)(FEMALE_EMPS)EMP_DEPEND EMP_DEPEND ← ← EMP_NAMES EMP_NAMES ×× DEPENDENTDEPENDENTACTUAL_DEPEND ACTUAL_DEPEND ← ← σσ S S N = E S S NS S N = E S S N (EMP_DEPEND)(EMP_DEPEND)RESULT RESULT ← ← ΠΠ E na me , De p N am eE n am e , Dep N a me (ACTUAL_DEPEND)(ACTUAL_DEPEND)

44

Understanding RA Understanding RA OperationsOperations

• SELECTSELECT• PROJECTPROJECT• Assignment and NamingAssignment and Naming• UNIONUNION• INTERSECTIONINTERSECTION• DIFFERENCEDIFFERENCE• Properties of operatorsProperties of operators• CARTESIAN PRODUCTCARTESIAN PRODUCT• JOINJOIN• DIVISIONDIVISION

45

Join OperationsJoin Operations• A Join is similar to Cartesian Product, but only A Join is similar to Cartesian Product, but only

selected pairs of tuples appear in the resultselected pairs of tuples appear in the result• It is used to combine related tuples from two It is used to combine related tuples from two

relations into a single tuple in a new relation. relations into a single tuple in a new relation. This is needed when information is contained in This is needed when information is contained in more than one relationmore than one relation

• There are three types of Join Operations:There are three types of Join Operations:– – Thieta-JoinThieta-Join– – Equi-JoinEqui-Join– – Natural JoinNatural Join

46

Thieta-JoinThieta-Join

R1 R1 ⋈ ⋈ < j o i n c o n d i t i o n >< j o i n c o n d i t i o n > R2 R2

A join condition(s) is of the form A A join condition(s) is of the form A θθ B, where A B, where A ∈∈ R1 and B R1 and B ∈∈ R2, R2,

and and θθ is one of {=, <, ≤, >, ≥} is one of {=, <, ≤, >, ≥}

47

Thieta-Join ExampleThieta-Join Example15. For each employee, list all the employees who 15. For each employee, list all the employees who earn more (than the first employee).earn more (than the first employee).

EMPLOYEEEMPLOYEE [Ename, [Ename, SSNSSN, DOB, Address, Sex, Salary, , DOB, Address, Sex, Salary, SuperSSN, Dno]SuperSSN, Dno]DEPARTMENTDEPARTMENT [DName, [DName, DNumberDNumber, MgrSSN, MgrStart], MgrSSN, MgrStart]

A A ← ← EMPLOYEEEMPLOYEEB B ←← EMPLOYEE EMPLOYEERESULT RESULT ← ← ΠΠ A.Ename, B.EnameA.Ename, B.Ename (A(A ⋈ ⋈ A.Salary < B.SalaryA.Salary < B.Salary B B))

48

Equi-JoinEqui-Join

R1 R1 ⋈⋈ < j o i n c o n d i t i o n >< j o i n c o n d i t i o n > R2 R2

Specialization of JoinSpecialization of JoinJoin condition only has Join condition only has equalityequality comparisons onlycomparisons only

49

Equi-Join ExampleEqui-Join Example16. List the names of the managers of 16. List the names of the managers of

each each department.department.

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, , DOB, Address, Sex, Salary, SuperSSN, Dno]SuperSSN, Dno]DEPARTMENTDEPARTMENT [ DName, [ DName, DNumberDNumber, MgrSSN, MgrStart], MgrSSN, MgrStart]

DEPT_MGRDEPT_MGR ← ← DEPARTMENT DEPARTMENT ⋈ ⋈ M G RS S N = S S NM G RS S N = S S N EMPLOYEEEMPLOYEE

RESULTRESULT ← ← ΠΠ E na m eE n a me (DEPT_MGR) (DEPT_MGR)

50

Natural JoinNatural Join

R1 R1 ** R2 R2

• Similar to equi-join except that the attributes that Similar to equi-join except that the attributes that are used for the join are those that have the are used for the join are those that have the same name in each relationsame name in each relation

• Consequently, they are not explicitly specifiedConsequently, they are not explicitly specified• The duplicate column is eliminatedThe duplicate column is eliminated

51

Natural Join ExampleNatural Join Example

Subject Student Degree

CS114 Anna BIT

CS114 Anna BA

CS115 Fred BSc

CS180 Anna BIT

CS180 Anna BA

Student Degree

Anna BIT

Anna BA

Fred BSc

* =

Subject Student

CS114 Anna

CS115 Fred

CS180 Anna

CS214 Bobby

52

Natural Join ExampleNatural Join Example

17. What is the result schema of the following 17. What is the result schema of the following query? What attributes is the join performed query? What attributes is the join performed on?on?DEPARTMENTDEPARTMENT [ DName, [ DName, DNumberDNumber, MgrSSN, MgrStart ], MgrSSN, MgrStart ]DEPT_LOCSDEPT_LOCS [ [ DNumberDNumber, , DlocationDlocation ] ]

DEPARTMENT * DEPT_LOCSDEPARTMENT * DEPT_LOCS

53

Natural Join ExampleNatural Join Example18. What is the dif ference between the results of 18. What is the dif ference between the results of the following queries? What attr ibutes are the following queries? What attr ibutes are

the the joins performed on?joins performed on?

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno ]Dno ]

DEPARTMENTDEPARTMENT [ DName, [ DName, DNumberDNumber, MgrSSN, MgrStart ], MgrSSN, MgrStart ]

EMPLOYEE * DEPARTMENTEMPLOYEE * DEPARTMENTandand

EMPEMP (MgrSSN ,Dnumber) (MgrSSN ,Dnumber) ←← ΠΠ S S N , Dn o S S N , Dno (EMPLOYEE) (EMPLOYEE) RESULTRESULT ← ← EMP * DEPARTMENTEMP * DEPARTMENT

54

DivisionDivisionR1 R1 ÷÷ R2 R2

• Result relation contains columns in R1, but not Result relation contains columns in R1, but not in R2in R2

• Relations R1 and R2 must be Relations R1 and R2 must be division division compatiblecompatible, i.e last n columns of R1 must be , i.e last n columns of R1 must be identically named to columns in R2, where n is identically named to columns in R2, where n is the degree of R2the degree of R2

• The result relation contains tuples t, such that a The result relation contains tuples t, such that a value in t appears in R1, in combination with value in t appears in R1, in combination with every tuple in R2every tuple in R2

55

Division ExampleDivision Example

Student Degree Subject

Anna BIT CS114

Anna BIT CS115

Anna BIT CS180

Fred BSc CS114

Fred BSc CS180

Student Degree

Anna BIT÷ =Subject

CS114

CS115

56

Division ExampleDivision Example22. Retrieve the names of employees who work 22. Retrieve the names of employees who work

on on all projects that John Smith works on.all projects that John Smith works on.

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno]Dno]

WORKS_ONWORKS_ON [ [ ESSNESSN,, Pno Pno, Hours ], Hours ]

SMITH SMITH ← ← σσ Ename=‘John Smith’Ename=‘John Smith’ (EMPLOYEE) (EMPLOYEE)

SMITH_PNOS SMITH_PNOS ← ← ΠΠ PnoPno (WORKS_ON (WORKS_ON ⋈ ⋈ ESSN=SSNESSN=SSN SMITH)SMITH)

SSN_PNOS SSN_PNOS ← ← ΠΠ ESSN, PnoESSN, Pno (WORKS_ON) (WORKS_ON)

SSNS(SSN) SSNS(SSN) ← ← SSN_PNOS SSN_PNOS ÷÷ SMITH_PNOS SMITH_PNOSRESULT RESULT ← ← ΠΠ EnameEname (SSNS * EMPLOYEE) (SSNS * EMPLOYEE)

57

ContentsContents

Relational AlgebraRelational Algebra• What is a Relational QueryWhat is a Relational Query• Relational Query LanguagesRelational Query Languages• Relational Algebra OperationsRelational Algebra Operations• Query Formulation in Relational AlgebraQuery Formulation in Relational Algebra• Exercises in Relational AlgebraExercises in Relational Algebra

58

Query Formulation in RAQuery Formulation in RA• Understand what the English query meansUnderstand what the English query means• Identify which relations, tuples (SELECT) and attributes Identify which relations, tuples (SELECT) and attributes

(PROJECT) that will be required for the query(PROJECT) that will be required for the query• Identify the relationships between required relations and Identify the relationships between required relations and

accordingly which binary operators can be usedaccordingly which binary operators can be used(JOIN, PRODUCT, UNION, DIVISION, …)(JOIN, PRODUCT, UNION, DIVISION, …)

• Formulate the query keeping in mind operator properties Formulate the query keeping in mind operator properties (Commutative/Associative, Order precedence, De (Commutative/Associative, Order precedence, De Morgan’s Laws)Morgan’s Laws)

59

Which RA Operator to use ?Which RA Operator to use ?• SELECT SELECT ←←• PROJECT PROJECT ←←

• UNION UNION ←←• INTERSECTIONINTERSECTION• DIFFERENCE DIFFERENCE ←←• CARTESIAN PRODUCT CARTESIAN PRODUCT ←←• JOINJOIN• DIVISIONDIVISION

• Use unary operators SELECT / Use unary operators SELECT / PROJECT when choosing tuples / PROJECT when choosing tuples / attr ibutes respectively from a single attr ibutes respectively from a single relat ionrelat ion

• Use binary operators UNION, Use binary operators UNION, PRODUCT, JOIN, … when defining PRODUCT, JOIN, … when defining the relat ionship between 2 or more the relat ionship between 2 or more relat ionsrelat ions

{ { σσ , ,ΠΠ ,, U,U, ——,, × } × } Complete Set of Complete Set of OperationsOperations

60

Complete Set of RA OperatorsComplete Set of RA Operators

• It has been proved that It has been proved that {{σσ, , ΠΠ, , ∪∪ ,, ——,, ×}×} is a is a complete set of RA operatorscomplete set of RA operators

• Each remaining relational algebra operator can Each remaining relational algebra operator can be expressed as a sequence of operations from be expressed as a sequence of operations from this setthis set

• These remaining operators have been definedThese remaining operators have been definedprimarily for convenience !primarily for convenience !

61

Expressing other operatorsExpressing other operators• IntersectionIntersection

R R ∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R )) ∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R ))

• (Thieta/Equi) Join(Thieta/Equi) JoinR R <c o nd i t i o n><c o nd i t i o n > S S ≡ ≡ σσ <c o n d i t i o n ><c o n d i t i o n > ( R( R × × S)S)

• Natural JoinNatural Join S(B1,B2, B3, … Bm)S(B1,B2, B3, … Bm)

R1 (B1, A2, A3, . . . An) R1 (B1, A2, A3, . . . An) ← ← ΠΠ ( A 1 , A 2 , A 3 , . . . A n )( A 1 , A 2 , A 3 , . . . A n ) R RR * S R * S ≡ ≡ ΠΠ (B 1 , A 2 , A 3 , . . . A n , B 2 , . . . B m )(B 1 , A 2 , A 3 , . . . A n , B 2 , . . . B m ) σσ <R . B 1 = S . B 1 ><R . B 1 = S . B 1> ( R1( R1 ×× S)S)

62

Expressing other operatorsExpressing other operators

• DivisionDivisionT1 T1 ← ← ΠΠ YY ( R ) ( R )T2 T2 ← ← ΠΠ YY ( ( S ( ( S × × T1 ) T1 ) — — R )R )R R ÷ S ≡ ÷ S ≡ T1 T1 — T2— T2

63

ContentsContents

Relational AlgebraRelational Algebra• What is a Relational QueryWhat is a Relational Query• Relational Query LanguagesRelational Query Languages• Relational Algebra OperationsRelational Algebra Operations• Query Formulation in Relational AlgebraQuery Formulation in Relational Algebra• Exercises in Relational AlgebraExercises in Relational Algebra

64

Relational Algebra ExercisesRelational Algebra ExercisesThese exercises use the Company database as an These exercises use the Company database as an

example to illustrate relational algebra queries that example to illustrate relational algebra queries that require the use of multiple relational algebra operatorsrequire the use of multiple relational algebra operators

• EMPLOYEEEMPLOYEE [ [SsnSsn, Fname, Mit, Lname, Dob, Address, , Fname, Mit, Lname, Dob, Address, Sex,Sex, Salary, Dno, SuperSSN]Salary, Dno, SuperSSN]

• DEPARTMENTDEPARTMENT [ [DnumberDnumber, Dname, MGRSSN,MgrStart], Dname, MGRSSN,MgrStart]• PROJECTPROJECT [ [PnoPno, PName, Plocation, DNum], PName, Plocation, DNum]• DEPENDENTDEPENDENT [ [ESSN,DepNameESSN,DepName, Sex, DOB, , Sex, DOB,

Relationship]Relationship]• WORKS_ONWORKS_ON [ [ESSNESSN, , PNoPNo, Hours], Hours]• DEPT_LOCSDEPT_LOCS [ [DNumberDNumber, , DLocationDLocation]]

65

RA ExerciseRA Exercise23. Retrieve the name and address of all 23. Retrieve the name and address of all

employees employees who work for the Research Department.who work for the Research Department.

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno ]Dno ]

DEPARTMENTDEPARTMENT [ Dname, [ Dname, DnumberDnumber, MgrSSN, MgrStart ], MgrSSN, MgrStart ]

RESEARCH_DEPT RESEARCH_DEPT ← ← σσ Dname=‘Research’Dname=‘Research’ (DEPARTMENT) (DEPARTMENT)RESEARCH_DEPT_EMPS RESEARCH_DEPT_EMPS ← ← (RESEARCH_DEPT(RESEARCH_DEPT⋈ ⋈ Dnumber=DnoDnumber=Dno EMPLOYEE)EMPLOYEE)RESULT RESULT ← ← ΠΠ Ename,AddressEname,Address (RESEARCH_DEPT_EMPS) (RESEARCH_DEPT_EMPS)

66

RA ExerciseRA Exercise24. For every project located in Houston, list the project 24. For every project located in Houston, list the project number, the controlling department number, and the number, the controlling department number, and the department manager’s name, address & birth datedepartment manager’s name, address & birth date..

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno]Dno]

DEPARTMENTDEPARTMENT [ Dname, [ Dname, DnumberDnumber, MgrSSN, MgrStart], MgrSSN, MgrStart]PROJECTPROJECT [ PName, [ PName, PnumberPnumber, Plocation, Dnum], Plocation, Dnum]

HOUSTON_PROJS HOUSTON_PROJS ← ← σσ Plocation=‘houston’Plocation=‘houston’ (PROJECT) (PROJECT)CONTR_DEPT CONTR_DEPT ← ← (HOUSTON_PROJS (HOUSTON_PROJS ⋈ ⋈ Dnum=DnumberDnum=Dnumber DEPARTMENT)DEPARTMENT)PROJ_DEPT_MGR PROJ_DEPT_MGR ← ← (CONTR_DEPT(CONTR_DEPT⋈ ⋈ MgrSSN=SSNMgrSSN=SSN EMPLOYEE)EMPLOYEE)RESULT RESULT ← ← ΠΠ Pnumber,Dnum,Ename,Address,Bdate Pnumber,Dnum,Ename,Address,Bdate (PROJ_DEPT_MGR)(PROJ_DEPT_MGR)

67

RA ExerciseRA Exercise25. Find the names of employees who work on all 25. Find the names of employees who work on all projects controlled by department 5.projects controlled by department 5.

EMPLOYEE EMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno]Dno]

PROJECT PROJECT [ PName, [ PName, PnumberPnumber, Plocation, Dnum], Plocation, Dnum]WORKS_ONWORKS_ON [ [ ESSNESSN, , PnoPno, Hours, Hours]]

DEPT5_PROJS(Pno) DEPT5_PROJS(Pno) ← ← ΠΠ PnumberPnumber ( (σσ Dnum=5Dnum=5 (PROJECT)) (PROJECT))EMP_PROJ(SSN,Pno) EMP_PROJ(SSN,Pno) ← ← ΠΠ ESSN,PnoESSN,Pno (WORKS_ON)(WORKS_ON)RESULT_EMP_SSNS RESULT_EMP_SSNS ← E← EMP_PROJ MP_PROJ ÷ ÷ DEPT5_PROJSDEPT5_PROJSRESULT RESULT ← ← ΠΠ EnameEname (RESULT_EMP_SSNS * EMPLOYEE ) (RESULT_EMP_SSNS * EMPLOYEE )

68

RA ExerciseRA Exercise26. List project numbers for projects that involve an employee 26. List project numbers for projects that involve an employee whose name is Smith, either as a worker or as a manager whose name is Smith, either as a worker or as a manager

of of the department that controls the project.the department that controls the project.

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, Dno], DOB, Address, Sex, Salary, SuperSSN, Dno]PROJECTPROJECT [ PName, [ PName, PnumberPnumber, Plocation, Dnum], Plocation, Dnum]DEPARTMENTDEPARTMENT [ Dname, [ Dname, DnumberDnumber, MgrSSN, MgrStart], MgrSSN, MgrStart]WORKS_ONWORKS_ON [ [ ESSN, PnoESSN, Pno, Hours], Hours]

SMITHS(ESSN) SMITHS(ESSN) ← ← ΠΠ SSNSSN ( (σσ Ename=‘Smith’Ename=‘Smith’ (EMPLOYEE)) (EMPLOYEE))SMITH_WORKER_PROJS SMITH_WORKER_PROJS ← ← ΠΠ PnoPno (WORKS_ON * SMITHS)(WORKS_ON * SMITHS)MGRS MGRS ← ← ΠΠ Ename,DnumberEname,Dnumber (EMPLOYEE (EMPLOYEE ⋈ ⋈ SSN=MgrSSNSSN=MgrSSN DEPARTMENT)DEPARTMENT)SMITH_MGRS SMITH_MGRS ← ← σσ Ename=‘Smith’Ename=‘Smith’ (MGRS) (MGRS)SMITH_MANAGED_DEPTS(Dnum) SMITH_MANAGED_DEPTS(Dnum) ← ← ΠΠ DnumberDnumber (SMITH_MGRS)(SMITH_MGRS)SMITH_MGR_PROJS(Pno) SMITH_MGR_PROJS(Pno) ← ← ΠΠ PnumberPnumber (SMITH_MANAGED_DEPTS * PROJECT)(SMITH_MANAGED_DEPTS * PROJECT)RESULT RESULT ← ← SMITH_WORKER_PROJS SMITH_WORKER_PROJS ∪ ∪ SMITH_MGR_PROJSSMITH_MGR_PROJS

69

RA ExerciseRA Exercise27. Retrieve the names of employees who have no 27. Retrieve the names of employees who have no dependents.dependents.

EMPLOYEEEMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno]Dno]

DEPENDENTDEPENDENT [ [ ESSNESSN, , Dep_NameDep_Name, Sex, DOB, Relationship], Sex, DOB, Relationship]

ALL_EMPS ALL_EMPS ← ← ΠΠ S SNSS N (EMPLOYEE)(EMPLOYEE)EMPS_WITH_DEPS(SSN) EMPS_WITH_DEPS(SSN) ← ← ΠΠ ES SNES SN (DEPENDENT)(DEPENDENT)EMPS_WITHOUT_DEPS EMPS_WITHOUT_DEPS ← ← ( ALL_EMPS ( ALL_EMPS ——

EMPS_WITH_DEPS)EMPS_WITH_DEPS)RESULT RESULT ← ← ΠΠ En a m eEn a m e (EMPS_WITHOUT_DEPS * EMPLOYEE (EMPS_WITHOUT_DEPS * EMPLOYEE ) )

70

RA ExerciseRA Exercise28. List the names of managers who have at least 28. List the names of managers who have at least one dependent.one dependent.

EMPLOYEE EMPLOYEE [ Ename, [ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN, , DOB, Address, Sex, Salary, SuperSSN, Dno]Dno]

DEPARTMENT DEPARTMENT [ Dname, [ Dname, DnumberDnumber, MgrSSN, MgrStart], MgrSSN, MgrStart]DEPENDENTDEPENDENT [ [ ESSNESSN, , Dep_NameDep_Name, Sex, DOB, Relationship], Sex, DOB, Relationship]

MGR(SSN) MGR(SSN) ← ← ΠΠ M g r S SNM g r SS N (DEPARTMENT)(DEPARTMENT)EMPS_WITH_DEPS(SSN) EMPS_WITH_DEPS(SSN) ← ← ΠΠ ES SNES SN (DEPENDENT)(DEPENDENT)MGRS_WITH_DEPS MGRS_WITH_DEPS ← ← (MGRS (MGRS ∩ ∩ EMPS_WITH_DEPS) EMPS_WITH_DEPS)RESULT RESULT ← ← ΠΠ En a m eEn a m e (MGRS_WITH_DEPS * EMPLOYEE) (MGRS_WITH_DEPS * EMPLOYEE)

71

ReviewReview• Relational algebra gives the theoretical foundations for Relational algebra gives the theoretical foundations for

Relational Query LanguagesRelational Query Languages– – Relational algebra operations operate on entire relations, Relational algebra operations operate on entire relations,

and produce results which are also relationsand produce results which are also relations– – Relational algebra expressions, consisting of a sequence Relational algebra expressions, consisting of a sequence

of relational algebra operators, specify a high-level of relational algebra operators, specify a high-level procedure to achieve a query resultprocedure to achieve a query result• However, relational algebraic query formulation is However, relational algebraic query formulation is

procedural, and therefore focuses on how a query result procedural, and therefore focuses on how a query result can be achievedcan be achieved

• Declarative query languages, e.g., SQL, allow the user to Declarative query languages, e.g., SQL, allow the user to specify specify what what info the user wants rather than info the user wants rather than how how the result the result is to be obtainedis to be obtained

72

Recommended ReadingsRecommended Readings

Elmasri & NavatheElmasri & NavatheChapter 7Chapter 7

73

Next ...Next ...

Module 7Module 7

Structured QueryStructured QueryLanguage (SQL)Language (SQL)