Database Ch-3and4

You might also like

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

Chapter three

Relational database design:


Relational database design (RDD) use tables to store information.
The standard fields and records are represented as columns (fields/
attributes) and rows (records/ tuple) in a table.
The design of a relational database is composed of four stages,
where the data are modeled into a set of related tables. The stages
are:
Define relations/attributes
Define primary keys
Define relationships
Normalization

11/29/2019 Database System 3-1


Functional dependency in DBMS
The attributes of a table is said to be dependent on each other when an
attribute of a table uniquely identifies another attribute of the same
table.
For example: Suppose we have a student table with attributes:
Stu_Id, Stu_Name, Stu_Age.

Here Stu_Id attribute uniquely identifies the Stu_Name attribute of


student table because if we know the student id we can tell the
student name associated with it.

This is known as functional dependency and can be written as


Stu_Id->Stu_Name or in words we can say Stu_Name is
functionally dependent on Stu_Id.
11/29/2019 Database System 3-2
Cont..
Normalization: is the process of minimizing redundancy from a
relation or set of relations.

❖ It ensures optimum structure .

❖ It ensures data atomic.

❖ It eliminates data inconsistencies and anomalies.

❖ It improves data integrity.

Anomalies in DBMS: There are three types of anomalies that occur


when the database is not normalized. These are – Insertion, update and
deletion anomaly.

11/29/2019 Database System 3-3


Cont..
Example: Suppose a library management system database store details
of a table and it has six attributes:

The above table is not normalized. We will see the problems that we
face when a table is not normalized.

11/29/2019 Database System 3-4


Cont..
Update anomaly: In the above table we have two rows that have the
same value gae for a table, If we want to update the age of mulu then
we have to update only one rows.

Therefore the data will become inconsistent.

11/29/2019 Database System 3-5


Cont..
Insert anomaly: if we want to add five new records in to a database ,
then we would not be able to insert the data into the table if Roll_No
field doesn’t allow nulls.

Delete anomaly: Suppose, if at a point of time the manager closes the


Book_No B10 then deleting the rows that are having Book_No as
B10 would also delete the information of student Alex since he is
assigned only to the table .
To overcome these anomalies we need to normalize the data

11/29/2019 Database System 3-6


Cont..
First Normal Form: If a relation is in 1NF:
❖ It should not contain multi-valued attribute.
❖ every attribute should be singled valued attribute
❖ It should be atomic .

11/29/2019 Database System 3-7


Cont..
Second normal form (2NF): A table is said to be in 2NF if the
following conditions hold:

Table is in 1NF (First normal form)

No non-prime attribute is dependent on the proper subset of any


candidate key of table.

No partial dependency exist.

An attribute that is not part of any candidate key is known as non-


prime attribute.

11/29/2019 Database System 3-8


Cont..

Conversion to 2NF

11/29/2019 Database System 3-9


Cont..
Third Normal form (3NF): A table is in 3NF for each functional
dependency X-> Y at least one of the following conditions hold:
Table must be in 2NF
Transitive functional dependency of non-prime attribute on any
super key should be removed.
In other words 3NF can be explained like this:
X is a super key of table
Y is a prime attribute of table
An attribute that is a part of one of the candidate keys is known as
prime attribute.

11/29/2019 Database System 3-10


Cont..

11/29/2019 Database System 3-11


Cont..
Boyce-Codd Normal Form (BCNF): is a stronger generalization of
third normal form.
A table is in Boyce-Codd Normal form if and only if at least one of
the following conditions are met for each functional dependency

A → B:

It should no non-trivial dependency(A trivial functional dependency


means that all columns of B are contained in the columns of A)

A is a super key

A relation is in BCNF if every determinant is a candidate key

11/29/2019 Database System 3-12


Cont..

11/29/2019 Database System 3-13


Individual Assignment One (5%)
1. Explain functionality of database system.

2. What is participation in DBS? explain briefly.

3. What is multiplicity in DBS? explain briefly

4. What are the basic difference between multiplicity and participation ?

5. What is ODL ? Explain with neat diagram and compare with E/R.

6. What is anomalies? Explain briefly with examples

Submission date 28/02/2012

11/29/2019 Database System 1


11/29/2019 Database System
Relational algebra
Relational Algebra is a procedural query language that consists of a
set of operations that take one or two relations as input and produce a
new relation as a result. The algebra operations enable a user to
retrieve specific request on a relational model

The sequence of the relational algebra that produces new relation


forms a relational algebra expression

The relational algebra is very important for several reasons:

It provides a formal foundation for relational model operation.

It is used as basis for implementing optimizing queries in RDBMS.

It incorporated in to the SQL for RDBMS.


11/29/2019 Database System 4-1
Cont..
Fundamental Operations of Relational Algebra : can be grouped into
two based on the number of relation operands of the operator. These
are:

Unary Operators .

Selection (σ)

Projection (Π)

