0% found this document useful (0 votes)
35 views22 pages

Relational Model

The document discusses the relational model for databases. It describes key concepts like relations, tuples, attributes, domains, relation schemas, and database instances. It also covers relational algebra operations like select, project, join, union, and set differences. Query languages like relational calculus and domain relational calculus are introduced. Views and modifying databases through insertion, deletion, and updating are also summarized.

Uploaded by

Ajay Kumar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views22 pages

Relational Model

The document discusses the relational model for databases. It describes key concepts like relations, tuples, attributes, domains, relation schemas, and database instances. It also covers relational algebra operations like select, project, join, union, and set differences. Query languages like relational calculus and domain relational calculus are introduced. Views and modifying databases through insertion, deletion, and updating are also summarized.

Uploaded by

Ajay Kumar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Relational Model

The relational model was proposed by Codd of IBM San Jose


Research Laboratory.
A relational database consists of a collection of tables.
A row in a table represents a relationships among a set of values.

Domain of an attribute: A set of permitted values


dom (A)
dom(A) x dom(B) = {<x,y> | xϵ dom(A), y ϵ dom(B)}
In general D1 x D2 x . . . . X D n-1 x Dn
 A relation is a subset of a Cartesian product of a list domains (i.e., a
set of tuples)
Let us consider the E-R Diagram

Deposit

Custmomer Branch

Borrow

Let R be a relation schema. If we say that a subset K of R is a


Superkey for R, we mean the following:
Let r(R) be a relation.
If t1, t2 ϵ r and t1 ≠ t2
then t1[K] ≠ t2[K]
A set of attributes.
Database Schema: A set of relation schemas
A set of relations
A Database Instance: All that data present in a database at given point
of time.
A relation schema is a list of attributes and their corresponding domains.
Example:
Relation Schemas:
Branch-Schema =(branch-name, assets, branch-city)
Customer-Schema =(customer-name, street, customer-city)
Deposit-Schema=(branch-name, a/c-number, customer-name, balance)
Borrow-Schema =(branch-name, loan-number, customer-name, amount)
Relations:
branch (branch-schema)
customer (customer-schema)
deposit (deposit-schema)
borrow (borrow-schema)
Query Languages:
Procedural Languages
Non-Procedural Languages
Pure Languages X Commercial Languages

The relational algebra The tuple relational calculus


(procedural) & the domain relational calculus (Non-procedural)

The Relational Algebra:


The relational algebra is an algebra defined over relations.
Consists of a set of operations that take one or two relations as input and produce
a new relation as their result.

The Fundamental operations:


Select, Project, Cartesian -product, Rename, Union and Set difference.
Additional Operations: set intersection, natural join, division and assignment.
Select Operation (σ):
σ branch-name=“university”(borrow)
σ amount>5000 (borrow)
σ p(r)

Select operation predicate a relation

Predicate may involve: =, ≠, <, ≤, >, ≥ and connectives: and (Λ) and or (˅)

Example: σ bname = “university” Λ amount > 5000 (borrow)

 The selection predicate may include comparisons between two attributes:

σ cname = street (customer)


Project Operation (Π)
 It is a unary operation that returns its argument relation, with certain columns left out
(duplicate rows one eliminated)

Example: i. Πbname, cname (borrow)

ii. “Find those customers who have the same name as their street”.
Πcname(σcname = street (customer))

Cartesian Product Operation(X)


This is a binary operation
Notation : r1 x r2, where r1 & r2 are relations

Example: deposit x borrow

The relation schema for the above is:


(deposit.branch-name, account-number,
deposit.customer-name, balance
barrow.branch-name, loan-number,
borrow.customer-name, amount)
“Find all customer names whose loan amount is greater than their
balance”.
Πdeposit.cname(σdeposit.cname=borrow.cname Λ amount > balance(deposit X borrow))

“Find the name of all the customers who live in the same city and
same street as Shiva”.

The Rename Operatin (ρ)


σ p(customer X Πstreet, ccity (σcname=“shiva”(customer)))

how to express p?

 The expression ρx(r) returns relation r under the name x.


