340 likes | 456 Views
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. Today. Relational Algebra Theoretical language Similar to SQL
E N D
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
Today • Relational Algebra • Theoretical language • Similar to SQL • Data Retrieval concept
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}
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)
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}
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))
Five Basic Operations • Selection: • Projection: • Cartesian product: X • Union: • Intersection: • Set difference: –
Selection • salary>10000(Staff) • AND, OR, NOT • , , ~ salary>10000(Staff)
Projection • staffNo,fName, lName, salary(Staff)
Union • RS • city(Branch) city(PropertyForRent)
Result table • city(Branch) city(PropertyForRent)
Set Difference • R – S • city (Branch) - city (PropertyForRent)
Result • city (Branch) - city (PropertyForRent)
Intersection • city (Branch) city (PropertyForRent) • R S = R – (R –S)
Result • city (Branch) city (PropertyForRent)
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.
List the elements of A in V that satisfies all the condition in B (W).
Match Making V W V W =
Cartesian Product • clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing)) • clinet.clinetNo=Viewing.clientNo (clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing)))
clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing))
clinet.clinetNo=Viewing.clientNo (clinetNo,fName,lName(Client) X (clientNo,propertyNo,comment(Viewing)))
Application • Join • Selection () and Cartesian product (X) • One of the major database data retrieval technique.
Join Operations • Theta join • Equijoin • Natural join • Outer join • Semijoin
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
Equijoin • clinetNo,fName,lName(Client)⊳⊲clinet.clinetNo=Viewing.clinetNoclientNo,propertyNo,comment(Viewing) • A special case of theta join.
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?
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’}