Rename(ρ)

11/29/2019 Database System 4-2


Cont..

Binary Operators .

Product (Cartesian Product) (Χ)

Union (U )

Intersection (˄)

Difference ( – )

The binary operators listed above are also known as set operators.

11/29/2019 Database System 4-3


Cont..

Unary Operations:

Select Operation : selects a subset of tuple from a relation instance


that satisfies a selection condition.

Is picking certain rows (tuple) from a relation(R).

It is denoted by σ C(R), Where σ (sigma) is used to denote the


SELECT operator, C(=, <, >, ≠, ≤, ≥) is a Boolean expression of the
select condition, and R is the relation or relational algebra
expression.

11/29/2019 Database System 4-4


Cont..

Example - From the “EMPLOYEES” relation to extract Senior


Mangers or those salary is greater than 30,000 then the selection
operation can be written as:

Employees(EmpId, Name, BDate, Age, Gender, Position, Salary)

δ position=“senior Manager” ˅ salary>=30,000

OR

11/29/2019 Database System 4-5


Cont..

Project Operation: forms a new relation by picking certain columns in


the relation.

Also called duplicate elimination. It is denoted by: Π A ( R), Where Π


(pi) represents the PROJECT operator and A is a set of attributes in
the relation R.

Example - To extract Employees Name and Position only from the


“EMPLOYEES” relation

11/29/2019 Database System 4-6


Cont..

Rename Operation: The renaming operator can be used to explicitly


rename resulting relations of an expression with in the query do not
think rename permanently the relation .

It is denoted by: Where ρ(rho) represents the


RENAME operator and S is a name for the new relation and A1, A2,
… An are new names for the attributes in the relation R.

Can be useful in connection with more complex operations such as


union and join

Eg: find out the maximum account balance in the bank.

Πaccount.bal(δaccount.bal <a.bal>(account Χ ρa (account)))


11/29/2019 Database System 4-7
Cont..

If no renaming is applied, the name of the attribute in the resulting


relation of select operation are the same as those in the original and in
the same order relation

For projection operation with no renaming the resulting relation has


the same attribute names as those in the projection list and in the same
order which they appear in the list.

After the renaming the name of the relation and the attributes can be
used as ordinary relation and attributes in a sequence of relational
algebra expressions:

11/29/2019 Database System 4-8


Binary Operations
Cartesian Product Operation: also known as Cross Product or Cross
Join or Product is binary set operation that generates a new relation
from two relation in a combinatorial fashion.

It is denoted by RΧS, Where Χ represents the PRODUCT operator


and R and S are relations to be joined.

The product operation are maps each tuple in relation with every
tuple in S.

11/29/2019 Database System 4-9


Cont..

Example - Consider the following relations R and S, then R ΧS is


given as shown to the right.

11/29/2019 Database System 4-10


Cont..

Consider the Employees, EmpTeams and Teams relation and develop


a relational algebra expression that retrieves the name and position of
Employees that work on Project 1 as Programmers and rename the
relation as Programmers1.

Employees(EmpId, Name, BDate, Age, Gender, Position, Salary)

EmpTeams(EmpId, TeamId)

Teams(TeamId, PrjId, Name, Descr)

11/29/2019 Database System 4-11


Cont..

11/29/2019 Database System 4-12


Cont..
Union Operation: The union operation on R and S denoted by R U S
results a relation that includes all tuples either in R or in S or in both.
Duplicates are eliminated from the result.

Intersection Operation: The intersection operation on R and S


denoted by R ˄ S results a relation that includes all tuples in both R
and S

Set Difference Operation: The result of the set difference operation on


R and S denoted by R−S is the set of elements in R but not in S.

11/29/2019 Database System 4-13


Cont..

For the set operations (Union, Intersection, product, Set difference)


the two relational operands R and S must have same type of tuples,
this condition is known as Union Compatibility .

Two relations R(A1, A2, … An) and S(B1, B2, … Bn) are said to be
union compatible if

They have same degree n, and

Domain(Ai) = Domain(Bi) for all i = 1, 2, … n

11/29/2019 Database System 4-14


Cont..

Example - Consider the following relations R and S, then R US and


R – S are given as shown to the right.

Find name and position of Employees that work on both Projects 1


and 2 as Programmers. It is Similar to the previous example.

11/29/2019 Database System 4-15


Additional Operations
Natural Join Operation: A frequent type of join connects two relations
by:

Equating attributes of the same name, and

Projecting out one copy of each pair of equated attributes.

Such a join is known as natural join and it is denoted by:


where represents the NATURAL JOIN operator and R and S are

relations to be joined

11/29/2019 Database System 4-16


Cont..

Consider the following relations R and S, then R S is given as


shown to the right

11/29/2019 Database System 4-17


Cont..

The pervious example that retrieves the name and position of


Employees that work on Project 1 as Programmers from the
modified relations below can be simplified as:

Employees(EmpId, FullName, BDate, Age, Gender, Position,


Salary)

EmpTeams(EmpId, TeamId)

Teams(TeamId, PrjId, TName, Descr)

Write the relational expression based on the above information.

11/29/2019 Database System 4-18


Cont..

Theta Join Operation: While the natural join enforces a join condition
by equating similar attributes in the relations to be joined; a theta join
joins relations to an arbitrary condition C.

The notation for theta join is:

The result of the theta operation is constructed by:

Taking the product of R and S, and

Selecting only those tuples satisfying the condition C.

11/29/2019 Database System 4-19


Cont..

Consider the following relations R and S, then is given as


shown to the right.

11/29/2019 Database System 4-20


Extended Operation
The basic relational algebra operations have been extended in several
ways to enhance the expressive power of the original relational
algebra. Some of the extended operations are: -

Outer Join

Extended Projection

Duplicate Elimination

Aggregation and Grouping

11/29/2019 Database System 4-21


Cont..
Outer Join Operation: The natural join operation results a tuple
when there is a match to the common attributes of the tuples in the
relations R and S. Such joins are known as inner join operation.

However, there are cases when we want to have all the tuples from the
participating relations and form the join when there is much. In such
cases outer join operations can be used to keep all the tuples in R, or
all those in S, or all those in both relations irrespective of they having
matching tuples in their common attributes.

11/29/2019 Database System 4-22


Cont..
The three types of outer join operators are

Left Outer Join: Keeps every tuples in the left relation R and when
there is no matching for tuples in R from tuples in S, the attributes
of S are filled (padded) with NULL values.

It is denoted by:

11/29/2019 Database System 4-23


Cont..
Right Outer Join: Similar to the left outer join operation it keeps all
tuples in the right relation S and when there is no matching for
tuples in S from tuples in R, the attributes of R are padded with
NULL values,

It is denoted by:

11/29/2019 Database System 4-24


Cont..
Full Outer Join: Keeps all tuples in both the left and right relations
when no matching tuples are found, padding them with NULL
values as needed

It is denoted by:

11/29/2019 Database System 4-25


Cont..
Aggregation and Grouping Operation: Aggregation functions
(operators) such as SUM, COUNT, MIN, MAX, and AVG are
collection operators that return a single value as a result.

Aggregation operators are not relational algebra operators but they


are used by the grouping operator (γ) that groups tuples according to
their values in one or more attributes.

It is denoted by: Where L is either the list of grouping


attributes in order or list of aggregation functions applied to the
attributes of the relation R

11/29/2019 Database System 4-26


Cont..

Example - Write a relational algebra that determines the number of


teams all the employees are working in.

11/29/2019 Database System 4-27


Introduction to Relational Calculus
A relational calculus is a declarative and nonprocedural expression
that specifies a retrieval request, and hence there is no description of
how to evaluate the query in a relational calculus. Rather, a relational
calculus expression specifies what to be retrieved.

The relational calculus is not the same as that of differential and


integral calculus in mathematics but takes its name from a branch of
symbolic logic termed as predicate calculus.

A relational calculus is classified into two as

Tuple Relational Calculus, and

Domain Relational Calculus


11/29/2019 Database System 4-28
Cont..
The Tuple Relational Calculus : A query in a tuple relational calculus
(tuple calculus) is expressed as: { t/p(t) } Where t. is a tuple variable
and p(t) a predicate (condition) that is to be true for the tuple t.

P(t) may have various conditions logically combined with OR (∨),


AND (∧), NOT(¬). It also uses quantifiers:

∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that


predicate Q(t) is true.

∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.

11/29/2019 Database System 4-29


Cont..

EXAMPLE: Find the loan number, branch, amount of loans of greater


than or equal to 10,000 amount
11/29/2019 Database System 4-30
Cont..
SOLUTION
{t| t ∈ loan ∧ t[amount]>=10,000}

Find the loan number for each loan of an amount greater or equal to
10,000.

{t| ∃ s ∈ loan(t[loan number] = s[loan number] ∧ s[amount]>=10000)

Find the names of all customers having a loan at the “ABC” branch.

{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name]) ∧ ∃ u ∈ depositor(


t[customer-name] = u[customer-name])}
11/29/2019 Database System 4-31
Cont..
The Domain Relational Calculus: A query in a domain relational
calculus (domain calculus) uses domain variables that take on values
from an attributes domain rather than values for an entire tuple.

It is expressed as Where tx1,x2…..xn


represent domain variables and P is the predicate as in the case of
tuple calculus.

Formulas in the predicate are build in the same ways as the tuple
calculus predicates.

11/29/2019 Database System 4-32


Cont..
Example - Retrieve name and description of all the teams working on
project named “banking db”.

Projects(PrjId, PName, SDate, DDate, CDate)

Teams(TeamId, PrjId, TName, Descr calculus predicates

11/29/2019 Database System 4-33

You might also like