Relational Models.
Relational Models.
Example of a Relation
attributes
(or columns)
tuples
(or rows)
Attribute Types
The set of allowed values for each attribute is called the domain
of the attribute
Attribute values are (normally) required to be atomic; that is,
indivisible
The special value null is a member of every domain. Indicated
that the value is “unknown”
The null value causes complications in the definition of many
operations
Relation Schema and Instance
A1, A2, …, An are attributes
Notation: p(r)
p is called the selection predicate
Defined as:
p(r) = {t | t r and p(t)}
Where p is a formula in propositional calculus consisting of
terms connected by : (and), (or), (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <.
Example of selection:
branch-name=“Perryridge”(account)
Select Operation – selection of rows (tuples)
Relation r
Relation r:
A,C (r)
Union Operation
Notation: r s
Defined as:
r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)
E.g. to find all customers with either an account or a loan
customer-name (depositor) customer-name (borrower)
Union of two relations
Relations r, s:
r s:
Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t r and t s}
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Set difference of two relations
Relations r, s:
r – s:
Set intersection of two relations
Relation r, s:
rs
Note: r s = r – (r – s)
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S) are disjoint. (That is,
R S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must be
used.
joining two relations -- Cartesian-product
Relations r, s:
r x s:
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
Renaming a Table
Allows us to refer to a relation, (say E) by more than one name.
x (E)
Relations r
rxs
A=C (r x s)
Joining two relations – Natural Join
Natural Join
r s
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
∪
(Union) Π name (instructor) ∪ Π name (student)
Output the set difference of tuples from the two input relations.
⋈
(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
Banking Example
Find the names of all customers who have a loan, an account, or both,
from the bank
customer-name (borrower) customer-name (depositor)
Find the names of all customers who have a loan and an account at
bank.
customer-name (borrower) customer-name (depositor)
Example Queries
Find the names of all customers who have a loan at the Perryridge branch.
customer-name (branch-name=“Perryridge”
(borrower.loan-number = loan.loan-number(borrower x loan)))
Find the names of all customers who have a loan at the Perryridge branch but
do not have an account at any branch of the bank.
– customer-name(depositor)
employee (person name, street, city)
works (person name, company name, salary)
company (company name, city)
E1 x E2