advanced database system - ch07

25
Relational Relational Algebra Algebra Prof. Yin-Fu Huang Prof. Yin-Fu Huang CSIE, NYUST CSIE, NYUST Chapter 7 Chapter 7

Upload: -

Post on 12-Jan-2017

38 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Advanced Database System - Ch07

Relational AlgebraRelational Algebra

Prof. Yin-Fu HuangProf. Yin-Fu HuangCSIE, NYUST CSIE, NYUST

Chapter 7Chapter 7

Page 2: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Eight operators of the relational algebra:1. The traditional set operators union, intersection, difference,

and Cartesian product.2. The special relational operators restrict, project, join, and

divide. (See Fig. 7.1)

7.17.1 IntroductionIntroduction

Page 3: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Page 4: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.27.2 Closure RevisitedClosure Revisited

The output from any given relational operation is another relation. Nested relational expressions

Page 5: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.37.3 The Original Algebra: SyntaxThe Original Algebra: Syntax (See Page178-179)

<relation exp> ::= Relation {<tuple exp commalist>}| <relvar name>| <relation op inv>| <with exp>| <introduced name>| (<relation exp>)

<relation op inv> ::= <project> | <nonproject> <project> ::= <relation exp> { [All But] <attribute name commalist>

} <nonproject> ::= <rename> | <union> | <intersect> | <minus>

| <times> | <where> | <join> | <divide>

Page 6: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.37.3 The Original Algebra: Syntax (Cont.)The Original Algebra: Syntax (Cont.)

<rename> ::= <relation exp> Rename ( <renaming commalist> ) <union> ::= <relation exp> Union <relation exp> <intersect> ::= <relation exp> Intersect <relation exp> <minus> ::= <relation exp> Minus <relation exp> <times> ::= <relation exp> Times <relation exp> <where> ::= <relation exp> Where <bool exp> <join> ::= <relation exp> Join <relation exp> <divide> ::= <relation exp> Divideby <relation exp> Per <per> <per> ::= <relation exp> | ( <relation exp>, <relation exp> ) <with exp> ::= With <name intro commalist> : <exp> <name intro> ::= <exp> As <introduced name>

Page 7: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Tuple-homogeneous Union, Intersect, and Difference (See Fig. 7.2) Product (See Fig. 7.3)

If we need to construct the Cartesian product of two relationsthat do have any such common attribute names, we must use

the Rename operator first to rename attributes appropriately. Restrict (See Fig. 7.4) Project (See Fig. 7.5)

7.47.4 The Original Algebra: Semantics The Original Algebra: Semantics

Page 8: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.2Fig. 7.2 Union, intersection, and differenceUnion, intersection, and difference

Page 9: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.3Fig. 7.3 Cartesian product exampleCartesian product example

Page 10: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.4Fig. 7.4 Restriction examplesRestriction examples

Page 11: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.5Fig. 7.5 Projection examplesProjection examples

Page 12: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Join• natural join (See Fig. 7.6)• θ-join (See Fig. 7.7)

((S Rename City As Scity)Times(P Rename City As Pcity))Where Scity > Pcity

Divide (See Fig. 7.8)

7.47.4 The Original Algebra: Semantics (Cont.) The Original Algebra: Semantics (Cont.)

Page 13: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.6Fig. 7.6 && Fig. 7.7Fig. 7.7

Page 14: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Fig. 7.8Fig. 7.8 Division examplesDivision examples

Page 15: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.57.5 ExamplesExamples

