# Relational Math 1 CSC 240 Blum Relational Algebra

- Slides: 55

Relational Math 1 CSC 240 (Blum)

Relational Algebra The rules for combining one or more numbers (or symbols standing in for numbers) to obtain another number is called algebra. For example, the rules for combining one or more Boolean variables (expressions which are either true or false) to obtain another Boolean is called Boolean algebra. The rules for combining one or more relations to obtain another relation is called relational algebra. 2 CSC 240 (Blum)

Operations The specific ways of combining elements are known as operations. E. g. addition is an algebraic operation E. g. ANDing is a Boolean operation unary if they act on one element and binary if they act on two Operations are called elements. E. g square root is a unary algebraic operation. E. g. addition is a binary algebraic operation. 3 CSC 240 (Blum)

Relation Table Relational algebra sounds so abstract. Recall a representation of a relation is a table. So relational algebra means that we do stuff to tables and get other tables out. A relational database is made of tables. Relational algebra tells us how to operate on tables. That is, relational algebra tells us what a Data Manipulation Language (DML) should do. 4 CSC 240 (Blum)

Synonym Recall our old synonyms Table Relation File Row Tuple Record Column Attribute Property Field A new synonym pair is Condition Predicate A condition is a Boolean, an expression that is true or false, e. g. Salary > 600000 or Name=“Smith” 5 CSC 240 (Blum)

Selection/Restriction A selection (a. k. a. restriction) picks out those rows from a table that meet some condition. Example: Let us select from the Customer table those people who are from PA. predicate ( R ) 6 CSC 240 (Blum)

Selection Example: Customers from PA (Design) Customer. * refers to all of the columns. The condition (predicate) selecting out particular rows. 7 CSC 240 (Blum)

Selection Example: PA Only (Data. Sheet) 8 CSC 240 (Blum)

Selection Example: PA Only (SQL) Condition 9 CSC 240 (Blum)

Condition can be compound. The selection condition may be a compound condition. Condition. A AND Condition. B Condition. A OR Condition. B Example: Let us select from the Customer table those people who from Philadelphia and from PA. (There are other Philadelphias, e. g. in Mississippi) 10 CSC 240 (Blum)

Added Some New Customers 11 CSC 240 (Blum)

Selection Example: Philadelphia AND PA (Design) 12 ANDed conditions are entered on the same line. CSC 240 (Blum)

Selection Example: Philadelphia AND PA (Data. Sheet) 13 CSC 240 (Blum)

Selection Example: Philadelphia AND PA (SQL) ANDed conditions 14 CSC 240 (Blum)

Selection Example: Customers from PA or NJ (Design) 15 ORed conditions are entered on separate lines. CSC 240 (Blum)

Selection Example: Customers from PA or NJ (Data. Sheet) 16 CSC 240 (Blum)

Selection Example: Customers from PA or NJ (SQL) ORed Conditions 17 CSC 240 (Blum)

Projection The projection operator picks out a set of columns that will belong to the resulting table. Recall the concept of views in which certain fields would be hidden from certain users. Example: Let us project from the Customer table the first and last names. column 1, column 2, … ( R ) 18 CSC 240 (Blum)

Projection Example: Customer’s first and last names (Design) Choose columns and check to show them. 19 CSC 240 (Blum)

Projection Example: Customer’s first and last names (Data. Sheet) 20 CSC 240 (Blum)

Projection Example: Customer’s first and last names (SQL) Columns that will appear. 21 CSC 240 (Blum)

Union Compatible Think of the records in a table as elements of a set. If two sets have the same sorts of records, that is, the same fields in the same order or minimally the same type fields in the same order, then the sets are said to be unioncompatible. Then you can consider forming The union of the two sets The intersection of the two sets The set difference 22 CSC 240 (Blum)

A Simpsons Database 23 CSC 240 (Blum)

Union The union of set A and set B contains all of the elements of set A as well as all of the elements of set B If an element belongs to set A and set B, the union contains only one copy of it. Example: let us make a table containing all of the names from the Character and Real. Person tables First. Name, Last. Name(Character) First. Name, Last. Name(Real. Person) 24 CSC 240 (Blum)

Union Example: Character and Real. Person names Step 1 would be to create union-compatible tables using projection. Step 2 would be to take the union of these tables. 25 CSC 240 (Blum)

Union Example: Character and Real. Person names (Data. Sheet) 26 CSC 240 (Blum)

Union Example: Character and Real. Person names (Data. Sheet) 27 CSC 240 (Blum)

Union Example: Character and Real. Person names (No Design ) Query-By-Example (QBE) which is what we do in Design View takes the join as its principle binary operation. While the union is a more fundamental binary operation in Relational algebra, the join is the more common operation in querying. SQL does have the union operation! 28 CSC 240 (Blum)

Union Example: Character and Real. Person names 29 CSC 240 (Blum)

