Uploaded by busybeingbored1

Relational ALgebra Calculus (1)

advertisement
Database Systems
(5COM2005)
Database Systems
Relational Algebra and
Calculus
Databases Systems
What we cover in this session
• Review Relational Algebra
• Relational Calculus
–
–
–
–
–
Prediction
Proposition
Tuples Relational Calculus
Domain Relational Calculus
Quantifier
Database Systems
Relational Algebra- Review
Relational algebra defines theoretical way of manipulating table contents using
relational operators
• Relational Algebra
–
–
–
–
–
–
–
–
SELECT -> (restriction)
PROJECT ->
CARTESIAN PRODUCT
JOIN (NATURAL, EQUI, THETA, Outer Join)
UNION, ∪
INTERSECT, ∩
DIFFERENCE, - (MINUS)
*DIVISION, ÷
Database Systems
Relational Algebra- Review
Database Systems
Relational Algebra- Review
Database Systems
08:41
Relation Algebra Review - example
List the Department Name, Building Name, Floor and Position of
all departments in the Graves House building.
Step 1- JOIN two relations to provide attributes needed
FloorSpace ⋈DepartmentNo Department
Step 2 - SELECTION of records related to Graves House
BuildingName=’Graves House’ (Step1)
Step 3 -
PROJECTION of attributes listed above
DepartmentName,BuildingName,Floor,Position (Step2)
Data Modelling for Databases
More on Relational Operator
• Aggregation and Grouping Operations
• Aggregation
– Summation or aggregation of data
– E.g. total at the bottom of a report
• Grouping
– Sub summation of data
– E.g. to subtotal in a report
Data Modelling for Databases
Aggregation Operator - Definition
• Applies the aggregation function list, AL, to
the relation R, where AL contains one or more
(<aggregate_function>,<attribute>) pairs
• Main Aggregation functions are:
–
–
–
–
–
COUNT
SUM
AVG
MIN
MAX
Data Modelling for Databases
Aggregation Operator fAL(R) - Example
• How many properties cost more than £350
per month to rent?
– Which aggregation function to use ? (count)
– What is the condition? Rent > 350
ρ R(myCount)fCOUNT propertyNo ( rent > 30 (PropertyForRent))
Equivalent SQL
Select count(*) as myCount from PropertyForRent where rent > 300
• Find the minimum, maximum and
average salary of staff
Data Modelling for Databases
Grouping Operator GA f AL(R)- Definition
• Groups the tuples of relation R by the grouping attributes, GA,
and then apply aggregation function AL, where AL contains
one or more <aggregate_function>, <attribute > pairs
a1, a2, …, an f <A1 a1>, <A2 a2>, ….<Ak ak>(R)
• The tuple of the R are partitioned into groups such that:
– All the tuples in a group have same value for a1, a2, …, an
– Tuples in different groups have different values for a1, a2,
…, an.
Data Modelling for Databases
Grouping Operator- Example
• Find the number of staff working in each brand and the sum
of their salaries
ρR(branchNo,myCount, mySum)branchNo F COUNT staffNo, SUM
salary(Staff)
Equivalent SQL
Select branchNo, count(*) as myCount, sum(salary) as mySum
From Staff
Group by branchNo
Data Modelling for Databases
Relational Calculus
• Relation calculus is a formal query language where we write
one declarative expression to specify a retrieval request
• Focus on what is to retrieve than how to retrieve it
• Therefore, it is a nonprocedural language
• However, relation algebra and relational calculus are
equivalent to one another
• Two types:
– Tuple Relational Calculus – focus on finding tuples for which a predicate is true
– Domain Relational Calculus – domain variables take their values from domain of
attributes rather than tuples of relations
Predicate:- In first-order login or predicate calculus, a predicate is a truth-valued function
with argument e.g. if P(x) is either true or false for x.
Data Modelling for Databases
Tuples Relational Calculus
• In tuple relational calculus we try to find
tuples for which the given predicate is true
– {t|F(t)} find the set of all tuples S such
that F(t) is true.
– Tuple variable, t range over a particular
database relation.
Data Modelling for Databases
Tuples Relational Calculus - Example
• Example
{t|Employee(t) and t.SALARY > 5000}
Here we have two condition:
• Range condition – t belongs to employee
• Attribute condition – salary of the tuple is
greater than 5000
Equivalent SQL
Select * from Employee where salary > 5000
Data Modelling for Databases
Tuples Calculus - Expression
• Tuple calculation expression should have
– for each tuple variable t, the range relation
R of t – EMPLOYEE(t)
– A condition to select particular
combination of tuple, selected
combinations
– A set of attributes to be retrieved, the
request attributes
Data Modelling for Databases
Tuples Calculus - Expression
Query - Retrieve the birthday and address of the
employee whose name is “Mike Miller”
{t.bDate,t.address|EMPLOYEE(t) and t.FNAME=‘Mike’
and t.LNAME=‘Miller’}
Range Relation
– EMPLOYEE
Selected Combination – t.FNAME = ‘Mike’ and
t.LNAME=‘Miller’
Requested Attribute - bDate, address
Data Modelling for Databases
Tuples Calculus – Expressions and Formulas
A general expression for a tuple relational calculus can
be formulated as:
{t1.A1, t2.A2, …, tn.An | COND(t1, t2, …, tn, tn+1, … tn+m)}
where,
t1, t2, …., tn+m are tuple variables
Ai is an attribute of the relation on which t1 ranges
COND is a condition
Data Modelling for Databases
Existential and Universal Quantifiers
There are two quantifiers, that can be used with
formulae to tell how many instances that predicate
applies to.
– Existential Quantifier, ∀
Also known as ‘there exist’ is used to formulate that must be
true for at least one instance from a relation R
– Universal Quantifier, ∃
Also known as ‘for all’, is used to formulate that must be true
for every instance belongs to relation R
Data Modelling for Databases
Existential Quantifiers - Example
Query
Retrieve the name and address of all employees who
work for ‘Research’ department
{t.fName,t.lName,t.address | Employee(t) and ∃d
(Department(d) and d.Name=‘Research ’ and d.number =
t.dNo}
Data Modelling for Databases
Transforming the Universal and Existential Quantifiers
(∀x)(P(x)) -> not(∃x)(not(P(x)))
(∃ x)(P(x)) -> not(∀x)(not(P(x)))
(∀x)(P(x) and Q(x))
-> not (∃x)not(P(x) and Q(x))
-> not (∃x)(not(P(x) or not( Q(x))))
(∀x)(P(x) or Q(x))
-> not (∃x)not(P(x) or Q(x))
-> not (∃x)(not(P(x) and not( Q(x))))
Data Modelling for Databases
Existential Quantifiers - Example
Query
Find the names of employees who have no dependents
{t.fName,t.lName| Employee(t) and not (∃d (Department(d) and
d.number = t.dNo)}
Appy transforming to use universal quantifier
-> {t.fName,t.lName| Employee(t) and (not(∃d (Department(d)) or not(
d.number = t.dNo))}
-> {t.fName,t.lName| Employee(t) and (∀ d) (not (Department(d)) or not(
d.number = t.dNo))}
Data Modelling for Databases
Database Systems
(5COM2005)
Database Systems
Download