• No results found

RELATIONAL ALGEBRA

N/A
N/A
Protected

Academic year: 2021

Share "RELATIONAL ALGEBRA"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

RELATIONAL ALGEBRA

Chapter 6

1

(2)

“Relational” Mathematics

⚫ A mathematical basis is a great way to formally express the myriad of requests we may want to make

⚫ Operations from Algebra and Set Theory

2

(3)

The Relational Algebra and Relational Calculus

Relational algebra

◦ Basic set of operations for the relational model

Relational algebra expression

◦ Sequence of relational algebra operations

Relational calculus

◦ Higher-level declarative language for

specifying relational queries

(4)

Relational Algebra

⚫ Unary Relational Operations

◦ SELECT (symbol:

σ

(sigma))

PROJECT (symbol: π (pi))

RENAME (symbol: ρ (rho))

• Relational Algebra Operations From Set Theory

◦ UNION ( ∪ ), INTERSECTION ( ∩ ), DIFFERENCE (or MINUS, – )

CARTESIAN PRODUCT ( x )

4

(5)

Operations

Operations: have relations as inputs and outputs (i.e., produce new relations)

◦ The result of an operation is a new relation, which may have been formed from one or more input relations

◦ The algebra is “closed” (all objects in relational algebra are relations)

Expressions: A sequence of relational algebra operations

The result of a relational algebra expression is also a relation that represents the result of a database query (or retrieval request)

5

(6)

Unary Relational Operations

SELECT operation - σ (sigma)

◦ Unary -- Applied to a single relation

• <selection condition> applied independently to each individual tuple t in R

If condition evaluates to TRUE, tuple selected

• <attribute > <op> [ <constant > | <attribute name>]

• Boolean conditions AND, OR, and NOT

(7)

SELECT Operation

Examples:

◦ Select the EMPLOYEE tuples whose department number is 4:

σ

DNO = 4

(EMPLOYEE)

◦ Select the EMPLOYEE tuples whose salary is greater than $30,000:

σ

SALARY > 30,000

(EMPLOYEE)

Also,

(8)

σ (Dno = 4 AND Salary > 25000)

OR (Dno = 5 AND Salary > 30000) (EMPLOYEE)

(9)

SELECT operation

Nested application of SELECT operations o σ

<cond1>

< cond2>

(R)) ≡

σ <cond1> AND < cond2> (R)

Is SELECT Commutative?

o Yes! ☺

o σ <condition1>< condition2> (R)) = σ <condition2>< condition1> (R))

Cascading of the SELECT operation may be applied in any order:

o σ

<cond1>

<cond2>

<cond3>

(R))) = σ

<cond2>

<cond3>

<cond1>

( R)))

9

(10)

PROJECT Operation - π (pi)

Selects columns from table and discards the other columns:

Eliminates duplicates

◦ Result of PROJECT operation is a set of distinct tuples

⚫ Project each employee’s first, last name, and salary:

π

LNAME, FNAME,SALARY

(EMPLOYEE)

(11)

π Lname, Fname, Salary (EMPLOYEE)

(12)

PROJECT operation

⚫ Nested application of PROJECT operations

o π

<list1>

<list2>

(R))

o Attributes in <list2> contains the attributes in

<list1>

⚫ Is PROJECT Commutative?

o No ☹

o π

<list1>

<list2>

(R)) ≠ π

<list2>

<list1>

(R))

12

(13)

Nesting Different Operators

List birthdates of employees in departments 3, 5, 7.

σ

Dno = 3 OR Dno = 5 OR Dno = 7

(EMPLOYEE) π

Bdate

(EMPLOYEE)

13

π

Bdate

Dno = 3 OR Dno = 5 OR Dno = 7

(EMPLOYEE))

(14)

RENAME Operation - ρ (rho)

⚫ Rename attributes in intermediate results

⚫ In-line expression:

⚫ Sequence of operations

≈ ρ

(15)

RENAME Operation

ρ

S

(R) only changes:

◦ The relation name to S

ρ

(B1, B2, …, Bn )

(R ) only changes:

◦ The column (attribute) names to B1, B1, …..Bn

ρ

