Professional Documents
Culture Documents
The Relational Model
The Relational Model
The Relational Model
INTRODUCTION
• Relational Model is the major data model for commercial
data processing applications. Data in the relational model
is represented by a relation. A relation consist of a
relational schema and relational instance.
• The relational schema consist of relation’s name, name
of each field and domain (range) of each field. For e.g.,
relational schema of students is shown below:-
• STUDENT (Rollno: integer, Name: string, Marks: integer,
Addr: string).
• A relational instance is a set of tuples (records) in which
each tuple has same number of fields as defined in the
relational schema.
EXAMPLE
DOMAIN
----- ----- ----- -----
----- ----- ----- -----
RELATION
102 B 81 H
TUPLES 103 C 82 I
(Records)
104 D 83 J
105 E 84 K
Fields (Attributes)
1. SELECTION ().
2. PROJECTION ().
3. UNION ().
4. INTERSECTION (∩).
5. CROSS PRODUCT (Cartesian Product) (x).
6. DIFFERENCE (Set difference) (–).
7. RENAME ().
8. DIVISION (/).
9. JOIN ( )
(Note:- 3-6 are called set operators (or Binary operations).
1,2,7 Unary operations because they operate on one relation )
SAMPLE TABLES
SID SNAME RATING AGE
(A) Sailors Table
1 S1 2 25
2 S2 3 26
3 S3 1 22
4 S4 2 21
5 S5 4 20
S1 2 S2 3 26
3 S3 1 22
4 S4 2 21
S2 3
4
S3
S4
1
2
22
21
5 S5 4 20
R1 1
1
11
12
10/09/2015
20/11/2015
UNION
• Union of two instances S1 and S2 returns a
relational instance, which contains all tuples that
occur in either s1 or s2 or both. Two relational
instances are called UNION compatible if they
have same number of fields. For e.g. S = S1 S2
SID SNAME RATING AGE
1 S1 2 35
2 S2 3 26
3 S3 1 22
4 S4 2 21
5 S5 4 20
INTERSECTION
• Intersection of two instances S1 and S2
returns a relational instance which contains all
tuples that occur in both instances. For e.g.
S= S1 ∩ S2
• Three types:
– Conditional or Theta Join
– Equi Join
– Natural Join
CONDITIONAL JOIN (Non-Equi Join)
A M A N A M A N
1 m NULL NULL 2 n 2 p
2 n 2 p NULL NULL 3 q
4 o NULL NULL NULL NULL 5 r
Full Outer Join
Full Outer Join
RENAME OPERATOR
• Rename operator is used to avoid naming conflicts.
Rename operator is represented by .
DIVISION OPERATOR
• The division operator is useful for representing same
special type of queries like:- Find the names of sailors
who have reserved all boats.
DIVISION OPERATOR
• The division operator is useful for representing same
special type of queries like:- Find the names of sailors
who have reserved all boats.
RELATIONAL CALCULUS
• Relational Calculus is a formal query language of
relational model. Relational Calculus is an
alternative to relational algebra, which is
procedural, the calculus is non-procedural.
Relational Calculus describes the answer without
specifying how the answer is to be computed.
Relational Calculus has important role in the
design of commercial language like SQL.
• Relational Calculus is of following types:-
1. Tuple Relational Calculus (TRC).
2. Domain Relational Calculus (DRC).
TUPLE RELATIONAL CALCULUS
• A tuple variable is a variable that takes on tuples of a particular
relational schema as values. Every value assigned to a tuple
variable has some number and types of fields.
• A tuple relational calculus query has a form:-
• A nonprocedural query language, where each query is of the form
{t | P (t ) }
• It is the set of all tuples t such that predicate P is true for t
• t is a tuple variable, t [A ] denotes the value of tuple t on attribute
A
• t r denotes that tuple t is in relation r
• P is a formula similar to that of the predicate calculus
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , , , , )
3. Set of connectives: and (), or (v)‚ not ()
4. Implication (): x y, if x if true, then y is true
x y x v y
5. Set of quantifiers:
t r (Q (t )) ”there exists” a tuple in t in relation r
such that predicate Q (t ) is true
t r (Q (t )) Q is true “for all” tuples t in relation r
Banking Example
• branch (branch_name, branch_city, assets )
• customer (customer_name, customer_street,
customer_city )
• account (account_number, branch_name,
balance )
• loan (loan_number, branch_name, amount )
• depositor (customer_name, account_number )
• borrower (customer_name, loan_number )
Example Queries
• Find the loan_number, branch_name, and amount for
loans of over $1200.
{ x , x , …, x | P (x , x , …, x )}
1 2 n 1 2 n
{ c | l, b, a ( c, l borrower l, b, a loan a
> 1200)}
Example Queries
• Find the names of all customers who have a
loan from the Perryridge branch.
{ c, a | l ( c, l borrower
b ( l, b, a loan b =
“Perryridge”))}
Pitfalls in Relational Database Design
• Relational database design requires that we find a “good”
collection of relation schemas. A bad design may lead to:-
– Repetition of Information.
– Inability to represent certain information.
• Design Goals: