Relational Algebra ICOM 06 Database Systems Relational Algebra Dr. Amir H. Chinaei Department of Electrical and Computer Engineering University of Puerto Rico, Mayagüez Slides are adapted from: Introduction. one of the two formal query languages associated with the relational model. Fundamental Property Operands: relations Operators: the most required functions in relational model Unary or Binary Result is also a relation Database System Concepts, th Ed. Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Roadmap R.A. Operators Core R.A. includes basic operators Advanced R.A. includes additional operators (that mostly can be defined in terms of Core R.A.) Extended R.A. Extends R.A. to support more features of SQL Basic Selection Projection Union Difference (Cross-)product, Additional Intersection Rename Joins Division Selection Selection extracts some rows from a relation R ::= C (R) R includes only those rows of R that satisfy C C is a Boolean condition Example: Selection Provides R ::= price<6.0 (Provides) R 7.0 6.00.00 8.00.00 6.00.00.00 6
Projection Projection extracts some columns from a relation R := A (R) A consists of some attributes of R R includes only those columns of R that are in A. Eliminate duplicate tuples, if any. Example: Projection 7.0 6.00.00 8.00.00 R ::= Foodservice, Food (Provides) Provides Foodservice Food R 7 8 Set-Based Operators Union R.A. borrows the following standard operators on sets: Union. R ::= R U R Intersection. R ::= R R Difference. R ::= R - R Cross-Product (Cartesian product). R ::= R R Union puts together all tuples that are in R or R or both R ::= R U R R and R must be union-compatible: They have the same number of attributes Corresponding attributes have the same domains 9 0 Intersection Intersection puts together all tuples that are in both relations R and R R ::= R R Difference Difference consists of those tuples of first relation which do not appear in the other R ::= R - R R and R must be union-compatible R and R must be union-compatible
Product Product pairs each tuple t of R with each tuple t of R R ::= R R Schema of R is the attributes of R and R, in order If an attribute name A is repeated in both R and R, it is represented by R.A and R.A R FS F P a a a R N d d b b b F b b c c c Example: Product R ::= R R R FS R.F P N R.F a a a a a a b b b b b b c c c c c c d d d d d d b b b b b b Renaming Renaming is useful to resolve possible name conflicts R ::= (R( F ), R) F is a list of terms of the form A O A N Returns R with columns renamed according to F Simplified notation: R ::= R (F) Student 0890 880 ID Name Major Adam Saniya Example: Renaming Math CS Students 0890 880 ID Fullname Plan Adam Saniya Students ::= (Name Fullname, Major Plan) Student Math CS 6 Joins One of the most useful operators in R.A. The most commonly used way to combine several relations Condition Joins (Theta-join) Equijoin Natural Join Outer join Theta-Join Theta-join joins two relations and extracts some rows from the result R ::= R C R = C (R R) Common special case: C consists solely of equalities (Equijoin) The schema of the result consists of attributes of R followed by those attributes of R that do not appear in C. 7 8
R Food Name R ::= Food Cuisine Example: Theta-Join Japanese CheapFood Price<.0 Name=Food CheapFood.0.00.0 Name Cuisine.00.0 A further special case Natural Join R ::= R R = A ( C (R R) ) An Equijoin in which equalities are specified on all common attributes of R and R The result is guaranteed not to have attributes with the same name 0 9 0 Food Food R ::= Food Cuisine Example: Natural Join Japanese CheapFood CheapFood R Food Cuisine Foodservice Price.00.0.0.0.00.0 Division Division. R ::= R / R It is not needed as often But sometimes simplifies a query significantly Find Foodservices that provide all foods Properties: R s attributes R s attributes R s attributes = R s attributes R s attributes R contains all tuples (T) s.t. for every tuple T R, T.T T (set of tuples of R) In other words, R R R Examples: Division Complex Expressions R A A A R A A R R A A * * * * * * * * * * * * R ::= R / R A R ::= R / R R A R6 A A R6 ::= R / R Expressions with several operators Example: R ::= Foodservice ( price<6.0 (Provides Precedence of operators:. Select, Project, Renaming. Products, Division, Joins. Intersection. Union, Difference Therefore, we could write: R ::= Foodservice price<6.0 (Provides How about this? R ::= Foodservice price<6.0 Provides Food)) Food) Food
Expression Trees Useful when writing complex queries Hands-on Example Find all students who like and frequent Pollo Tropical. (Contest ) Example: Find all Foodservice names that provide meatball for less that 6 bucks and their rate is over 6. Name Rate>6 Food= Price<6.0 FoodService Food Provides 6 Hands-on Example Find names of all ICOM students who do not frequent a foodservice that its rate is greater than 8 (Contest ) Hands-on Example Find all Foodservice names that provide at least two foods with the same price (Contest ) 7 8 Relational Algebra on Bags Recall: SQL tables are multisets (bags) Bags are a more general data structure Some operations are more efficient on bags Following R.A. operators produce a different result on a bag than on a set Projection, Division, Union, Intersection, Difference Bag projection and bag division do not eliminate duplicates from the result. U B, B, B Bag Union. R::= R U B R R: all tuples of R and R Example: {,,} U B {,,,}= {,,,,,,} Bag Intersection. R ::= R B R R: common tuples of R and R with the minimum show up Example: {,,} B {,,,} = {,} Bag Difference. R ::= R B R R: all tuples appear in R # of times appear in R Example: {,,} B {,,,} = {} Do sets laws hold for bags? 9 0
Extended R.A. Examples:,, Distinct eliminates duplicates from R R ::= (R) Order sorts R based on list of attributes in A R ::= A (R) Extended Projection R ::= A (R) A consists of some attributes of R, as well as (arithmetic) expressions and attribute renaming R A A R = (R) A A NewA A A+A A A A A R R= A,A (R) A A A R = A NewA, A, A+A,A (R) Extended R.A. cont d Agg aggregates values of an attribute to compute a function Sum(A), Avg(A), Count(A), Min(A), Max(A) Not applicable on a relation, rather on an attribute Group groups tuples based on attributes R ::= A (R) Where, A consists of some attributes of R may include some aggregate functions A A A R Examples: Agg, Sum (A) = 7 Count (A) = Avg (A) = R = A,A, Avg(A), Sum(A) (R) A A Avg(A) Sum(A). 6 Outer join R ::= R O R Extended R.A. cont d An extended natural join in which no tuple from R or R is omitted. Example: R A A R O R R A A A A A Relational Completeness Definition. A query language is at least as expressive as core relational algebra is said to be relationally complete. Examples Safe Relational Calculus R.A. SQL (has additional expressive power) Recommended Exercise: from Ramakrishnan s..7 (ignore Relational Calculus) 6 6