0% found this document useful (0 votes)
11 views111 pages

Unit 2

ER diagrams
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)
11 views111 pages

Unit 2

ER diagrams
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/ 111

DATABASE MANAGEMENT UNIT -2

SYSTEMS RELATIONAL MODEL

© Kalasalingam academy of research and education


Course Outline:

Course description:
CO 1 Apply the database management
system concepts. This course is designed to introduce under
graduate students to the foundations of
CO 2 Design relational and ER model for
database design. database systems, focusing on basics such as
the relational algebra and data model, schema
CO 3. Examine issues in data storage and query normalization, query optimization, and
processing and frame appropriate solutions.
transactions.
CO 4. Analyze the role and issues like efficiency, privacy,
security, ethical responsibility and strategic advantage in
data management

CO 5. Build applications to schedule concurrent


executions with recovery mechanisms.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Syllabus
Relational Model – Catalog – Types – Keys -
Relational Algebra- Domain - Tuple Relational
Calculus - SQL – Data Definition - Queries In
SQL – Updates - Views – Integrity and Security
Unit 2
– Sub Queries - Correlated Sub Queries -
Relational Database Design – Functional
Outcomes
Dependences And Normalization For Relational
Databases (up to BCNF). CO2:
Apply normalization steps to eliminate redundant
data.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Unit 2 RELATIONAL MODEL

Formulate, using SQL, solutions to a broad


Lesson 1. Relational model and keys range of query and data update problems.

Lesson 2. Relational Algebra and Calculus

Lesson 3. DDL and DML in SQL

Lesson 4. Views and subqueries in SQL

Lesson 5. Functional Dependences and


Normalization (upto BCNF)

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Reference:

Abraham Silberschatz, Henry F. Korth and Sudarshan S., Database System Concepts, McGraw-Hill , 6th
Edition, 2011.
Ramez Elmasri and Shamkant B. Navathe. Fundamental Database Systems, Addison-Wesley, 5th
Edition, 2005.
Raghu Ramakrishnan, Database Management System, Tata McGraw-Hill, 3rd Edition, 2006.
Hector Garcia-Molina, Jeff Ulman and Jennifer Widom, Database Systems: The Complete Book,
Prentice Hall, 2003.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Lesson 1: INTRODUCTION TO RELATIONAL MODEL
attributes
(or columns)
Example of a relation

tuples
(or rows)

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Relational Model

© Kalasalingam academy of research and education COURSE NAME


Attribute Types

The set of allowed values for each attribute is called the domain of the attribute
Attribute values are (normally) required to be atomic; that is, indivisible
The special value null is a member of every domain. Indicated that the value is “unknown”
The null value causes complications in the definition of many operations

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Types of attributes

© Kalasalingam academy of research and education COURSE NAME


Simple Attributes

Simple attributes are those attributes which


can not be divided further.

© Kalasalingam academy of research and education COURSE NAME


Composite Attributes

Composite attributes are those attributes which


are composed of many other simple attributes.

© Kalasalingam academy of research and education COURSE NAME


Single Valued Attributes

Single valued attributes are those attributes


which can take only one value for a given
entity from an entity set.

© Kalasalingam academy of research and education COURSE NAME


Multi Valued Attributes

Multi valued attributes are those attributes


which can take more than one value for a given
entity from an entity set.

© Kalasalingam academy of research and education COURSE NAME


Derived Attributes

Derived attributes are those attributes which


can be derived from other attribute(s).

© Kalasalingam academy of research and education COURSE NAME


Key Attributes

Key attributes are those attributes which can


identify an entity uniquely in an entity set.

© Kalasalingam academy of research and education COURSE NAME


Relation Schema and Instance

A1, A2, …, An are attributes


R = (A1, A2, …, An ) is a relation schema

Example: instructor = (ID, name, dept_name, salary)

Formally, given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn


Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai  Di

The current values (relation instance) of a relation are specified by a table

An element t of r is a tuple, represented by a row in a table

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Combine Schemas?

Suppose we combine instructor and department into inst_dept


◦ (No connection to relationship set inst_dept)
Result is possible repetition of information

© Kalasalingam academy of research and education COURSE NAME


