SlideShare una empresa de Scribd logo
1 de 73
1
Relational AlgebraRelational Algebra
2
database Modulesdatabase Modules
Module 1: Database SystemsModule 1: Database Systems
Module 2: Relational ModelModule 2: Relational Model
Module 3: Entity Relationship ModelModule 3: Entity Relationship Model
Module 4: ER to Relational MappingModule 4: ER to Relational Mapping
Module 5: FDs and NormalizationModule 5: FDs and Normalization
Module 6: Relational AlgebraModule 6: Relational Algebra
Module 7: SQLModule 7: SQL
Module 8: Database SystemsModule 8: Database Systems
ArchitectureArchitecture
FundamentalFundamental
ss
√√
DataData
ModelingModeling√√
DataData
DesignDesign√√
DataData
AccessAccess
ArchitectureArchitecture
3
MotivationMotivation
The relational data modelThe relational data model
provides a means ofprovides a means of
defining the databasedefining the database
structure and constraintsstructure and constraints
A data model must alsoA data model must also
provide a set ofprovide a set of
operations to manipulateoperations 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 all
employees who earn more than 55Kemployees who earn more than 55K
Increment the salary of all employeesIncrement the salary of all employees
in the printing department by 10%in the printing department by 10%
What is the address of employeeWhat is the address of employee
JonesJones
The basic set of relational model operationsThe basic set of relational model operations
constitute 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 relational database can beData in a relational database can be
manipulated in the following ways:manipulated in the following 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 attributes in existing: Values of attributes in existing
tuplestuples
may be changedmay be changed
–– RETRIEVERETRIEVE: Attributes of specific tuples,: Attributes of specific tuples,
entireentire
tuples, or even entire relations may betuples, or even entire relations may be
retrievedretrieved
• Relational Query Languages should provide allRelational Query Languages should provide all
of the aboveof the above
6
Relational Query LanguagesRelational Query Languages
Relational 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 Algebra
SQLSQL
• Declarative languageDeclarative language
–– Users specify what theUsers specify what the
result of the query shouldresult of the query should
be, DBMS decidesbe, DBMS decides
operations and order ofoperations and order of
executionexecution
• OperationsOperations
–– Provides commands toProvides commands to
create and modifycreate and modify
database structure anddatabase structure and
constraints (DDL)constraints (DDL)
–– Provides commands toProvides commands to
insert, delete, update andinsert, delete, update and
retrieve (DML)retrieve (DML)
RARA
• Procedural languageProcedural language
–– Algebraic expressionsAlgebraic expressions
specify an order ofspecify an order of
operations ie. How theoperations ie. How the
query will be processedquery will be processed
• OperationsOperations
–– Provides operators, thatProvides operators, that
enable a user to specifyenable a user to specify
retrieval 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 AlgebraRelational Algebra
OperationsOperations
• Relational algebra operations are applied onRelational algebra operations are applied on
relationsrelations
• Result of relational algebra operations are alsoResult of relational algebra operations are also
relations, i.e the algebra operations producerelations, i.e the algebra operations produce
new relations from oldnew relations from old
• A sequence of relational algebra operationsA sequence of relational algebra operations
forms a relational algebra expression, whoseforms a relational algebra expression, whose
result will also be a relationresult will also be a relation
10
Types of RA OperationsTypes of RA Operations
• Set operations from mathematical set theorySet operations from mathematical set theory
(Applicable because each relation is also a(Applicable because each relation is also a setset
of tuples)of tuples)
–– UNIONUNION
–– INTERSECTIONINTERSECTION
–– DIFFERENCEDIFFERENCE
–– CARTESIAN PRODUCTCARTESIAN PRODUCT
• Operations developed specifically for RDBsOperations developed specifically for RDBs
–– SELECTSELECT
–– PROJECTPROJECT
–– JOINJOIN
–– DIVISIONDIVISION
11
Operators and NotationOperators and Notation
Traditional SetTraditional Set
OperatorsOperators
• IntersectionIntersection ∩∩
• UnionUnion ∪∪
• DifferenceDifference ——
• Cartesian ProductCartesian Product ××
Specific DatabaseSpecific Database
OperatorsOperators
• SelectSelect σσ
• ProjectProject ΠΠ
• JoinJoin ⋈⋈
• DivisionDivision ÷÷
12
Understanding RAUnderstanding 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 ThirdDiscuss Third
13
SelectSelect
σσ << selection condition >selection condition > ( < relation name > )( < relation name > )
Select those rows which satisfy a given condition ThisSelect those rows which satisfy a given condition This
operation is also called “restriction”operation is also called “restriction”
NAME SALAR
Y
ADDRESS DEPT
Smith 50k St. Lucia Printing
Dilbert 40k Taringa Printing
Jones 60k Kenmore Printing
Trump 65k Auchenflowe
r
Head Office
Harriso
n
78k St. Lucia Head Office
SelectedSelected
TuplesTuples
14
Select ExampleSelect Example
1. 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]
σσ Dno = 4Dno = 4 (EMPLOYEE)(EMPLOYEE)
15
Select ExampleSelect Example
2. List all details of employees earning more than2. 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]
σσ Salary > 30000Salary > 30000 (EMPLOYEE)(EMPLOYEE)
16
Select ExampleSelect Example
3. 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 work
in 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 ∧∧ Salary > 25000)Salary > 25000) ∨∨
(Dno = 5 ∧ Salary > 30000)(Dno = 5 ∧ Salary > 30000) (EMPLOYEE)(EMPLOYEE)
17
ProjectProject
ΠΠ< atribute column name´s< atribute column name´s >> ( < relation name > )( < relation name > )
Select those columns specified in the listSelect those columns specified in the list
NAME SALAR
Y
ADDRESS DEPT
Smith 50k St. Lucia Printing
Dilbert 40k Taringa Printing
Jones 60k Kenmore Printing
Trump 65k Auchenflowe
r
Head Office
Harriso
n
78k St. Lucia Head Office
SelectedSelected
attributesattributes
18
Project ExampleProject Example
4. For each employee, list their name, date of birth4. 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,Bdate,SalaryEname,Bdate,Salary (EMPLOYEE)(EMPLOYEE)
19
Project ExampleProject Example
5. List the salaries paid to employees in each5. 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]
ΠΠ Dno, SalaryDno, Salary (EMPLOYEE)(EMPLOYEE)
20
Handling Complex QueriesHandling Complex Queries
Formulation of complex queries may requireFormulation of complex queries may require
several relational algebra operations one afterseveral relational algebra operations one after
the otherthe other
–– Operations can be written as a single relationalOperations can be written as a single relational
algebraalgebra 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
creatingcreating intermediateintermediate result relationsresult relations
Intermediate Results have to beIntermediate Results have to be “assigned”“assigned” toto
temporary relations which must betemporary relations which must be “named”“named”
21
Relation Assignment and NamingRelation Assignment and Naming
• Relation AssignmentRelation Assignment
–– Result RelationResult Relation ←← Relational ExpressionRelational Expression
• Relation NamingRelation Naming
–– TEMPTEMP ←←
• Attribute (re)NamingAttribute (re)Naming
–– TEMP (dept, emp-salary)TEMP (dept, emp-salary) ←←
22
Assignment ExampleAssignment Example
6. 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’ andLabel 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 Example
7. List the names and salaries of all employees who7. 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]
ΠΠ Ename,SalaryEname,Salary (( σσ Dno = 5Dno = 5 (EMPLOYEE )(EMPLOYEE ) ))
EMPS - DEP5EMPS - DEP5 ←← σσ Dno = 5Dno = 5 (EMPLOYEE )(EMPLOYEE )
RESULTRESULT ←← ΠΠ Ename,SalaryEname,Salary (EMPS_ DEP5(EMPS_ DEP5))
Query withQuery with
ExpressionExpression
Query withQuery with
IntermediateIntermediate
RelationsRelations
24
Understanding RAUnderstanding RA
OperationsOperations
• SELECTSELECT
• PROJECTPROJECT
• Assignment and NamingAssignment and Naming
• UNIONUNION
• INTERSECTIONINTERSECTION
• DIFFERENCEDIFFERENCE
• Properties of operatorsProperties of operators
• CARTESIAN PRODUCTCARTESIAN PRODUCT
• JOINJOIN
• DIVISIONDIVISION
25
Basic Set OperatorsBasic Set Operators
• Relation is aRelation is a setset ofof
tuples (no duplicates)tuples (no duplicates)
• Set theory, andSet theory, and
hence elementary sethence elementary set
operators also applyoperators also apply
to relationsto relations
–– UNIONUNION
–– INTERSECTIONINTERSECTION
–– DIFFERENCEDIFFERENCE
–– CARTESIANCARTESIAN
PRODUCTPRODUCT
Union
A∪B
Intersection
A∩B
Difference
A - 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 theThey have the same degree nsame degree n, (number of, (number of
columns)columns)
–– Their columnsTheir columns have corresponding domainshave corresponding domains, i.e, i.e
dom(Ai) = dom(Bi) for 1dom(Ai) = dom(Bi) for 1 ≤≤ ii ≤≤ nn
Applies to union, intersection and differenceApplies to union, intersection and difference
27
…… Union CompatibilityUnion Compatibility
Although 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
UnionUnion
R1R1 ∪∪ R2R2
Produces a relation that includes all tuplesProduces a relation that includes all tuples
that appear only in R1, or only in R2, or inthat 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 Example
8.Identify the employees who either work on8.Identify the employees who either work on
projects or have dependents.projects or have dependents.
WORKS_ONWORKS_ON [[ESSN, PNoESSN, PNo, Hours], Hours]
DEPENDENTDEPENDENT [[ESSN, Dep_NameESSN, Dep_Name, Sex, DOB,, Sex, DOB,
Relationship]Relationship]
WORKS_ONWORKS_ON ∪∪ DEPENDENTDEPENDENT
The relations are not UNION compatible !The relations are not UNION compatible !
30
Union ExampleUnion Example
9.List the ESSN’s of employees who either have9.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]
ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) ∪∪ ΠΠ ESSNESSN (WORKS_ON )(WORKS_ON )
31
IntersectionIntersection
R1R1 ∩∩ R2R2
Produces a relation that includes the tuplesProduces a relation that includes the tuples
that appearthat appear
in 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 have
dependents 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]
ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) ∩∩ ΠΠ MGRSSNMGRSSN (DEPARTMENT )(DEPARTMENT )
33
DifferenceDifference
R1R1 -- R2R2
Produces a relation that includes all theProduces a relation that includes all the
tuples thattuples that
appear 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 have
dependents 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]
ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) —— ΠΠ ESSNESSN (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 ∪ A
associative (A ∪ B) ∪ C = A ∪ ( B ∪ C )
A∩ B commutative A ∩ B = B ∩ A
associative (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 right in theOperators performed left to right in the
expressionexpression
• ( ) can be used to alter operator precedence,( ) can be used to alter operator precedence,
that is operations in ( ) will be performed beforethat is operations in ( ) will be performed before
even if they have a lower precedence ordereven if they have a lower precedence order
HigherHigher
LowerLower
38
Precedence ExamplePrecedence Example
12.12. List all employees who are male, and eitherList 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]
ΠΠ EnameEname ((σσ (Sex = ‘M’ and (Salary < 40000 or Dnum = 5))(Sex = ‘M’ and (Salary < 40000 or Dnum = 5)) (EMPLOYEE )(EMPLOYEE ) ))
How does the above solution differ from the following?How does the above solution differ from the following?
ΠΠ EnameEname ((σσ (Sex = ‘M’ and Salary < 40000 or Dnum = 5)(Sex = ‘M’ and Salary < 40000 or Dnum = 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 Example
13. List all projects which are neither located in13. 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]
ΠΠ PnamePname ((σσ not (Plocation = ‘Brisbane’) and not (Dnum=4)not (Plocation = ‘Brisbane’) and not (Dnum=4) (PROJECT)(PROJECT)))
≡≡
ΠΠ PnamePname ((σσ not (Plocation = ‘Brisbane’ or Dnum=4)not (Plocation = ‘Brisbane’ or Dnum=4) (PROJECT)(PROJECT) ))
≡≡
ΠΠ PnamePname ((σσ not (Plocation <> ‘Brisbane’ and Dnum <> 4)not (Plocation <> ‘Brisbane’ and Dnum <> 4) (PROJECT)(PROJECT) ))
41
Cartesian ProductCartesian Product
R1R1 ×× R2R2
• Also known as a cross-product or cross-join R1Also known as a cross-product or cross-join R1
and R2 needand 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 withBm) is a relation Q with n + m attributesn + m attributes QQ
(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 tuplesQ has one tuple for each combination of tuples
from R1 and R2, thus if R1 has r tuples and R2from R1 and R2, thus if R1 has r tuples and R2
has t tuples, thenhas t tuples, then Q will haveQ 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 Example
14. For each female employee, list the names14. For each female employee, list the names
of all of her dependents.of all of her dependents.
EMPLOYEEEMPLOYEE [Ename,[Ename,SSNSSN,DOB,Address,Sex,Salary,SuperSSN, Dno],DOB,Address,Sex,Salary,SuperSSN, Dno]
DEPENDENTDEPENDENT [[ESSN, DepNameESSN, DepName, Sex, DOB, Relationship], Sex, DOB, Relationship]
FEMALE_EMPSFEMALE_EMPS ←← σσ Sex = ‘F’Sex = ‘F’ (EMPLOYEE)(EMPLOYEE)
EMP_NAMESEMP_NAMES ←← ΠΠ Ename, SSNEname, SSN (FEMALE_EMPS)(FEMALE_EMPS)
EMP_DEPENDEMP_DEPEND ←← EMP_NAMESEMP_NAMES ×× DEPENDENTDEPENDENT
ACTUAL_DEPENDACTUAL_DEPEND ←← σσ SSN = ESSNSSN = ESSN (EMP_DEPEND)(EMP_DEPEND)
RESULTRESULT ←← ΠΠ Ename, DepNameEname, DepName (ACTUAL_DEPEND)(ACTUAL_DEPEND)
44
Understanding RAUnderstanding 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 onlyA 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 twoIt 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 inThis 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
R1R1 ⋈⋈ < join condition>< join condition> R2R2
A join condition(s) is of the form AA join condition(s) is of the form A θθ B, where AB, where A
∈∈ R1 and BR1 and B ∈∈ R2,R2,
andand θθ is one of {=, <, ≤, >, ≥}is one of {=, <, ≤, >, ≥}
47
Thieta-Join ExampleThieta-Join Example
15. For each employee, list all the employees who15. 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]
AA ←← EMPLOYEEEMPLOYEE
BB ←← EMPLOYEEEMPLOYEE
RESULTRESULT ←← ΠΠ A.Ename, B.EnameA.Ename, B.Ename (A(A⋈⋈ A.Salary< B.SalaryA.Salary< B.Salary BB))
48
Equi-JoinEqui-Join
R1R1 ⋈⋈ < join condition>< join condition> R2R2
Specialization of JoinSpecialization of Join
Join condition only hasJoin condition only has equalityequality comparisons onlycomparisons only
49
Equi-Join ExampleEqui-Join Example
16. List the names of the managers of16. List the names of the managers of
eacheach
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 ←← DEPARTMENTDEPARTMENT ⋈⋈ MGRSSN = SSNMGRSSN = SSN EMPLOYEEEMPLOYEE
RESULTRESULT ←← ΠΠ EnameEname (DEPT_MGR)(DEPT_MGR)
50
Natural JoinNatural Join
R1R1 ** R2R2
• Similar to equi-join except that the attributes thatSimilar to equi-join except that the attributes that
are used for the join are those that have theare 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 following17. What is the result schema of the following
query? What attributes is the join performedquery? 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 Example
18. What is the difference between the results of18. What is the difference between the results of
the following queries? What attributes arethe following queries? What attributes are
thethe
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 * DEPARTMENT
andand
EMPEMP(MgrSSN ,Dnumber)(MgrSSN ,Dnumber) ←← ΠΠ SSN,DnoSSN,Dno (EMPLOYEE)(EMPLOYEE)
RESULTRESULT ←← EMP * DEPARTMENTEMP * DEPARTMENT
54
DivisionDivision
R1R1 ÷÷ R2R2
• Result relation contains columns in R1, but notResult relation contains columns in R1, but not
in R2in R2
• Relations R1 and R2 must beRelations R1 and R2 must be divisiondivision
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 isidentically named to columns in R2, where n is
the degree of R2the degree of R2
• The result relation contains tuples t, such that aThe result relation contains tuples t, such that a
value in t appears in R1, in combination withvalue 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 Example
22. Retrieve the names of employees who work22. Retrieve the names of employees who work
onon
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,, PnoPno, Hours ], Hours ]
SMITHSMITH ←← σσ Ename=‘John Smith’Ename=‘John Smith’ (EMPLOYEE)(EMPLOYEE)
SMITH_PNOSSMITH_PNOS ←← ΠΠ PnoPno (WORKS_ON(WORKS_ON ⋈⋈ ESSN=SSNESSN=SSN SMITH)SMITH)
SSN_PNOSSSN_PNOS ←← ΠΠ ESSN, PnoESSN, Pno (WORKS_ON)(WORKS_ON)
SSNS(SSN)SSNS(SSN) ←← SSN_PNOSSSN_PNOS ÷÷ SMITH_PNOSSMITH_PNOS
RESULTRESULT ←← ΠΠ 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 attributesIdentify 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 andIdentify 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 propertiesFormulate 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 ?
• SELECTSELECT ←←
• PROJECTPROJECT ←←
• UNIONUNION ←←
• INTERSECTIONINTERSECTION
• DIFFERENCEDIFFERENCE ←←
• CARTESIAN PRODUCTCARTESIAN PRODUCT ←←
• JOINJOIN
• DIVISIONDIVISION
• Use unary operators SELECT /Use unary operators SELECT /
PROJECT when choosing tuples /PROJECT when choosing tuples /
attributes respectively from a singleattributes respectively from a single
relationrelation
• Use binary operators UNION,Use binary operators UNION,
PRODUCT, JOIN, … when definingPRODUCT, JOIN, … when defining
the relationship between 2 or morethe relationship between 2 or more
relationsrelations
{{ σσ ,,ΠΠ,, U,U, ——,, × }× } Complete Set ofComplete Set of
OperationsOperations
60
Complete Set of RA OperatorsComplete Set of RA Operators
• It has been proved thatIt has been proved that {{σσ,, ΠΠ,, ∪∪,, ——,, ×}×} is ais a
complete set of RA operatorscomplete set of RA operators
• Each remaining relational algebra operator canEach remaining relational algebra operator can
be expressed as a sequence of operations frombe expressed as a sequence of operations from
this setthis set
• These remaining operators have been definedThese remaining operators have been defined
primarily for convenience !primarily for convenience !
61
Expressing other operatorsExpressing other operators
• IntersectionIntersection
RR ∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R ))∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R ))
• (Thieta/Equi) Join(Thieta/Equi) Join
RR <condition><condition> SS ≡≡ σσ <condition><condition> ( 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) ←← ΠΠ (A1, A2, A3, . . . An)(A1, A2, A3, . . . An) RR
R * SR * S ≡≡ ΠΠ (B1, A2, A3, ... An, B2, ... Bm)(B1, A2, A3, ... An, B2, ... Bm) σσ <R.B1 = S.B1><R.B1 = S.B1> ( R1( R1××S)S)
62
Expressing other operatorsExpressing other operators
• DivisionDivision
T1T1 ←← ΠΠ YY ( R )( R )
T2T2 ←← ΠΠ YY ( ( S( ( S ×× T1 )T1 ) —— R )R )
RR ÷ S ≡÷ S ≡ T1T1 — 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 Exercises
These exercises use the Company database as anThese exercises use the Company database as an
example to illustrate relational algebra queries thatexample 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 Exercise
23. Retrieve the name and address of all23. Retrieve the name and address of all
employeesemployees
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_DEPTRESEARCH_DEPT ←← σσ Dname=‘Research’Dname=‘Research’ (DEPARTMENT)(DEPARTMENT)
RESEARCH_DEPT_EMPSRESEARCH_DEPT_EMPS ←← (RESEARCH_DEPT(RESEARCH_DEPT⋈⋈ Dnumber=DnoDnumber=Dno EMPLOYEE)EMPLOYEE)
RESULTRESULT ←← ΠΠ Ename,AddressEname,Address (RESEARCH_DEPT_EMPS)(RESEARCH_DEPT_EMPS)
66
RA ExerciseRA Exercise
24. For every project located in Houston, list the project24. For every project located in Houston, list the project
number, the controlling department number, and thenumber, 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_PROJSHOUSTON_PROJS ←← σσ Plocation=‘houston’Plocation=‘houston’ (PROJECT)(PROJECT)
CONTR_DEPTCONTR_DEPT ←← (HOUSTON_PROJS(HOUSTON_PROJS ⋈⋈ Dnum=DnumberDnum=Dnumber DEPARTMENT)DEPARTMENT)
PROJ_DEPT_MGRPROJ_DEPT_MGR ←← (CONTR_DEPT(CONTR_DEPT⋈⋈ MgrSSN=SSNMgrSSN=SSN EMPLOYEE)EMPLOYEE)
RESULTRESULT ←← ΠΠ Pnumber,Dnum,Ename,Address,BdatePnumber,Dnum,Ename,Address,Bdate (PROJ_DEPT_MGR)(PROJ_DEPT_MGR)
67
RA ExerciseRA Exercise
25. Find the names of employees who work on all25. Find the names of employees who work on all
projects controlled by department 5.projects controlled by department 5.
EMPLOYEEEMPLOYEE [ Ename,[ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN,, DOB, Address, Sex, Salary, SuperSSN,
Dno]Dno]
PROJECTPROJECT [ 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_SSNSRESULT_EMP_SSNS ← E← EMP_PROJMP_PROJ ÷÷ DEPT5_PROJSDEPT5_PROJS
RESULTRESULT ←← ΠΠ EnameEname (RESULT_EMP_SSNS * EMPLOYEE )(RESULT_EMP_SSNS * EMPLOYEE )
68
RA ExerciseRA Exercise
26. List project numbers for projects that involve an employee26. List project numbers for projects that involve an employee
whose name is Smith, either as a worker or as a managerwhose name is Smith, either as a worker or as a manager
ofof
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_PROJSSMITH_WORKER_PROJS ←← ΠΠ PnoPno (WORKS_ON * SMITHS)(WORKS_ON * SMITHS)
MGRSMGRS ←← ΠΠ Ename,DnumberEname,Dnumber (EMPLOYEE(EMPLOYEE ⋈⋈ SSN=MgrSSNSSN=MgrSSN DEPARTMENT)DEPARTMENT)
SMITH_MGRSSMITH_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)
RESULTRESULT ←← SMITH_WORKER_PROJSSMITH_WORKER_PROJS ∪∪ SMITH_MGR_PROJSSMITH_MGR_PROJS
69
RA ExerciseRA Exercise
27. Retrieve the names of employees who have no27. 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_EMPSALL_EMPS ←← ΠΠ SSNSSN (EMPLOYEE)(EMPLOYEE)
EMPS_WITH_DEPS(SSN)EMPS_WITH_DEPS(SSN) ←← ΠΠ ESSNESSN (DEPENDENT)(DEPENDENT)
EMPS_WITHOUT_DEPSEMPS_WITHOUT_DEPS ←← ( ALL_EMPS( ALL_EMPS ——
EMPS_WITH_DEPS)EMPS_WITH_DEPS)
RESULTRESULT ←← ΠΠ EnameEname (EMPS_WITHOUT_DEPS * EMPLOYEE(EMPS_WITHOUT_DEPS * EMPLOYEE ))
70
RA ExerciseRA Exercise
28. List the names of managers who have at least28. List the names of managers who have at least
one dependent.one dependent.
EMPLOYEEEMPLOYEE [ Ename,[ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN,, DOB, Address, Sex, Salary, SuperSSN,
Dno]Dno]
DEPARTMENTDEPARTMENT [ Dname,[ Dname, DnumberDnumber, MgrSSN, MgrStart], MgrSSN, MgrStart]
DEPENDENTDEPENDENT [[ ESSNESSN,, Dep_NameDep_Name, Sex, DOB, Relationship], Sex, DOB, Relationship]
MGR(SSN)MGR(SSN) ←← ΠΠ MgrSSNMgrSSN (DEPARTMENT)(DEPARTMENT)
EMPS_WITH_DEPS(SSN)EMPS_WITH_DEPS(SSN) ←← ΠΠ ESSNESSN (DEPENDENT)(DEPENDENT)
MGRS_WITH_DEPSMGRS_WITH_DEPS ←← (MGRS(MGRS ∩∩ EMPS_WITH_DEPS)EMPS_WITH_DEPS)
RESULTRESULT ←← ΠΠ EnameEname (MGRS_WITH_DEPS * EMPLOYEE)(MGRS_WITH_DEPS * EMPLOYEE)
71
ReviewReview
• Relational algebra gives the theoretical foundations forRelational 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 sequenceRelational algebra expressions, consisting of a sequence
of relational algebra operators, specify a high-levelof relational algebra operators, specify a high-level
procedure to achieve a query resultprocedure to achieve a query result
• However, relational algebraic query formulation isHowever, relational algebraic query formulation is
procedural, and therefore focuses on how a query resultprocedural, and therefore focuses on how a query result
can be achievedcan be achieved
• Declarative query languages, e.g., SQL, allow the user toDeclarative query languages, e.g., SQL, allow the user to
specifyspecify whatwhat info the user wants rather thaninfo the user wants rather than howhow the resultthe result
is to be obtainedis to be obtained
72
Recommended ReadingsRecommended Readings
Elmasri & NavatheElmasri & Navathe
Chapter 7Chapter 7
73
Next ...Next ...
Module 7Module 7
Structured QueryStructured Query
Language (SQL)Language (SQL)

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Relational model
Relational modelRelational model
Relational model
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalization
 
ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...ER to relational Mapping: Data base design using ER to relational language. C...
ER to relational Mapping: Data base design using ER to relational language. C...
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Normalization
NormalizationNormalization
Normalization
 
Data Mining with R programming
Data Mining with R programmingData Mining with R programming
Data Mining with R programming
 
Dbms Lecture Notes
Dbms Lecture NotesDbms Lecture Notes
Dbms Lecture Notes
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
12 SQL
12 SQL12 SQL
12 SQL
 
Er diagrams presentation
Er diagrams presentationEr diagrams presentation
Er diagrams presentation
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Er model
Er modelEr model
Er model
 
Introduction to dataset
Introduction to datasetIntroduction to dataset
Introduction to dataset
 

Similar a Algebra relacional

database management system
database management systemdatabase management system
database management systemNivetha Ganesan
 
Deep Dive : Spark Data Frames, SQL and Catalyst Optimizer
Deep Dive : Spark Data Frames, SQL and Catalyst OptimizerDeep Dive : Spark Data Frames, SQL and Catalyst Optimizer
Deep Dive : Spark Data Frames, SQL and Catalyst OptimizerSachin Aggarwal
 
DataBase Management systems (IM).pptx
DataBase Management systems (IM).pptxDataBase Management systems (IM).pptx
DataBase Management systems (IM).pptxGooglePay16
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization Hafiz faiz
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuyVo27
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL ServerStéphane Fréchette
 
Change RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBChange RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBApaichon Punopas
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Rakibul Hasan Pranto
 
