0% found this document useful (0 votes)
28 views

Relations and Relational Algebra

database essential is all what you need when you are trying ro be a professional data base wroter in such a modren word of coding while all other companies are hiring

Uploaded by

FahadJassim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Relations and Relational Algebra

database essential is all what you need when you are trying ro be a professional data base wroter in such a modren word of coding while all other companies are hiring

Uploaded by

FahadJassim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

RELATIONS AND

RELATIONAL ALGEBRA

Lecture 2
Relations

■ We will use tables to represent relations

Alice [email protected]

Ben [email protected]

Cathy [email protected]
Relations

■ This is a relation between people and email addresses

Alice [email protected]

Ben [email protected]

Cathy [email protected]
Relations
■ A mathematician would say that it is a set of
pairs
– < Alice, aaa @ abc.com>
– < Ben, bbb @ abc.com>
– < Cathy, ccc @ abc.com>

Alice [email protected]

Ben [email protected]

Cathy [email protected]
Relations
■ Each value in the first column is a name, each value in
the second column is an email address
■ In general, each column has a domain
– a set from which all possible values can come

Alice [email protected]

Ben [email protected]

Cathy [email protected]
Relations
■ A mathematical relation is a set of tuples: sequences of
values
■ Each tuple corresponds to a row in a table

Alice [email protected] 0115111111

Ben [email protected] 0115222222

Cathy [email protected] 0115333333


Relations: terminology

■ Degree of a relation: how long the tuples are, or how


many columns the table has
– In the first example (name,email) degree of the
relation is 2
– In the second example (name,email,telephone)
degree of the relation is 3
– Often relations of degree 2 are called binary,
relations of degree 3 are called ternary, etc.
■ Cardinality of the relation: how many different tuples
are there, or how many different rows the table has
Relational model: data manipulation

■ Data is represented as relations


■ Manipulation of data (query and update operations)
corresponds to operations on relations
■ Relational algebra describes those operations
– They take relations as arguments and produce new relations
■ Think of numbers and corresponding operators +,,\, *
■ Or, think of Boolean values and corresponding operators
&,|,! (and, or, not)
■ Relational algebra contains two kinds of operators: common
set-theoritic operators, and operators specific to relations
Union

■ Standard set-theoretic definition of union


A  B = {x: x  A or x  B}

■ For example, {a,b,c}  {a,d,e} = {a,b,c,d,e}

■ For relations, we want the result to be a relation again


– a proper table, with each column associated with a single domain
of values

■ So we require in order to take a union of relations R and S


that R and S have the same number of columns, and that
corresponding columns have the same domains
Union-compatible relations

■ Two relations R and S are union-compatible if they have


the same number of columns and corresponding columns
have the same domains
Example: not union-compatible

(different number of columns)

Alice aaa 111111 Tom 1980

Ben bbb 222222 Sam 1985

Cathy ccc 333333 Steve 1986


Example: not union-compatible

(different domains for the second column)

Alice aaa Tom 1980

Ben bbb Sam 1985

Cathy ccc Steve 1986


Example: union-compatible

(same number of columns and same domains)

Alice 1970 Tom 1980

Ben 1971 Sam 1985

Cathy 1972 Steve 1986


Union of two relations

■ Let R and S be two union-compatible relations. Then their


union R  S is a relation which contains tuples from both
relations
R  S = {x: x  R or x  S}.

■ Note that union is a partial operation on relations


– it is only defined for some (compatible) relations, not for all of
them

■ Similar to division for numbers


– result of division by 0 is not defined
Example: shopping lists

R S R  S

Cheese 1.34 Cream 5.00 Cheese 1.34


Milk 0.80 Soap 1.00 Milk 0.80
Bread 0.60 Bread 0.60
Eggs 1.20 Eggs 1.20
Soap 1.00 Soap 1.00
Cream 5.00
Difference of two relations

Let R and S be two union-compatible relations. Then their


difference (R  S) is a relation which contains tuples,
which are in R but not in S:
R  S = {x: x  R and x  S}
■ Note that difference is also a partial operation on
relations
Example

R S R  S

Cheese 1.34 Cream 5.00 Cheese 1.34


Milk 0.80 Soap 1.00 Milk 0.80
Bread 0.60 Bread 0.60
Eggs 1.20 Eggs 1.20
Soap 1.00
Intersection of two relations

Let R and S be two union-compatible relations. Then their