What About Smaller Schemas?
Suppose we had started with inst_dept. How would we know to split up (decompose) it into instructor and department?
Write a rule “if there were a schema (dept_name, building, budget), then dept_name would be a candidate key”
Denote as a functional dependency:
 dept_name  building, budget
In inst_dept, because dept_name is not a candidate key, the building and budget of a department may have to be repeated.
This indicates the need to decompose inst_dept
Not all decompositions are good. Suppose we decompose
employee(ID, name, street, city, salary) into
 employee1 (ID, name)
 employee2 (name, street, city, salary)
The next slide shows how we lose information -- we cannot reconstruct the original employee relation -- and so, this is a lossy
decomposition.

© Kalasalingam academy of research and education COURSE NAME


Schema vs Instance

© Kalasalingam academy of research and education COURSE NAME


Relations are Unordered

Order of tuples is irrelevant


Tuples may be stored in an arbitrary order.
 Example: instructor relation with unordered tuples

Unordered
Tuples

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Keys

Let K  R
K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)
Example: {ID} and {ID,name} are both superkeys of instructor.

Superkey K is a candidate key if K is minimal


Example: {ID} is a candidate key for Instructor
One of the candidate keys is selected to be the primary key.
Foreign key constraint: Value in one relation must appear in another
Referencing relation
Referenced relation
Example – dept_name in instructor is a foreign key from instructor referencing department

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Types of keys

© Kalasalingam academy of research and education COURSE NAME


Super key

Super Key is the set of all the keys which help to identify rows in a table uniquely.
This means that all those columns of a table than capable of identifying the other columns of that table
uniquely will all be considered super keys.
Super Key is the superset of a candidate key.
The Primary Key of a table is picked from the super key set to be made the table’s identity attribute.

© Kalasalingam academy of research and education COURSE NAME


Super key

© Kalasalingam academy of research and education COURSE NAME


Primary key

A primary key is a column of a table or a set of columns that helps to identify every record present in that
table uniquely.
There can be only one primary Key in a table.

Also, the primary Key cannot have the same values repeating for any row.

Every value of the primary key has to be different with no repetitions.

The PRIMARY KEY (PK) constraint put on a column or set of columns will not allow them to have any
null values or any duplicates.
One table can have only one primary key constraint.

Any value in the primary key cannot be changed by any foreign keys (explained below) which refer to it.

© Kalasalingam academy of research and education COURSE NAME


Primary and Candidate key

© Kalasalingam academy of research and education COURSE NAME


Candidate Key

Candidate keys are those attributes that uniquely identify rows of a table.

 The Primary Key of a table is selected from one of the candidate keys.

So, candidate keys have the same properties as the primary keys.

 There can be more than one candidate keys in a table.

© Kalasalingam academy of research and education COURSE NAME


Foreign key

Foreign Key is used to establish relationships between two tables.

A foreign key will require each value in a column or set of columns to match the Primary Key of the
referential table.
 Foreign keys help to maintain data and referential integrity.

© Kalasalingam academy of research and education COURSE NAME


Foreign key

© Kalasalingam academy of research and education COURSE NAME


Example for keys

© Kalasalingam academy of research and education COURSE NAME


Example for Keys

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Composite Key

Composite Key is a set of two or more attributes that help identify each tuple in a table uniquely.

 The attributes in the set may not be unique when considered separately.

However, when taken all together, they will ensure uniqueness.

© Kalasalingam academy of research and education COURSE NAME


Unique Key

Unique Key is a column or set of columns that uniquely identify each record in a table.

 All values will have to be unique in this Key.

A unique Key differs from a primary key because it can have only one null value, whereas a primary Key
cannot have any null values.

© Kalasalingam academy of research and education COURSE NAME


Schema Diagram for University Database

© Kalasalingam academy of research and education COURSE NAME


First Lesson Summary:
Introduction to relational model
Topic 1
Attribute Types Example of relation
Topic 2
Relational Schemas and Instances
Unordered relations
Topic 3
Types of keys
Types of keys

Topic 4

Schematic diagram of University


database

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Lesson – 2
RELATIONAL QUERY LANGUAGES
A query language is a language in which a user requests information from the database.
These languages are usually on a higher level than that of a standard programming language.
Relational query languages use relational algebra to break the user requests and instruct the DBMS to execute the
requests.
 It is the language by which user communicates with the database.
