0% found this document useful (0 votes)
22 views112 pages

Unit 2

Uploaded by

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

Unit 2

Uploaded by

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

Department of Computer Science & Engineering

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.

Prepared and compiled by 2


Safdar Sardar Khan, Asst. Professor, CSE
Introduction to Relational Database:

Relational Model:
– Collection of multiple Relations or Tables
– Tables consist of Rows & Columns
– This is primary data model for commercial purpose.

Advantages of Relational Data Model:


• Simple & easy to understand
• Simple data presentation
• Complex queries can be expressed in easy way

A Relation consists of:


– Relational Schema
– Relational Instance

Prepared and compiled by 3


Safdar Sardar Khan, Asst. Professor, CSE
Introduction to Relational Database:
Relational Schema:
– Defines column heads in a table
– Also specifies relation name, field name (column) and domain of
each field. For e.g.

Student(S_ID: Number(10), Name: Char(30), Address: Varchar(30))

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

SID Name Address Fields or Columns


101 Ajay Bhopal
102 Amit Ujjain Tuples or Records
103 Ankit Indore
Prepared and compiled by 4
Safdar Sardar Khan, Asst. Professor, CSE
Introduction to Relational Database:

SID Name Address Fields or Columns


101 Ajay Bhopal
102 Amit Ujjain Tuples or Records
103 Ankit Indore
104 Ankush Bhopal

Degree of Relation:
Number of fields in a relation i.e. 3

Cardinality:
Number of tuples in a relation i.e. 4

Prepared and compiled by 5


Safdar Sardar Khan, Asst. Professor, CSE
Characteristics of Relational Database:

•Relation`s name should be distinct from all other relations in database


•Each cell in relation contains exactly one atomic value
•All values in an attribute are of same domain
•Column name in a relation should be distinct
•Each tuple should have distinct value
•Order of attribute & tuples has no significance

Prepared and compiled by 6


Safdar Sardar Khan, Asst. Professor, CSE
Codd`s Rules:
• Edgar F. Codd has proposed 12 rules for a relational database model.
• These rules are defined to understand that what are required for a database to
be called as relational database.
Rule 1: Information Rule
Rule 2: Guaranteed Access Rule
Rule 3: Systematic treatment of NULL values
Rule 4: Dynamic online catalog based on relational model
Rule 5: Comprehensive data sub language rule
Rule 6: View Updating Rule
Rule 7: High level Insert, Update & Delete
Rule 8: Physical Data Independence Rule
Rule 9: Logical Data Independence Rule
Rule 10: Integrity Independence Rule
Rule 11: Distribution Independence
Rule 12: Non Supervision Rule

Prepared and compiled by


7
Safdar Sardar Khan, Asst. Professor, CSE
Codd`s Rules:
Rule 1: Information Rule-
The data stored in a database, may it be user data or metadata, must be a value of some
table cell. Everything in a database must be stored in a table format.

Rule 2: Guaranteed Access Rule


Every single data element (value) is guaranteed to be accessible logically with a
combination of table-name, primary-key (row value), and attribute-name (column value)

Rule 3: Systematic treatment of NULL values


This is a very important rule because a NULL can be interpreted as one the following −
data is missing, data is not known, or data is not applicable.

Rule 4: Dynamic online catalog based on relational model


The structure description of the entire database must be stored in an online catalog,
known as data dictionary, which can be accessed by authorized users.

Prepared and compiled by 8


Safdar Sardar Khan, Asst. Professor, CSE
Codd`s Rules:
Rule 5: Comprehensive data sub language rule
A database can only be accessed using a language having linear syntax that supports data
definition, data manipulation, and transaction management operations.

Rule 6: View Updating Rule


All the views of a database, which can theoretically be updated, must also be updatable
by the system.

Rule 7: High level Insert, Update & Delete


A database must support high-level insertion, updation, and deletion.

Rule 8: Physical Data Independence Rule


The data stored in a database must be independent of the applications that access the
database. Any change in the physical structure of a database must not have any impact
on how the data is being accessed by external applications.

Prepared and compiled by 9


Safdar Sardar Khan, Asst. Professor, CSE
Codd`s Rules:
Rule 9: Logical Data Independence Rule
The logical data in a database must be independent of its user’s view (application). Any
change in logical data must not affect the applications using it.

Rule 10: Integrity Independence Rule


All its integrity constraints can be independently modified without the need of any
change in the application.

Rule 11: Distribution Independence


The end-user must not be able to see that the data is distributed over various locations.
Users should always get the impression that the data is located at one site only.

Rule 12: Non Supervision Rule


If a system has an interface that provides access to low-level records, then the interface
must not be able to bypass security and integrity constraints.

Prepared and compiled by 10


Safdar Sardar Khan, Asst. Professor, CSE
Department of Computer Science & Engineering

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.

Prepared and compiled by


Safdar Sardar Khan, Asst. Professor, CSE 12
13
Select Operation
• Notation: σ p(r)
• p is called the selection predicate

• 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:

∏A1, A2, …, Ak (r)


where A1, A2 are attribute names and r is a relation name.

• Duplicate rows removed from result, since relations are sets

• E.g. To eliminate the branch-name attribute of account from relation


Account (account-number, balance, branch-name)
∏account-number, balance (account)

Prepared and compiled by 17


Safdar Sardar Khan Asst. Professor, CSE
18
Project Operation – Example
• Relation r: A B C

α 10 1
α 20 1
β 30 1
β 40 2

∏ A,C (r) A C A C

α 1 α 1
α 1 = β 1
β 1 β 2
β 2

Prepared and compiled by 19


Safdar Sardar Khan, Asst. Professor, CSE
20
Union Operation

• 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

• E.g. to find all customers with either an account or a loan


∏customer-name (depositor) ∪ ∏customer-name (borrower)

Prepared and compiled by 21


Safdar Sardar Khan, Asst. Professor, CSE
Union Operation – Example
• Relations r, s:
A B A B

α 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)

Prepared and compiled by 25


Safdar Sardar Khan, Asst. Professor, CSE
Set-Intersection Operation - Example

• Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1

r s
•r ∩ s A B

α 2

Prepared and compiled by 26


Safdar Sardar Khan, Asst. Professor, CSE
27
Set Difference Operation

• Notation r – s

• Defined as:
r – s = {t | t ∈ r and t ∉ s}

• Set differences must be taken between compatible relations.


• r and s must have the same arity
• attribute domains of r and s must be compatible

Prepared and compiled by 28


Safdar Sardar Khan, Asst. Professor, CSE
Set Difference Operation – Example
• Relations r, s:
A B A B

α 1 α 2
α 2 β 3
β 1
s
r

r – s: A B

α 1
β 1

Prepared and compiled by 29


Safdar Sardar Khan, Asst. Professor, CSE
30
Cartesian-Product Operation

• Notation r x s

• Defined as:
r x s = {t q | t ∈ r and q ∈ s}

Prepared and compiled by 31


Safdar Sardar Khan, Asst. Professor, CSE
Cartesian Product:
• To understand how SQL processes a join, it is important to
understand the concept of the Cartesian product.

• A query that lists multiple tables in the FROM clause without


a WHERE clause produces all possible combinations of rows
from all tables.

•This result is called the Cartesian product.

select *
from one, two;

Prepared and compiled by 32


Safdar Sardar Khan, Asst. Professor, CSE
33
34
Cartesian-Product Operation-Example

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.

Prepared and compiled by 39


Safdar Sardar Khan, Asst. Professor, CSE
40
Types of JOIN:
JOIN

INNER JOIN OUTER JOIN

(Outcome contains only (Outcome contains all tuples


matching tuples) from all tables)

•Theta Join •Left Join


•Equi Join •Right Join
•Natural Join •Full Join
•Self Join

Prepared and compiled by 41


Safdar Sardar Khan, Asst. Professor, CSE
Types of JOIN:

Inner Join

Lef Full
Prepared and compiled by
Right
42

t Safdar Sardar Khan, Asst. Professor, CSE


43
44
45
INNER Join or EQUI Join
• This is a simple JOIN in which the result is based on matched data as per
the equality condition specified in the SQL query.

•The syntax for Inner Join is,

SELECT column-name-list FROM


table-name1 INNER JOIN table-name2 on
table-name1.column-name = table-name2.column-name;

Prepared and compiled by 46


Safdar Sardar Khan, Asst. Professor, CSE
Natural JOIN
• Natural Join is a type of Inner join which is based on column
having same name and same datatype present in both the
tables to be joined.

•The syntax for Natural Join is,

SELECT * FROM table-name1 NATURAL JOIN table-name2;

Prepared and compiled by 47


Safdar Sardar Khan, Asst. Professor, CSE
48
49
Outer JOIN
• Outer Join is based on both matched and unmatched data.
• Outer Joins subdivide further into,

Left Outer Join


Right Outer Join

Full Outer Join

Prepared and compiled by 50


Safdar Sardar Khan, Asst. Professor, CSE
LEFT Outer Join
• The left outer join returns a result set table with the matched
data from the two tables and then the remaining rows of the
left table and null from the right table's columns.

•Syntax for Left Outer Join is,


SELECT column-name-list FROM
table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Prepared and compiled by 51


Safdar Sardar Khan, Asst. Professor, CSE
52
53
Left Outer Join – Example-2
loan borrower
loan-number branch-name amount customer-name loan-number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Left Outer Join


loan Borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null

Prepared and compiled by 54


Safdar Sardar Khan, Asst. Professor, CSE
RIGHT Outer Join
• The right outer join returns a resultset table with the matched
data from the two tables being joined, then the remaining
rows of the right table and null for the remaining left table's
columns.

•Syntax for Right Outer Join is,

SELECT column-name-list FROM


table-name1 RIGHT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Prepared and compiled by 55


Safdar Sardar Khan, Asst. Professor, CSE
Right Outer Join – Example-2
loan borrower
loan-number branch-name amount customer-name loan-number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

• Right Outer Join


loan borrower

loan-number branch-name amount customer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes

Prepared and compiled by 56


Safdar Sardar Khan, Asst. Professor, CSE
FULL Outer Join
• The full outer join returns a result set table with the matched
data of two table then remaining rows of both left table and
then the right table.

•Syntax of Full Outer Join is,

SELECT column-name-list FROM


table-name1 FULL OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Prepared and compiled by 57


Safdar Sardar Khan, Asst. Professor, CSE
Full Outer Join – Example-2
loan borrower
loan-number branch-name amount customer-name loan-number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Full Outer Join


loan borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes

Prepared and compiled by 58


Safdar Sardar Khan, Asst. Professor, CSE
59
60
61
62
63
Thank You

64
Department of Computer Science & Engineering

CS3CO25 DBMS

Extended Relational Algebra Operations

Presented By:
Safdar Sardar khan
Asst.
65 Prof., CSE 65
Extended Relational-Algebra-Operations

• Rename Operation

• Division Operation

• Aggregate Functions

• Generalized Projection

Prepared and compiled by 66


Safdar Sardar Khan, Asst. Professor, CSE
Rename Operation

• Allows us to name, and therefore to refer to, the results of


relational-algebra expressions.
• Allows us to refer to a relation by more than one name.
Example:
ρ x (E)
returns the expression E under the name X

• If a relational-algebra expression E has arity n, then


ρx (A1, A2, …, An) (E)
returns the result of expression E under the name X, and with the
attributes renamed to A1, A2, …., An.

Prepared and compiled by 67


Safdar Sardar Khan, Asst. Professor, CSE
Division Operation
r÷s
• 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)

Prepared and compiled by 68


Safdar Sardar Khan, Asst. Professor, CSE
Division Operation – Example

Relations r, s: A B
B
α 1 1
α 2
2
α 3
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
r ÷ s: A r
α
β

Prepared and compiled by 69


Safdar Sardar Khan, Asst. Professor, CSE
Another Division Example
Relations r, s:
A B C D E D E

α 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 γ

Prepared and compiled by 70


Safdar Sardar Khan, Asst. Professor, CSE
Aggregate Functions and Operations

• Aggregation function takes a collection of values and returns a single


value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
• Aggregate operation in relational algebra

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

Prepared and compiled by 71


Safdar Sardar Khan, Asst. Professor, CSE
Aggregate Operation – Example
• Relation r: A B C

α α 7
α β 7
β β 3
β β 10

sum-C
g sum(c) (r)
27

Prepared and compiled by 72


Safdar Sardar Khan, Asst. Professor, CSE
Aggregate Operation – Example
• Relation account grouped by branch-name:

branch-name account-number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

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.)

• Result of aggregation does not have a name


• Can use rename operation to give it a name
• For convenience, we permit renaming as part of aggregate operation

branch-name
g sum(balance) as sum-balance (account)

Prepared and compiled by 74


Safdar Sardar Khan, Asst. Professor, CSE
Generalized Projection

• Extends the projection operation by allowing arithmetic functions to


be used in the projection list.

∏ F1, F2, …, Fn(E)


• E is any relational-algebra expression
• Each of F1, F2, …, Fn are are arithmetic expressions involving
constants and attributes in the schema of E.

• Given relation credit-info(customer-name, limit, credit-balance), find


how much more each person can spend:
∏customer-name, limit – credit-balance (credit-info)

Prepared and compiled by 75


Safdar Sardar Khan, Asst. Professor, CSE
A B C A C-B

1 4 7 1 3

2 5 9 2 4

3 6 10 3 4

4 7 10 4 3

76
Thank You

Prepared and compiled by 77


Safdar Sardar Khan, Asst. Professor, CSE
Department of Computer Science & Engineering

CS3CO25 DBMS

Relational Algebra Examples

Presented By:
Safdar Sardar Khan
Asst.
78 Prof., CSE 78
Banking Example

customer (customer-name, customer-street, customer-only)


account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Find all loans of over $1200

σamount > 1200 (loan)

Prepared and compiled by 79


Safdar Sardar Khan, Asst. Professor, CSE
Banking Example Cont..
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Display the loan number for each loan of an amount greater than $1200

∏loan-number (σamount > 1200 (loan))

Prepared and compiled by 80


Safdar Sardar Khan, Asst. Professor, CSE
Banking Example Cont..

customer (customer-name, customer-street, customer-only)


account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Find the names of all customers who have a loan, an account, or both,
from the bank

∏customer-name (borrower) ∪ ∏customer-name (depositor)

Prepared and compiled by 81


Safdar Sardar Khan, Asst. Professor, CSE
Banking Example Cont..
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Find the names of all customers who have a loan and an account at bank.

∏customer-name (borrower) ∩ ∏customer-name (depositor)

Prepared and compiled by 82


Safdar Sardar Khan, Asst. Professor, CSE
Banking Example Cont..
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

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))

Prepared and compiled by 83


Safdar Sardar Khan, Asst. Professor, CSE
Banking Example Cont..

customer (customer-name, customer-street, customer-only)


account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
Find the names of all customers who have a loan at the ‘New Market’ branch
but do not have an account at any branch of the bank.
∏customer-name (σbranch-name = “New Market” ∧
borrower.loan-number = loan.loan-number
(borrower x loan))) – ∏customer-name(depositor)

Prepared and compiled by 84


Safdar Sardar Khan, Asst. Professor, CSE
Thank You

85
Department of Computer Science & Engineering

CS3CO25 DBMS

Modification of the Database, Views

Presented By:
Safdar Sardar Khan
Asst.
86 Prof., CSE 86
Modification of the Database

• The content of the database may be modified using the following


operations:
• Deletion
• Insertion
• Updating

• All these operations are expressed using the assignment operator.

Prepared and compiled by 87


Safdar Sardar Khan, Asst. Professor, CSE
Deletion:

• A delete request is expressed similarly to a query, except instead of


displaying tuples to the user, the selected tuples are removed from
the database.

• Can delete only whole tuples; cannot delete values on only particular
attributes

• A deletion is expressed in relational algebra by:


r←r–E
where r is a relation and E is a relational algebra query.

Prepared and compiled by 88


Safdar Sardar Khan, Asst. Professor, CSE
Deletion Examples
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
• Delete all account records in the Perryridge branch.
account ← account – σ (account)
branch-name = “Perryridge”
• Delete all loan records with amount in the range of 0 to 50
loan ← loan – σ (loan)
amount ≥ 0 and amount ≤ 50

Prepared and compiled by 89


Safdar Sardar Khan, Asst. Professor, CSE
Insertion

• To insert data into a relation, we either:


• specify a tuple to be inserted
• write a query whose result is a set of tuples to be inserted

• in relational algebra, an insertion is expressed by:


r← r ∪ E
where r is a relation and E is a relational algebra expression.

• The insertion of a single tuple is expressed by letting E be a constant


relation containing one tuple.

Prepared and compiled by 90


Safdar Sardar Khan, Asst. Professor, CSE
Insertion Examples
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

• Insert information in the database specifying that Smith has $1200 in


account A-973 at the Perryridge branch.

• account ← account ∪ {(“Perryridge”, A-973,


1200)}
• depositor ← depositor ∪ {(“Smith”, A-973)}

Prepared and compiled by 91


Safdar Sardar Khan, Asst. Professor, CSE
Updating

• A mechanism to change a value in a tuple without charging all values


in the tuple

• Use the generalized projection operator to do this task


r ← ∏ F1, F2, …, FI, (r)

• 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

Prepared and compiled by 92


Safdar Sardar Khan, Asst. Professor, CSE
Update Examples
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
• Make interest payments by increasing all balances by 5 percent.
account ← ∏ AN, BN, BAL * 1.05 (account)

where AN, BN and BAL stand for account-number, branch-name and


balance, respectively.
• Pay all accounts with balances over $10,000 6 percent interest
and pay all others 5 percent
account ← ∏ AN, BN, BAL * 1.06 (σ BAL > 10000 (account))
∪ ∏AN, BN, BAL * 1.05 (σBAL ≤ 10000 (account))
Prepared and compiled by 93
Safdar Sardar Khan, Asst. Professor, CSE
Update Examples

customer (customer-name, customer-street, customer-only)


account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
• Pay all accounts with balances over $10,000 6 percent interest
and pay all others 5 percent
account ← ∏ AN, BN, BAL * 1.06 (σ BAL > 10000 (account))
∪ ∏AN, BN, BAL * 1.05 (σBAL ≤ 10000 (account))

Prepared and compiled by 94


Safdar Sardar Khan, Asst. Professor, CSE
Views
• In some cases, it is not desirable for all users to see the entire logical
model (i.e., all the actual relations stored in the database.)

• Consider a person who needs to know a customer’s loan number but


has no need to see the loan amount. This person should see a
relation described, in the relational algebra, by
∏customer-name, loan-number (borrower loan)

• Any relation that is not of the conceptual model but is made visible
to a user as a “virtual relation” is called a view.

Prepared and compiled by 95


Safdar Sardar Khan, Asst. Professor, CSE
View Definition
• A view is defined using the create view statement which has the
form
create view v as <query expression>
where <query expression> is any legal relational algebra query
expression. The view name is represented by v.

• Once a view is defined, the view name can be used to refer to the
virtual relation that the view generates.

• View definition is not the same as creating a new relation by


evaluating the query expression
• Rather, a view definition causes the saving of an expression; the expression is
substituted into queries using the view.

Prepared and compiled by 96


Safdar Sardar Khan, Asst. Professor, CSE
View Examples
• Consider the view (named all-customer) consisting of branches and
their customers.
create view all-customer as
∏branch-name, customer-name (depositor account)
∪ ∏branch-name, customer-name (borrower loan)

• We can find all customers of the Perryridge branch by writing:


∏customer-name (σbranch-name = “Perryridge” (all-customer))

Prepared and compiled by 97


Safdar Sardar Khan, Asst. Professor, CSE
Updates Through View

• 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)

• Since we allow a view name to appear wherever a relation name is


allowed, the person may write:

branch-loan ← branch-loan ∪ {(“Perryridge”, L-37)}

Prepared and compiled by 98


Safdar Sardar Khan, Asst. Professor, CSE
Thank You

Prepared and compiled by 99


Safdar Sardar Khan, Asst. Professor, CSE
Department of Computer Science & Engineering

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

Prepared and compiled by 101


Safdar Sardar Khan, Asst. Professor, CSE
Tuple Relational Calculus
• 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

Prepared and compiled by 102


Safdar Sardar Khan, Asst. Professor, CSE
Tuple Relational Calculus Example:
emp(fname, lname, ssn, dno, salary)
dept(dname, dno, mgrssn)

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

Prepared and compiled by 103


Safdar Sardar Khan, Asst. Professor, CSE
Tuple Relational Calculus Example:
emp(ename, ssn, dno, salary)
dept(dname, dno, mgrssn)

1. Find name of all employees using tuple relational calculus.


{t | ∃ e ∈ emp (t[ename]=e[ename])}

2. Find name of all employees whose salary more than 35000.


{t | ∃ e ∈ emp (t[ename]=e[ename] ∧ e[salary]>35000)}

Prepared and compiled by 104


Safdar Sardar Khan, Asst. Professor, CSE
Tuple Relational Calculus Example:
emp(ename, ssn, dno, salary)
dept(dname, dno, mgrssn)

3. Find name of employees working for dept no D5.


{t | ∃ e ∈ emp (t[ename]=e[ename] ∧ e[dno] = ‘D5’)}

4. Find employee name who are manager.


{t | ∃ e ∈ emp (t[ename]=e[ename] ∧
∃ d ∈ dept ( e[ssn] =d[mgrssn])}

Prepared and compiled by 105


Safdar Sardar Khan, Asst. Professor, CSE
Tuple Relational Calculus Example:
emp(ename, ssn, dno, salary)
dept(dname, dno, mgrssn)

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])}

Prepared and compiled by 106


Safdar Sardar Khan, Asst. Professor, CSE
Domain Relational Calculus

• A nonprocedural query language equivalent in power to the tuple


relational calculus
• Each query is an expression of the form:

{ < x1, x2, …, xn > | P(x1, x2, …, xn)}

• x1, x2, …, xn represent domain variables


• P represents a formula similar to that of the predicate calculus

Prepared and compiled by 107


Safdar Sardar Khan, Asst. Professor, CSE
Domain Relational Calculus Example:
emp(fname, lname, ssn, dno, salary)
dept(dname, dno, mgrssn)
emp dept
p q r s t l m n
fname lname ssn dno salary dname dno mgrssn
Ajay Kumar 1 D5 30000 Research D5 3
Bhanu Singh 3 D5 40000 Admin D4 6
Ram Dubey 9 D1 35000 HR D3 -
Namit Sharma 6 D4 30000
Jayesh Shukla 2 D3 45000

Prepared and compiled by 108


Safdar Sardar Khan, Asst. Professor, CSE
Domain Relational Calculus Example:
p q r s t
emp(fname, lname, ssn, dno, salary)
dept(dname, dno, mgrssn)
l m n

1. Find first name, last name of all employees using domain relational
calculus.
{<p,q>|<p,q,r,s,t> ∈ emp}

2. Find name of all employees whose salary more than 35000.


{<p,q>| <p,q,r,s,t> ∈ emp ∧ t>35000}

Prepared and compiled by 109


Safdar Sardar Khan, Asst. Professor, CSE
Domain Relational Calculus Example:
p q r s t
emp(fname, lname, ssn, dno, salary)
dept(dname, dno, mgrssn)
l m n

3. Find all details of employees working for dept no D5.


{<p,q,r,s,t >|<p,q,r,s,t> ∈ emp ∧ s =‘D5’}

4. Find employee name who are manager.


{<p,q>| ∃ r ,n (<p,q,r,s,t> ∈ emp ∧ <l,m,n> ∈ dept ∧ r=n)}

Prepared and compiled by 110


Safdar Sardar Khan, Asst. Professor, CSE
Domain Relational Calculus Example:
p q r s t
emp(fname, lname, ssn, dno, salary)
dept(dname, dno, mgrssn)
l m n

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)}

Prepared and compiled by 111


Safdar Sardar Khan, Asst. Professor, CSE
Thank You

Prepared and compiled by 112


Safdar Sardar Khan, Asst. Professor, CSE

You might also like