S (B1, B2, …, Bn )

(R) changes both:

◦ The relation name to S, and

◦ The column (attribute) names to B1, B1, …..Bn

ρ RESULT (F,M,L,S,B,A,SX,SAL,SU, DNO) (EMPLOYEE)

(16)

RELATIONAL ALGEBRA

Set Theory

16

(17)

UNION, INTERSECTION, and MINUS

⚫ Merge the elements of two sets in various ways

⚫ Binary operations

⚫ Relations must have the same type of tuples

17

R S

(18)

Relational Algebra Operations from Set Theory

UNION

R ∪ S

◦ Includes all tuples in either R, or S, or in both R and S

INTERSECTION

R S

◦ Includes all tuples that are in both R and S

SET DIFFERENCE (or MINUS)

R S

◦ Includes all tuples in R but not in S

R S

R S

R S

(19)

UNION operation -

⚫ The result of R ∪ S, is a relation that includes all tuples that are either in R or in S or in both R and S

⚫ Duplicate tuples are eliminated

⚫ R and S must be “type compatible” (or UNION compatible)

19

R S

(20)

Type Compatibility

⚫ R (A

1

, A

2

, ..., A

n

) and S(B

1

, B

2

, ..., B

n

) are type compatible if:

◦ They have the same number of attributes

◦ The domains of corresponding attributes are

type compatible i.e., dom(A

i

) = dom(B

i

) for i =

1, 2, ..., n.

(21)

UNION Operation Example

List the social security numbers of all employees who either work in Dept. 5 or directly supervise an employee who works in Dept. 5.

DEP5_EMPS ← σDno=5 (EMPLOYEE) RESULT1 ← π Ssn(DEP5_EMPS)

RESULT2(SSN) ← πSuper_ssn(DEP5_EMPS) RESULT ← RESULT1 ∪ RESULT2

(22)

INTERSECTION operation- ∩

⚫ R ∩ S includes all tuples that are in both R and S

⚫ The attribute names in the result will be the same as the attribute names in R

⚫ The two operand relations R and S must be “type compatible”

22

R S

(23)

DIFFERENCE operator –

Also called MINUS or EXCEPT

⚫ The result of R – S, is a relation that

includes all tuples that are in R but not in S

⚫ The attribute names in the result will be the same as the attribute names in R

⚫ The two operand relations R and S must be “type compatible”

23

R S

(24)

UNION, INTERSECT, and DIFFERENCE

STUDENT ∪ INSTRUCTOR

STUDENT INSTRUCTOR

STUDENT

INSTRUCTOR INSTRUCTOR - STUDENT

(25)

UNION, INTERSECT, and DIFFERENCE

Both union and intersection are commutative operations:

◦ R ∪ S = S ∪ R, and R ∩ S = S ∩ R

Both union and intersection can be treated as n-ary operations

◦ as both are associative operations

◦ R ∪ (S ∪ T) = (R ∪ S) ∪ T

◦ (R ∩ S) ∩ T = R ∩ (S ∩ T)

The minus operation is not commutative

◦ R – S ≠ S – R

(26)

Joins

⚫ Join information between 2 or more tables

Binary operators

◦ Cross Product - X

◦ Theta - θ

◦ Equi-join

◦ Natural

26

(27)

The CARTESIAN operation - X

CROSS PRODUCT or CROSS JOIN

Combine tuples from two relations in a combinatorial fashion, i.e. an exhaustive pairing of tuples

Q = R(A

1

, A

2

, . . ., A

n

) x S(B

1

, B

2

, . . ., B

m

)

Relation Q has degree n + m attributes:

Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.

The two operands do NOT have to be "type compatible”

When is this operation useful?

27

(28)

Database COMPAN Y Ex., Find all

dependants of female employes

Not Join

Compatible

(29)

Find all dependants of female employes

FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE)

EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)

(30)

EMP_DEPENDENTS ← EMPNAMES x DEPENDENT

contains every combination

(31)

ACTUAL_DEPS ←

σ SSN=ESSN(EMP_DEPENDENTS) RESULT ←

π FNAME, LNAME, DEPENDENT_NAME

(ACTUAL_DEPS)