These relational query languages can be procedural or non-procedural.
A procedural query language will have set of queries instructing the DBMS to perform various transactions in the
sequence to meet the user request.
Relational algebra is a procedural query language.
Non-procedural queries will have single query on one or more tables to get result from the database.
Relational Calculus is a non procedural language which informs what to do with the tables, but doesn’t inform how
to accomplish this.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Relational Algebra- Operations

© Kalasalingam academy of research and education COURSE NAME


RELATIONAL ALGEBRA - SELECT OPERATION

σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450.

σsubject = "database" and price = "450" or year > "2010"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books published
after 2010.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
Select operation - example

© Kalasalingam academy of research and education COURSE NAME


PROJECT OPERATION – SELECTION OF COLUMNS

∏subject, author (Books)


Selects and projects columns named as subject and author from the relation Books.
Relation r:

A,C (r)

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
UNION OF TWO RELATIONS

∏ author (Books) ∪ ∏ author (Articles)


Output − Projects the names of the authors who have either written a book or an article or both.
Relations r, s:

r  s:

© Kalasalingam academy of research and education COURSE NAME


Union -example

© Kalasalingam academy of research and education COURSE NAME


SET INTERSECTION OF TWO RELATIONS

Relation r, s:

rs

Note: r  s = r – (r – s)

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
SET DIFFERENCE OF TWO RELATIONS

∏ author (Books) − ∏ author (Articles)


Output − Provides the name of authors who have written books but not articles.
Relations r, s:

r – s:

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
RENAMING A TABLE

 Allows us to refer to a relation, (say E) by more than one name.


 x (E)
returns the expression E under the name X
Relations r

r x  s (r)

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
TUPLE RELATIONAL CALCULUS

Tuple Relational Calculus is specified to select the tuples in a relation.


Filtering variable uses the tuples of a relation.
The result of the relation can have one or more tuples
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

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
PREDICATE CALCULUS FORMULA

Set of attributes and constants


Set of comparison operators: (e.g., , , , , , )
Set of connectives: and (), or (v)‚ not ()
Implication (): x  y, if x if true, then y is true
x  y x v y
Set of 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 is true “for all” tuples t in relation r

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
Example Queries

Find the ID, name, dept_name, salary for instructors whose salary is greater than $80,000
{t | t  instructor  t [salary ]  80000}
Notice that a relation on schema (ID, name, dept_name, salary) is implicitly defined by the query
As in the previous query, but output only the ID attribute value

{t |  s instructor (t [ID ] = s [ID ]  s [salary ]  80000)}


Notice that a relation on schema (ID) is implicitly defined by the query

COURSE NAME: DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
DOMAIN RELATIONAL CALCULUS

In domain relational calculus the filtering variable uses the domain of attributes.
It uses the same operators as tuple 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

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
EXAMPLE QUERIES

Find the ID, name, dept_name, salary for instructors whose salary is greater than $80,000
{< i, n, d, s> | < i, n, d, s>  instructor  s  80000}

 As in the previous query, but output only the ID attribute value


{< i> | < i, n, d, s>  instructor  s  80000}

Find the names of all instructors whose department is in the Watson building
{< n > |  i, d, s (< i, n, d, s >  instructor
  b, a (< d, b, a>  department  b = “Watson” ))}

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
Topic 1

Second Lesson Summary:


Relational Algebra- Select

Topic 2

Project operation
Relational Query Languages
Topic 3 Relational Algebra
Set Union Tuple Relational Calculus
Topic 4
Domain Relational Calculus
Set Intersection

Topic 5

Set Difference

Topic 6

Tuple Relational Calculus

Topic 7
Domain Relational Calculus

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Lesson 3: SQL

Initially SQL is named as SEQUEL meaning Structured English Query Language.


Later it is named as SQL – Structured Query Language
SQL is developed in IBM San Jose Research Laboratory.
Many standards such as SQL-86, SQL-89, SQL-92 , SQL:1999, SQL:2003 exist.
SQL is a programming language to query databases.
Information can be fetched from database using SQL.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


SQL - History

© Kalasalingam academy of research and education COURSE NAME


SQL Commands

© Kalasalingam academy of research and education COURSE NAME


DATA DEFINITION LANGUAGE

The SQL data-definition language (DDL) allows the specification of information about relations,
including:
The schema for each relation.
The domain of values associated with each attribute.
Integrity constraints

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
DOMAIN TYPES

