Разделы презентаций


Relational Algebra

Содержание

CONTENTSQuery languages in DBProperties of binary operationsRelational algebra operationsExamples Equivalent transformation and optimization of relational algebra expressions

Слайды и текст этой презентации

Слайд 1Lecture 6. Relational algebra
National Aviation University
Computer Science Faculty
Department of Software

Engineering

Lecture 6. Relational algebraNational Aviation UniversityComputer Science FacultyDepartment of Software Engineering

Слайд 2CONTENTS
Query languages in DB

Properties of binary operations

Relational algebra operations

Examples

Equivalent

transformation and optimization of relational algebra expressions

CONTENTSQuery languages in DBProperties of binary operationsRelational algebra operationsExamples Equivalent transformation and optimization of relational algebra expressions

Слайд 3Query languages
Language categories:
procedural (HOW to receive)
nonprocedural (WHAT to receive)
Formal languages:
relational

algebra
relational calculus (tuple-oriented and domain-oriented)
Query language is a language that

allows to extract data from database.

Formal languages are basis for creation of DB query languages (Alpha, QUEL, QBE, SQL)

Query languagesLanguage categories:procedural (HOW to receive)nonprocedural (WHAT to receive)Formal languages:relational algebrarelational calculus (tuple-oriented and domain-oriented)Query language is

Слайд 4Relational algebra closure and properties of binary operations
Algebra = data

(of the defined type) + operations. Algebra is closured if result

of any operation are the same type as a data in argument. Closure property allows to embed operations in each other.
Relational algebra = relations + operations.
Relational algebra closured.

Property of binary relations:
Operation ϕ is commutative if А ϕ В = B ϕ A
Operation ϕ is associative if (А ϕ В) ϕ С = А ϕ (В ϕ С)
Operation ϕ is distributive with respect with other operation θ, if А ϕ (В θ С ) = (А ϕ В) θ (А ϕ С)

Relational algebra closure and properties of binary operationsAlgebra = data (of the defined type) + operations. Algebra

Слайд 5Relational algebra operations
Basic operations:
set-theoretic (union, intersection, difference)
projection
selection
cartesian product,
join
division
Additional operations
assignment
renaming
generalized

projection
outer join

Relational algebra operationsBasic operations:set-theoretic (union, intersection, difference)projectionselectioncartesian product, joindivisionAdditional operationsassignmentrenaminggeneralized projectionouter join…

Слайд 6Set-theoretic operations



Two relations R and S are (union) compatible if:
R

and S have the same arity/ that is they have

the same number of attributes.
Domains of corresponding attributes are compatible (the 1-st attribute of R is defined on the same domain as the 1-st attribute of S and so on) .

Set-theoretic operations require compatibility of their operands

Set-theoretic operationsTwo relations R and S are (union) compatible if:R and S have the same arity/ that

Слайд 7Union operation
Union of two compatible relations R and S

with schemas R(A) and S(A), where A is a set

of attributes, is the relation T with schema T(A) that contains tuples of both relations, but without duplicates.

The operation commutative, associative and distributive with respect to the intersection .

Example:

Union operation Union of two compatible relations R and S with schemas R(A) and S(A), where A

Слайд 8Difference operation
Difference of two compatible relations R and S

with schemas R(A) and S(A), where A is a set

of attributes, is the relation T with schema (A) that contains only those tuples of the relation R, which are not present in the relation S.

The operation not commutative and associative and distributive.

Т(А) = R(А) ⎯ S(А) = {t | t ∈ R & t ∉ S}

Example:

Note: Relational algebra do not use set-theoretic complement operation!!!

Difference operation Difference of two compatible relations R and S with schemas R(A) and S(A), where A

Слайд 9Intersection operation
Intersection of two compatible relations R and S with

schemas R(A) and S(A), where A is a set of

attributes, is the relation T with schema T(A) that contains tuples which simultaneously present in both relations.

The operation commutative, associative and distributive with respect to union.

Т(А) = R(А) ∩ S(А) = {t | t ∈ R & t ∈ S}

Example:

Intersection is expressed via difference: R ∩ S = R – (R – S)

Intersection operationIntersection of two compatible relations R and S with schemas R(A) and S(A), where A is

Слайд 10Projection operation
Projection of the relation R with schema R(A), where

A is a set of attributes, with respect to the

