RelationalModel
RelationalModel
Let R S1 X S2
Table Relation
Column Attribute/Domain
Row Tuple
Values in a column Domain
Table Definition Schema of a Relation
Populated Table Extension
Example
Properties OF RELATIONS
Every tuple contains exactly one value for each attribute
(atomic)
The tuples are not considered to be ordered, even
though they appear to be in the tabular form.
There is no ordering of the attributes
There are no duplicate tuples
Relations vs. Tables
A relation cannot have a duplicate tuple by definition;
tables, if ill-managed, can
Rows and columns are ordered, where tuples and
attributes are not
A table must be created with at least one attribute; a
relation needs none
Tables may contain nulls, a relation cannot
A special null value is used to represent values that are
unknown or inapplicable to certain rows
Candidate Key
Keys (Super, Candidate, Primary, Alternate)
Superkey of R: A set of attributes SK of R such that
no two tuples in any valid relation instance r(R) will
have the same value for SK. That is, for any distinct
tuples t1 and t2 in r(R), t1[SK] t2[SK] (Uniqueness
property).
Candidate Key of R: A "minimal" superkey; that is, a
superkey K such that removal of any attribute from K
results in a set of attributes that is not a superkey.
Example: The CAR relation schema:
CAR(State, Reg#, SerialNo, Make, Model, Year)
has two candidate keys Key1 = {State, Reg#}, Key2 =
{SerialNo}, which are also superkeys. {SerialNo,
Make} is a superkey but not a candidate key.
If a relation has several candidate keys, one is
chosen arbitrarily to be the primary key. Other
candidate keys are called Alternate keys.
Supplier-part Database
Foreign key
A relation schema may have an attribute that
corresponds to the primary key of another relation.
The attribute is called a foreign key.
Operations in Relational Model
Relational Algebra
Relational Calculus
Tuple relational calculus
Domain relational calculus
Relational Algebra
1 2
2 3
1 s
r
A B
n Symbolic form- r s 1
n r UNION s
2
1
3
Intersect
1 2
2 3
1 s
r
n Symbolic form- r ∩ s
A B
n r INTERSECT s
1
2
1
3
Difference
Difference operates on two sets and returns a set
containing all tuples occuring in one but not the other,
using MINUS
For Difference, the sets operated upon must be of the
same type - known as union compatibility
Example: Difference
Relations r, s: A B A B
1 2
2 3
1 s
r
n Symbolic form- r – s
A B
n r MINUS s
1
1
Cartesian Product
All attribute names must be different
A Cartesian Product is the set of all ordered pairs such
that in each pair, the first element comes from the first
set, and the second element comes from the second set
However, since the result of a relational operator is a
relation, the result of each pair is a single tuple
containing all the elements of both of the source
tuples
Uses keyword TIMES
Example- Cartesian product
n Relations r, s:
A B C D E
1 10 a
10 a
2 20 b
r 10 b
n r TIMES s s
n Symbolic form- r x s
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
Restrict
A B C D
1 7
5 7
r WHERE ((A=B) AND (D>5))
12 3
Symbolic form- A=B ^ D > 5 (r) 23 10
A B C D
1 7
23 10
Project
Returns specified columns
Yields a vertical subset
The general form is a commalist of attributes to be
kept in the result
All attributes are kept, all tuples are kept
An alternative specification is to name the attributes to
be excluded:
P { ALL BUT WEIGHT}
Project Operation – Example
Relation r: A B C
10 1
20 1
30 1
40 2
r {A, C}
Symbolic form- A,C (r) A C A C
1 1
1 = 1
1 2
2
Join – Natural Join
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r NATURAL JOIN s r s
n r s A B C D E
1 a
1 a
1 a
1 a
2 b
Join – Theta Join
Used to join relations based on matching attributes,
where the values are not equal
Given relations a and b, and attributes X and Y, this
can be expressed as follows:
(a TIMES b) WHERE X theta Y
a (X theta Y) b
When theta is set to = the result can be made to be
that of natural join (project away the duplicate
attribute, and rename the kept one)
Divide - Example
S be a relation of suppliers, P one of parts, and SP the
mediator
S JOIN ( S {S#} DIVIDEDBY P {p#}
PER SP {S#, P#} )
Will return a relation with suppliers who supply all
parts, only
Division- symbolic form
Notation: c b
c = (A1, …, Am , B1, …, Bn )
b = (B1, …, Bn)
The result of c b is a relation on schema
c – b = (A1, …, Am)
c b = { t | t c-b (c) u b ( tu c ) }
Where tu means the concatenation of tuples t
and u to produce a single tuple
Division Operation – Example
n Relations r, s:
A B
B
1 1
2
3 2
1 s
1
1
3
4
6
n r s: A 1
2
r
Another Division Example
n Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
n r s:
A B C
a
a
Relational Algebra
basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
All other operators (intersect, join, divide) can be
specified in terms of basic operators
Division Operation
Property
Let q = r s
Then q is the largest relation satisfying q x s r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
To see why
R-S (r) simply reorders attributes of r
7
7
3
10
27
Aggregate Operation – Example
Relation account grouped by branch-name:
branch_name account_number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
What is the Algebra for?