Fixed length character string - char(n).


Variable length character strings - varchar(n). .
Integer - int.
Small integer - smallint.
Floating point number, float(n).

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
DOMAIN TYPES IN SQL

© Kalasalingam academy of research and education COURSE NAME


CREATE TABLE CONSTRUCT

An SQL relation is defined using the create table command:


create table r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
r is the name of the relation
each Ai is an attribute name in the schema of relation r
Di is the data type of values in the domain of attribute Ai

Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
CREATE TABLE CONSTRUCT

© Kalasalingam academy of research and education COURSE NAME


INTEGRITY CONSTRAINTS IN CREATE TABLE

not null
primary key (A1, ..., An )
foreign key (Am, ..., An ) references r
Example:
create table instructor (
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department);
primary key declaration on an attribute automatically ensures not null

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
INTEGRITY CONSTRAINTS IN CREATE TABLE

© Kalasalingam academy of research and education COURSE NAME


SELECT

Select is used to display the records from table.


All records can be displayed using *
Eg. Select * from emp;
This will fetch all the columns from table.
To select particular columns, where can be used with select
Eg. Select regno from stud where name = ‘john’;
This will fetch the regno of john.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
THE SELECT CLAUSE

© Kalasalingam academy of research and education COURSE NAME


THE SELECT CLAUSE

© Kalasalingam academy of research and education COURSE NAME


THE WHERE CLAUSE

The where clause specifies conditions that the result must satisfy.
Corresponds to the selection predicate of the relational algebra.
To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = ‘Comp. Sci.'
Comparison results can be combined using the logical connectives and, or, and not
To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = ‘Comp. Sci.' and salary > 80000

Comparisons can be applied to results of arithmetic expressions.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
THE WHERE CLAUSE

© Kalasalingam academy of research and education COURSE NAME


THE WHERE CLAUSE

© Kalasalingam academy of research and education COURSE NAME


DML – DATA MANIPULATION LANGUAGE
DML includes commands which modifies database
Insert
Insert is used to insert values into table. One of the methods used is substitution method
Delete
It is temporary deletion which deletes rows of a table.
Update
This command is used to update values in a table
Alter
This command is used to add columns or rename a table.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
DML – DATA MANIPULATION LANGUAGE

© Kalasalingam academy of research and education COURSE NAME


Types of DML

© Kalasalingam academy of research and education COURSE NAME


Topic 1
History of SQL

Topic 2 Third Lesson Summary:


Data Definition Language

Topic 3
History of SQL
Create table Data Definition Language
Topic 4 Select Clause
Data Manipulation Language Create Clause
Topic 5:
Data Manipulation Language
WHERE clause
Insert
Delete
Drop

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Lesson 4: VIEWS

In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual
relations stored in the database.)
Consider a person who needs to know an instructors name and department, but not the salary.
This person should see a relation described, in SQL, by

select ID, name, dept_name


from instructor

A view provides a mechanism to hide certain data from the view of certain users.
Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is
called a view.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
View vs Table

© Kalasalingam academy of research and education COURSE NAME


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

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
EXAMPLE VIEWS

A view of instructors without their salary


create view faculty as
select ID, name, dept_name
from instructor
Find all instructors in the Biology department
select name
from faculty
where dept_name = ‘Biology’
Create a view of department salary totals
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
EXAMPLE VIEWS

© Kalasalingam academy of research and education COURSE NAME


VIEWS DEFINED USING OTHER VIEWS

create view physics_fall_2009 as


select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = ’Physics’
and section.semester = ’Fall’
and section.year = ’2009’;
create view physics_fall_2009_watson as
select course_id, room_number
from physics_fall_2009
where building= ’Watson’;

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
VIEWS DEFINED USING OTHER VIEWS

© Kalasalingam academy of research and education COURSE NAME


NESTED SUBQUERIES

A query which is inside another query is said to be nested subquery


The inner query is executed first and then fetching these values outer query is executed
The inner query is said to be parent query
The outer query is said to be sub query
Eg. (Select regno from stud where (select name from stud where age = 10));
This first executes inner query and fetches name and then outer query is executed which displays the
regno.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
NESTED SUBQUERIES

© Kalasalingam academy of research and education COURSE NAME