Πcustomer.cname(σcustomer2.street = customer.street Λ customer2.ccity = customer.ccity
(customer X (Πstreet, ccity(σcname=“shiva”(ρcustomer2(customer))))))
The Union Operation (U)
“Find all customers of the “university” branch.
Πcname(σbname = “university” (borrow)) U
Πcname(σbname = “university” (deposit))

The Set Difference Operation (-)


“Find all customers of Racecourse branch who have an account there but not
a loan”.
Πcname(σbname = “Racecourse” (deposit))
- Πcname(σbname = “Racecourse” (borrow))

“Find the largest account balance in the bank”.


Πbalance(deposit) – Πdeposit.balance (σdeposit.balance < d.balance(deposit X ρd(deposit) )

“Find the name of the person who has got the highest balance”.
Additional Operations:
Set Intersection Operation (∩)
“Find all customers with both a loan and an account at the Majestic branch”.
Πcname(σbname = “majestic” (borrow)) ∩
Πcname(σbname = “majestic” (deposit))
r Λ s = r – (r-s)

Natural Join Operation ( )

The natural join is a binary operation that allows us to combine certain


selections and a Cartesian product into one operation.

The natural join operation forms a Cartesian product of its two arguments,
performs a selection forcing equality on those attributes that appear in both
relation schemas and finally removes duplicate columns.
r1(R1) r2(R2)
A1 A2 A2 B
a b b d
c b e f

r1 x r2
A1 R1.A2 R2.A2 B
a b b d
a b c f
c b b d
c b e f

A1 R1r.A
1 2 r2 R2.A2 B
a b b d
c b b d
r s = ΠRUS (σr.A1 = s.A1 ∩ r.A2 = s.A2… ∩ r.An = s.An (r x s))
where R ∩ S = {A1,A2,…An}
“Find all customers who have a loan at the bank and the cities in
which they live”
Πborrow.cname, ccity (σborrow.cname = customer.cname (borrow X customer) )
≡ Πcname,ccity (borrow customer)

“Find the assets and name of all branches which have depositors living
in Calcutta”.
Πasset, bname (σccity = “calcutta” (branch deposit customer) )

“Find all customers who have both an account and a loan at the
university branch”.
Division Operation (÷)
“Find all customers who have an account at all branches located in
Delhi”.
Πbname, cname (deposit) ÷ Πbname (σbcity = “Delhi” (branch ))

Formally, let r(R) and s(S) be relations, and let S ⊆ R. the relation r ÷
s is a relation on schema R-S. A tuple t is in r ÷ s if for every tuple t s ϵ
s, there is a tuple tr in r satisfying both of the following.

tr [S] = ts [S]
tr [R - S] = ts [R - S]

Let r(R) and s(S) be given with S ⊆ R:


r ÷ s = ΠR - S(r) - ΠR – S ((ΠR - S(r) x s) - r)
The Assignment Operation ()
temp  ΠR - S(r)
result  temp - ΠR – S ((temp x s) - r)

Tuple Relational Calculus:

 A non-procedural language.
 Query format: {t│p(t)}

“Find all customers who have a loan for an amount greater than 5000”.
{t │ ∃ s ϵ borrow (t[cname] = s[cname] Ʌ s[amount] > 5000) }

“Find the branch-name, loan-number, customer-name and amount for


loans of over 5000”.
{t │ t ϵ borrow Ʌ t[amount] > 5000) }
The Domain Relational Calculus:
 user domain variables:

“Find the branch name, loan number, customer name and amount for
loans of over 5000”.
{<b, l, c, a> │ <b, l, c, a> ϵ borrow Ʌ a > 5000 }

“Find all customers who have a loan for an amount > 5000”.
{<c> │ ∃ b, l, a (<b, l, c, a> ϵ borrow Ʌ a > 5000 ) }

Modifying the Database: (In relational algebra)


Database modifications are expressed using the assignment operator.
Deletion: r  r – E
“Delete all of MGR’s accounts”
deposit  deposit - σbcname = “MGR” (deposit)
“Find all customers having a loan from the saltlake branch and the
cities in which they live”.

{t │ ∃ s ϵ borrow (t[cname] = s[cname] Ʌ s[bname] = “saltlake”


Ʌ ∃ u ϵ customer (u[cname] = s[cname] Ʌ t[ccity] = u[ccity]))}

