Relational Model & Algebra Overview
Relational Model & Algebra Overview
• Student= Tuple-1
• Tuple-2
Relation
• Tuple-3
• Int. String String
• Domain
15
Constraints
• Constraints:
Data Types,Not NUll,Unique, Default-e.g exam
fees ,Check(Range)- e.g age
Identify the Domain Constraint
A MNO 12000
8004 16000
● Primary Key can’t accept null [Link] Key can accept null
values.
• We can assign only one Primary key in a table but we can assign
more than one foreign key.
• Delete Cascade: if a record in the parent table is deleted then
corresponding record from the child table wil automatically
Enterprise Constraints
● also referred as Semantic Constraint
● Example:
In College System a class can have
maximum of 70 students.
Mapping ER and EER Model to the
Relational Model
• There are several processes and algorithms
available to convert ER Diagrams into
Relational Schema.
• ER diagrams mainly comprise of −
• Entity and its attributes
• Relationship, which is association among
entities.
Ignore derived Atrributes.
3.2 Relational Algebra-Unary and set
operations, Relational algebra
queries
Relational Model
• Query Language:
– is a language in which a user request information from the
database.
– Higher level than standard programming language.
– Query Language classified as :
• Procedural Query language
• Non Procedural Query Language
• Procedural Query language:
– user instruct the system to perform a sequence of operation on DB to
compute desired result.
– Example : Relational Algebra
• Non-Procedural Query language:
– The user describe the desired information without giving a specific
procedure.
– Example : Tuple relational calculus, Domain relational calculus
33
Relational Algebra
• It consist of a set of a set of operations that take one or two
relations as input and produce a new relation as their
result.
Cartesian
Select Intersection
product
Set
Rename Division
difference
Assignment
36
• The primary operations that we can perform
using relational algebra are:
• Select
• Project
• Union
• Set Difference
• Cartesian product
• Rename
Relational Algebra Operators
• Operator No: 1
• Name of Operator : Select
• Purpose :
– selects the tuples (rows) that satisfy the given predicate
(condition)
• Symbol : Greek letter sigma- σ
• Syntax: σ (Predicate) (relation-name/Expression)
• Note :
– Predicate can be defined using operations =, ≠, <, ≤, >, ≥, ^(and) ,
v(or)
• Example :
38
Select Operation (σ)
41
Relational Algebra Operators
• Example on Select:
1. Query : Find all the tuples from player relation for which country is
“India”
2. Ans : σ(country = ‘India’) (Player)
Output :
42
Relational Algebra Operators
• Example on Select:
2. Query : Select all the players for which runs are greater than or equal to
10000
– Ans : σ(runs ≥ 10000) (Player)
– Output :
43
Relational Algebra Operators
• Example on Select:
3. Query : Select all the players for which runs are greater than 6000 and
age less than 25.
– Ans : σ(runs > 6000 ^ age < 25) (Player)
– Output :
44
Relational Algebra Operators
• Operator No: 2
• Name of Operator : Project
• Purpose :
– Is a unary operation and it returns its arguments relation
with certain attributes left out
• Symbol : Greek letter pi- ∏
• Syntax: ∏(A1,A2,A3…An)(Relation/Expression)
• Note : It eliminates duplicate tuples
• Example :
45
Project Operation (∏)
• Example on Project:
1. Query : Find all the countries in a Player.
2. Ans : ∏ (Country ) (Player)
Output :
Country
India
Pakistan
England
Australia
47
Relational Algebra Operators
• Example on Project:
1. Query : Find all the team-id and countries from the table Player.
2. Ans : ∏ (Team_id, Country ) (Player)
Output :
Team_id Country
101 India
102 Pakistan
103 England
104 Australia
48
Relational Algebra Operators
Output :
49
Relational Algebra Operators
• Operator No: 3
• Name of Operator : Union
• Purpose :
– The resultant relation P has tuples drawn from R and S ,such that a tuple in P is either in
R or S or in both of them.
• Symbol : U
• Syntax: P = R U S
• Note :
– It eliminates duplicate tuples
– For union R and S relation must be compatible with each other.
50
Union Operation (∪)
• Example on Union:
1. Query : find names of the customers having an account or loan.
2. Ans : ∏ (cust_name ) (Depositor) U ∏ (cust_name ) (Borrower)
Output :
Cust-
Name
ANIL
OMKAR
RAHUL
RAJ
RAMESH
SACHIN
SUMIT
53
Relational Algebra Operators
• Operator No: 4
• Name of Operator : Difference
• Purpose :
• P contains those tuples in R but not in S.
• Means removes common tuples from the first relation
• Symbol : -
• Syntax: P = R - S
• Note :
– It eliminates duplicate tuples
– For difference R and S relation must be compatible with each other.
54
Set Difference (-)
• Example on Difference:
1. Query : find names of the customers having a loan but not the account.
2. Ans : ∏ (cust_name) (Borrower) - ∏ (cust_name) (Depositor)
Output :
Cust-Name
RAMESH
ANIL
56
Relational Algebra Operators
• Operator No: 5
• Name of Operator : Intersection
• Purpose :
– Common tuples in R and S
– The resultant relation P has tuples drawn from R and S ,such that a tuple in P is in R and
S
• Symbol : -
• Syntax: P = R ⋂ S
• Note :
– It eliminates duplicate tuples
– For intersection R and S relation must be compatible with each other.
57
Relational Algebra Operators
• Example on intersection:
1. Query : find names of the customers having an account and Loan.
2. Ans : ∏ (cust_name) (Depositor) ⋂ ∏ (cust_name) (Borrower)
Output :
Cust-
Name
SACHIN
RAJ
58
Relational Algebra Operators
• Operator No: 6
• Name of Operator : Cartesian product
• Purpose :
– It is the concatenation of tuples belonging to the two relations.
– The resultant relation P contains all possible combinations of tuples in
R and S.
• Symbol : X
• Syntax: P = R X S
• Note :
– Not eliminates duplicate tuples
– No compatible relations required.
59
Cartesian Product (X)
A B C D
a1 b1 c1 d1
a1 b1 c2 d2
a1 b1 c3 d3
a1 b1 c4 d4
a2 b2 c1 d1
a2 b2 c2 d2
a2 b2 c3 d3
a2 b2 c4 d4 61
Relational Algebra Operators
R.A B S.A C D
a1 b1 a1 c1 d1
a1 b1 a2 c2 d2
a2 b2 a1 c1 d1
a2 b2 a2 c2 d2
62
Relational Algebra Operators
• P= Emp X Branch
Emp.E# Ename Branch.E# Br-name Salary
1 ABC 1 VASHI 1000
1 ABC 2 THANE 2000
2 XYZ 1 VASHI 1000
2 XYZ 2 THANE 2000
63
Relational Algebra Operators
• Query : Find E#, Name of Employee along with Branch name and
salary.
• Ans : σ(Emp.E# =Branch.E#) (Emp X Branch)
Emp.E# Ename Branch.E# Br-name Salary
64
Relational Algebra Operators
• Final ans :
• ∏ (Emp.E#, Ename, Br-name, Salary) (σ(Emp.E# =Branch.E#) (Emp X Branch) )
E# Ename Br-name Salary
1 ABC VASHI 1000
2 XYZ THANE 2000
65
Relational Algebra Operators
• Operator No: 7
• Name of Operator : Assignment
• Purpose :
– To assign sub-expression to some temporary variable.
– 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.
• Symbol : 🡨
• Syntax: P🡨 expression
66
Relational Algebra Operators
67
Relational Algebra Operators
• Query : Find the names of customer who have a
loan at “Perryridge” branch.
• Solution: R1 🡨 σ(branch-name = “Perryridge”)(Loan)
68
Relational Algebra Operators
• R2 🡨 R1 x borrower
Cust-name [Link]-no [Link]-no branch-name amount
Adams L-16 L-15 Perryridge 1500
Adams L-16 L-16 Perryridge 1300
Curry L-93 L-15 Perryridge 1500
Curry L-93 L-16 Perryridge 1300
Hayes L-15 L-15 Perryridge 1500
Hayes L-15 L-16 Perryridge 1300
Jackson L-14 L-15 Perryridge 1500
Jackson L-14 L-16 Perryridge 1300
Jones L-17 L-15 Perryridge 1500
Jones L-17 L-16 Perryridge 1300
Smith L-11 L-15 Perryridge 1500
Smith L-11 L-16 Perryridge 1300
Smith L-23 L-15 Perryridge 1500
Smith L-23 L-16 Perryridge 1300
Williams L-17 L-15 Perryridge 1500
Williams L-17 L-16 Perryridge 1300
69
Relational Algebra Operators
• R4🡨
∏customer-name(R3) Cust-name
Adams
Hayes
70
Relational Algebra Operators
• Final answer :
• ∏cust-name(σ[Link]-no = [Link]-no( ( σ branch-
name = “Perryridge”(loan)) x borrower))
Cust-name
Adams
Hayes
71
Relational Algebra Operators
• Operator No: 8
• Name of Operator : Rename
• Purpose :
– use to rename existing relation & form a new relation.
• Symbol : ρ (a Greek letter --rho)
• Syntax:
– 1. ρnew-relation-name(existing relation-name)
72
Rename Operation (ρ)
A B
a1 b1
a2 b2
• P= R X R
A B A B
a1 b1 a1 b1
a1 b1 a2 b2
a2 b2 a1 b1
a2 b2 a2 b2
74
Relational Algebra Operators
• Or P= R X ρS(M,N)(R)
A B M N
a1 b1 a1 b1
a1 b1 a2 b2
a2 b2 a1 b1
a2 b2 a2 b2
75
Relational Algebra Operators
• Query : Find the largest account balance in the bank.
• Solution:
• R1 🡨 Account X ρd(Account) R1-Output = 6 attributes and 49 tuples
account [Link]
• R2 🡨 ∏[Link](σ([Link]<[Link]) (R1))
– Output of R2 – 1 attribute and 6 tuples
Balanc
e
500
400
700
750
350
76
Relational Algebra Operators
• Query : Find the largest account balance in the bank.
• Solution: cont.…
• R3 🡨 ∏ balance(Account) -output 1 attribute and 7 tuples
• R4 🡨 R3 – R2 final answer – 900
R3 R2
• R4 = -
(900)
77
Relational Algebra Operators
• Exercise
• Query : Find the names of all customer who live on the same
street and in the same city as that of “Smith”.
• Solution :
• You have to write
• Relational algebra equation ? ?
• Output :
Cust-name
Curry
Smith
78
Relational Algebra Operators
[Link]
operation-in-relational-algebra-dbms
• Operator No: 9
• Name of Operator : Division
• Purpose : To divide
• Symbol : ÷
• Syntax: P = R ÷ S
• Example : 1
• A R B B S P=R÷S
a1 b1 A
a1 b2 b1 a1
a2 b1
a3 b1
b2
a4 b2 a5
a5 b1
a5 b2
79
Relational Algebra Operators
• Example : 2
• R S P=R÷S
A B
a1 B A
b1
a1 b2
a1
a2 b1 b1 a2
a3 b1 a3
a4 b2
a5
a5
b1
a5 b2
• Example : 3 and 4 Keeping R as it is and changing S relation….
• S P=R÷S S P=R÷S
B A
a1 B A
• a2 b1
a3 b2
a4
b3
a5
80
Relational Algebra Operators
• Operator No: 10
• Name of Operator : Natural Join
• Purpose :
– Operation forms a Cartesian product of its argument, performs a selection forcing
equality on those attribute that appear in both relation schemas.
• Symbol : ⋈
• Syntax: P = R ⋈ S
• Example : suppose
• EMP Branch
Emp- Street City Branch-Name
Emp- Sal
name name
XYZ SEC-8 AIROLI XYZ SBI-A 1500
ABC SEC-3 VASHI ABC SBI-V 1300
IJK NAB MULUND PQR SBI-M 5300
NML Sec-5 CBD NML SBI-C 1500
88
Relational Algebra Operators
• Example : EMP Branch
• Solution:
∏ ([Link]-name, Street, City, Branch-name, Sal) (σ([Link]-name = [Link]-name) (Emp X Branch) )
Or
EMP ⋈ Branch
Final output :
Emp-name Street City Branch-Name Sal
XYZ SEC-8 AIROLI SBI-A 1500
ABC SEC-3 VASHI SBI-V 1300
NML Sec-5 CBD SBI-C 1500
89
Relational Algebra Operators
• Types of Join : Left Outer join ,Right outer join, Full outer join
• Left outer join : operation allows keeping all tuple in the left relation. However,
if there is no matching tuple is found in right relation, then the attributes of right
relation in the join result are filled with null values.
• Example : EMP Branch
EMP Branch
Final output :
Emp-name Street City Branch-Name Sal
XYZ SEC-8 AIROLI SBI-A 1500
ABC SEC-3 VASHI SBI-V 1300
IJK NAB MULUND NULL NULL
NML Sec-5 CBD SBI-C 1500
91
Relational Algebra Operators
• Right outer join : operation allows keeping all tuple in the right relation.
However, if there is no matching tuple is found in the left relation, then the
attributes of the left relation in the join result are filled with null values.
• Example : EMP Branch
EMP Branch
Final output :
Emp-name Street City Branch-Name Sal
XYZ SEC-8 AIROLI SBI-A 1500
ABC SEC-3 VASHI SBI-V 1300
PQR NULL NULL SBI-M 5300
NML Sec-5 CBD SBI-C 1500
92
Relational Algebra Operators
• Full outer join : all tuples from both relations are included in the result,
irrespective of the matching condition.
• Example : EMP Branch
EMP Branch
Final output :
93
Relational Algebra Operators
• Query : Find names of the branches located in
“Brooklyn” city.
• Solution :
• R1 🡨 ∏branch-name (σbranch-city = “Brooklyn” (branch))
Branch-
nm
Brighton
Downtown
94
Relational Algebra Operators
• Query : Find all customer names who have an
account at all branches located in “Brooklyn” city.
• Solution : CONT….
• R2 🡨∏customer-name, branch-name (depositor ⋈ account)
Customer-name Branch-name
Hayes Perry ridge
Johnson Downtown
Johnson Brighton
Jones Brighton
Lindsay Redwood
Smith Mianus
Turner Round Hill
95
Relational Algebra Operators
• Query : Find all customer names who have an
account at all branches located in “Brooklyn” city.
• Solution : CONT….
• R 3🡨 R2 ÷ R1
Customer- Branch-
Customer- name name
Hayes Perry ridge
name Johnson Downtown
Johnson Johnson
Jones
= Brighton
Brighton
÷
Lindsay Redwood
Smith Mianus
Turner Round Hill
96
Extended Relational Algebra operators
• 1. Insertion :
– To insert a tuple in the existing relation.
Example :
97
Extended Relational Algebra operators
• 2. Update: (Generalized Projection)
– To modify selected tuple of relation.
Syntax :
R 🡨 ∏ F1, F2, …, Fn (r)
Where F1,F2,... Can attribute or formula
Example:
– Make interest payments by increasing all balances
by 5 percent.
account ← ∏ AN, BN, BAL * 1.05 (account)
98
Extended Relational Algebra operators
• 2. Delete:
– To delete selected tuple from relation.
Syntax :
R 🡨R-E
Example:
Delete all account records in the Perryridge branch.
account
account ← account – σ branch-name = “Perryridge” (account)
99