set of attributes В, where В ⊂ А, is a such relation S with the schema S(B) that contains tuples of the relation R by deleting from them values that belong attributes A - B.

S(B) = R[B] = {t[B] ⏐ t ∈ R}

Projection is denoted as: R[B] или πВ(R)

Example:

Note: Duplicate tuples are deleted

Projection operationProjection of the relation R with schema R(A), where A is a set of attributes, with

Слайд 11θ-comparability of attributes and tuples
Let’s θ is any of the

following comparison operators : =, ≠, < ≤, >, ≥.

The attributes A and B the same or different relations are θ-comparable, if for any values a ∈ A and b ∈ B expression а θ b is defined.

Set of attributes М = (A1,…, Ak) and N = (B1,…,Bn) are θ-comparable, if k = n and (Ai, Bi) are θ‑comparable. In this case expression M θ N means the following:
M θ N = (A1 θ B1) & … & (Ak θ Вk)

If t is a relation tuple, that contains sets of θ‑comparable attributes M and N, then the notation t[M] θ t[N] means the following: (t[A1] θ t[B1]) & … & (t[Ak] θ t[Вk]).

θ-comparability of attributes and tuplesLet’s θ is any of the following comparison operators : =, ≠, <

Слайд 12Selection (restriction) operation
Let’s М and N are sets of θ-comparable

attributes of the relation R with schema R(A). Then selection

(restriction) of the relation R on condition М θ N, denoted as R [М θ N], is such relation T with schema T(A), which tuples satisfy the condition t[М] θ t[N].

Т(А) = R[M θ N] = {t | t ∈ R & t[M] θ t[N]}

Example:

The operation also has the following notation: σM θ N(R)

One of the attributes set M or N may be constant.

Selection (restriction) operationLet’s М and N are sets of θ-comparable attributes of the relation R with schema

Слайд 13Cartesian product
Cartesian product of two rations R and S with

schemas R(А) and S(B) (A and B are sets of

attributes), denoted as R(A)×S(B), is a relation T of degree n+m with schema T(А, B) that contains all possible concatenations of tuples of relations R and S:

The operation is commutative and associative and distributive with respect of union and intersection.

Т(А,В) = R(А) x S(B) = {(t1,t2) | t1 ∈ R & t2 ∈ S}

Example:

Cartesian productCartesian product of two rations R and S with schemas R(А) and S(B) (A and B

Слайд 14Join operation
Let us M and N are sets of

θ-comparable attributes. Join of the relations R and S with

schemas R(A,M) and S(N,B) on a condition М θ N, denoted as R[М θ N]S, is a relation T with a schema T(A,M,N,B) which tuples are concatenation only those tuples of R and S, for which sets of attributes N and M satisfy condition М θ N.

T = R[М θ N]S = {(t1, t2) ⏐ t1 ∈ R ∧ t2 ∈ S ∧ t1[М] θ t2[N]}

Example:

The operation commutative and associative

The operation is also denoted as: R M θ NS
Join is expressed via the product and the selection: R M θ NS = σM θ N(R x S)

Join operation Let us M and N are sets of θ-comparable attributes. Join of the relations R

Слайд 15Join and natural join
Join on a condition of equality (=)

is called as equi-join .

Natural join is a join

on equality condition by attributes with the same names with deleting in the result relation one of the compared set of attributes.

Natural join is denoted by the symbol * (for example R*S).

Example:

R S R[B,C=B,C]S R*S

Join and natural joinJoin on a condition of equality (=) is called as equi-join . Natural join

Слайд 16Semijoin
Semijoin is join of two relations with deleting from the

resulting relation attributes of one of the joined relations.
Semijoin

is denoted as: R[M θ N)S

R[М θ N)S = {(t1) ⏐ t1 ∈ R ∧ t2 ∈ S ∧ t1[М] θ t2[N]}

Example:

R S R[B,C=B,C)S

R[М θ N)S = (R[М θ N]S)[A] – where А is a set of attributes of the relation R

Semijoin is expressed via the join and projection in such a way:

SemijoinSemijoin is join of two relations with deleting from the resulting relation attributes of one of the

Слайд 17Image of the tuple
Image of the relation R(M,N) with respect

the tuple t1 ∈ R[M], that is denoted as It1(R),

is the set of such tuples t2 ∈ R[N], that concatenation of tuples (t1,t2) belongs to the relation R.

It1 ∈ R[M](R) = {(t2) ⏐ t2 ∈ R[N] ∧ (t1,t2) ∈ R}

Examples:

R Ia1∈ R[A](R) Ia2∈ R[A](R) I(a1,b1)∈ R[A,B](R) Ic2∈ R[C](R)

Image of the tupleImage of the relation R(M,N) with respect the tuple t1 ∈ R[M], that is

Слайд 18Division operation (1)
Division of two relations R(M,N) and S(K,L) with

respect of set of compatible attributes N and M, denoted

as R [N ÷ K] S, is a relation T(M) with such tuples t ∈ R[M] whose images It(R) contain all tuples of the projection S[K].

T(M) = R[N÷K]S = {t ⏐t ∈ R[M] & It(R) ⊇ S[K]}

The operation allows to formulate queries like this: - Output teachers that teach lectures of ALL types.

Division operation (1)Division of two relations R(M,N) and S(K,L) with respect of set of compatible attributes N

Слайд 19Division operation(2)
Example:
R

S

S[C] R[C÷C]S

Division operation is expressed by other operations of RA:
R[N÷K]S = R[M] – ((R(M) x S[K]) – R)[M]
Division operation is not commutative and associative

Division operation(2)Example:R              S

Слайд 20FAC (FNo, Name, Dean, Bld, Fund)
DEP (DNo, FNo, Name, Head,

Bld, Fund)
TCH (TNo, DNo, Name, Post, Tel, Salary, Comm)
GRP (GNo,

DNo, Course, Num, Quantity, CurNo)
SBJ (SNo, Name)
ROM (RNo, Num, Building, Seats) LEC (TNo, GNo, SNo, RNo, Type, Day, Week)

Example of DB for RA queries

FAC (FNo, Name, Dean, Bld, Fund)DEP (DNo, FNo, Name, Head, Bld, Fund)TCH (TNo, DNo, Name, Post, Tel,

Слайд 21Examples of queries in RA (1)
Projection: Output list jf teacher

names and posts:
TCH[Name, Post] πName,Post(TCH)
Selection: Output information about CSF faculty:
FAC[Name =

‘CSF’] σName=‘CSF’(FAC)
Join: Output information about faculries and their departments:

FAC[FNo = FNo]DEP FAC FNo=FNoDEP

Examples of queries in RA (1)Projection: Output list jf teacher names and posts:	TCH[Name, Post]	πName,Post(TCH)Selection: Output information about

Слайд 22Examples of queries in RA (2)
Composition of join, selection and

projection
1) Outputs names of faculties and their departments
(FAC[FNo = FNo]DEP)

[FAC.Name, DEP.Name] πFAC.Name, DEP.Name(FAC FNo=FNoDEP)
2) Outputs names of faculties with fund > 1000 and their departments :
((FAC[Fund > 1000])[FNo = FNo]DEP) [FAC.Name, DEP.Name] πFAC.Name, DEP.Name((σFund >1000(FAC)) FNo=FNoDEP)

Examples of queries in RA (2)Composition of join, selection and projection 1) Outputs names of faculties and

Слайд 23Examples of queries in RA (3)
Basic query tables
Name=‘CSF’ – selection

condition
Query calculation path
Post =‘prof’ – selection condition Name - output
Name -

output

Outputs names of professors from CSF faculty and subjects that they teach.

((((((FAC[FNo=Fno]DEP) [DNo=DNo]TCH) [TNo=TNo]LEC) [SNo=SNo]SBJ) [FAC.Name=‘CSF’]) [Post=‘prof’]) [TCH.Name, SBJ.Name]

Examples of queries in RA (3)Basic query tablesName=‘CSF’ – selection conditionQuery calculation pathPost =‘prof’ – selection condition

Слайд 24Examples of division operation
1) Output teacher numbers that teach in ALL

groups:
((LEC[TNo,GNo])[GNo÷GNo]GRP)[TNo]
2) Output teacher numbers that teach in ALL groups of

the first course:
((LEC[TNo,GNo])[GNo÷GNo](GRP[Course=1]))[TNo]
3) Output teacher names that teach in ALL groups of the first course:
(((LEC[TNo,GNo])[GNo÷GNo](GRP[Course=1])) [TNo=TNo]TCH)[TCH.Name]

LEC(GNo,TNo,...)