Follow with σ / π

(32)

The JOIN Operation

⚫ Denoted by

⚫ Combine related tuples from two relations

⚫ General join condition of the form

<condition> AND <condition>

AND...AND <condition>

⚫ Example:

32

(33)

THETA JOIN Operation

⚫ R

θ

S

◦ θ can be any general boolean expression on the attributes of R and S

◦ Each term of θ relates a tuple from R with a tuple from S using any {<, ≤, >, ≥, =, ≠}

In practice, θ is constructed with just one or more equality conditions “AND”ed together:

θ ← R.Ai = S.Bj AND R.Ak = S.Bl AND R.Ap = S.Bq

◦ Each attribute pair is called a join attribute

33

(34)

Variations of JOIN

EQUIJOIN

◦ Only = comparison operator used

◦ Always have one or more pairs of attributes that have identical values in every tuple

NATURAL JOIN

◦ Denoted by *

◦ Removes second (superfluous) attribute in an

EQUIJOIN condition

(35)

35

Retrieve the name of the manager of each department

Combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple.

Equijoin

DEPT_MGR ← DEPARTMENT

MGRSSN = SSN

EMPLOYEE

(36)

NATURAL JOIN Operator

A simplification of an Equijoin joins join attributes with the same name

Q ← R(A,B,C,D) * S(C,D,E)

◦ The implicit join condition is

● R.C = S.C AND R.D = S.D

◦ Resulting relation Q(A, B, C, D, E)

36

(37)

List information about each department including its location

Combine DEPARTMENT with DEPT_LOCATIONS

DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS

37

(38)

38

Use EQUIJOIN

◦ π

Fname, Lname

(EMPLOYEE

Dno = DNnumber

Dlocation = ‘Ann Arbor’

(DEPT_LOCATIONS)))

Use NATURAL Join

◦ π

Fname, Lname

(EMPLOYEE * ρ

(Dno, Dlocation)

Dlocation =

‘Ann Arbor’

(DEPT_LOCATIONS)))

Find the full names of employees who

work in Ann Arbor

(39)

Joins – Binary Operations

39

Name Notation Notes

Cross-Product R X S Combines all tuples and attributes of R, S Useful when followed by selection

Join R <joincondition> S Only match related tuples

Theta-Join R θ S θ can be any general boolean expression on the attributes of R and S

Each term of θ relates a tuple from R with a tuple from S using any {<, ≤, >, ≥, =, ≠}

Equi-Join R θ S

θ ←

R.Ai = S.Bj AND R.Ak = S.Bl AND R.Ap = S.Bq

Combines all attributes of R, S

Constructed with = as the only comparison operator

More equality conditions may be AND’d together

Natural Join R * S A simplification of an Equijoin Combines all attributes of R, S

Joins attributes with the same name

Matching attributes are implicitly matched

(40)

Relational Algebra e-Book

40

References

Related documents

Stephen Matthews, Partner – Moscow, Igor Gorchakov, Partner – Moscow, Mona Vaswani, Partner – London, Richard Smith, Partner – London, Jeffrey Sullivan, Partner – London,

These coding guidelines should not be viewed as what must be done in a DO-254 These coding guidelines should not be viewed as what must be done in a DO-254 program. What must be done

The abstract presented the results of the ANRS IPERGAY trial demonstrating the high effectiveness (86% relative reduction of HIV incidence with TDF/ FTC vs. placebo) and

- [F-Expl] showed that long-span curvature increased with increase of Die #3 position, or the increase of the intensity of straightening under the conditions of this study.

IPv6 is having many advantages over IPv4 addressing mechanism such as configuration of network becomes much simpler because there is no need of NAT (Network

Groups represent logical and as fields each record in this table can create such as repeating group data in the number and item Avoid such records to relational database

FROM Course, (SELECT CID, COUNT(*) AS cnt FROM Enroll GROUP BY CID) t WHERE t.CID = Course.CID AND min_enroll &gt; t.cnt AND title LIKE ’CPS%’;. ƒ New subquery is inefficient

• Queries in relational algebra are applied to relation instances, result of a query is again a relation instance. • Six basic operators in