Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Database Management System

_____________________________________________________________________________________

Relational Data Model


Relational data model is the primary data model, which is used widely around the
world for data storage and processing. This model is simple and have all the
properties and capabilities required to process data with storage efficiency.

Concepts
Tables: In relation data model, relations are saved in the format of Tables. This
format stores the relation among entities. A table has rows and columns, where
rows represent records and columns represents the attributes.
Ex: 1. Customer relation

2. Branch relation

3. Account relation

4. Loan relation

Prepared by Krishna Kumari Ganga Page 1


Database Management System
_____________________________________________________________________________________

Database schema: Database schema is the logical design of the database.

Database instance: Database instance is a snapshot of the data in the database at a


given instant in time.

Tuple: A single row of a table, which contains a single record for that relation, is
called a tuple.

Relation instance: A finite set of tuples in the relational database system


represents relation instance. Relation instances do not have duplicate tuples.

Relation schema: This describes the relation name (table name), attributes and
their names.

Relation key: Each row has one or more attributes which can identify the row in
the relation (table) uniquely, is called the relation key.
Ex: 1. Barrower relation

2. Depositor relation

Attribute domain: Every attribute has some pre-defined value scope, known as
attribute domain.

Prepared by Krishna Kumari Ganga Page 2


Database Management System
_____________________________________________________________________________________

Constraints
Every relation has some conditions that must hold for it to be a valid relation.
These conditions are called Relational Integrity Constraints. There are three main
integrity constraints.
1. Key Constraints
2. Domain constraints
3. Referential integrity constraints

1. Key Constraints: There must be at least one minimal subset of attributes in


the relation, which can identify a tuple uniquely. This minimal subset of
attributes is called key for that relation. If there are more than one such
minimal subsets, these are called candidate keys.
Key constraints forces that:
 In a relation with a key attribute, no two tuples can have identical value
for key attributes.
 Key attribute cannot have NULL values.
Key constrains are also referred to as Entity Constraints.

2. Domain constraints
Attributes have specific values in real-world scenario. For example, age can
only be positive integer. The same constraints has been tried to employ on
the attributes of a relation. Every attribute is bound to have a specific range
of values. For example, age cannot be less than zero and telephone number
cannot be an outside 0-9.

3. Referential integrity constraints


This integrity constraints works on the concept of Foreign Key. A key
attribute of a relation can be referred in other relation, where it is called
foreign key.

Referential integrity constraint states that if a relation refers to a key


attribute of a different or same relation, that key element must exist.

Prepared by Krishna Kumari Ganga Page 3


Database Management System
_____________________________________________________________________________________

E-R diagram for the banking enterprise

A database schema, along with primary key and foreign key dependencies, can be
depicted pictorially by schema diagrams.

Schema diagram for the banking enterprise

Prepared by Krishna Kumari Ganga Page 4


Database Management System
_____________________________________________________________________________________

Relational Algebra and Relational Calculus

Relational database systems are expected to be equipped with a query language


that can assist its users to query the database instances. There are two kinds of
query languages − relational algebra and relational calculus.

Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses operators to
perform queries. An operator can be either unary or binary. They accept relations
as their input and yield relations as their output. Relational algebra is performed
recursively on a relation and intermediate results are also considered relations.

The fundamental operations of relational algebra are as follows:


1. Select
2. Project
3. Union
4. Set different
5. Cartesian product
6. Rename

1. Select Operation (σ)


It selects tuples that satisfy the given predicate from a relation.
Notation : σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional
logic formula which may use connectors like and, or, and not. These terms may use
relational operators like − =, ≠, ≥, < , >, ≤.
Ex 1: select the tuples of the loan relation where the branch is “Perryridge,”
σ branch-name =“Perryridge” (loan)
The result is

2. Find all tuples in which the amount lent is more than $1200
σ amount>1200 (loan)
3. Find the tuples pertaining to loans of more than $1200 made by the Perryridge
branch
σ branch-name =“Perryridge”∧ amount>1200 (loan)

Prepared by Krishna Kumari Ganga Page 5


Database Management System
_____________________________________________________________________________________

2. Project Operation (∏)


It projects column(s) that satisfy a given predicate.
Notation: ∏A1, A2, An (r)
Where A1, A2 …An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
Ex1:

Ex 2: List all loan numbers and the amount of the loan


Π loan-number, amount(loan)
The result is

3. Union Operation (∪)


It performs binary union between two given relations and is defined as
r ∪ s = { t | t ∈ r or t ∈ s}
Notation: r U s
Where r and s are either database relations or relation result set.
For a union operation to be valid, the following conditions must hold −
 r and s must have the same number of attributes.
 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
Ex 1:

Prepared by Krishna Kumari Ganga Page 6


Database Management System
_____________________________________________________________________________________

