Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

34
Relational Algebra Chapter 4 CIS 458 Sungchul Hong

Transcript of Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Page 1: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Relational Algebra

Chapter 4

CIS 458

Sungchul Hong

Page 2: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Last Class

• Relational data model.

• Transform ERD to relational database schema.– One-to-one relationship– One-to-many relationship– Many-to-many relationship

Page 3: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Today

• Relational Algebra– Theoretical language– Similar to SQL– Data Retrieval concept

Page 4: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Sets

• A set is any well-defined list, collection, or class of objects.

• Elements or members of the set.

• A = {1, 3, 7, 10}

• B= {x | x is even}

Page 5: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Subsets

• If every element in a set A is also a member of a set B, then A is called a subset of B.

• AB (Proper)

Page 6: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Union

• The union of sets A and B is the set of all elements which belong to A or to B or to both.

• AB

• S={a,b,c,d} T={f,b,d,g}

Page 7: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

The Relational Algebra

• The relational algebra is a theoretical language with operations that work on one or more relations to define another relation without changing the original relation(s).

• Operands and results are relations. name, address (Student)

• The output from one operation can become the input to another operation. name=“John”(name, address (Student))

Page 8: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Five Basic Operations

• Selection: • Projection: • Cartesian product: X

• Union: – Intersection:

• Set difference: –

Page 9: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
Page 10: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Selection

salary>10000(Staff)

• AND, OR, NOT, , ~

salary>10000(Staff)

Page 11: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Projection

staffNo,fName, lName, salary(Staff)

Page 12: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Set Operations

Page 13: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Union

• RScity(Branch) city(PropertyForRent)

Page 14: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

city(Branch) city(PropertyForRent)

Page 15: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

City

London

Aberdeen

Glasgow

Bristol

city(Branch) city(PropertyForRent)

Result table

Page 16: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Set Difference

• R – Scity (Branch) - city (PropertyForRent)

Page 17: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

city (Branch) - city (PropertyForRent)

Page 18: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

city

Bristol

city (Branch) - city (PropertyForRent)

Result

Page 19: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Intersection

city (Branch) city (PropertyForRent)

• R S = R – (R –S)

Page 20: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

city (Branch) city (PropertyForRent)

Page 21: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

city

Aberdeen

London

Glasgow

city (Branch) city (PropertyForRent)

Result

Page 22: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Division

• R S

• The Division operation defines a relation over the attributes C that consists of the set of tuples from R that match the combination of every tuple in S.

Page 23: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

List the elements of A in V that satisfies all the condition in B (W).

Page 24: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Name Condition

John Smart

John Kind

Tom Smart

Tom Kind

Nick Smart

Condition

Smart

Kind

V W

V W =

Name

John

Tom

Match Making

Page 25: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Cartesian Product

clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing))

clinet.clinetNo=Viewing.clientNo (clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing)))

Page 26: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing))

Page 27: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

clinet.clinetNo=Viewing.clientNo (clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing)))

Page 28: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
Page 29: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Application

• Join– Selection () and Cartesian product (X)

• One of the major database data retrieval technique.

Page 30: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Join Operations

• Theta join

• Equijoin

• Natural join– Outer join– Semijoin

Page 31: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Theta Join

• R ⊳⊲ F S

• The Theta join operation defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S. The predicate F is of the form R.aj S.bi where may be one of the comparison operators ( <, , >, , =, )– E.g.) R ⊳⊲ R.salary S.salary S

Page 32: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Equijoin

clinetNo,fName,lName(Client) ⊳⊲ clinet.clinetNo=Viewing.clinetNo clientNo,propertyNo,comment(Viewing)

• A special case of theta join.

Page 33: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Natural Join

• R ⊳⊲ S • The Natural join is an Equijoin of the two

relations R and S over all common attributes x. One occurrence of each common attribute is eliminated from the result.

clinetNo,fName,lName(Client) ⊳⊲ clientNo,propertyNo,comment(Viewing)– No common attribute?

Page 34: Relational Algebra Chapter 4 CIS 458 Sungchul Hong.

Other Topics

• Outer Join• Aggregation (COUNT, SUM AVG, MIN, MAX), Group• Relational Calculus

– Tuple Relational Calculus• {S | Staff(S) S.salary > 10000}

– Domain Relational Calculus• List the staff who manage properties for rent in Glasgow• {sN, fN, lN, posn, sex, DOB, sal, bN|(sN1, city)(Staff(sN, fN, lN,

posn, sex, DOB, sal, bN) PropertyForRent(pN, st, cty, pc, typ, rms, rnt, oN, sN1, bN1) (sN = sN1) cty=‘Glasgow’}