UNION ALL is a variation on the UNION operation that does not eliminate duplicate records from the results. It is somewhat faster because the system does not have to look for the possibility of duplications. The result is “weird” in that we usually do not want duplicate records. 30 CSC 240 (Blum)

Intersection The intersection of set A and set B contains only the elements that belong both to set A and to set B. Example: let us make a table containing all of the names of people who play themselves on the Simpsons. First. Name, Last. Name(Character) First. Name, Last. Name(Real. Person) Again the first step is to make “union-compatible” tables. 31 CSC 240 (Blum)

Intersection Example: People playing themselves (Data. Sheet) 32 CSC 240 (Blum)

Intersection Example: People playing themselves (Design, step 1) 33 CSC 240 (Blum)

Intersection Example: People playing themselves (Design, step 2) Dragging a field icon from one table to another establishes a relationship. Right click on a line to remove a relationship from the query. 34 CSC 240 (Blum)

Intersection Example: People playing themselves (SQL) SQL has an INTERSECT operation like its UNION operation, but it is not supported by Access. 35 CSC 240 (Blum)

Intersection Example: People playing themselves (Design, version 2) concatenation subquery Uses concatenation and a subquery. 36 CSC 240 (Blum)

Intersection Example: People playing themselves (SQL, version 2) Note: Access adds lots of parentheses. 37 CSC 240 (Blum)

Set Difference The set difference of Set A and Set B is all of the elements in Set A that are not also elements of set B. Example: Simpsons characters who are not real people. First. Name, Last. Name(Character) - First. Name, Last. Name(Real. Person) Again the first step is to make “union-compatible” tables. 38 CSC 240 (Blum)

Set Difference Example: Characters that are not real people (Data. Sheet) Ernest Borgnine and James Brown removed. 39 CSC 240 (Blum)

Set Difference Example: Characters that are not real people (Design) Same as the second version of the Intersection query except IN NOT IN 40 CSC 240 (Blum)

Set Difference Example: Characters that are not real people (SQL) 41 CSC 240 (Blum)

Cartesian Product A row in the Cartesian product of Table A and Table B is the concatenation of a row from Table A and a row from Table B. All possible combinations of a row from A and a row from B are made. A B On its own the Cartesian product is not very useful, but it is the first ingredient in a join, which is very useful in querying relational databases. 42 CSC 240 (Blum)

How big is the Cartesian product? Degree(B) A D G J B E H K C F I L Cardinality(B) Cardinality(A) Degree(A) A G D D J N O U E V Cardinality(A B) = Cardinality(A) * Cardinality(B) Degree(A B) = Degree(A) + Degree(B) 43 CSC 240 (Blum)

44 A B C A N A B C G O A B C D U A B C D E A B C J V D E F A N D E F G O D E F D U D E F J V G H I A N G H I G O G H I D U G H I D E G H I J V J K L A N J K L G O J K L D U J K L D E J K L J V CSC 240 (Blum)

Perform a Selection on the Cartesian Product Recall that (Most of) our tables correspond to entities. Entities have relationships. Relationships are realized by having fields in two tables take values from the same domain. E. g. That a Character is voiced by a Real Person is represented by having the Person. ID (which identifies a person in the Real. Person Table) appear the Character Table. 45 CSC 240 (Blum)

46 CSC 240 (Blum)

Perform a Selection on the Cartesian Product (Cont. ) The Cartesian product of the Character and Real. Person Tables has rows in which the person voices the character and rows in which the person does not voice the character. What distinguishes the former is that the Character. Person. ID matches the Real. Person. ID. We can use this condition (predicate) to select out the meaningful rows. 47 CSC 240 (Blum)

48 A B C A N A B C G O A B C D U A B C D E A B C J V D E F A N D E F G O D E F D U D E F J V G H I A N G H I G O G H I D U G H I D E G H I J V J K L A N J K L G O J K L D U J K L D E J K L J V match CSC 240 (Blum)

After the selection A B C A N D E F D U D E F D E G H I G O J K L J V Now we have a table with two identical columns. We can eliminate one (or both) by projecting. 49 CSC 240 (Blum)

After projection A B C N D E F U D E F E G H I O J K L V • The combination of Cartesian product, selection and projection allows you to bring together the related information that was placed in different tables. • This is the key operation in querying. • It is called a join. 50 CSC 240 (Blum)

Real. Person Table 51 CSC 240 (Blum)

Credential Table 52 CSC 240 (Blum)

Cartesian Product of Character and Credential Tables (in Excel) The credentials belong to Groening. 53 CSC 240 (Blum)

Question Do I throw out people even if I don’t have any credentials for them? There are different types of joins. 54 CSC 240 (Blum)

References Database Systems, Rob and Coronel Database Systems, Connolly and Begg 55 CSC 240 (Blum)