Chapter3-Relational Model
Chapter3-Relational Model
Chapter3-Relational Model
Dr. M. Brindha
Assistant Professor
Department of CSE
NIT, Trichy-15
Example of a Relation
Basic Structure
• Formally, given sets D1, D2, …. Dn a relation r is a subset of
D 1 x D2 x … x D n
Thus a relation is a set of n-tuples (a1, a2, …, an) where
each ai Di
• Example: if
customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x
customer-city
Attribute Types
• Each attribute of a relation has a name
• 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
• E.g. multivalued attribute values are not atomic
• E.g. composite attribute values are not atomic
attributes
(or columns)
customer-name customer-street customer-city
customer
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
E.g. account relation with unordered tuples
Database
• A database consists of multiple relations
• Information about an enterprise is broken up into parts,
with each relation storing one part of the information
• “Pure” languages:
• Relational Algebra
• Tuple Relational Calculus
• Domain Relational Calculus
• Find the loan number for each loan of an amount greater than
$1200
Example Queries
• Find the names of all customers who have a loan, an
account, or both, from the bank
• Set intersection
• Natural join
• Division
• Assignment
Set-Intersection Operation
• Notation: r s
• Defined as:
• r s ={ t | t r and t s }
• Assume:
• r, s have the same arity
• attributes of r and s are compatible
• Note: r s = r - (r - s)
Set-Intersection Operation - Example
• Relation r, s:
•r s
Natural-Join Operation
Notation: r s
• Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R S obtained as
follows:
• Consider each pair of tuples tr from r and ts from s.
• If tr and ts have the same value on each of the attributes in R S, add
a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s
• Example:
R = (A, B, C, D)
S = (E, B, D)
• Result schema = (A, B, C, D, E)
• r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Natural Join Operation – Example
• Relations r, s:
Division Operation
• Suited to queries that include the phrase “for all”.
• Let r and s be relations on schemas R and S
respectively where
• R = (A1, …, Am, B1, …, Bn)
• S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
r s = { t | t R-S(r) u s ( tu r ) }
Division Operation – Example
Another Division Example
Assignment Operation
• The assignment operation () provides a convenient way to
express complex queries.
• Write query as a sequential program consisting of
• a series of assignments
• followed by an expression whose value is displayed as a result of
the query.
• Assignment must always be made to a temporary relation
variable.
• Example: Write r s as
temp1 R-S (r)
temp2 R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
• The result to the right of the is assigned to the relation
variable on the left of the .
• May use variable in subsequent expressions.
Example Queries
• Find all customers who have an account from at least the
“Downtown” and the Uptown” branches.
Example Queries
• Find all customers who have an account at all branches
located in Brooklyn city.
Extended Relational-Algebra-Operations
• Generalized Projection
• Outer Join
• Aggregate Functionss
Generalized Projection
• Extends the projection operation by allowing
arithmetic functions to be used in the projection list.
• Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
• Inner Join
loan Borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
To see why
• R-S,S(r) simply reorders attributes of r