DBMS (Unit 2) Notes

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

UNIT 2

Relational data model


It represents database as a collection of relations (Tables)
• Attribute: Each column in a Table. Attributes are the
properties which define a relation. e.g., Student_Rollno,
NAME,etc.
• Tables – In the Relational model the, relations are saved in
the table format.
• Tuple – It is nothing but a single row of a table, which
contains a single record.
• Relation Schema: A relation schema represents the name
of the relation with its attributes.
• Degree: The total number of attributes which in the
relation is called the degree of the relation.
• Attribute domain – Every attribute has some pre-defined
value and scope which is known as attribute domain
Characteristics of relation
1. Ordering of tuples in a relation
No Particular order
2. Values and NULLs in the tuples
Values-Domain of an attribute must contain atomic values only.
i.e Indivisible.
e.g Rollno,Customer_id are atomic value
But address is not atomic as it can be dividable into
area,city,state,country
NULL-Value is unknown or does not exist
3. No two tuples are identical in a relation-each tuple in a relation must be
uniquely identified by its contents. In other words, two tuples with the
same value for all the attributes (that is, duplicate tuples) cannot exist in a
relation.
Relational Integrity constraints

Relational integrity constraint is used to ensure accuracy and consistency of


data in relational database.
• Domain constraints
• Key constraints
• Referential integrity constraints
Domain Constraints
A domain is a unique set of values permitted for an attribute in a table.
Domain constraints specify that within each tuple, and the value of each
attribute must be unique.or Domain constraints is valid set of values for an
attribute
Key constraints
An attribute that can uniquely identify a tuple in a relation is called the key of
the table. The value of the attribute for different tuples in the relation has
to be unique.
Primary Key-It is the key which can easily identify each row of a table
uniquely.
Referential integrity constraints
Referential integrity constraints is based on the concept of Foreign Keys. A
foreign key is an important attribute of a relation which should be referred
to in other relationships. Referential integrity constraint state happens
where relation refers to a key attribute of a different or same relation.
• a super key is composed of a set of attributes that identify a unique record
Employee(Eno,Ename), (CustomerID,CustomerName)

• candidate key referred to as a “minimal” of a super key.


The Relational Algebra

The relational algebra is a procedural query language


It 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 in the relational algebra are select, project,
union, set difference, Cartesian product, and rename
Fundamental Operations
The select, project, and rename operations are called unary operations,
because they operate on one relation.
The union, set difference, Cartesian product,intersection operate on pairs of
relations and are, therefore, called binary operations.
The Select Operation
The select operation selects tuples that satisfy a given predicate.
We use the lowercase Greek letter sigma (σ) to denote selection.
The predicate appears as a subscript to σ.
The argument relation is in parentheses after the σ.
Eg. To select those tuples of the loan relation where the branch is
“Perryridge,” we write
σ branch-name = “Perryridge” (loan)
The Project Operation
The project operation is a unary operation that returns its argument relation, with
certain
attributes left out. Since a relation is a set, any duplicate rows are eliminated.
Projection is denoted by the uppercase Greek letter pi (Π).
We list those attributes that we wish to appear in the result as a subscript to Π.
The argument relation follows in parentheses.
Eg. we write the query to list all loan numbers and the amount of the loan as
Π loan-number , amount (loan)

Composition of Relational Operations


Con-sider the more complicated query “Find those customers who live in Harrison.” We
write:
Π customer -name (σcustomer -city = “Harrison” (customer ))
Rename (ρ)
Rename is a unary operation used for renaming attributes of a
relation.
• ρ (a/b)R will rename the attribute 'b' of relation by 'a'.
• ρ (b1,b2,b3)R
• ρ S(R)  R is the old relation name and S is new relation name
The Union Operation
Suppose there are two relations R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted by ∪.

We know how to find the names of all customers with a loan in the bank:
Πcustomer -name (borrower )
We also know how to find the names of all customers with an account in the bank:
Π customer -name (depositor )
We need the union of these two sets; that is, we need all customer names that appear in
either or both of the two relations. We find these data by the binary operation union,
denoted, as in set theory, by ∪. So the expression needed is
Π customer -name (borrower ) ∪ Πcustomer -name (depositor )
Set Intersection
Suppose there are two relations R and S. The set intersection operation contains all
tuples that are in both R & S.
It is denoted by intersection ∩.

Suppose that we wish to find all customers who have both a loan and an account. Using
set intersection, we can write
Π customer -name (borrower ) ∩ Πcustomer -name (depositor )

Set Difference:
Suppose there are two relations R and S. The set intersection operation contains all
tuples that are in R but not in S.
It is denoted by intersection minus (-).
Notation: R - S
Cartesian product
The Cartesian product, also referred to as a cross-join, returns all the rows in all the
tables listed in the query. Each row in the first table is paired with all the rows in the
second table. This happens when there is no relationship defined between the two
tables.
It is denoted by X.
Aggregate function

An aggregate function operates on a set of values (tuples) and


computes one single value as output.
• The Set Functions in Relational Algebra
• sum(): computes the sum of all values in the (numeric)
set
• avg(): computes the average of all values in the
(numeric) set
• max(): finds the maximum value of all values in the set
• min(): finds the minimum value of all values in the set
• count(): returns the cardinaility (number of elements)
in the set
Groups are formed based on common values in one or more
attributes
Ex: grouping employee tuples based on their dno attribute:
• Find the average salary for each department
We must first form groups of employee tuples based on their
DNO attribute (grouped by department number)
And then compute the average Salary :
Tuple Relational Calculus (TRC) in DBMS
Tuple Relational Calculus is a non-procedural query language unlike relational algebra
In Tuple Calculus, a query is expressed as
{t| P(t)}
where t = resulting tuples,
P(t) = known as Predicate/Conditions
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
1)Find the loan number, branch, amount of loans of greater than or equal to
10000 amount .

{t| P(t)}

{t| t ∈ loan ∧ t[amount]>=10000}

Output-
Find the loan number for each loan of an amount greater or equal to 10000

{ t.loan number | loan(t) ^ t.amount >=10000 }

{t| P(t)}
Find all data of account having balance greater than or equal to 9000

{t| t ∈ Account ∧ t[balance]>=9000}

Output-
Find account number of account having balance less than 9000 amount

{ t.account number | account(t) ^ t.balance <9000 }


Student

1)Query to display the last name of those students where age is greater than 30

{ t.Last_Name | Student(t) AND t.age > 30 }

2)Query to display the last name of those students where age is greater than 27

{ t.Last_Name | Student(t) AND t.age > 27}

3) Query to display all data of those students where age is greater than 27

{t| t ∈ Student ∧ t[age]>27}


Domain Relational Calculus
In Domain Relational Calculus, a query is expressed as
{ < x1, x2, x3, ..., xn > | P (x1, x2, x3, ..., xn ) }
where, < x1, x2, x3, …, xn > represents resulting domains variables and
P (x1, x2, x3, …, xn ) represents the condition or formula equivalent to the Predicate calculus

{< account number, branch name, balance > | < account number, branch name, balance > ∈ Account
∧ (balance > 7000)}
Find the account number, branch, balance of accounts of greater than 7000 amount.
Find the loan number, branch, amount of loan having amount greater than 150 amount

{< loan number, branch name, Amount > | < loan number, branch name, Amount > ∈ Loan ∧
(Amount ≥ 150)}

Find the loan number for each loan of an amount greater or equal to 150

{< loan number> | branch name, Amount < loan number, branch name, Amount > ∈ Loan ∧
(Amount ≥ 150)}

Find the branch name for each loan of an amount greater than 90

{< branch name > | loan number, Amount < loan number, branch name, Amount > ∈ Loan ∧
(Amount>90)}
For R=(A,B,C) & let r1,r2 both be relations on schema R. Give the expression in
both tuple relational calculus and domain relational calculus that is equivalent
to relational algebra
i) A,B(r1)
TRC-{ t.A,t.B | r1(t)}

DRC-{<A,B>|C < A,B,C > ∈ r1}

ii) B=19(r2)
TRC- {t|t ∈ (r2) ^ t[B]=19}

DRC- {<A,B,C> | <A,B,C> ∈ (r2) ^ B=19}


iii) r1Ur2
TRC- {t|t ∈ r1 U t ∈ r2}
DRC-{<A,B,C> | <A,B,C> ∈ r1 U <A,B,C> ∈ r2 }

iv) r1n r2
TRC- {t|t ∈ r1 n t ∈ r2}
DRC- {<A,B,C> | <A,B,C> ∈ r1 n <A,B,C> ∈ r2 }

v) r1-r2
TRC- {t|t ∈ r1 ¬ t ∈ r2}
DRC-{<A,B,C> | <A,B,C> ∈ r1 ¬ <A,B,C> ∈ r2 }
Storage-device hierarchy
Primary Storage
Main Memory
Cache Memory
Secondary Storage
Flash Memory
Magnetic Disk Storage
Tertiary Storage
Optical Storage
Tape Storage
Storage Hierarchy Cost Decreases
Access time Increases
INDEXING
It is a data structure technique which allows you to quickly retrieve
records from a database file.

There are two basic kinds of indices:


1) Ordered indices.-Based on a sorted ordering of the values
2) Hash indices-Based on a uniform distribution of values across a range of
buckets. The bucket to which a value is assigned is determined by a
function,called a hash function.
The indexing has various attributes:
• Access Types: This refers to the type of access such as value
based search, range access, etc.
• Access Time: It refers to the time needed to find particular
data element or set of elements.
• Insertion Time: It refers to the time taken to find the
appropriate space and insert a new data.
• Deletion Time: Time taken to find an item and delete it as
well as update the index structure.
• Space Overhead: It refers to the additional space required by
the index.
Pointer
Sequential File Organization or Ordered Index File: In this, the indices are based
on a sorted ordering of the values
These Ordered or Sequential file organization might store the data in a dense
or sparse format
Dense Index: For every search key value in the data file, there is an index record.
Sparse Index- The index record appears only for a few items in the data file
To locate a record, we find the index record with the largest search key value
less than or equal to the search key value we are looking for.
Clustering index/Primary Index

• If the file containing the records is sequentially ordered, a


clustering index is an index whose search key also defines the
sequential order of the file . Clustering indices are also called
as primary indices.
• the term primary index may appear to denote an index on a
primary key
• Here in Clustering index, main file is sorted and index is
created on non key attribute values(unique value)
Primary Index
Non clustering index/Secondary Index
Indices whose search key specifies an order different from the sequential
order of the file are called non clustering indices, or secondary indices.
Multilevel Indices

Indices with two or more levels are called multilevel indices.


If an index is small enough to be kept entirely in main
memory, the search time to find an entry is low. However, if
the index is so large that not all of it can be kept in memory,
index blocks must be fetched from disk when required
Multilevel Indexing

You might also like