NESTED SUBQUERIES

© Kalasalingam academy of research and education COURSE NAME


SUBQUERIES IN THE FROM CLAUSE

SQL allows a subquery expression to be used in the from clause


Find the average instructors’ salaries of those departments where the average salary is greater
than $42,000.”
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
SUBQUERIES IN THE FROM CLAUSE

Another way to write above query


select dept_name, avg_salary
from (select dept_name, avg (salary)
from instructor
group by dept_name) as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;

Note that we do not need to use the having clause

© Kalasalingam academy of research and education COURSE NAME


SUBQUERIES IN THE FROM CLAUSE

© Kalasalingam academy of research and education COURSE NAME


SCALAR SUBQUERY

© Kalasalingam academy of research and education COURSE NAME


Fourth Lesson Summary:
View Definition
Example views
Topic 1
View Definition using other views
Views
Nested Subqueries
Topic 2
Subqueries in SQL Subqueries in the FROM clause
Scalar Subquery

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Lesson 5: Definition of FUNCTIONAL DEPENDENCY

Functional Dependency depicts the dependency between attributes.


That is the properties of an entity is said to be attributes.
When one attribute is changed, how other attribute gets affected, shown by functional
dependency.
The left side of functional dependency is said to be dependent.
The right side of funtional dependency is said to be determinant.
The dependency occurs between primary key and non- primary key attribute.

COURSE NAME DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
FUNCTIONAL DEPENDENCY

© Kalasalingam academy of research and education COURSE NAME


TYPES OF FUNCTIONAL DEPENDENCY

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


What is TRIVIAL FUNCTIONAL DEPENDENCY?

Functional dependency may be of trivial.


If one attribute is subset of another attribute then there remains trivial functional dependency.
Eg. Empno can be subset of Empno.
Empname can be subset of Empname.
When there is no common dependency between attributes then they are non-trivial.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


NON-TRIVIAL FUNCTIONAL DEPENDENCY

A → B has a non-trivial functional dependency if B is not a subset of A.


When A intersection B is NULL, then A → B is called as complete non-trivial.
ID → Name,
Name → DOB

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


A Lossy Decomposition

© Kalasalingam academy of research and education


Lossless join decomposition

A B C A B B C
 1 A  1 1 A
 2 B  2 2 B
r A,B(r) B,C(r)

A B C
 1 A
 2 B

© Kalasalingam academy of research and education


NORMALIZATION

Normalization is the process of organizing the data in the database.


Normalization is used to minimize the redundancy from a relation or set of relations. It is also used to eliminate the
undesirable characteristics like Insertion, Update and Deletion Anomalies.
Normalization divides the larger table into the smaller table and links them using relationship.
The normal form is used to reduce redundancy from the database table.
There are three types of anomalies that occur when the database is not normalized.
These are – Insertion, update and deletion anomaly.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


NORMALIZATION

© Kalasalingam academy of research and education COURSE NAME


NORMALIZATION

© Kalasalingam academy of research and education COURSE NAME


EXAMPLE FOR ANOMALIES

Suppose a manufacturing company stores the employee details in a table named employee that has four
attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for
storing employee’s address and emp_dept for storing the department details in which the employee works

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

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


Anaomalies

© Kalasalingam academy of research and education COURSE NAME


UPDATE, INSERT AND DELETE ANOMALY
Update anomaly:In the above table we have two rows for employee Rick as he belongs to two departments of
the company.
If we want to update the address of Rick then we have to update the same in two rows or the data will become
inconsistent.
 If somehow, the correct address gets updated in one department but not in other then as per the database, Rick
would be having two different addresses, which is not correct and would lead to inconsistent data.
Insert anomaly:Suppose a new employee joins the company, who is under training and currently not assigned
to any department then we would not be able to insert the data into the table if emp_dept field doesn’t allow
nulls.
Delete anomaly:Suppose, if at a point of time the company closes the department D890 then deleting the rows
that are having emp_dept as D890 would also delete the information of employee Maggie since she is assigned
only to this department.
To overcome these anomalies we need to normalize the data.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


First Normal Form

Domain is atomic if its elements are considered to be indivisible units