GRP(GNo, Course...)

TCH(TNo, Name...)

Examples of division operation1)	Output teacher numbers that teach in ALL groups:	((LEC[TNo,GNo])[GNo÷GNo]GRP)[TNo] 2)	 Output teacher numbers that teach

Слайд 25Additional operations
Additional operations
Assignment
Renaming
Generalized projection
Outer join

Additional operations Additional operationsAssignmentRenamingGeneralized projectionOuter join…

Слайд 26Assignment operation
The assignment operation (←) provides a convenient way to

express complex queries, write query as a sequential program consisting

of a series of assignments by using intermediate temporal relations followed by an expression whose value is displayed as a result of the query.

Пример: Output teacher names that teach in ALL groups of the first course:
(((LEC[TNo,GNo])[GNo÷GNo](GRP[Course=1])) [TNo=TNo]TCH)[TCH.Name]
Temp1 ← LEC[TNo,GNo]
Temp2 ← GRP[Course=1]
Temp3 ← Temp1[GNo÷GNo]Temp2
Temp4 ← Temp3[TNo=TNo]TCH
Temp4[TCH.Name]
Assignment operationThe assignment operation (←) provides a convenient way to express complex queries, write query as a

Слайд 27Rename operation
The rename operation allows us to name, and therefore

to refer to, the results of relational-algebra expressions.
It has the

following syntax:
ρR(A1,A2,...,An)(E)

Where: Е – relational algebra expression,
R(A1,A2,...,An) – name of the relation ant its attributes that is calculated by expression E.

Example: Output names of faculties and their departments. Rezulted relation should have name FAC_DEP with attributes FName и DName respectively:
ρFAC_DEP(FName,DName)(FAC[FNo=FNo]DEP)[FAC.Name,DEP.Name]

Rename operationThe rename operation allows us to name, and therefore to refer to, the results of relational-algebra

Слайд 28Generalized projection operation
Extends the projection operation by allowing arithmetic functions

to be used in the projection list. R[F1, F2,...,Fn] πF1, F2,...,Fn(R)
Where: R –

relation or expression of relational algebra;
F1, F2,...,Fn – are arithmetic expressions involving constants and attributes in the schema of E.

Example: Output teacher names and their salary + commission

TCH[Name, Salary + Commission]
Generalized projection operationExtends the projection operation by allowing arithmetic functions to be used in the projection list.

Слайд 29Outer join
Outer join is an extension of the join operation

that avoids loss of information.

Computes the join and then adds

tuples form one relation that does not match tuples in the other relation to the result of the join.
Outer joinOuter join is an extension of the join operation that avoids loss of information.Computes the join

Слайд 30Outer join – example of ordinary join
1) Ordinary join

(inner join)
FAC [Fno=Fno] DEP
FAC DEP

Outer join – example of ordinary join1) Ordinary join    (inner join)FAC [Fno=Fno] DEPFAC

Слайд 31Left outer join




FAC FNo Name Dean
F-1 CSF Ann

F-2 CTF Dick F-3 CEF Bob F-4 CYB John


DEP DNo Name Head FNo

D-1 SE Kate F-1 D-2 DBMS Lucy F-1 D-3 CAD Dave F-2 D-4 PL Stiv NULL D-5 CAM Sam NULL

2) Left outer join FAC 〈Fno=Fno] DEP
FAC DEP



FNo Name Dean DNo Name Head FNo

F-1 CSF Ann D-1 SE Kate F-1 F-1 CSF Ann D-2 DBMS Lucy F-1 F-2 CTF Dick D-3 CAD Dave F-2 F-3 CEF Bob null null null null F-4 CYB John null null null null

FAC

DEP


Left outer joinFAC	 FNo   Name	 Dean 	 F-1	CSF	Ann 	 F-2	CTF	Dick 	 F-3	CEF	Bob

Слайд 32Right outer join




FAC FNo Name Dean
F-1 CSF Ann

F-2 CTF Dick F-3 CEF Bob F-4 CYB John


DEP DNo Name Head FNo

D-1 SE Kate F-1 D-2 DBMS Lucy F-1 D-3 CAD Dave F-2 D-4 PL Stiv NULL D-5 CAM Sam NULL

