Unit 2
Unit 2
CS3CO25 DBMS
Introduction
to
Relational Database Management System
Presented By:
Safdar Sardar Khan
Asst. Prof., CSE 1
SYLLABUS
Unit II
Relational Database: Relational Databases, Relational Algebra, Relational Algebra
Operation, Tuple Relational Calculus, Domain Relational Calculus. Data Definition
with SQL, Inserts, Delete and Update Statements in SQL, Views, Data Manipulation
with SQL, PL/ SQL constructs: Triggers, Cursors etc.
Relational Model:
– Collection of multiple Relations or Tables
– Tables consist of Rows & Columns
– This is primary data model for commercial purpose.
Relational Instance:
– It is a set of tuples (also called records) in which tuples have same
no of fields as in relational schema
– Also can be called a relation or table
Degree of Relation:
Number of fields in a relation i.e. 3
Cardinality:
Number of tuples in a relation i.e. 4
CS3CO25 DBMS
Relational Algebra
11
11
Relational Algebra
● Procedural language-
“What data is needed and how to find those data”
● Six basic operators
● select
● project
● union
● set difference
● Cartesian product
● rename
● The operators take one or more relations as inputs and give a new
relation as a result.
• 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=“Indore”(account)
Prepared and compiled by 14
Safdar Sardar Khan, Asst. Professor, CSE
15
16
Project Operation
• Notation:
α 10 1
α 20 1
β 30 1
β 40 2
∏ A,C (r) A C A C
α 1 α 1
α 1 = β 1
β 1 β 2
β 2
• 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
α 1 α 2
α 2 β 3
β 1
s
r
r ∪ s: A B
α 1
α 2
β 1
β 3
Prepared and compiled by 22
Safdar Sardar Khan, Asst. Professor, CSE
23
24
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)
• Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1
r s
•r ∩ s A B
α 2
• Notation r – s
• Defined as:
r – s = {t | t ∈ r and t ∉ s}
α 1 α 2
α 2 β 3
β 1
s
r
r – s: A B
α 1
β 1
• Notation r x s
• Defined as:
r x s = {t q | t ∈ r and q ∈ s}
select *
from one, two;
Relations r, s: A B C D E
α 1 α 10 a
β 2 β 10 a
β 20 b
r γ 10 b
s
r x s:
A B C D E
α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
Prepared and compiled by 35
Safdar Sardar Khan, Asst. Professor, CSE
Composition of Operations
• Can build expressions using multiple operations
• Example: σA=C(r x s)
A B C D E
•r x s
α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
• σA=C(r x s)
A B C D E
α 1 α 10 a
β 2 β 20 a
β 2 β 20 b
36
Prepared and compiled by
Safdar Sardar Khan, Asst. Professor, CSE
Thank You
37
Department of Computer Science & Engineering
CS3CO25 DBMS
Joins in DBMS
Presented By:
Safdar Sardar Khan
Asst.
38 Prof., CSE 38
SQL Joins:
• SQL Join is used to fetch data from two or more tables, which is joined
to appear as single set of data.
• It is used for combining column from two or more tables by using
values common to both tables.
• Join Keyword is used in SQL queries for joining two or more tables.
• Minimum required condition for joining table, is (n-1) where n, is
number of tables.
• A table can also join to itself, which is known as, Self Join.
Inner Join
Lef Full
Prepared and compiled by
Right
42
64
Department of Computer Science & Engineering
CS3CO25 DBMS
Presented By:
Safdar Sardar khan
Asst.
65 Prof., CSE 65
Extended Relational-Algebra-Operations
• Rename Operation
• Division Operation
• Aggregate Functions
• Generalized Projection
Relations r, s: A B
B
α 1 1
α 2
2
α 3
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
r ÷ s: A r
α
β
α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1 s
β a γ a 1
β a γ b 3
γ a γ a 1
γ a γ b 1
γ a β b 1
r
r ÷ s: A B C
α a γ
γ a γ
G1, G2, …, Gn
g F1( A1), F2( A2),…, Fn( An ) (E)
• E is any relational-algebra expression
• G1, G2 …, Gn is a list of attributes on which to group (can be empty)
• Each Fi is an aggregate function
• Each Ai is an attribute name
α α 7
α β 7
β β 3
β β 10
sum-C
g sum(c) (r)
27
branch-name
g sum(balance) (account)
branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
Prepared and compiled by 73
Safdar Sardar Khan, Asst. Professor, CSE
Aggregate Functions (Cont.)
branch-name
g sum(balance) as sum-balance (account)
1 4 7 1 3
2 5 9 2 4
3 6 10 3 4
4 7 10 4 3
76
Thank You
CS3CO25 DBMS
Presented By:
Safdar Sardar Khan
Asst.
78 Prof., CSE 78
Banking Example
Display the loan number for each loan of an amount greater than $1200
Find the names of all customers who have a loan, an account, or both,
from the bank
Find the names of all customers who have a loan and an account at bank.
Find the names of all customers who have a loan at ‘M.P.Nagar’ branch.
∏customer-name (σbranch-name=“M.P.Nagar” ∧ borrower.loan-number =loan.loan-number(borrower x loan))
85
Department of Computer Science & Engineering
CS3CO25 DBMS
Presented By:
Safdar Sardar Khan
Asst.
86 Prof., CSE 86
Modification of the Database
• Can delete only whole tuples; cannot delete values on only particular
attributes
• Each F is either
i
• the ith attribute of r, if the ith attribute is not updated, or,
• if the attribute is to be updated Fi is an expression, involving only constants
and the attributes of r, which gives the new value for the attribute
• Any relation that is not of the conceptual model but is made visible
to a user as a “virtual relation” is called a view.
• Once a view is defined, the view name can be used to refer to the
virtual relation that the view generates.
• Consider the person who needs to see all loan data in the loan
relation except amount. The view given to the person, branch-loan,
is defined as:
create view branch-loan as
∏branch-name, loan-number (loan)
CS3CO25 DBMS
Relational Calculus
Presented By:
Safdar Sardar Khan
Asst.
100 Prof., CSE 100
Relational Calculus
• Tuple Relational Calculus is a non-procedural query language unlike
relational algebra.
• Tuple Calculus provides only the description of the query not the
methods to solve it.
• Thus, it explains what to do but not how to do.
• Specifies, what is to be retrieved rather than how to retrieved.
2 Types:
i. Tuple Relational Calculus
ii. Domain Relational Calculus
{t | P (t) }
emp dept
fname ssn dno salary dname dno mgrssn
Ajay 1 D5 30000 Research D5 3
Bhanu 3 D5 40000 Admin D4 6
Ram 9 D1 35000 HR D3 -
Namit 6 D4 30000
Jayesh 2 D3 45000
5. Find name of employees along with dept name for which they work.
{t | ∃ e ∈ emp ∧ ∃ d ∈ dept (t[ename]=e[ename] ∧
t[dname]=d[ename] ∧ e[dno] =d[dno])}
1. Find first name, last name of all employees using domain relational
calculus.
{<p,q>|<p,q,r,s,t> ∈ emp}
5. Find all details of employees along with dept name for which they
work.
{<p,q,r,s,t,l >| ∃ s, m (<p,q,r,s,t> ∈ emp ∧ <l,m,n> ∈ dept ∧ s=m)}