Ex 2: Find the names of all bank customers who have either an account or a loan or
both.
1. First find the names of all customers with a loan in the bank as
Π customer-name (borrower)
2. Now find the names of all customers with an account in the bank as
Π customer-name (depositor)
3. Now the union of these two sets will give the names of all bank customers
who have either an account or a loan or both.
Π customer-name (borrower ) ∪ Π customer-name (depositor)
The result is

4. Set Difference (−)


The result of set difference operation is tuples, which are present in one relation
but are not in the second relation.
Notation: r – s
Ex1:

Prepared by Krishna Kumari Ganga Page 7


Database Management System
_____________________________________________________________________________________

Ex 2: Find all customers of the bank who have an account but not a loan
Π customer-name (depositor) – Π customer-name (borrower )
The result is

5. Cartesian Product (Χ)


Combines information of two different relations into one.
Notation: r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
Ex 1:

Ex 2: Find the names of all customers who have a loan at the Perryridge branch.
σ branch-name =“Perryridge”(borrower × loan)
The result is

Prepared by Krishna Kumari Ganga Page 8


Database Management System
_____________________________________________________________________________________

6. Rename Operation (ρ)


The results of relational algebra are also relations but without any name. The
rename operation allows us to rename the output relation. Rename operation is
denoted with small Greek letter rho ρ.
Notation: ρ x (E)
Where the result of expression E is saved with name of x.
Ex: Find the names of all customers who live on the same street and in the same
city as Smith.”
Π customer-street, customer-city (σ customer-name = “Smith” (customer))
However, in order to find other customers with this street and city, we must
reference the customer relation a second time. In the following query, we use the
rename operation on the preceding expression to give its result the name smith-
addr and to rename its attributes to street and city, instead of customer-street and
customer-city:
Π customer.customer-name (σ customer.customer-street=smith-addr.street ∧ customer.customer-city=smith-addr.city
(customer × ρ smith-addr(street, city) (Π customer-street, customer-city (σ customer-name = “Smith”
(customer)))))

Additional operations
1. Set intersection Operation
2. Natural join Operation
3. Division Operation
4. Assignment Operation

1 The Set-Intersection Operation (∩): We can find all customers who have both
a loan and an account using set intersection as below.
Π customer-name (borrower) ∩ Π customer-name (depositor)
The result is

Notation: r ∩ s
Note that we can rewrite any relational algebra expression that uses set intersection
by replacing the intersection operation with a pair of set-difference operations as:
r ∩ s = r − (r − s)

Prepared by Krishna Kumari Ganga Page 9


Database Management System
_____________________________________________________________________________________

2. The Natural-Join Operation ( ): It is often desirable to simplify certain


queries that require a Cartesian product. The natural join is a binary operation that
allows us to combine certain selections and a Cartesian product into one operation.
It is denoted by the “join” symbol .
Notation: r s
Ex 1:

Ex 2: Find the names of all customers who have a loan at the bank, along with the
loan number and the loan amount.
Here we first form the Cartesian product of the borrower and loan relations. Then,
we select those tuples that pertain to only the same loan-number, followed by the
projection of the resulting customer-name, loan-number, and amount:
Π customer-name, loan.loan-number, amount (σ borrower .loan-number =loan.loan-number (borrower × loan))
Ex 3: Find the names of all customers who have a loan at the bank, and find the
amount of the loan.
Π customer-name, loan-number, amount (borrower loan)
The result is

Prepared by Krishna Kumari Ganga Page 10


Database Management System
_____________________________________________________________________________________

Ex 4: Find the names of all branches with customers who have an account in the
bank and who live in Harrison.
Π branch-name (σ customer-city =“Harrison” (customer account depositor))
The result is

3. The Division Operation (÷): The division operation, denoted by ÷, is suited to


queries that include the phrase “for all.”
Notation: r ÷ s
Ex 1:

Ex 2: Suppose that we wish to find all customers who have an account at all the
branches located in Brooklyn.
r1 = Π branch-name (σ branch-city =“Brooklyn” (branch))
The result is

Ex 3: Find all (customer-name, branch-name) pairs for which the customer has an
account at a branch
r2 = Π customer-name, branch-name (depositor account)
The result is

Prepared by Krishna Kumari Ganga Page 11


Database Management System
_____________________________________________________________________________________

Now, we need to find customers who appear in r2 with every branch name in r1.
The operation that provides exactly those customers is the divide operation. We
formulate the query by writing
Π customer-name, branch-name (depositor account) ÷ Π branch-name (σ branch-city =“Brooklyn”
(branch))
The result of this expression is a relation that has the schema (customer-name) and
that contains the tuple (Johnson).
r ÷ s = Π R−S ((Π R−S (r) × s) – Π R−S,S(r))
4. The Assignment Operation: The assignment operation, denoted by ←, works
like assignment in a programming language.
To illustrate this operation, write r ÷ s as
temp1 ← ΠR−S (r)
temp2 ← ΠR−S ((temp1 × s) − ΠR−S,S(r))
result = temp1 − temp2

Prepared by Krishna Kumari Ganga Page 12

You might also like