3) Right outer join FAC [Fno=Fno〉 DEP
FAC DEP



FNo Name Dean DNo Name Head FNo

F-1 CSF Ann D-1 SE Kate F-1 F-1 CSF Ann D-2 DBMS Lucy F-1 F-2 CTF Dick D-3 CAD Dave F-2 null null null D-4 PL Stiv null null null null D-5 CAM Sam null

FAC

DEP


Right outer joinFAC	 FNo   Name	 Dean 	 F-1	CSF	Ann 	 F-2	CTF	Dick 	 F-3	CEF	Bob

Слайд 33Full outer join
4) Full outer join FAC 〈Fno=Fno〉 DEP
FAC

DEP




FNo Name Dean DNo Name

Head FNo

F-1 CSF Ann D-1 SE Kate F-1 F-1 CSF Ann D-2 DBMS Lucy F-1 F-2 CTF Dick D-3 CAD Dave F-2 F-3 CEF Bob null null null null F-4 CYB John null null null null null null null D-4 PL Stiv null null null null D-5 CAM Sam null

FAC

DEP




Inner join

Left outer join

Right outer join


Full outer join

Full outer join4) Full outer join   FAC 〈Fno=Fno〉 DEPFAC   DEPFNo  Name

Слайд 34Equivalent transformations of relational expressions
1) Commutativity of selection: σF(σG(R))=σG(σF(R))=σF&G(R)
2) Commutativity

of selection and projection:
πG(σF(R))=σF(πG(R))=σF&G(R), если G ⊇ F
3) Distributivity of

selection and product
σF(R х S) = σF(R) x σF(S)
4) Distributivity of selection and set-theoretic operations:
σF(R ∪ S)=σF(R) ∪ σF(S), σF(R ∩ S)=σF(R) ∩ σF(S)
5) Distributivity of selection and join:
σF(R S) = σF(R) S, если условие F относится к R
6) Distributivity of projection and set-theoretic operations :
πF(R ∪ S)=πF(R) ∪ πF(S), πF(R ∩ S)=πF(R) ∩ πF(S)
Equivalent transformations of  relational expressions1) Commutativity of selection: σF(σG(R))=σG(σF(R))=σF&G(R) 2) Commutativity of selection and projection: 	πG(σF(R))=σF(πG(R))=σF&G(R),

Слайд 35
Optimization of RA expressions
R(A,B) S(C,D) R(A,B)

S(C,D) RA,B) S(C,D)

R(A,B) S(C,D)

× × ×

σD=9

σB=C & D=9 σB=C σB=C B=C

σD=9

πA

πA

πA

πA

πA(σB=C & D=9(R(A, B)x S(C,D)))

Optimization of RA expressionsR(A,B)  S(C,D)    R(A,B)  S(C,D)    RA,B)

Слайд 36General rules of RA expressions optimization
General rules of RA expressions

optimization:
Transform each selection σF1&...&Fn(E) to the sequence of selections σF1(...

σFn(E))
Move each selection downwards of the tree as far as it is possible (thus (vertical) size of the relation is reduced).
Adjacent selection and cartesian product are replaced by join.
Move each projection downwards of the tree as far as it is possible (thus (horizontal) size of the relation is reduced).
Transform each cascade of adjacent selections and projections into single projection or selection with subsequent projection
General rules of RA expressions optimizationGeneral rules of RA expressions optimization: Transform each selection σF1&...&Fn(E) to the

Слайд 37Relational Algebra: Summary
Relational Algebra:
Formal language for handling data in

relational model
Procedural language, how to retrieve data
No practical

relevance for querying DB
Formal basis for query optimization
Important terms & concepts:
Union R ∪ S, difference R – S, intersection R ∩ S
Projection π(R)
Selection σ(R)
Cartesian product R x S
Joins R S
Difference
Relational Algebra: SummaryRelational Algebra: Formal language for handling data in relational model Procedural language, how to retrieve

Обратная связь

Если не удалось найти и скачать доклад-презентацию, Вы можете заказать его на нашем сайте. Мы постараемся найти нужный Вам материал и отправим по электронной почте. Не стесняйтесь обращаться к нам, если у вас возникли вопросы или пожелания:

Email: Нажмите что бы посмотреть 

Что такое TheSlide.ru?

Это сайт презентации, докладов, проектов в PowerPoint. Здесь удобно  хранить и делиться своими презентациями с другими пользователями.


Для правообладателей

Яндекс.Метрика