DBMS Unit (1,2,3)
DBMS Unit (1,2,3)
Database
Management system
Lecture-1
DATA and INFORMATION
• To understand what is database, first we need a brief understanding
of about DATA and INFORMATION
• DATA
Data: In simple words, data is raw, isolated facts about an entity or
an object which is stored in recorded form. For example, text, audio,
image, video etc.
• INFORMATION
Information: It is a processed, meaningful and usable data.
DATA and INFORMATION
5 is a DATA or
INFORMATION?
a. DATA
b. INFORMATION
“You can have data without
information, but you cannot have
information without data.” —
Daniel Keys Moran
DATABASE and DBMS
Database System
Database DBMS
Microsoft: SQL
Server, Oracle
Structured Unstructured
(9i,10g,11c,12i),
MySQL, IBM: DB2
DATABASE
• Companies have their own Database System.
• Structure Data: Database that can be stored in some particular
structure.
• Structure used for structured database is RDBMS.
• Unstructured Data: Database that has no particular structure to store
data
DBMS
• Users will fetch the data and will perform the operations: update,
insert, delete.
Concurrent access
Security Problems
anomalies
a situation where there are multiple tables within a
database that deal with the same data but may receive it
from different inputs.
Data Inconsistency
Software: It is the actual DBMS. Between the physical database itself and the users of
Data: Data acts as the bridge between the machine components and user components.
Components of Database System
Users: There are number of users who can access or retrieve
data on demand using the applications and the interfaces provided
by DBMS. The users can be:
• Naive users
• Online users
• Application Programmers
• Sophisticated Users
• Data base Administrator ( DBA)
Different Types of Database Users in DBMS
1. Application Programmers
• As its name shows, application programmers are the one who writes application
programs that uses the database.
• These programs meet the user requirement and made according to user requirements.
They interact with DBMS through DML (Data manipulation language) calls. And all
these functions are performed by generating a request to the DBMS.
End Users
End users are those who access the database from the terminal end.
They use the developed applications and they don’t have any
knowledge about the design and working of the database.
Their main motto is just to get their task done.
following layers:
• Database Server
◼ Database Schema
The description of the database
Specified during database design
Expected to change when requirements change
◼ Schema Diagram
Most data models have certain conventions for displaying schemas as
diagrams
Displays only some aspects of a schema, such as names of record types
and data items
Some constraints are difficult to represent
◼ Schema Construct
Each object in the schema (e.g., student, course)
Example
Database State or Snapshot
Internal schema at the internal level to describe data storage structures and
access paths. Typically uses physical data model.
Conceptual schema at the conceptual level to describe the structure and
constraints for the whole data se. Uses a conceptual or an implementation data
model.
External schema at the external level to describe the various user views.
Usually uses the same data model as the conceptual level.
◼ Logical level: describes data stored in database, and the relationships among
the data.
◼ View level: application programs hide details of data types. Views can also
hide information (such as an employee’s salary) for security purposes.
Three Level Architecture of DBMS
Three Level Architecture of DBMS
◼ External Level
◼ Conceptual Level
◼ Internal Level
External Level or View level
▪ The External view is described by means of a schema called external schema that
correspond to different views of the data.
▪ Similarly the Conceptual view is defined by conceptual schema, which describes all
the entities, attributes, and relationship together with integrity constraints.
▪ Internal View is defined by internal schema, which is a complete description of the
internal model, containing definition of stored records, the methods of representation,
the data fields, and the indexes used.
Data Independence
Achievement of Three Level Architecture
Data Independence
Data does not help in decision making Information helps in decision making
Database
• A database is a collection of logically related data that is
designed to meet the information needs of an
organization
• The related information when placed is an organized
form makes a database.
• The organization of data/information is necessary
because unorganized information has no meaning.
Purpose of DBMS
An example
• University Database:
Data about students, faculty, courses, research-laboratories,
course registration/enrollment etc. Reflects the state of
affairs of the academic aspects of the university.
Purpose: To keep an accurate track of the academic
activities of the university.
Purpose of DBMS
Before DBMSs were introduced, organizations usually stored
information in file processing system which has a number of
disadvantages:
• Data redundancy and inconsistency
• Difficulty in accessing data
• Data isolation
• Integrity problems
• Atomicity problems
• Concurrent-access anomalies
• Security problems
Database Management System
• DBMS A database management system is the software
system that allows users to define, create and maintain a
database and provides controlled access to the data.
• A database management system (DBMS) is basically a
collection of programs that enables users to store,
modify, and extract information from a database as per
the requirements.
Operations on databases
• To add new information
• To view or retrieve the stored information
• To modify or edit the existing information
• To remove or delete the unwanted information
• Arranging the information in a desired order etc.
Components of Database
• Five major components in database system environment:
– Hardware
– Software
– Data
– Users
– Procedures
Components of Database System
• Hardware: It is the actual computer system used for keeping
and accessing the database. DBMS hardware consists of
secondary storage devices like hard disks.
Database
Server
Three-tier architecture
User
Client Client Client Client Application or Client Layer
Application machine 1 machine 1 machine 1
Business Layer
Application Database Server
Server
1-Tier Architecture
• A simple one tier architecture
example would be anytime you
install a Database in your system
and access it to practice SQL
queries. But such architecture is
rarely used in production.
When you book train ticket
online follows which type of
architecture?
Schema
• It is logical representation of database. Or
• Overall design of the database is called a schema
• Data is logically represented in the form of tables.
Example: Design schema of entity ‘student’.
Reg. Number Name Age Address
Important Point
Attribute
Attribute
Types of Attributes
• Simple and composite attributes
• Single-valued and multi-valued attributes
• Stored and Derived attributes
Relationships
• A Relationship is an association among entities For
example:
1. an employee works_at a department
2. a student enrolls in a course.
• Here, Works_at and Enrolls are called relationships.
• Relationships are represented by diamond-shaped box
Relationships
Relationship Set
A set of relationships of similar ty pe is called relationship
Set
The following relationship set "Enrolls(E1,E2,E3)" depicts:
S1 is enrolled in C2,S2 is enrolled in Cl and S3 is enrolled in
C3
Relationship Descriptive Attribute
Like entities, a relationship too can have attributes . These
attributes are called descriptive attributes
For instance, the depositor relationship set between entity
sets customer and account may have the attribute access-
date
Degree of Relationship
The number of different entity sets participating in a
relationship set is called as degree of a relationship set
1. Unary
2. Binary
3. Ternary
4. N-ary
Degree of Relationship
Unary Relationship:{degr ee = 1)
A unary relationship is only one entity participate in a
relationship, the relationship is ca lled as unary relationship.
1) For example,one person is married to only one
person
Degree of Relationship
Binary Relationship: (degree = 2 )
A binary relationship is when two entities participate in a
relationship, and is the most common relationship degree.
2) For example, Student is enrolled in Course
Degree of Relationship
Mapping Cardinality
• One-to-One
• One-to-Many
• Many -to-One
• Many -to-Many
Attribute Types
Attributes in ER Model
An Entity is a "thing" or "object" in the real world that is
distinguishable from other objects.
for eg: Student, Teacher, Class, Course, Employee, Customer,
account
In one to many, second table is created by merging second entity and relationship.
a1 will be a foreign key
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
1. Find maximum relations
2. Find minimum relations
Example Question
Attributes of R1 and R2 are different. Suppose R2 has same named attribute say
B, then attribute in R2 will be written as “R2.B”.
Cartesian Product or Cross Product
Attributes of R1 and R2 are different. Suppose R2 has same named attribute say
B, then attribute in R2 will be written as “R2.B”.
Composition of Operations
Composition of Operations
Composition of Operations
Composition of Operations
Example:
SQL
SQL
DDL-COMMANDS
CREATE
• SQL CREATE TABLE statement is used to create
table in a database.
• If you want to create a table, you should name the
table and define its column and each column's data
type.
• To create the table:
• create table table-name (column-name1 datatype1,
column-name2 datatype2, column-name3
datatype3, column-name4 datatype4 );
create table STUDENTS(id number(10), name
varchar(20), age number(4));
• You can verify it, if you have created the table
successfully by looking at the message displayed by
the SQL Server, else you can use DESC command as
follows:
• SQL> DESC STUDENTS;
Practice:
Create a table Employee having attributes as id,
name, department, age.
Inserting Data into Table
Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.
Example
SELECT * FROM STUDENTS
WHERE name=‘Rohan’;
SQL AND, OR and NOT Operators
1. The AND and OR operators are used to filter records based
on more than one condition.
2. The AND operator displays a record if all the conditions
separated by AND are TRUE.
3. The OR operator displays a record if any of the conditions
separated by OR is TRUE.
4. The NOT operator displays a record if the condition(s) is
NOT TRUE.
SQL AND, OR and NOT Operators
AND Syntax
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
SQL AND, OR and NOT Operators
AND Example
select all fields from "Customers" where country
is “India" AND city is “City2":
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
SQL UNIQUE Constraint
DML-COMMANDS
• Data Manipulation Language (DML) statements are
used for managing data in database. DML
commands are not auto-committed. It means
changes made by DML command are not
permanent to database, it can be rolled back.
• SQL INSERT statement is a SQL query. It is used to
insert a single or a multiple records in a table.
• There are two ways to insert data in a table:
• By SQL insert into statement
– By specifying column names
– Without specifying column names.
• By SQL insert into select statement
1.SQL INSERT
Inserting data directly into a table
• Syntax:
• SELECT <column1>,<column2> from <tablename>;
• Eg:
• Here is a table of students from where we want to retrieve distinct information For
example: distinct home-town.
• SELECT DISTINCT home_town FROM students
• Now, it will return two rows.
HOME_TOWN
Lucknow
Varanasi
Student_Name Gender Mobile_Numbe HOME_TOWN
r
Rahul Ojha Male 7503896532 Lucknow
Disha Rai Female 9270568893 Varanasi
Sonoo Male 9990449935 Lucknow
Jaiswal
HOME_TOWN
Lucknow
Varanasi
SQL SELECT COUNT
• The SQL COUNT() function is used to return the number of
rows in a query.
• The COUNT() function is used with SQL SELECT statement and
it is used to count the number of rows in a table having
enormous data.
• For example: If you have a record of the voters in a selected
area and want to count the number of voters then it is very
difficult to do it manually but you can do it easily by using the
SQL SELECT COUNT query.
Syntax:
• SELECT COUNT (expression) FROM tables WHERE cond
itions;
Example:
return the total number of names of customers.
SELECT COUNT(name) FROM Customers;
Now add one more row with Name kept blank:
• When you use the drop statement it deletes the table's row
together with the table's definition so all the relationships of
that table with other tables will no longer be valid.
• When you drop a table:
• Table structure, its Relationship ,Integrity constraints and
Access privileges will also be dropped
• On the other hand when we TRUNCATE a table, the table
structure remains the same, so you will not face any of the
above problems.
SQL ROWNUM Clause
Length()
SQL MIN() and MAX() Functions
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL MIN() and MAX() Functions
AVG() Syntax
SELECT AVG(column_name) FROM table_name WHERE
condition;
The SUM() function returns the total sum of a numeric column.
SUM() Syntax
SELECT SUM(column_name) FROM table_name WHERE
condition;
SQL LIKE Operator
6. select all customers with a ContactName that starts with "a" and
ends with "o":
Length()
SQL QUERIES-3
• Rollback command
• This command restores the database to last
commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
• Following is Rollback command's syntax,
• rollback to savepoint-name;
3. Savepoint command
• rollback to B; 1 abhi
2 adam
• SELECT * from class; 4 alex
• rollback to A; 5 abhijit
6 chris
• SELECT * from class; 7 bravo
• Rollback command
• This command restores the database to last
commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
• Following is Rollback command's syntax,
• rollback to savepoint-name;
3. Savepoint command
• rollback to B; 1 abhi
2 adam
• SELECT * from class; 4 alex
• rollback to A; 5 abhijit
6 chris
• SELECT * from class; 7 bravo
CONSTRAINTS
• Sometimes the user wants to impose certain
restrictions on the data being stored in a table.
• These restrictions are called constraints.
• Constraints control the data being entered in a table.
• Once the data constraints are part of a table column
specification ,the oracle engine checks the data being
entered into that column against the data
constraints.
• If the data satisfy the constraint check,it is stored in
the column,otherwise the data is rejected.
• There are five types of constraints:
• Primary Key Constraint
• Unique Constraint
• Not Null Constraint
• Foreign Key Constraint
• Check Constraint
1.PRIMARY KEY CONSTRAINT
• Primary Key is applied to a column so that this
column can be used to uniquely identify each row
in the table.
• A table can have only one primary key.
• A primary key has following features:
• a) Primary key does not allow duplicate values.
• b)Primary Key does not allow null values
• Syntax:
• Columnname datatype(size) primary key
Example:
• Create table student(id number(10) primary
key,name varchar(10),city varchar(10));
or
• Create table student(id number(10),name
char(20),city varchar(10),primary key(id));
2.UNIQUE CONSTRAINT
• In a table there may be more than one column which forms the
candidate key.
• The unique constraint is used for the candidate key columns so
that they contain the unique values for different entities.
• Unique constraint will not allow the duplicate values in the
column to which they are attached.
• A unique key has following characteristics:
1. Unique key will not allow duplicate values.
2. Unique key can accept null value.
3. A table can have many unique constraints.
Syntax:
• Column datatype(size) unique
Example:
Create table student(id number(10) primary
key,name varchar(20) unique, city varchar(10));
3.NOT NULL CONSTRAINT
• Not null column constraint ensures that a table column cannot
be left empty.
• When a Not Null constraint is attached to a column then ,that
column becomes a mandatory column i.e. a value must be
entered into this column entire record will be rejected.
Syntax: columnname datatype(size) not null;
Eg: create table student(id number(10) not null);
5.FOREIGN KEY CONSTRAINT
• Foreign key is a column whose values are derived from the
primary key of other table.
• Foreign key enables us to establish the relationship between the
tables.
• The table in which the foreign key is defined is called as
foreign table or detail table
• The table whose primary key is referenced by the foreign table
is called as primary table or master table.
Foreign key has following characteristics:
1. Foreign key can contain only those values that are present in
the primary key of the master table
2. The values in the foreign key can be null or duplicate values.
3. In foreign key , if we insert a value that is not present in the
primary key of master table then that record will be rejected.
4. A record in the master table cannot be deleted if the
corresponding record in the foreign table exists.
Syntax: column_name datatype(size) references
master_table_name(column name)
Example: let department and course table as master and detail
tables
• Create table department(did number(10) primary key,
dname varchar(10));
After creating the master table, the detail table can be created as
following:
• Create table course(cid number(10) primary key, d_id
number(10) references department(did));
6.CHECK CONSTRAINT
• Check constraint is used to specify the data validation for a
column.
• Check constraint is a Boolean expression that is evaluated to
either true or false.
• Before inserting a value into the column(to which the check
constraint is attached),the oracle evaluates the Boolean
condition.
• If the condition is evaluated to true then the value is accepted
by the database.
• If the condition is evaluated to false then the value is rejected
by the database and an error message is displayed
• Syntax:
• Columnname datatype(size) check (boolean
expression)
• Eg:suppose that in the employee table the age of
the employee must be between 22 and 30
• Create table employee(emp_id char(10) primary
key,ename varchar(20),dept_name varchar(20)
check (dept_name in (‘cse’,’ece’,’me’)),age
number(10) check(age between 22 and 30));
• Q: Which of the following is not a class of constraint
in SQL Server?
a) NOT NULL
b) CHECK
c) NULL
d) UNIQUE
• Which of the constraint can be enforced one per
table?
a) Primary key constraint
b) Not Null constraint
c) Foreign Key constraint
d) Check constraint
• How can a SQL developer add a key on a table?
a) While creating a table
b) With Alter table command
c) both A and B
Key
• A key in DBMS is an attribute or a set of attributes that help to uniquely
identify a tuple (or row) in a relation (or table).
• Keys are also used to establish relationships between the different tables
and columns of a relational database.
• Individual values in a key are called key values.
Keys
• A key could either be a combination of more than one attribute (or columns) or
just a single attribute. The main motive of this is to give each record a unique
identity.
There are broadly seven types of keys in DBMS:
1.Super Key
2.Candidate Key
3.Primary Key
4.Composite Key
5.Alternative Key
6.Artificial Key
7.Foreign Key
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 that are capable of
identifying the other columns of that table uniquely will all be
considered super keys.
Super Key
• The Primary Key of a table is selected from one of the candidate keys.
• Candidate keys are defined as the set of fields from which the
primary key can be selected.
• It is an attribute or set of attributes that can act as a primary key for a
table to uniquely identify each record in that table.
Candidate Key
• A table can have multiple choices for a primary key; however, it can
choose only one.
• So, all the keys which did not become the primary Key are called
alternate keys.
• (The candidate key which are not selected for primary key are known
as secondary keys or alternative keys)
• Alternate Key can be any of the Candidate Keys except for the Primary
Key.
• E.g. of Alternate Key is “Name, Address” as it is the only other
Candidate Key that is not a Primary Key.
Foreign Key
• Usually a foreign key in one table refers to the primary key (PK) of
another table. This way references can be made to link information
together and it is an essential part of database normalization
• The purpose of the foreign key is to ensure referential integrity of the
data i.e. only values that are supposed to appear in the database are
permitted
Foreign Key
Let consider
Department(Department_ID, Department_Name, Manager_ID,
Location_ID) with Department_ID as an Primary Key.
Employee (Emp_id, Emp_name, Department_ID)
can be defined as the Foreign Key as it can refer to the Department_ID
attribute of the Departments table (the referenced or parent table), a
Foreign Key value must match an existing value in the parent table or
be NULL.
Composite Key
Maximum salary in
EMPLOYEES table
…
Types of Group Functions
– AVG
– COUNT
– MAX
Group
– MIN functions
– SUM
cust_details table
orders table
cid Cname addres oid oamount cid
s
1 Ram Delhi 1 55 1
2 Sham Chd 2 80 2
4 Amit Delhi 4 90 3
Inner Join
• Select records that has matching values in both tables
• Inner join will show common records
Query:
Display cname, order amount of every customer (which
customer has placed order of what amount)?
select cid, cname, oamount from cust_details JOIN
orders ON cust_details.cid = orders.cid
Query:
Find all the customers with their order details.
select cust_details.cid, cname, oamount from
cust_details LEFT JOIN orders ON cust_details.cid =
orders.cid
Right Outer Join
select cname, email from cust_details right join orders on
cust_details.cid = orders.cid
Query:
Find all the orders with their customer details.
select cust_details.cid, cname, oamount from
cust_details RIGHT JOIN orders ON cust_details.cid =
orders.cid
1.Cross JOIN or Cartesian Product
• This type of JOIN returns the cartesian product of
rows from the tables in Join.
• It will return a table which consists of records which
combines each row from the first table with each row
of the second table.
• Cross JOIN Syntax is:
• SELECT column-name-list from table-name1 CROSS
JOIN table-name2;
• SELECT * from class cross JOIN class_info;
Example of Cross JOIN
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
4 alex 3 CHENNAI
Cross JOIN query will be,
• SELECT * from class cross JOIN class_info;
2.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 query.
Inner Join Syntax is,
• SELECT <column-name-list> from <table-name1> INNER JOIN
<table-name2> WHERE <table-name1.column-name> =
<table-name2.column-name>;
• SELECT * from class, class_info where class.id = class_info.id;
• The result table will look like,
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
3.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.
Natural Join Syntax is:
SELECT * from table-name1 NATURAL JOIN table-
name2;
• SELECT * from class NATURAL JOIN class_info;
Example of Natural JOIN
1 abhi
1 DELHI
2 adam
2 MUMBAI
3 alex
3 CHENNAI
4 anu
• Natural join query will be,
• SELECT * from class NATURAL JOIN class_info;
• The result table will look like,
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
Maximum salary in
EMPLOYEES table
…
Types of Group Functions
– AVG
– COUNT
– MAX
Group
– MIN functions
– SUM
SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Example
Stu_marks(stu_ID, marks)
Stu_details(student_ID, Name)
Example
By:
Navjot Kaur
System Programming Domain
Functional Dependency
Functional Dependency
X → Y
The left side of FD is known as a determinant, the right side of
the production is known as a dependent.
Functional Dependency
Assume we have an employee table with attributes: Emp_Id,
Emp_Name, Emp_Address.
Eid→ dept
Ename→dept
Functional Dependency
1. Multivalued dependency
• Multivalued dependency occurs in the situation where there
are multiple independent multivalued attributes in a single
table.
• A multivalued dependency is a complete constraint between
two sets of attributes in a relation.
• It requires that certain tuples be present in a relation.
Types of Functional Dependency
1. Multivalued dependency
1. Multivalued dependency
Step-02:
Recursively add the attributes to the result set which can be
functionally determined from the attributes already contained
in the result set.
Closure helps to find all possible candidate keys in a
relation.
Closure: Example
1. R(ABCD)
FD {A→ B, B→ C, C→ D}
A +: means what A can determine
= {A B C D} A will determine itself because of reflexive rule
(self loop)
C+ = {CD} not a CK
D+= {D} not a CK
Only A is Candidate Key
Closure: Example
1. R(ABCD)
FD {A→ B, B→ C, C→ D}
Prime Attribute: which is used in making the candidate key
Prime Attribute = {A}
Non Prime: {BCD}
Closure: Example
2. R(ABCD)
FD { A→B, B→C, C→D, D→A)
Closure: Example
3. R(A,B,C,D,E)
FD : {A→B, B→C, C→D, D→E}
Find closure of A,B,C,D,E, AD+, CD+
Closure
Example-
For example, when we try to update one data item having its
copies scattered over several places, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
2. Deletion anomalies − We tried to delete a record that is
unwanted but it leads to deletion of the data that us wanted.
Example: we wanted to delete details of student but did not
want to delete branch information. Now because branch info.
Is not stored separately so it will also be deleted.
Insertion anomaly:
For Example, If we try to delete a record from STUDENT with STUD_NO =1.
Anomalies
1. Update anomalies − If data items are scattered and are not
linked to each other properly, then it could lead to strange
situations.
For example, when we try to update one data item having its
copies scattered over several places, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
2. Deletion anomalies − We tried to delete a record, but parts
of it was left undeleted because of unawareness, the data is
also saved somewhere else.
Insertion anomaly:
For Example, If we try to delete a record from STUDENT with STUD_NO =1.
Closure
Step-01: Add the attributes contained in the attribute set for
which closure is being calculated to the result set.
Step-02:
Recursively add the attributes to the result set which can be
functionally determined from the attributes already contained
in the result set.
Closure
Example-
Consider a relation R ( A , B , C , D , E , F , G ) with the functional
dependencies-
A → BC
BC → DE
D→F
CF → G
Closure
Closure of attribute A-
A+ = { A }
= { A , B , C } ( Using A → BC )
= { A , B , C , D , E } ( Using BC → DE )
= { A , B , C , D , E , F } ( Using D → F )
= { A , B , C , D , E , F , G } ( Using CF → G )
Thus,
A+ = { A , B , C , D , E , F , G }
Closure
Closure of attribute D-
D+ = { D }
= { D , F } ( Using D → F )
We can not determine any other attribute using attributes D and F
contained in the result set.
Thus,
D+ = { D , F }
Closure
Closure of attribute set {B, C}-
{ B , C }+= { B , C }
= { B , C , D , E } ( Using BC → DE )
= { B , C , D , E , F } ( Using D → F )
= { B , C , D , E , F , G } ( Using CF → G )
Thus,
{ B , C }+ = { B , C , D , E , F , G }
Functional Dependency
Functional Dependency
X → Y
The left side of FD is known as a determinant, the right side of
the production is known as a dependent.
Functional Dependency
Assume we have an employee table with attributes: Emp_Id,
Emp_Name, Emp_Address.
Eid→ dept
Ename→dept
Normalization
What is it and what is need of normalization:
Process of organizing data in DB.
1. Minimize redundancy in relation
2. Divide big table into small tables
Need
To remove anomolies
Normalization
• If a database design is not perfect, it may contain
anomalies, which are like a bad dream for any database
administrator. Managing a database with anomalies is next
to impossible.
Problems Without Normalization
If a table is not properly normalized and have data redundancy
then it will not only consume extra memory space but will also
make it difficult to handle and update the database, without
facing data loss.
In this table, a new branch civil cant be added until you have the sid
of the student who took admission in that branch civil. You cant
enter data without sid because entire branch data is kept in a single
table.
Anomalies
2. Update anomalies − If data items are scattered and are not
linked to each other properly, then it could lead to strange
situations.
For example, when we try to update one data item having its
copies scattered over several tables, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
3. Deletion anomalies − We tried to delete a record that is
unwanted but it leads to deletion of the data that we wanted.
Example: we wanted to delete details of student but did not
want to delete branch information. Now because branch info.
Is not stored separately so it will also be deleted.
Anomalies
Insertion anomaly:
For Example, If we try to delete a record from STUDENT with STUD_NO =1.
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.
Normalization Types
Normalization Types
First Normal Form (1NF)
1 10 1 82 A
2 10 2 87 B
3 11 1 86 C
4 11 2 85 D
5 11 4 80 E
Example2: check whether this Relation is in 2NF or
not? scor stud subj Mar Teac
eid entid ectid ks her
1. Display teacher of student with sid=10
1 10 1 82 A
2. Display teacher for subjectid=1 2 10 2 87 B
3 11 1 86 C
(studentid,subjectid) → teacher
4 11 2 85 D
To know the teacher, subject id is enough. 5 11 4 80 E
Subject:
subjectid subjectname teacher
Final tables:
Student: studentid name regno branch address
Score table
scoreid studentid subjectid marks
subject
subjectid subjectname teacher
3NF: No transitive dependency
When a non prime attribute in a table depends upon
other non prime attribute.
Consider score table with additional 2 attributes:
scoreid studentid subjectid marks examname Total marks
A→B
A: non prime and B: prime :{not allowed in BCNF}
BCNF: 3.5NF
studentid subject professor
101 Java P.Java
101 C P.C
102 Java P.Java
103 C++ P.C++
104 java P.Java
Ternary relationship
5NF:
2. Customer buying from supplier and supplier can have one or
more products.
5NF:
3. Product used by customer can be supplied by one or more
suppliers
5NF
Binary Relationships can be:
Supplier-customer
Customer-product
Product-supplier
5NF
5NF: final tables
Supplier product customer
Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
Boyce Codd normal form (BCNF)
•For BCNF, the table should be in 3NF, and for every FD, LHS is super
key.
Candidate key: {EMP-ID, EMP-DEPT}
Fourth normal form (4NF)
So to make the above table into 4NF, we can decompose it into two
tables:
Normalization