Access Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsAccess Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsTeamstudio
 
Discovering the plan cache (#SQLSat211)
Discovering the plan cache (#SQLSat211)Discovering the plan cache (#SQLSat211)
Discovering the plan cache (#SQLSat211)Jason Strate
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R langsenthil0809
 
Python Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL DatabasesPython Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL DatabasesMats Kindahl
 

Similar a Algebra relacional (20)

Database part2-
Database part2-Database part2-
Database part2-
 
database management system
database management systemdatabase management system
database management system
 
Deep Dive : Spark Data Frames, SQL and Catalyst Optimizer
Deep Dive : Spark Data Frames, SQL and Catalyst OptimizerDeep Dive : Spark Data Frames, SQL and Catalyst Optimizer
Deep Dive : Spark Data Frames, SQL and Catalyst Optimizer
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
Introduction to mysql part 1
Introduction to mysql part 1Introduction to mysql part 1
Introduction to mysql part 1
 
Rdbms
RdbmsRdbms
Rdbms
 
DataBase Management systems (IM).pptx
DataBase Management systems (IM).pptxDataBase Management systems (IM).pptx
DataBase Management systems (IM).pptx
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
 
Change RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBChange RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDB
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)
 
Module02
Module02Module02
Module02
 
Access Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsAccess Data from XPages with the Relational Controls
Access Data from XPages with the Relational Controls
 
Query compiler
Query compilerQuery compiler
Query compiler
 
Discovering the plan cache (#SQLSat211)
Discovering the plan cache (#SQLSat211)Discovering the plan cache (#SQLSat211)
Discovering the plan cache (#SQLSat211)
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
 
Python Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL DatabasesPython Utilities for Managing MySQL Databases
Python Utilities for Managing MySQL Databases
 

Último

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Último (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Algebra relacional

  • 2. 2 database Modulesdatabase Modules Module 1: Database SystemsModule 1: Database Systems Module 2: Relational ModelModule 2: Relational Model Module 3: Entity Relationship ModelModule 3: Entity Relationship Model Module 4: ER to Relational MappingModule 4: ER to Relational Mapping Module 5: FDs and NormalizationModule 5: FDs and Normalization Module 6: Relational AlgebraModule 6: Relational Algebra Module 7: SQLModule 7: SQL Module 8: Database SystemsModule 8: Database Systems ArchitectureArchitecture FundamentalFundamental ss √√ DataData ModelingModeling√√ DataData DesignDesign√√ DataData AccessAccess ArchitectureArchitecture
  • 3. 3 MotivationMotivation The relational data modelThe relational data model provides a means ofprovides a means of defining the databasedefining the database structure and constraintsstructure and constraints A data model must alsoA data model must also provide a set ofprovide a set of operations to manipulateoperations 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 all employees who earn more than 55Kemployees who earn more than 55K Increment the salary of all employeesIncrement the salary of all employees in the printing department by 10%in the printing department by 10% What is the address of employeeWhat is the address of employee JonesJones The basic set of relational model operationsThe basic set of relational model operations constitute the Relational Algebraconstitute the Relational Algebra
  • 4. 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. 5 What is a Relational QueryWhat is a Relational Query • Data in a relational database can beData in a relational database can be manipulated in the following ways:manipulated in the following 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 attributes in existing: Values of attributes in existing tuplestuples may be changedmay be changed –– RETRIEVERETRIEVE: Attributes of specific tuples,: Attributes of specific tuples, entireentire tuples, or even entire relations may betuples, or even entire relations may be retrievedretrieved • Relational Query Languages should provide allRelational Query Languages should provide all of the aboveof the above
  • 6. 6 Relational Query LanguagesRelational Query Languages Relational 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. 7 SQL and Relational AlgebraSQL and Relational Algebra SQLSQL • Declarative languageDeclarative language –– Users specify what theUsers specify what the result of the query shouldresult of the query should be, DBMS decidesbe, DBMS decides operations and order ofoperations and order of executionexecution • OperationsOperations –– Provides commands toProvides commands to create and modifycreate and modify database structure anddatabase structure and constraints (DDL)constraints (DDL) –– Provides commands toProvides commands to insert, delete, update andinsert, delete, update and retrieve (DML)retrieve (DML) RARA • Procedural languageProcedural language –– Algebraic expressionsAlgebraic expressions specify an order ofspecify an order of operations ie. How theoperations ie. How the query will be processedquery will be processed • OperationsOperations –– Provides operators, thatProvides operators, that enable a user to specifyenable a user to specify retrieval requests onlyretrieval requests only
  • 8. 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. 9 Relational AlgebraRelational Algebra OperationsOperations • Relational algebra operations are applied onRelational algebra operations are applied on relationsrelations • Result of relational algebra operations are alsoResult of relational algebra operations are also relations, i.e the algebra operations producerelations, i.e the algebra operations produce new relations from oldnew relations from old • A sequence of relational algebra operationsA sequence of relational algebra operations forms a relational algebra expression, whoseforms a relational algebra expression, whose result will also be a relationresult will also be a relation
  • 10. 10 Types of RA OperationsTypes of RA Operations • Set operations from mathematical set theorySet operations from mathematical set theory (Applicable because each relation is also a(Applicable because each relation is also a setset of tuples)of tuples) –– UNIONUNION –– INTERSECTIONINTERSECTION –– DIFFERENCEDIFFERENCE –– CARTESIAN PRODUCTCARTESIAN PRODUCT • Operations developed specifically for RDBsOperations developed specifically for RDBs –– SELECTSELECT –– PROJECTPROJECT –– JOINJOIN –– DIVISIONDIVISION
  • 11. 11 Operators and NotationOperators and Notation Traditional SetTraditional Set OperatorsOperators • IntersectionIntersection ∩∩ • UnionUnion ∪∪ • DifferenceDifference —— • Cartesian ProductCartesian Product ×× Specific DatabaseSpecific Database OperatorsOperators • SelectSelect σσ • ProjectProject ΠΠ • JoinJoin ⋈⋈ • DivisionDivision ÷÷
  • 12. 12 Understanding RAUnderstanding 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 ThirdDiscuss Third
  • 13. 13 SelectSelect σσ << selection condition >selection condition > ( < relation name > )( < relation name > ) Select those rows which satisfy a given condition ThisSelect those rows which satisfy a given condition This operation is also called “restriction”operation is also called “restriction” NAME SALAR Y ADDRESS DEPT Smith 50k St. Lucia Printing Dilbert 40k Taringa Printing Jones 60k Kenmore Printing Trump 65k Auchenflowe r Head Office Harriso n 78k St. Lucia Head Office SelectedSelected TuplesTuples
  • 14. 14 Select ExampleSelect Example 1. 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] σσ Dno = 4Dno = 4 (EMPLOYEE)(EMPLOYEE)
  • 15. 15 Select ExampleSelect Example 2. List all details of employees earning more than2. 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] σσ Salary > 30000Salary > 30000 (EMPLOYEE)(EMPLOYEE)
  • 16. 16 Select ExampleSelect Example 3. 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 work in 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 ∧∧ Salary > 25000)Salary > 25000) ∨∨ (Dno = 5 ∧ Salary > 30000)(Dno = 5 ∧ Salary > 30000) (EMPLOYEE)(EMPLOYEE)
  • 17. 17 ProjectProject ΠΠ< atribute column name´s< atribute column name´s >> ( < relation name > )( < relation name > ) Select those columns specified in the listSelect those columns specified in the list NAME SALAR Y ADDRESS DEPT Smith 50k St. Lucia Printing Dilbert 40k Taringa Printing Jones 60k Kenmore Printing Trump 65k Auchenflowe r Head Office Harriso n 78k St. Lucia Head Office SelectedSelected attributesattributes
  • 18. 18 Project ExampleProject Example 4. For each employee, list their name, date of birth4. 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,Bdate,SalaryEname,Bdate,Salary (EMPLOYEE)(EMPLOYEE)
  • 19. 19 Project ExampleProject Example 5. List the salaries paid to employees in each5. 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] ΠΠ Dno, SalaryDno, Salary (EMPLOYEE)(EMPLOYEE)
  • 20. 20 Handling Complex QueriesHandling Complex Queries Formulation of complex queries may requireFormulation of complex queries may require several relational algebra operations one afterseveral relational algebra operations one after the otherthe other –– Operations can be written as a single relationalOperations can be written as a single relational algebraalgebra 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 creatingcreating intermediateintermediate result relationsresult relations Intermediate Results have to beIntermediate Results have to be “assigned”“assigned” toto temporary relations which must betemporary relations which must be “named”“named”
  • 21. 21 Relation Assignment and NamingRelation Assignment and Naming • Relation AssignmentRelation Assignment –– Result RelationResult Relation ←← Relational ExpressionRelational Expression • Relation NamingRelation Naming –– TEMPTEMP ←← • Attribute (re)NamingAttribute (re)Naming –– TEMP (dept, emp-salary)TEMP (dept, emp-salary) ←←
  • 22. 22 Assignment ExampleAssignment Example 6. 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’ andLabel 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. 23 Assignment ExampleAssignment Example 7. List the names and salaries of all employees who7. 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] ΠΠ Ename,SalaryEname,Salary (( σσ Dno = 5Dno = 5 (EMPLOYEE )(EMPLOYEE ) )) EMPS - DEP5EMPS - DEP5 ←← σσ Dno = 5Dno = 5 (EMPLOYEE )(EMPLOYEE ) RESULTRESULT ←← ΠΠ Ename,SalaryEname,Salary (EMPS_ DEP5(EMPS_ DEP5)) Query withQuery with ExpressionExpression Query withQuery with IntermediateIntermediate RelationsRelations
  • 24. 24 Understanding RAUnderstanding RA OperationsOperations • SELECTSELECT • PROJECTPROJECT • Assignment and NamingAssignment and Naming • UNIONUNION • INTERSECTIONINTERSECTION • DIFFERENCEDIFFERENCE • Properties of operatorsProperties of operators • CARTESIAN PRODUCTCARTESIAN PRODUCT • JOINJOIN • DIVISIONDIVISION
  • 25. 25 Basic Set OperatorsBasic Set Operators • Relation is aRelation is a setset ofof tuples (no duplicates)tuples (no duplicates) • Set theory, andSet theory, and hence elementary sethence elementary set operators also applyoperators also apply to relationsto relations –– UNIONUNION –– INTERSECTIONINTERSECTION –– DIFFERENCEDIFFERENCE –– CARTESIANCARTESIAN PRODUCTPRODUCT Union A∪B Intersection A∩B Difference A - B A B A B A B
  • 26. 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 theThey have the same degree nsame degree n, (number of, (number of columns)columns) –– Their columnsTheir columns have corresponding domainshave corresponding domains, i.e, i.e dom(Ai) = dom(Bi) for 1dom(Ai) = dom(Bi) for 1 ≤≤ ii ≤≤ nn Applies to union, intersection and differenceApplies to union, intersection and difference
  • 27. 27 …… Union CompatibilityUnion Compatibility Although 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. 28 UnionUnion R1R1 ∪∪ R2R2 Produces a relation that includes all tuplesProduces a relation that includes all tuples that appear only in R1, or only in R2, or inthat 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. 29 Union ExampleUnion Example 8.Identify the employees who either work on8.Identify the employees who either work on projects or have dependents.projects or have dependents. WORKS_ONWORKS_ON [[ESSN, PNoESSN, PNo, Hours], Hours] DEPENDENTDEPENDENT [[ESSN, Dep_NameESSN, Dep_Name, Sex, DOB,, Sex, DOB, Relationship]Relationship] WORKS_ONWORKS_ON ∪∪ DEPENDENTDEPENDENT The relations are not UNION compatible !The relations are not UNION compatible !
  • 30. 30 Union ExampleUnion Example 9.List the ESSN’s of employees who either have9.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] ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) ∪∪ ΠΠ ESSNESSN (WORKS_ON )(WORKS_ON )
  • 31. 31 IntersectionIntersection R1R1 ∩∩ R2R2 Produces a relation that includes the tuplesProduces a relation that includes the tuples that appearthat appear in both R1 and R2.in both R1 and R2. R1 and R2 must be union compatible.R1 and R2 must be union compatible.
  • 32. 32 Intersection ExampleIntersection Example 11. List the ESSN’s of employees who have11. List the ESSN’s of employees who have dependents 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] ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) ∩∩ ΠΠ MGRSSNMGRSSN (DEPARTMENT )(DEPARTMENT )
  • 33. 33 DifferenceDifference R1R1 -- R2R2 Produces a relation that includes all theProduces a relation that includes all the tuples thattuples that appear 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. 34 Difference ExampleDifference Example 11. List the ESSN’s of employees who have11. List the ESSN’s of employees who have dependents 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] ΠΠ ESSNESSN (( DEPENDENT )DEPENDENT ) —— ΠΠ ESSNESSN (WORKS_ON )(WORKS_ON )
  • 35. 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. 36 Commutative and AssociativeCommutative and Associative A ∪ B Commutative A ∪ B = B ∪ A associative (A ∪ B) ∪ C = A ∪ ( B ∪ C ) A∩ B commutative A ∩ B = B ∩ A associative (A ∩ B) ∩ C = A ∩ ( B ∩ C ) A — B not commutative A — B ≠ B — A not commutative (A — B) — C ≠ A — (B — C )
  • 37. 37 Operator PrecedenceOperator Precedence ==, ≠,, ≠, <, >, ≤, ≥<, >, ≤, ≥ notnot andand oror σσ,, ΠΠ ∩∩, ∪, —, ×, +, ⋈, ∪, —, ×, +, ⋈ • Operators performed left to right in theOperators performed left to right in the expressionexpression • ( ) can be used to alter operator precedence,( ) can be used to alter operator precedence, that is operations in ( ) will be performed beforethat is operations in ( ) will be performed before even if they have a lower precedence ordereven if they have a lower precedence order HigherHigher LowerLower
  • 38. 38 Precedence ExamplePrecedence Example 12.12. List all employees who are male, and eitherList 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] ΠΠ EnameEname ((σσ (Sex = ‘M’ and (Salary < 40000 or Dnum = 5))(Sex = ‘M’ and (Salary < 40000 or Dnum = 5)) (EMPLOYEE )(EMPLOYEE ) )) How does the above solution differ from the following?How does the above solution differ from the following? ΠΠ EnameEname ((σσ (Sex = ‘M’ and Salary < 40000 or Dnum = 5)(Sex = ‘M’ and Salary < 40000 or Dnum = 5) (EMPLOYEE )(EMPLOYEE ) ))
  • 39. 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. 40 DeMorgan’s Law ExampleDeMorgan’s Law Example 13. List all projects which are neither located in13. 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] ΠΠ PnamePname ((σσ not (Plocation = ‘Brisbane’) and not (Dnum=4)not (Plocation = ‘Brisbane’) and not (Dnum=4) (PROJECT)(PROJECT))) ≡≡ ΠΠ PnamePname ((σσ not (Plocation = ‘Brisbane’ or Dnum=4)not (Plocation = ‘Brisbane’ or Dnum=4) (PROJECT)(PROJECT) )) ≡≡ ΠΠ PnamePname ((σσ not (Plocation <> ‘Brisbane’ and Dnum <> 4)not (Plocation <> ‘Brisbane’ and Dnum <> 4) (PROJECT)(PROJECT) ))
  • 41. 41 Cartesian ProductCartesian Product R1R1 ×× R2R2 • Also known as a cross-product or cross-join R1Also known as a cross-product or cross-join R1 and R2 needand 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 withBm) is a relation Q with n + m attributesn + m attributes QQ (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 tuplesQ has one tuple for each combination of tuples from R1 and R2, thus if R1 has r tuples and R2from R1 and R2, thus if R1 has r tuples and R2 has t tuples, thenhas t tuples, then Q will haveQ will have r * t tuplesr * t tuples
  • 42. 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. 43 Cartesian Product ExampleCartesian Product Example 14. For each female employee, list the names14. For each female employee, list the names of all of her dependents.of all of her dependents. EMPLOYEEEMPLOYEE [Ename,[Ename,SSNSSN,DOB,Address,Sex,Salary,SuperSSN, Dno],DOB,Address,Sex,Salary,SuperSSN, Dno] DEPENDENTDEPENDENT [[ESSN, DepNameESSN, DepName, Sex, DOB, Relationship], Sex, DOB, Relationship] FEMALE_EMPSFEMALE_EMPS ←← σσ Sex = ‘F’Sex = ‘F’ (EMPLOYEE)(EMPLOYEE) EMP_NAMESEMP_NAMES ←← ΠΠ Ename, SSNEname, SSN (FEMALE_EMPS)(FEMALE_EMPS) EMP_DEPENDEMP_DEPEND ←← EMP_NAMESEMP_NAMES ×× DEPENDENTDEPENDENT ACTUAL_DEPENDACTUAL_DEPEND ←← σσ SSN = ESSNSSN = ESSN (EMP_DEPEND)(EMP_DEPEND) RESULTRESULT ←← ΠΠ Ename, DepNameEname, DepName (ACTUAL_DEPEND)(ACTUAL_DEPEND)
  • 44. 44 Understanding RAUnderstanding RA OperationsOperations • SELECTSELECT • PROJECTPROJECT • Assignment and NamingAssignment and Naming • UNIONUNION • INTERSECTIONINTERSECTION • DIFFERENCEDIFFERENCE • Properties of operatorsProperties of operators • CARTESIAN PRODUCTCARTESIAN PRODUCT • JOINJOIN • DIVISIONDIVISION
  • 45. 45 Join OperationsJoin Operations • A Join is similar to Cartesian Product, but onlyA 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 twoIt 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 inThis 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. 46 Thieta-JoinThieta-Join R1R1 ⋈⋈ < join condition>< join condition> R2R2 A join condition(s) is of the form AA join condition(s) is of the form A θθ B, where AB, where A ∈∈ R1 and BR1 and B ∈∈ R2,R2, andand θθ is one of {=, <, ≤, >, ≥}is one of {=, <, ≤, >, ≥}
  • 47. 47 Thieta-Join ExampleThieta-Join Example 15. For each employee, list all the employees who15. 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] AA ←← EMPLOYEEEMPLOYEE BB ←← EMPLOYEEEMPLOYEE RESULTRESULT ←← ΠΠ A.Ename, B.EnameA.Ename, B.Ename (A(A⋈⋈ A.Salary< B.SalaryA.Salary< B.Salary BB))
  • 48. 48 Equi-JoinEqui-Join R1R1 ⋈⋈ < join condition>< join condition> R2R2 Specialization of JoinSpecialization of Join Join condition only hasJoin condition only has equalityequality comparisons onlycomparisons only
  • 49. 49 Equi-Join ExampleEqui-Join Example 16. List the names of the managers of16. List the names of the managers of eacheach 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 ←← DEPARTMENTDEPARTMENT ⋈⋈ MGRSSN = SSNMGRSSN = SSN EMPLOYEEEMPLOYEE RESULTRESULT ←← ΠΠ EnameEname (DEPT_MGR)(DEPT_MGR)
  • 50. 50 Natural JoinNatural Join R1R1 ** R2R2 • Similar to equi-join except that the attributes thatSimilar to equi-join except that the attributes that are used for the join are those that have theare 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. 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. 52 Natural Join ExampleNatural Join Example 17. What is the result schema of the following17. What is the result schema of the following query? What attributes is the join performedquery? 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. 53 Natural Join ExampleNatural Join Example 18. What is the difference between the results of18. What is the difference between the results of the following queries? What attributes arethe following queries? What attributes are thethe 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 * DEPARTMENT andand EMPEMP(MgrSSN ,Dnumber)(MgrSSN ,Dnumber) ←← ΠΠ SSN,DnoSSN,Dno (EMPLOYEE)(EMPLOYEE) RESULTRESULT ←← EMP * DEPARTMENTEMP * DEPARTMENT
  • 54. 54 DivisionDivision R1R1 ÷÷ R2R2 • Result relation contains columns in R1, but notResult relation contains columns in R1, but not in R2in R2 • Relations R1 and R2 must beRelations R1 and R2 must be divisiondivision 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 isidentically named to columns in R2, where n is the degree of R2the degree of R2 • The result relation contains tuples t, such that aThe result relation contains tuples t, such that a value in t appears in R1, in combination withvalue in t appears in R1, in combination with every tuple in R2every tuple in R2
  • 55. 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. 56 Division ExampleDivision Example 22. Retrieve the names of employees who work22. Retrieve the names of employees who work onon 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,, PnoPno, Hours ], Hours ] SMITHSMITH ←← σσ Ename=‘John Smith’Ename=‘John Smith’ (EMPLOYEE)(EMPLOYEE) SMITH_PNOSSMITH_PNOS ←← ΠΠ PnoPno (WORKS_ON(WORKS_ON ⋈⋈ ESSN=SSNESSN=SSN SMITH)SMITH) SSN_PNOSSSN_PNOS ←← ΠΠ ESSN, PnoESSN, Pno (WORKS_ON)(WORKS_ON) SSNS(SSN)SSNS(SSN) ←← SSN_PNOSSSN_PNOS ÷÷ SMITH_PNOSSMITH_PNOS RESULTRESULT ←← ΠΠ EnameEname (SSNS * EMPLOYEE)(SSNS * EMPLOYEE)
  • 57. 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. 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 attributesIdentify 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 andIdentify 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 propertiesFormulate the query keeping in mind operator properties (Commutative/Associative, Order precedence, De(Commutative/Associative, Order precedence, De Morgan’s Laws)Morgan’s Laws)
  • 59. 59 Which RA Operator to use ?Which RA Operator to use ? • SELECTSELECT ←← • PROJECTPROJECT ←← • UNIONUNION ←← • INTERSECTIONINTERSECTION • DIFFERENCEDIFFERENCE ←← • CARTESIAN PRODUCTCARTESIAN PRODUCT ←← • JOINJOIN • DIVISIONDIVISION • Use unary operators SELECT /Use unary operators SELECT / PROJECT when choosing tuples /PROJECT when choosing tuples / attributes respectively from a singleattributes respectively from a single relationrelation • Use binary operators UNION,Use binary operators UNION, PRODUCT, JOIN, … when definingPRODUCT, JOIN, … when defining the relationship between 2 or morethe relationship between 2 or more relationsrelations {{ σσ ,,ΠΠ,, U,U, ——,, × }× } Complete Set ofComplete Set of OperationsOperations
  • 60. 60 Complete Set of RA OperatorsComplete Set of RA Operators • It has been proved thatIt has been proved that {{σσ,, ΠΠ,, ∪∪,, ——,, ×}×} is ais a complete set of RA operatorscomplete set of RA operators • Each remaining relational algebra operator canEach remaining relational algebra operator can be expressed as a sequence of operations frombe expressed as a sequence of operations from this setthis set • These remaining operators have been definedThese remaining operators have been defined primarily for convenience !primarily for convenience !
  • 61. 61 Expressing other operatorsExpressing other operators • IntersectionIntersection RR ∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R ))∩ S ≡ ( R ∪ S ) – (( R — S ) ∪ ( S — R )) • (Thieta/Equi) Join(Thieta/Equi) Join RR <condition><condition> SS ≡≡ σσ <condition><condition> ( 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) ←← ΠΠ (A1, A2, A3, . . . An)(A1, A2, A3, . . . An) RR R * SR * S ≡≡ ΠΠ (B1, A2, A3, ... An, B2, ... Bm)(B1, A2, A3, ... An, B2, ... Bm) σσ <R.B1 = S.B1><R.B1 = S.B1> ( R1( R1××S)S)
  • 62. 62 Expressing other operatorsExpressing other operators • DivisionDivision T1T1 ←← ΠΠ YY ( R )( R ) T2T2 ←← ΠΠ YY ( ( S( ( S ×× T1 )T1 ) —— R )R ) RR ÷ S ≡÷ S ≡ T1T1 — T2— T2
  • 63. 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. 64 Relational Algebra ExercisesRelational Algebra Exercises These exercises use the Company database as anThese exercises use the Company database as an example to illustrate relational algebra queries thatexample 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. 65 RA ExerciseRA Exercise 23. Retrieve the name and address of all23. Retrieve the name and address of all employeesemployees 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_DEPTRESEARCH_DEPT ←← σσ Dname=‘Research’Dname=‘Research’ (DEPARTMENT)(DEPARTMENT) RESEARCH_DEPT_EMPSRESEARCH_DEPT_EMPS ←← (RESEARCH_DEPT(RESEARCH_DEPT⋈⋈ Dnumber=DnoDnumber=Dno EMPLOYEE)EMPLOYEE) RESULTRESULT ←← ΠΠ Ename,AddressEname,Address (RESEARCH_DEPT_EMPS)(RESEARCH_DEPT_EMPS)
  • 66. 66 RA ExerciseRA Exercise 24. For every project located in Houston, list the project24. For every project located in Houston, list the project number, the controlling department number, and thenumber, 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_PROJSHOUSTON_PROJS ←← σσ Plocation=‘houston’Plocation=‘houston’ (PROJECT)(PROJECT) CONTR_DEPTCONTR_DEPT ←← (HOUSTON_PROJS(HOUSTON_PROJS ⋈⋈ Dnum=DnumberDnum=Dnumber DEPARTMENT)DEPARTMENT) PROJ_DEPT_MGRPROJ_DEPT_MGR ←← (CONTR_DEPT(CONTR_DEPT⋈⋈ MgrSSN=SSNMgrSSN=SSN EMPLOYEE)EMPLOYEE) RESULTRESULT ←← ΠΠ Pnumber,Dnum,Ename,Address,BdatePnumber,Dnum,Ename,Address,Bdate (PROJ_DEPT_MGR)(PROJ_DEPT_MGR)
  • 67. 67 RA ExerciseRA Exercise 25. Find the names of employees who work on all25. Find the names of employees who work on all projects controlled by department 5.projects controlled by department 5. EMPLOYEEEMPLOYEE [ Ename,[ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN,, DOB, Address, Sex, Salary, SuperSSN, Dno]Dno] PROJECTPROJECT [ 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_SSNSRESULT_EMP_SSNS ← E← EMP_PROJMP_PROJ ÷÷ DEPT5_PROJSDEPT5_PROJS RESULTRESULT ←← ΠΠ EnameEname (RESULT_EMP_SSNS * EMPLOYEE )(RESULT_EMP_SSNS * EMPLOYEE )
  • 68. 68 RA ExerciseRA Exercise 26. List project numbers for projects that involve an employee26. List project numbers for projects that involve an employee whose name is Smith, either as a worker or as a managerwhose name is Smith, either as a worker or as a manager ofof 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_PROJSSMITH_WORKER_PROJS ←← ΠΠ PnoPno (WORKS_ON * SMITHS)(WORKS_ON * SMITHS) MGRSMGRS ←← ΠΠ Ename,DnumberEname,Dnumber (EMPLOYEE(EMPLOYEE ⋈⋈ SSN=MgrSSNSSN=MgrSSN DEPARTMENT)DEPARTMENT) SMITH_MGRSSMITH_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) RESULTRESULT ←← SMITH_WORKER_PROJSSMITH_WORKER_PROJS ∪∪ SMITH_MGR_PROJSSMITH_MGR_PROJS
  • 69. 69 RA ExerciseRA Exercise 27. Retrieve the names of employees who have no27. 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_EMPSALL_EMPS ←← ΠΠ SSNSSN (EMPLOYEE)(EMPLOYEE) EMPS_WITH_DEPS(SSN)EMPS_WITH_DEPS(SSN) ←← ΠΠ ESSNESSN (DEPENDENT)(DEPENDENT) EMPS_WITHOUT_DEPSEMPS_WITHOUT_DEPS ←← ( ALL_EMPS( ALL_EMPS —— EMPS_WITH_DEPS)EMPS_WITH_DEPS) RESULTRESULT ←← ΠΠ EnameEname (EMPS_WITHOUT_DEPS * EMPLOYEE(EMPS_WITHOUT_DEPS * EMPLOYEE ))
  • 70. 70 RA ExerciseRA Exercise 28. List the names of managers who have at least28. List the names of managers who have at least one dependent.one dependent. EMPLOYEEEMPLOYEE [ Ename,[ Ename, SSNSSN, DOB, Address, Sex, Salary, SuperSSN,, DOB, Address, Sex, Salary, SuperSSN, Dno]Dno] DEPARTMENTDEPARTMENT [ Dname,[ Dname, DnumberDnumber, MgrSSN, MgrStart], MgrSSN, MgrStart] DEPENDENTDEPENDENT [[ ESSNESSN,, Dep_NameDep_Name, Sex, DOB, Relationship], Sex, DOB, Relationship] MGR(SSN)MGR(SSN) ←← ΠΠ MgrSSNMgrSSN (DEPARTMENT)(DEPARTMENT) EMPS_WITH_DEPS(SSN)EMPS_WITH_DEPS(SSN) ←← ΠΠ ESSNESSN (DEPENDENT)(DEPENDENT) MGRS_WITH_DEPSMGRS_WITH_DEPS ←← (MGRS(MGRS ∩∩ EMPS_WITH_DEPS)EMPS_WITH_DEPS) RESULTRESULT ←← ΠΠ EnameEname (MGRS_WITH_DEPS * EMPLOYEE)(MGRS_WITH_DEPS * EMPLOYEE)
  • 71. 71 ReviewReview • Relational algebra gives the theoretical foundations forRelational 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 sequenceRelational algebra expressions, consisting of a sequence of relational algebra operators, specify a high-levelof relational algebra operators, specify a high-level procedure to achieve a query resultprocedure to achieve a query result • However, relational algebraic query formulation isHowever, relational algebraic query formulation is procedural, and therefore focuses on how a query resultprocedural, and therefore focuses on how a query result can be achievedcan be achieved • Declarative query languages, e.g., SQL, allow the user toDeclarative query languages, e.g., SQL, allow the user to specifyspecify whatwhat info the user wants rather thaninfo the user wants rather than howhow the resultthe result is to be obtainedis to be obtained
  • 72. 72 Recommended ReadingsRecommended Readings Elmasri & NavatheElmasri & Navathe Chapter 7Chapter 7
  • 73. 73 Next ...Next ... Module 7Module 7 Structured QueryStructured Query Language (SQL)Language (SQL)