csc 240 (blum) 1 relational math. relational algebra csc 240 (blum) 2 the rules for combining one or...

55
CSC 240 (Blum) 1 Relational Math

Upload: arthur-quinn

Post on 13-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

CSC 240 (Blum)1

Relational Math

Page 2: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Relational Algebra

CSC 240 (Blum)2

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.

Page 3: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Operations

CSC 240 (Blum)3

The specific ways of combining elements are known as operations.E.g. addition is an algebraic operationE.g. ANDing is a Boolean operation

Operations are called unary if they act on one element and binary if they act on two elements. E.g square root is a unary algebraic

operation.E.g. addition is a binary algebraic operation.

Page 4: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Relation Table

CSC 240 (Blum)4

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.

Page 5: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Synonym

CSC 240 (Blum)5

Recall our old synonymsTable 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”

Page 6: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection/Restriction

CSC 240 (Blum)6

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 )

Page 7: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Customers from PA (Design)

CSC 240 (Blum)7

Customer.* refers to all of the columns.

The condition (predicate) selecting out particular rows.

Page 8: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: PA Only (DataSheet)

CSC 240 (Blum)8

Page 9: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: PA Only (SQL)

CSC 240 (Blum)9

Condition

Page 10: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Condition can be compound.

CSC 240 (Blum)10

The selection condition may be a compound

condition.

ConditionA AND ConditionB

ConditionA OR ConditionB

Example: Let us select from the Customer table

those people who from Philadelphia and from

PA. (There are other Philadelphias, e.g. in

Mississippi)

Page 11: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Added Some New Customers

CSC 240 (Blum)11

Page 12: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Philadelphia AND PA (Design)

CSC 240 (Blum)12

ANDed conditions are entered on the same line.

Page 13: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Philadelphia AND PA (DataSheet)

CSC 240 (Blum)13

Page 14: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Philadelphia AND PA (SQL)

CSC 240 (Blum)14

ANDed conditions

Page 15: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Customers from PA or NJ (Design)

CSC 240 (Blum)15

ORed conditions are entered on separate lines.

Page 16: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Customers from PA or NJ (DataSheet)

CSC 240 (Blum)16

Page 17: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Selection Example: Customers from PA or NJ (SQL)

CSC 240 (Blum)17

ORed Conditions

Page 18: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Projection

CSC 240 (Blum)18

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.

column1,column2,… ( R )

Page 19: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Projection Example: Customer’s first and last names (Design)

CSC 240 (Blum)19

Choose columns and check to show them.

Page 20: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Projection Example: Customer’s first and last names (DataSheet)

CSC 240 (Blum)20

Page 21: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Projection Example: Customer’s first and last names (SQL)

CSC 240 (Blum)21

Columns that will appear.

Page 22: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Compatible

CSC 240 (Blum)22

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 union-compatible.

Then you can consider forming The union of the two setsThe intersection of the two sets The set difference

Page 23: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

A Simpsons Database

CSC 240 (Blum)23

Page 24: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union

CSC 240 (Blum)24

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 BIf 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 RealPerson tables

FirstName,LastName(Character) FirstName,LastName(RealPerson)

Page 25: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Example: Character and RealPerson names

CSC 240 (Blum)25

Step 1 would be to create union-compatible tables using projection. Step 2 would be to take the union of these tables.

Page 26: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Example: Character and RealPerson names (DataSheet)

CSC 240 (Blum)26

Page 27: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Example: Character and RealPerson names (DataSheet)

CSC 240 (Blum)27

Page 28: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Example: Character and RealPerson names (No Design )

CSC 240 (Blum)28

Query-By-Example (QEB) 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!

Page 29: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Union Example: Character and RealPerson names

CSC 240 (Blum)29

Page 30: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

UNION ALL

CSC 240 (Blum)30

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.

Page 31: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection

CSC 240 (Blum)31

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.

FirstName,LastName(Character)

FirstName,LastName(RealPerson)

Again the first step is to make “union-compatible” tables.

Page 32: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (DataSheet)

CSC 240 (Blum)32

Page 33: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (Design, step 1)

CSC 240 (Blum)33

Page 34: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (Design, step 2)

CSC 240 (Blum)34

Dragging a field icon from one table to another establishes a relationship. Right click on a line to remove a relationship from the query.

Page 35: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (SQL)

CSC 240 (Blum)35

SQL has an INTERSECT operation like its UNION operation, but it is not supported by Access.

Page 36: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (Design, version 2)

CSC 240 (Blum)36

concatenation

Uses concatenation and a subquery.

subquery

Page 37: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Intersection Example: People playing themselves (SQL, version 2)

CSC 240 (Blum)37

Note: Access adds lots of parentheses.

Page 38: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Set Difference

CSC 240 (Blum)38

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.

FirstName,LastName(Character) -FirstName,LastName(RealPerson)

Again the first step is to make “union-compatible” tables.

Page 39: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Set Difference Example: Characters that are not real people (DataSheet)

CSC 240 (Blum)39

Ernest Borgnine and James Brown removed.

Page 40: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Set Difference Example: Characters that are not real people (Design)

CSC 240 (Blum)40

Same as the second version of the Intersection query except IN NOT IN

Page 41: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Set Difference Example: Characters that are not real people (SQL)

CSC 240 (Blum)41

Page 42: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Cartesian Product

CSC 240 (Blum)42

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 BOn 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.

Page 43: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

How big is the Cartesian product?

CSC 240 (Blum)43

A B C

D E F

G H I

J K L

Degree(A)

Car

dina

lity

(A) A N

G O

D U

D E

J VC

ardi

nali

ty(B

)

Degree(B)

Cardinality(AB) = Cardinality(A) * Cardinality(B)

Degree(AB) = Degree(A) + Degree(B)

Page 44: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

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 D E

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

Page 45: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Perform a Selection on the Cartesian Product

CSC 240 (Blum)45

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 PersonID (which identifies a

person in the RealPerson Table) appear the Character

Table.

Page 46: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

CSC 240 (Blum)46

Page 47: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Perform a Selection on the Cartesian Product (Cont.)

CSC 240 (Blum)47

The Cartesian product of the Character and RealPerson 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.PersonID matches the RealPerson.PersonID.

We can use this condition (predicate) to select out the meaningful rows.

Page 48: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

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 D E

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

Page 49: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

After the selection

CSC 240 (Blum)49

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.

Page 50: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

After projection

CSC 240 (Blum)50

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.

Page 51: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

RealPerson Table

CSC 240 (Blum)51

Page 52: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Credential Table

CSC 240 (Blum)52

Page 53: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Cartesian Product of Character and Credential Tables (in Excel)

CSC 240 (Blum)53

The credentials belong to Groening.

Page 54: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

Question

CSC 240 (Blum)54

Do I throw out people even if I don’t have any credentials for them?

There are different types of joins.

Page 55: CSC 240 (Blum) 1 Relational Math. Relational Algebra CSC 240 (Blum) 2 The rules for combining one or more numbers (or symbols standing in for numbers)

References

CSC 240 (Blum)55

Database Systems, Rob and CoronelDatabase Systems, Connolly and Begg