Exam 1: ((Sp Join S) Where P#=P#(‘P2’)) {Sname}

Exam 2: (((P Where Color=Color(‘Red’))

Join Sp ) {S#} Join S) {Sname}

Exam 3: ((S {S#} Divideby P {P#} Per Sp {S#, P#})

Join S) {Sname}

Exam 4: S {S#} Divideby (Sp Where S#=S#(‘S2’)) {P#}

Per Sp {S#, P#}

Exam 5: (((S Rename S# As Sa) {Sa, City} Join

(S Rename S# As Sb) {Sb, City})

Where Sa < Sb) {Sa, Sb}

Exam 6: ((S {S#} Minus (Sp Where P#=P#(‘P2’)) {S#})

Join S) {Sname}

Page 16: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.67.6 What Is the Algebra For?What Is the Algebra For?

The operators join, intersect, and divide can be defined interms of the other five.

Of the remaining five, however, none can be defined in terms of the other four, so we can regard those five as constituting a primitive or minimum set.

Some possible applications:1. Defining a scope for retrieval2. Defining a scope for update3. Defining integrity constraints4. Defining derived relvars5. Defining stability requirements6. Defining security constraints

Page 17: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.67.6 What Is the Algebra For? (Cont.)What Is the Algebra For? (Cont.)

A high-level, symbolic representation of the user‘s intentTransformation rules

((Sp Join S) Where P#=P#(‘P2’)) {Sname}

((Sp Where P#=P#(‘P2’)) Join S) {Sname} The algebra thus serves as a convenient basis for optimization. A language is said to be relationally complete if it is at least as

powerful as the algebra.

Page 18: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.77.7 Further PointsFurther Points

Associativity and Commutativity• Associative: Union, Intersect, Times, Join

e.g. (A Union B) Union C = A Union (B Union C) = A Union B Union C

• Commutative: Union, Intersect, Times, Joine.g. A Union B = B Union A

Some Equivalencese.g. r { } = Table_Dum if r=empty,

Table_Dee otherwise (a nullary projection) r Join Table_Dee = Table_Dee Join r = r

r Times Table_Dee = Table_Dee Times r = r

Page 19: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.77.7 Further Points (Cont.)Further Points (Cont.)

Some GeneralizationsIf s contains no relations at all, then:

The join of all relations in s is defined to be Table_Dee.The union of all relations in s is defined to be the empty r

elation.The intersection of all relations in s is defined to be the “

universal” relation.

Page 20: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

(See Page 196) <semijoin> ::= <relation exp> Semijoin <relation exp> <semiminus> ::= <relation exp> Semiminus <relation exp> <extend> ::= Extend <relation exp> Add ( <extend add commalist> ) <extend add> ::= <exp> As <attribute name> <summarize> ::= Summarize <relation exp> Per <relation exp>

Add ( <summarize add commalist> ) <summarize add> ::= <summary type> [ ( <scalar type> ) ]

As <attribute name> <summary type> ::= Count | Sum | Avg | Max | Min | All | Any

| Countd | Sumd | Avgd| … <tclose> ::= Tclose <relation exp>

7.87.8 Additional Operators

Page 21: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.87.8 Additional Operators (Cont.)

Semijoin (a Join b) {X, Y}e.g. S Semijoin (Sp Where P#=P#(‘P2’))

Semidifference a Minus (a Semijoin b)e.g. S Semiminus (Sp Where P#=P#(‘P2’))

Extende.g. Extend P Add (Weight* 454) As Gmwt(See Fig. 7.9)

Page 22: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.87.8 Additional Operators (Cont.)Exam 1: Extend S Add ‘Supplier’ As TagExam 2: Extend (P Join Sp) Add (Weight* Qty) As ShipwtExam 3: (Extend S Add City As Scity) {All But City}

RenameExam 4: Extend P Add (Weight* 454 As Gmwt,

Weight* 16 As Ozwt)Exam 5: Extend S

Add Count((Sp Rename S# As X) Where X=S#) As Np (See Fig. 7.10)

Page 23: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

7.87.8 Additional Operators (Cont.)

Exam 1: Summarize Sp Per P {P#} Add (Sum(Qty) As Totqty, Avg(Qty) As Avgqty)

Exam 2: Summarize Sp Per S {S#} Add Count As Np• Summarize is not a primitive operator.

Extend Exam 3: Summarize S Per S {City}Add Avg(Status) As Avg_Status Exam 4: Summarize Sp Per Sp { }Add Sum(Qty) As Grandtotal

Summarize

e.g. Summarize Sp Per P {P#} Add Sum(Qty) As Totqty(See Fig. 7.11)

Tclose the transitive closure of a

Page 24: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

Groupe.g. SP Group (P#, Qty) As PQ (See Fig. 7.12)

Ungroupe.g. SPQ Ungroup PQ

The reversibility of the Group and Ungroup operations (See Fig. 7.13)Functionally dependency

7.97.9 Grouping and Ungrouping

Page 25: Advanced Database System - Ch07

Advanced Database System Yin-Fu Huang

The End.The End.