“Find all customers having a loan or an account or both at the


majestic branch”.
“Find all customers who have both an account and a loan at the
majestic branch”.
“Find all customers who have an account at the Rajawada branch
but not the loan at the Rajawada branch”.
“Find all customers who have an account at all branches located at
Bangalore”.

{t │ ∀ u ϵ branch (u[bcity] = “Bangalore” 


∃ s ϵ deposit (t[cname] = s[cname] Ʌ u[bname] = s[bname])) }
Insertion: r  r U E
Updating: .,: δ A  E (r)
e.g.,: δ balance  balance * 1.05 (deposit)

Views:
 Any relation that is not part of the conceptual model but is made
visible to a user as a “virtual relation” is called a view.
A view must be recomputed for each query that refers to it.
A view definition causes the saving of an expression to be substituted
into queries using the view.

Create view V as < Query expression >


e.g., create view all-customer as
Πbname, cname (deposit) U Πbname, cname (borrow)

“Find all customers of the university branch”.


Πcname (σbname = “university” (all-customer ))
Updates through views and Null values:
Create view loan-info as
Πbname, loan-number, cname (borrow)

Loan-info  loan-info U {(“Anna”, 50, “Jayalallitha”)}


i. Reject or
ii. Insert (“Anna”, 50, “Jayalallitya”, null) into the borrow relation
(comparisons with null defined to be false)

Create view branch-city as Πbname, cname (borrow customer)


Branch-city  branch-city U {(“whitefield”,”Bangalore”)}

(“whitefield”, null, null, null)  borrow


(null, null, “Bangalore”)  customer

Consider σbname, ccity (branch-city))


Result unsatisfactory.
Safety of Expressions:

{t │ ¬ (t ϵ borrow)}

 There are infinitely many tuples that are not in borrow.


 The domain of a tuple relational formula : p
Dom (P)
 We say that an expression {t │ p(t)} is safe if all values that appear
in the result are values from dom (p).

Expressive Power of Languages:

The tuple relational calculus and the domain relational calculus


restricted to safe expressions is equivalent in expressive power to
the relational algebra.
Customer Relation:

Customer-name Street Customer-city


Shiva Miyya Kanpur
Kaveri Racecourse Bangalore
MGR MGR Hyderabad
Ahilya Rajawada Indore
Laloo Defence Patna
Priyanka Trremuthi Delhi
Ganesh Dhankawadi Pune
Pvrao Rastrapathibavan Delhi
Satri Satri Ahmadabad
Branch relation:

Branch-name Assets Branch-city


University 1900000 Indore
Godhacolony 200000 Inndore
Majestic 550000 Bangalore
Racecourse 3000000 Bangalore
Saltlake 110000 Calcutta
Narima 80000000 Bombay
Anna 70000 Madras
Rajawada 8000000 Indre
Palikabazar 78000000 Delhi
Redfort 80000000 Delhi
Borrow relation:

Branch-name Loan-number Customer name Amount


Majestic 90 Suresh 10000
Majestic 10 Kaveri 7000
Saltlake 80 Laloo 1000
Racecourse 20 Pvrao 5000
Nariman 60 Kaveri 2500
University 25 Shiva 3000
Palikabazar 36 Laloo 600
Nariman 17 Laloo 800
University 63 Ganesh 6500
University 77 Ahilya 7000
Rajawada 89 Chitale 10000
Rajawada 37 Ahilya 50000
Majestic 10 Shiva 7000
Deposit relation:
Branch-name Account-number Customer name Balance
Rajawada 81 John 10000
Majestic 26 Kaveri 50000
Palikabazar 19 Pvrao 60000
Palikabazar 86 Laloo 17000
Majestic 11 Pvrao 80000
Racecourse 99 Pvrao 77000
Nariman 13 Pvrao 2005000
Rajawada 34 Chitale 70000
Anna 98 Laloo 30000000
Majestc 69 Laloo 90000000
Redport 18 Laloo 7000000
Godhacolony 29 Ganesh 200000
University 33 Shiva 180500
Racecourse 88 Shiva 77000
Racecourse 88 Kaveri 770000

You might also like