intersection (R  S ) is a relation which contains tuples,
which are both in R and S:
R  S = {x: x  R and x  S}
■ Note that intersection is also a partial operation on
relations
Example

R S R  S

Cheese 1.34 Cream 5.00 Soap 1.00


Milk 0.80 Soap 1.00
Bread 0.60
Eggs 1.20
Soap 1.00
Cartesian product

■ Cartesian product is a total operation on relations


■ Usual set-theoretic definition of product
R  S = {<x,y>: x  R, y  S}
■ Under the standard definition, if
<Cheese, 1.34>  R and <Soap,1.00>  S, then
< <Cheese, 1.34>, <Soap,1.00>>  R  S
(the result is a pair of tuples)
Extended Cartesian product

■ Extended Cartesian product flattens the result in a


4-element tuple: <Cheese, 1.34, Soap, 1.00>
■ For the rest of the course, “product” means extended
product.
Example
R S RS

Cheese 1.34 Cream 5.00 Cheese 1.34 Cream 5.00


Milk 0.80 Soap 1.00 Milk 0.80 Cream 5.00
Bread 0.60 Bread 0.60 Cream 5.00
Eggs 1.20 Eggs 1.20 Cream 5.00
Soap 1.00 Soap 1.00 Cream 5.00
Cheese 1.34 Soap 1.00
Milk 0.80 Soap 1.00
Bread 0.60 Soap 1.00
Eggs 1.20 Soap 1.00
Soap 1.00 Soap 1.00
Projection

■ Let R be a relation with n columns, and X is a


set of column identifiers (at the moment, we
will use numbers, but later we will give then
names, like “Email”, or “Telephone”)
■ Then projection of R on X is a new relation
 X(R), which only has columns from X
■ For example, 1,2(R) is a table with only the 1st
and 2nd columns from R
Examples

R:
1 2 3

Alice [email protected] 0115111111

Ben [email protected] 0115222222

Cathy [email protected] 0115333333


Example: 1,3 ( R)

Alice 0115111111

Ben 0115222222

Cathy 0115333333
Example: 2,3 ( R)

[email protected]
0115111111 0115111111

[email protected]
0115222222 0115222222

[email protected]
0115333333 0115333333
Selection
■ Let R be a relation with n columns, and  is a property
represented as a condition
■ Selection from R, subject to condition  is defined as
follows:
  (R) , which is the set of rows from R that satisfy the
condition 
What is a meaningful comparison
■  is a comparator which makes sense when applied to
values from i and j columns (= , , maybe also , , , 
if there is a natural order on values)
■ We can always at least tell if two values in the same
domain are equal or not (database values are finitely
represented)
■ In some cases, it makes sense to compare values from
different column domains; for example, if both are
domains contain strings, or both contain dates
■ For example, 1975 > 1987 is a meaningful comparison,
“Anne” = 1981 is not
■ We can only use a comparison in selection property if its
result is true or false, never undefined
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)


R
Insomnia Nolan 2002
Magnolia Anderson 1999
Insomnia Skjoldbjaerg 1997
Memento Nolan 2000
Gattaca Niccol 1997
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)

Insomnia Nolan 2002


Magnolia Anderson 1999
Insomnia Skjoldbjaerg 1997
Memento Nolan 2000
Gattaca Niccol 1997
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)

Insomnia Nolan 2002


Magnolia Anderson 1999
Insomnia Skjoldbjaerg 1997
Memento Nolan 2000
Gattaca Niccol 1997
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)

Insomnia Nolan 2002


Magnolia Anderson 1999
Insomnia Skjoldbjaerg 1997
Memento Nolan 2000
Gattaca Niccol 1997
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)

Insomnia Nolan 2002


Magnolia Anderson 1999
Insomnia Skjoldbjaerg 1997
Memento Nolan 2000
Gattaca Niccol 1997
Example: selection

■ col(3) < 2002 and col(2) = Nolan (R)

Memento Nolan 2000


Summary
– Data is represented as tables
– Operations on tables
■ union of two union-compatible tables (tables with the
same number of columns and the same domains for
corresponding columns)
■ set difference of two union-compatible tables
■ intersection of two union-compatible tables
■ extended Cartesian product of two tables
■ project a table on some of its columns
■ select rows in a table satisfying a property
– Result of an operation is again a table, so operations can be
chained
Example exam question

■ What is the result of


1,3( col(2) = col(4) (R  S) ), where R and S are:

R S
Anne 111111 Chris 333333

Bob 222222 Dan 111111


End of Lecture

You might also like