Examples of non-atomic domains:
Set of names, composite attributes
Identification numbers like CS101 that can be broken up into parts
A relational schema R is in first normal form if the domains of all attributes of R are atomic
Non-atomic values complicate storage and encourage redundant (repeated) storage of data
Example: Set of accounts stored with each customer, and set of owners stored with each account

© Kalasalingam academy of research and education COURSE NAME


First Normal Form (Cont’d)

Atomicity is actually a property of how the elements of the domain are used.
Example: Strings would normally be considered indivisible
Suppose that students are given roll numbers which are strings of the form CS0012 or EE1127
If the first two characters are extracted to find the department, the domain of roll numbers is not atomic.
Doing so is a bad idea: leads to encoding of information in application program rather than in the database.

© Kalasalingam academy of research and education COURSE NAME


NORMAL FORMS – 1NF (FIRST NORMAL FORM)

A relation will be 1NF if it contains an atomic value.


It states that an attribute of a table cannot hold multiple values. It must hold only single-valued attribute.
First normal form disallows the multi-valued attribute, composite attribute, and their combinations.
The given table is not in 1NF because multi-valued for EMP_PHONE

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


DECOMPOSITION IN 1NF

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


2NF - SECOND NORMAL FORM

A relation is said to be in second normal form, it should satisfy first normal form.
Non-key attributes are fully functional dependent on the primary key in the second normal form.
Example: A teacher can teach more than one subject.
In the given table the non-prime attribute TEACHER_AGE depends on TEACHER_ID which is a
proper subset of candidate key. So it violates the property of 2NF.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


DECOMPOSITION IN 2NF

To convert the given table to 2NF, the given table is decomposed into two tables:

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


THIRD NORMAL FORM (3NF)

A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
If there is no transitive dependency for non-prime attributes, then the relation must be in third normal
form.
A relation is in third normal form if it holds atleast one of the following conditions for every non-trivial
function dependency X → Y.
X is a super key.
Y is a prime attribute, i.e., each element of Y is part of some candidate key.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


EXAMPLE
Superkey in this table : {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME,EMP_ZIP} so on…
Candidate key: {EMP_ID}

The non-prime attributes transitively dependent on superkey.


It violates the rule of third normal form.
 EMP_STATE and EMP_CITY is moved to new table EMPLOYEE_ZIP having EMP_ZIP as primary key.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


BCNF –BOYCE CODED NORMAL FORM

BCNF is the advance version of 3NF. It is stricter than 3NF.


A table is in BCNF if every functional dependency X → Y, X is the super key of the table.
For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example: Let's assume there is a company where employees work in more than one department.

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


BCNF

In the above table Functional Dependencies are:


EMP_ID-> EMP_COUNTRY
EMP_DEPT-> {DEPT_TYPE, EMP_DEPT_NO}
Candidate key: {EMP_ID, EMP_DEPT}
So, the given table is decomposed into three tables:
EMP_COUNTRY – Attributes: EMP_ID, EMP_COUNTRY
EMP_DEPT – Attributes: EMP_DEPT, DEPT_TYPE, EMP_DEPT_NO
EMP_DEPT_MAPPING - Attributes: EMP_ID, EMP_DEPT

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS


This unit focuses on the basic concepts of Relational model , keys and its types,
Topic 1
Relational algebra and Relational calculus in Database Management System.
Relational Model
Topic 2
Lesson 1: Represents the introduction to Relational model , keys and its types.
Keys
Topic 3 Lesson 2: Represents the relational query languages such as relational algebra
Relational Algebra and relational calculus such as domain and tuple relational calculus.
Topic 4
Domain and Tuple Relational Calculus Lesson 3: Represents the Structured Query Language, datatypes, DDL, Select
clause, create , DML, insert, delete, drop and rename queries.
Topic 5
Data Definition and Data Manipulation Lesson 4: Introduces the concept of views and the subqueries using where
Language clause and from clause.
Topic 6
Views Lesson 5: Describes the functional dependencies, anomalies, normalization
forms such as 1NF, 2NF, 3NF and BCNF.
Topic 7
Subqueries
Topic 8
Functional Dependency
Topic 9 Second Unit Summary
Normalization

COURSE NAME : DATABASE MANAGEMENT SYSTEMS


© Kalasalingam academy of research and education
Thank You!

© Kalasalingam academy of research and education DATABASE MANAGEMENT SYSTEMS

You might also like