0% found this document useful (0 votes)
8 views139 pages

Dbms Com Notes 2080

The document provides an introduction to Database Management Systems (DBMS), defining key terms such as data, information, database, and DBMS, along with their examples. It discusses the advantages and disadvantages of file systems and DBMS, outlines various applications of DBMS, and explains different types of database users and architectures. Additionally, it covers database models, schemas, and differences between DBMS and file systems, as well as between two-tier and three-tier architectures.

Uploaded by

fhyajshekh
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)
8 views139 pages

Dbms Com Notes 2080

The document provides an introduction to Database Management Systems (DBMS), defining key terms such as data, information, database, and DBMS, along with their examples. It discusses the advantages and disadvantages of file systems and DBMS, outlines various applications of DBMS, and explains different types of database users and architectures. Additionally, it covers database models, schemas, and differences between DBMS and file systems, as well as between two-tier and three-tier architectures.

Uploaded by

fhyajshekh
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/ 139

Next Chapter

UNIT-1:Introduction
to DataBase System
Database Management System
1. Define the following terms with example:
i. Data iii. Database
ii. Information iv. DBMS
Ans: - The above terms are defined bellow respectively: -

 Data: - Data are raw facts that can be recorded and have implicit meanings. Data helps create fact-
based information. For example: - If you have data on the grades achieved by all student, you can infer
the top performers and average grades.
 Information: - Data that has been transformed into a more useful or intelligent format is known as
information.
For example: - Report card sheet.
The information is needed for the following reasons:
 To gain knowledge about the surroundings.
 To keep the system up to date.
 To know about the rules and regulation of the society.
 Database: - A database is the collection of related persistent data that contains information relevant to
an enterprise. A data is called the repository or container for a collection of data files. For example: -
A school database that maintains information about the school’s students, courses and grades.
 DBMS (Database management system): - Data management includes a way to store an organize data
as well as a way to manipulate that data. The main purpose of data base management system is to
manage vast amount of data. E.g. Oracle, SQL server, MySQL and MS Access.
2. What is file system? Write down the advantage and disadvantage of file system.
Ans: - A file system is a simple way of grouping files on a hard drive or other storage media. A file system
helps you defines your files and retrieve them when you need them. Various files organized in directories
form a file system. The directory has other folders and files. Basic functions such as administration, file
naming, and granting access rules. Performed by the file system.

 Advantage of file system:


a. Backup: Backups of database stored in files on computerized systems can be performed more
quickly and automatically.
b. Compactness: data can be stored compactly.
c. Data Retrieval: computerized systems offer improved data retrieval techniques too easily and
efficiently retrieve data stored in files.
d. Editing: Information Saved as files on your computer can be easily edited.
e. Remote access: Computer-based system allow remote access to data.

Grade: -10 1
Database Management System
f. Sharing: Data stored in files on computer-based system can be shared simultaneously by
multiple users.

 Dis-advantage of file system:


a. Data redundancy: The same information can be duplicated in different files.
b. Data inconsistency: Because of data redundancy, it is possible that data may not be in
consistent state.
c. Difficulty in accessing data: Accessing data in file-handling systems is neither convenient
nor efficient.
d. Limited data sharing: The data is scattered across various file.
e. Integrity problems: Data integrity means that the data contained in the database must be
correct and consistent.
f. Atomicity problems: All operations in the database must be atomic. In other words, it must
be all or nothing.
3. What is DBMS? Write down the advantage and disadvantage of DBMS.
Ans: - The program that manage all database access is known as a database management system (DBMS).
E.g. Oracle, SQL server, MySQL and MS Access.

 Advantages: -
 Significantly strengthened data exchange
 Evolved data protection
 Data integrity is maintained
 Better decision-making
 Data is backed up
 Disadvantage: -
 Hardware and software expenditures
 Scale and complexity of management
 Massive dimensions
 Failure has a significant impact
 Frequent requirements for updates
4. Explain the application of DBMS.
Ans: - Some the main applications of DBMS are given and explained below: -

a. Banking: - To store information about customers, their account number, balance, etc.
b. Airlines: - For reservations and schedule information.

Grade: -10 2
Database Management System
c. Telecommunication: - To keep records of customers call, balance left, generating monthly bills, etc.
d. Universities: - To keep records of students, courses, marks of students, etc.
e. Sales: - To keep information of customers, products list, purchase information, etc.
f. Manufacturing: - To store orders, cracking productions of items, etc.
g. Human resources: - To keep records of employee, their salary, bonus, etc.
5. Explain the types of database users.
Ans: - There are mainly 4 types of database users. They are: -

i. Naive users/parametric users: - A naive user is a user who just usage a previously created
application. For example: A customer using an ATM can simply call a program that checks
their username, password, account balance, and ultimately withdraws a certain amount from
their account.
ii. Application programmer users: - These are computer professionals who write application
programs. It builds a user interface for interacting with the database, so even beginners can
easily operate it. Application programmers use many application development tools for rapid
application development.
iii. Sophisticated users: - Experienced users never interact with databases through application
programmers. Because such users are interested and create their own queries in the database
her query language. Analysts submit queries to examine data in the database and fall into this
category.
iv. Specialized users: - Professional users are demanding users. Such users create very
sophisticated and specialized data base application. Scientist and researchers fall into this
category.
6. Explain DBMS (Database management system) architecture with figure and example.
Ans: - The design of a DBMS depends on its architecture is used to handle large number of networked pcs,
web server database server and other components.

Types of Architecture: -

Grade: -10 3
Database Management System

Fig: - Types of Architecture


i) Single (one) Tier Architecture: - The simplest DBMS architecture is a single-tier design. On
a single server or platform, all DBMS components are located. Direct access to the database is
available to end users. This direct connection allows the DBMS to provide fast response. This
is why programmers often use this architecture to extend their local application. In this
structure, all changes made by clients are reflected directly in the database, and all processing
is done on a single server.

Fig: - One tier architecture

ii) Two Tier Architecture: - A two tier DBMS architecture is similar to the basic concept of a
client server architecture. In a two tier structure the server provides database functionality and
allows client to perform operation on the database over a direct internet connection via an API
(Application program interface). For example: ODBC (Open Database connectivity), JDBC
(Java Database Connection).

Grade: -10 4
Database Management System
Fig: - Two tier architecture

iii) Three Tier Architecture: - The Three tier DBMS architecture, the most widely used DBMS
architecture, adds another layer, called the middle tier or application tier, between the server
(database tier) and the client system (presentation tier) to handle query processing in the server,
handle the load. Reduce servers. This application, or middle tier, presents an abstract view of
the database to the end user. All user requests are handled by the application layer as there is
no direct connection between client and server.

Fig: - Three tier architecture

7. What is database model and describe its type.


Ans: - A particular kind of data model is a database model. A database model may contain entities, their
relationships, data flow, tables, and more, depending on the model being used. For instance, in a hierarchal
database mode, the data model arranges data into parent and child segments of a tree-like structure.

 There are four common database modules that serve different type of data and information.
a) Hierarchical Databases: - A hierarchical model represents data through a set of records, which
are organized in a hierarchical or ordered structure, and a database is a collection of such disjoint
trees.
Example: The above network model can be expressed in hierarchical model

Fig: - Hierarchical Databases

Grade: -10 5
Database Management System

b) Network Database: - In a network model, data is represented by a set of records, and


relationships between data are represented by links.
Example: The above hierarchical relational model can be expressed in network model as
follows

Fig: - Network Database


c) Relational Database: - A relational model describes database design through a collection of
tables (relationships). It represents both the data and the relationships between that data. Each
table consists of a number of columns (attributes) with unique names. This is the most widely
used data model. The relational model is a lower level abstraction than the E-R model. Database
models are often run in E-R models and converted to relational mode.
Three key terms are commonly used in relational models: relationships, attributes, and
domains. A relation is just a table with rows and columns. A named column in a relation
is called an attribute, and after all a domain is just the set of values an attribute can take.
The following diagram provides an overview of the relational database model.

Fig: - Relational Database

Grade: -10 6
Database Management System
d) Object-oriented database: - A combination of relational databases and object-oriented
programming makes up an object-oriented database. There are many things made with object-
oriented programming languages like C++ and Java that can be saved in relational databases, but
those things are better suited for object-oriented databases.
Example,
Consider an object that represents a bank account. The object can contain the instance
variables account number and balance. Objects can contain interest payment methods that
add interest to the balance. Unlike entities in the E-R model, each object has a unique ID.
It is irrelevant to the value it contains. Two objects with the same value are different. This
distinction is maintained at the physical layer by assigning unique object identifiers.

8. Explain database schema.


Ans: - A database schema defines how data is organized in a relational database. This includes logical
constraints such as table names, fields, data types, and relationships between these entities. Schemas often use
a visual representation to convey the architecture of a database and form the basis of an organization's data
management conventions. This process s of database schema design is also called data modeling.

 Although the term schema is widely used, it generally refers to three types of schema: conceptual
database schema, logical database schema, and physical database schema.
 A conceptual schema provides a complete picture of what the system contains, how it is organized,
and what business rules are involved. A conceptual model is typically created as part of the process of
gathering initial project requirements.
 Logical database schemas are less abstract than conceptual schemas. Uniquely defines schema objects
with information such as table names, field names, entity relationships, and constraints, all rules
governing the database. However, they usually do not contain technical requirements.
 Physical database schemas provide the technical information that the logical database schema type
lacks in addition to the contextual information, such as table names, field names, entity relationships,
et cetera. That is, it also includes the syntax that will be used to create these data structures within disk
storage.
9. Difference between DBMS and file system.
Ans: - Difference between DBMS and file system are given below:-
DBMS File System
DBMS is a collection of data. In DBMS, the File system is a collection of data. In this
user is not required to write the procedures. system, the user has to write the procedures for
managing the data.

Grade: -10 7
Database Management System
DBMS gives an abstract view of data that File system provides the detail of the data
hides the details. representation and storage of data
DBMS provides a crash recovery mechanism, File system doesn't have a crash mechanism,
i.e. DBMS protects the user from the system i.e., if the system crashes while entering some
failure. data, then the content of the file will lost.
DBMS provides a good protection mechanism. It is very difficult to protect a file under the file
system.
DBMS contains a wide variety of sophisticated File system can't efficiently store and retrieve
techniques to store and retrieve the data the data.
Redundancy is control in DBMS. Redundancy not control in file system.
DBMS provide backup and recovery. Data lost in file system can't be recovered.

10. Difference between two tier and three tier architecture.


Ans: - Difference between two tier and three tier architecture are given below: -
Parameter Two-tier Three-tier
Meaning and Purpose The two-tier DB architecture The three-tier DB architecture
is a client- server architecture. is a type of web-based
application.
Number of Layers It contains mainly two layers- It mainly contains three
It mainly the Data Tier layers- O, the Data Layer, the
(Database Tier), the Data and Business Layer, and the Client
the Client Tier. Layer.
Location of Application A A two-tier DB architecture A three-tier DB architecture
two- Logic either buries the application buries the process or
logic within the server application logic in the
database, on the in the m client middle-tier. Thus, it acts as a
(inside the UI), or both of separate entity from the
them. Client/ User Interface and the
data Interface.
Building and Maintenance A two-tier DB architecture A A three-tier DB architecture
is comparatively much easier buries the process or
to maintain as well as build. application logic in the
middle-tier. Thus, it acts as a

Grade: -10 8
Database Management System
separate entity from the
Client/ User Interface and the
data Interface.
Speed of Operation It runs at a comparatively It runs and works at a
slower pace. comparatively faster pace.
Security The two-tier DB architecture The three-tier DB architecture
allows the client to does not allow its clients and
communicate directly with the database to communicate
database- thus making it less directly- thus making it
secure. comparatively secure in the
long run.
Loss of Performance The two-tier DB architecture The three-tier DB architecture
leads to a performance loss leads to a performance loss
with an increase in the number when we happen to run a
of users. system over the Internet. Yet,
Management System that one it exhibits an overall better
can create with the MS- performance as compared to
Access, etc. its two-tier counterpart.
Examples A few examples of the two- A few examples of the three-
tier database architecture are tier database architecture are a
the Railway Reservation website on the Internet, the
System, Contact. process of designing
registration forms with
buttons, labels, text boxes, and
many more.

11. What is data Administrators?


Ans: - The process who had control over both data and the program that access those data are called
administrators.

THE END

Grade: -10 9
Next Chapter
UNIT-2:Entity
Relationship
Mode(ER-Model)
Unit-1: Introduction to Database System
Questions.
1.Define the following terms with an example.
a.Data b.Information c.Database d. DBMS
2.What is File System ? Write down the advantages and disadvantages of of File System. (VVI)
3.What is DBMS ? Write down the advantages and disadvantages of DBMS.
4.Explain the applications of DBMS.
5.Explain the types of Database Users.
6.Explain DBMS (database) architecture with figures and examples. (VVI)
7.What is database model and describe its types.
8.Explain database Schema.
9.Differentiate between DBMS and File System.
10.Differentiate between two-tier and three -tier database architecture.
Unit-2: Entity Relationship Model(ER-Model)
Questions.
1.What do you mean by ER-Model ? Explain the different components of ER-Model.
2.Define entity and entity set .Explain the different types of entity set.
3.What is Attribute ? Explain the types of attributes in ER-Model.
4.What is keys in DBMS ? Explain the different types of keys in DBMS.
5.What do you mean by relationship ? Explain the types of relationship in dbms.
6.What is mapping cardinalities ? Explain the types of cardianlities.

concept of ER-Model
What is ER-Model
 Definition :An Entity–relationship model (ER model) describes the structure of a
database with the help of a diagram, which is known as Entity Relationship Diagram (ER
Diagram).

 The main components of E-R model are: entity set,Attributes and relationship set.

A simple ER Diagram:
 In the following diagram we have two entities -Student and College and their

relationship.

 The relationship between Student and College is many to one as a college can

have many students however a student cannot study in multiple colleges at the
same time.

 Student entity has attributes such as Stu_Id, Stu_Name & Stu_Addr and

College entity has attributes such as Col_ID & Col_Name.


Converting ER-diagram into table
Table:
Student- Entity Type

Roll_no Name Sex

Attributes

Schema : Student ( Roll_no , Name , Sex)


1.What do you mean by ER-Model ? List out the different components of ER-Model with symbols.
An Entity–relationship model (ER model) describes the structure of a database with the help of a diagram,
which is known as Entity Relationship Diagram (ER Diagram).
The components of ER model are :
2.Define entity and entity set .Explain the different types of entity set or type.
 An entity may be any object, class, person or place. An entity is a real-world thing which can be
distinctly identified like a person, place or a concept. It is an object which is distinguishable from
others. If we cannot distinguish it from others then it is an object but not an entity.
Entity Type
 The entity type is a collection of the entity having similar attributes.
 So, an entity type in an ER diagram is defined by a table name(here, STUDENT) and a set of
attributes(here, Roll_no, Student_name, Age, Mobile_no).

 We use a rectangle to represent an entity type in the E-R diagram, not entity.

Types of Entity type(set)


1.Strong Entity Type or set
2.Weak Entity Type or set
Strong Entity Type:
 An entity type which has a primary key attribute is known as strong entity type.
 In the ER-diagram It is represented by a rectangle.
 This type of entity has a primary key attribute which uniquely identifies each record in atable.
strong entity type or set
1. Weak Entity Type:
 Definition : An entity type that does not have primay key attribute and depends on
another strong entity type via foreign key attribute,is called Weak Entity Type.
 . Weak entity type can't be identified on its own. It depends upon some other strong entity
for its distinct identity.

Weak Entity Type

 A weak entity is represented by a double outlined rectangle.

Example- If we have two tables of Customer(Customer_id, Name, Mobile_no, Age, Gender) and
Address(Locality, Town, State, Customer_id). Here we cannot identify the address uniquely as
there can be many customers from the same locality. So, for this, we need an attribute of Strong
Entity Type i.e ‘Customer’ here to uniquely identify entities of 'Address' Entity Type.

Entity Set
 Definition : Entity Set is a collection of entities of the same entity type.
 We can say that entity type is a superset of the entity set as all the entities are
included in the entity type.
 Let's try to understand this with the help of an example.
 Example 1: In the below example, two entities E1 (2, Angel, 19, 8709054568) and E2(4,
Analisa, 21, 9847852156) form an entity set.
3.What is Attribute ? Explain the types of attributes in ER-Model.

 Definition : An attribute in an Entity-Relationship Model describes the properties or


characteristics of an entity.
 It is represented by an oval or ellipse shape in the ER diagram. Every oval shape
represents one attribute and is directly connected to its entity which is in the rectangle in
shape.

Attribute

Types of Attributes

 In ER diagram, attributes associated with an entity set may be of the following types-

1. Simple attributes
2. Composite attributes
3. Single valued attributes
4. Multi valued attributes
5. Derived attributes
6. Key attributes
1.Simple Attribute

Definition : An attribute which contains an atomic(single) value and cannot be divided further is
called a simple attribute.

Example-

2.Composite attribute:

 Definition : An attribute which is a combination of two or more simple attributes is


called a composite attribute.

 In an Entity-Relationship diagram, it is represented by an ellipse, and that ellipse


comprises of other ellipses.
Example-

Here, the attributes “Name” and “Address” are composite attributes as they are
composed of many other simple attributes.
3.Single Valued Attributes-

 Single valued attributes are those attributes which can take only one value for a given
entity from an entity set.
Example- Here, all the attributes are single valued attributes as they can take only one specific
value for each entity.
4.Multi Valued Attributes-

 Definition : Multi valued attributes are those attributes which can take more than one
value for a given entity from an entity set.
 Definition : An attribute which contains more than one value for a given entity.
 In an entity-relationship diagram, these attributes are represented by a double

oval shape.

Multivalued attribute

Example- Here, the attributes “Mob_no” and “Email_id” are multi valued attributes as they cantake
more than one values for a given entity.

5.Derived attribute:

 Definition : An attribute which can be derived from other attribute(s) is called a derived
attribute.

 In an entity-relationship diagram, these attributes are represented by a dashed


oval shape.

For example, Age is a derived attribute as it changes over time and can be derived from another
attribute DOB (Date of birth).

Here, the attribute “Age” is a derived attribute as it can be derived from the
attribute “DOB”.

6.Key attribute:

 Definition : An attribute which can uniquely identify an entity in an entity set is called a
key attribute. It represents a primary key in the ER diagram.

 In an Entity-Relationship diagram, the key attribute is denoted by an oval with an


underlying line.

Here, the attribute “Roll_no” is a key attribute as it can


identify anystudent uniquely.
4.What is keys in DBMS ? Explain the different types of keys in DBMS.

Definition : KEYS in DBMS is an attribute or set of attributes which helps you to


uniquely identify a row(tuple) in a relation(table).

 They allow you to find the relation between two tables. Keys help you uniquely identify a
row in a table by a combination of one or more columns in that table.

Types of Keys
 Generally,There are four different types of key:

1. Primary Key 2. candidate Key 3. Super Key 4. Forign Key


1.Super Key
 Definition : Super Key is defined as a set of attributes within a table that can uniquely
identify each record within a table.
 Super Key is a superset of Candidate key.

 In the table defined above super key would include student_id, (student_id,
name), phone etc.

 Confused? The first one is pretty simple as student_id is unique for every row of data,
hence it can be used to identity each row uniquely.Next comes, (student_id, name), now
name of two students can be same, but their student_id can't be same hence this
combination can also be a key.Similarly, phone number for every student will be unique,
hence again, phone can also be a key.So they all are super keys.

2.Candidate Key
 Candidate keys are defined as the minimal set of fields which can uniquely identify each
record in a table.

 It is an attribute or a set of attributes that can act as a Primary Key for a table to uniquely
identify each record in that table. There can be more than one candidate key.
 In our example, student_id and phone both are candidate keys for table Student.
 A candiate key can never be NULL or empty. And its value should be unique.
Properties of Candidate key:

a. It must contain unique values


b. Candidate key may have multiple attributes
c. Must not contain null values
d. It should contain minimum fields to ensure uniqueness
e. Uniquely identify each record in a table
3.Primary Key
 Definition : Primary key is a candidate key that is most appropriate to become the main
key for any table. It is a key that can uniquely identify each record in a table.
Rules for defining Primary key:
a. A table cannot have more than one primary key.
b. Two rows can't have the same primary key value
c. It must for every row to have a primary key value.
d. The primary key field cannot be null.
e. The value in a primary key column can never be modified or updated if any foreign key
refers to that primary key.
5.Composite Key

 Definition :Key that consists of two or more attributes that uniquely identify any record
in a table is called Composite key.
 But the attributes which together form the Composite key are not a key independentely or
individually.

 In the above picture we have a Score table which stores the marks scored by a student in
a particular subject.
 In this table student_id and subject_id together will form the primary key, hence it is a
composite key.
6.Foreign Key
 Definition : Foreign keys are the attributes in a table ,whose value match as primary key
in another table.
 Foreign Key can contain duplicate value
 Also can contain Null Value.

Foreign Key Primary Key

EMPLOYEE DEPARTMENT
Dep.No Dep.Name Dep.Loc
E.No E.Name Salary Dep.No
D1 Sales Delhi
1 Rahul 5000 D1
D2 Purchase Bangalore
2 Roshan 10000 D2
3 Hari 15000
4 Bikash 5000 D4
5. Explain different types of relationship with examples.
Ans: The logical representation among entities is called relationship. There are four different types of
relationship namely,
1. One to One
2. One to Many
3. Many to One
4. Many to Many

1. One to One Relationship


Definition : When a single instance of an entity is associated with a single instance of another entity
then it is called one to one relationship.
For example, a person has only one passport and a passport is given to one person.

Example 2: one student can enroll only for one course and a course will also have only one Student.

2. One to Many Relationship


Definition : When a single instance of an entity is associated with more than one instances of another
entity then it is called one to many relationship.
For example 1 – a customer can place many orders but a order cannot be placed by many customers.

pg. 5
Database Management System-2080
System

Sub: -DBMS Unit-2


Unit
Assignment

Example 2: one student can enroll to many courses

3. Many to One Relationship


Definition : When more than one instances of an entity is associated with a single instance of
anotherentity then it is called many to one relationship.
Example 1:many students can study in a single college but a student cannot study in many colleges at
the sametime.

Example 2: Student enrolls for only one Course but a Course can have many Students

4. Many to Many Relationship


When more than one instances of an entity is associated with more than one instances of another
entity then it is called many to many relationship.
Example 1, a can be assigned to many projects and a project can be assigned to many
students.

2080 Questions Page 19


Database Management System-2080
System

Example 2: many students can enroll for more than one courses.

6. What is Mapping Cardinalities ? Discuss with an example

Definition : Cardinality defines the number of entities in one entity set, which can be
associatedwith the number of entities of other set via relationship set.

For binary relationship set R on an entity set A and B, there are four possible
mappingcardinalities. These are as follows:

1. One to one (1:1)


2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)

1. One-to-one − By this cardinality constraint, One entity from entity set A can be associated
with at most one entity of entity set B and vice versa.

2. One-to-many −By
By this cardinality constraint, One entity from entity set A can be associated
with more than one entities of entity set B however an entity from entity set B, can be

2080 Questions Page 20


Database Management System-2080

associated with at most one entity.

3. Many-to-one − By this cardinality constraint, More than one entities from entity set A can be
associated with at most one entity of entity set B, however an entity from entity set B can be
associatedwith more than one entity from entity set A.

4. Many-to-many −By this cardinality constraint, One entity from A can be associated with
more thanone entity from B and vice versa.

2080 Questions Page 21


Unit-5:Relational Database Design

5.1:Functional Dependencies and its types


5.2:Normalization
5.2.1:Definition
5.2.2 : Normal Forms(1NF,2NF,3NF)

Assignments
1.What is functional Dependency ? Explain the different types of functional dependency .
2.Differentiate between trivial and non-trivial functional dependencies.
3.Define Normalization . Write the advantages and disadvantages of Normalization.
4.Explain the different types of normalization.
4.Differentiate between :
a.1NF and 2NF .
b.2NF an d 3NF
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Contents
4. DataBase Design
4.1 Functional Dependencies:
Introduction,
Trivial and Non-trivial Dependencies,
Closure of a Set of Dependencies,
Closure of a Set of Attributes.
4.2 Data Normalization:
Introduction,
Decomposition and
Functional Dependencies INF, 2NF and 3NF

References
https://en.wikipedia.org/wiki/Functional_dependency
https://beginnersbook.com/2015/04/non-trivial-functional-dependency-in-dbms/
https://www.geeksforgeeks.org/functional-dependency-and-attribute-closure/
imp- https://www.minigranth.com/dbms-tutorial/closure-of-functional-
dependency/#:~:text=The%20Closure%20Of%20Functional%20Dependency,%E2%80%9
C%7BF%7D%2B%E2%80%9D.

https://www.gatevidyalay.com/functional-dependency-in-dbms/
https://www.gatevidyalay.com/closure-of-an-attribute-set/
https://www.gatevidyalay.com/database-normalization/

DBMS P a g e 1 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

4.1 Database Design


Introduction
 Database design is the organization of data according to a database model. The
designer determines what data must be stored and how the data elements interrelate.
With this information, they can begin to fit the data to the database model. Database
management system manages the data accordingly.
 Database design involves classifying data and identifying interrelationships. This
theoretical representation of the data is called an ontology. The ontology is the theory
behind the database's design.
 Database designs also include ER (entity-relationship model) diagrams. An ER
diagram is a diagram that helps to design databases in an efficient way.
 while designing a database out of an entity-relationship model, the main problem
existing in that raw database is redundancy. Redundancy is storing the same data item
in more than one place. A redundancy creates several problems like the following:
 Extra storage space: strong the same data in many places takes large amount of disk
pace.
 Entering same data more than once during data insertion.
 Deleting data from more than one place during deletion.
 Modifying data in more than one place.
 Anomalies may occur in the database if insertion, deletion, modification etc. are not
done properly.
 It creates inconsistency and unreliability in the database.

To solve this problem the raw database needs to be normalized. Normalization is


a step by step process of removing different kinds of redundancy and normally at each
step. At each step a specific rule is followed to remove specific kind of impurity in
order to give the database a slim and clean look. The process of reducing data
redundancy and removing database modification anomaly in a relational database
is called normalization.

DBMS P a g e 2 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

4.1 Functiona Dependency


Introduction

 Definition : Functional Dependency in DBMS also known as “FDs” means a


relationship. A relationship which only exists when an attribute can determine other
attribute functionally.
 The first attribute does not computes or calculates the value of second attribute but
searches value of the tuple corresponding to the first attribute and fetches that value of
the attribute.
 Functional Dependency in DBMS is denoted using an arrow between two or more
attributes such as :

FD : A  B
Here, A & B are the attributes present in any relation.
 “A B” means, “B” is functionally dependent upon “A” or “A”
functionally determines “B”. Functional dependency acts as
a constraint between set of attributes present in any database.
 The left side of FD is known as a determinant and the right side is
known as a dependent.

Examples

 Functional Dependency in DBMS and Keys are the most important concepts that are
used as a foundation in database normalization. We will try to explain you with example
how actually functional dependency works.

Example-1 : Consider a table student_details containing details of some students.

Example : student_details Table

DBMS P a g e 3 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

 We can conclude from Roll_No attribute in the table, we are able to determine the
Name of student uniquely and same is the case with marks too. Hence, we can say that
Name and Marks are functionally dependent on Roll_No but the vice versa is not true.
 FD1 : Roll_No  Name
 FD2 : Roll_No Marks

NOTE : In the above scenario, Two Roll_No can have same Name(i.e. Anoop in the
above table as Roll_No-1 and Roll_No-5 but two same Name cannot have same
Roll_No. This is how Functional dependency in DBMS concept works.

Example-2 : Consider the table student_details containing details of some students.

 Here, Name is not functionally dependent upon Roll_No as whenever we will


try to search the value of Name against Roll_No attribute, two different names
will be provided which is practically not possible.
 Hence, Functional Dependency in DBMS concept exists when an attribute is
able to uniquely determine another attribute.

DBMS P a g e 4 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Introduction
 Definition : The attributes of a table are said to be dependent on each other when an
attribute of a table uniquely identifies another attribute of the same table.

Examples:

Example 1:

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.

Example 2:

Assume we have an employee table with attributes: Emp_Id, Emp_Name,


Emp_Address.
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee
table because if we know the Emp_Id, we can tell that employee name associated with
it.
Functional dependency can be written as:
Emp_Id → Emp_Name
We can say that Emp_Name is functionally dependent on Emp_Id.

Formally:

If column A of a table uniquely identifies the column B of same table then it can represented
as A->B (Attribute B is functionally dependent on attribute A)

DBMS P a g e 5 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Introduction
 Definition : In any relation, a functional dependency α → β holds if- Two or more
tuples having same value of attribute α also have same value for attribute β.
Mathematically,
If α and β are the two sets of attributes in a relational table R where-
α⊆R
β⊆R
Then, for a functional dependency to exist from α to β,
If t1[α] = t2[α], then t1[β] = t2[β]

α β

t1[α] t1[β]
fd : α → β
t2[α] t2[β]

……. …….

Example
Table 2:
Table 1:
α β
α β a 1
a 1 b 2
b 2 a 3
a 1 c 4
c 4

In a relational table R1,There exists In a relational table R2, There


functional dependency from α → β doesnot exist functional dependency
because two same values (i.e.a) of α
because two same values (i.e.a) of α
attribute have same values for β
attribute. attribute do not have same values for β
attribute.

DBMS P a g e 6 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Rules for Functional Dependency

Rule-01:
A functional dependency X → Y will always hold if all the values of X are unique (different)
irrespective of the values of Y.
Example
Consider the following table:
A B C D E
5 4 3 2 2
8 5 3 2 1
1 9 3 3 5
4 7 3 3 8
The following functional dependencies will always hold since all the values of attribute ‘A’
are unique-

A→B
A → BC
A → CD
A → BCD
A → DE
A → BCDE
In general, we can say following functional dependency will always hold-

A → Any combination of attributes A, B, C, D, E


Similar will be the case for attributes B and E.

Rule-02:
A functional dependency X → Y will always hold if all the values of Y are same irrespective
of the values of X.
Example
Consider the following table-

DBMS P a g e 7 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

A B C D E
5 4 3 2 2
8 5 3 2 1
1 9 3 3 5
4 7 3 3 8
The following functional dependencies will always hold since all the values of attribute ‘C’ are
same-

A→C

AB → C
ABDE → C

DE → C
AE → C

In general, we can say following functional dependency will always hold true-
Any combination of attributes A, B, C, D, E → C

Combining Rule-01 and Rule-02 we can say-


In general, a functional dependency α → β always holds-
If either all values of α are unique or if all values of β are same or both.
Rule-03:
For a functional dependency X → Y to hold, if two tuples in the table agree on the value of
attribute X, then they must also agree on the value of attribute Y.
Rule-04:
For a functional dependency X → Y, violation will occur only when for two or more same
values of X, the corresponding Y values are different.

DBMS P a g e 8 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Types of Functional Dependencies


 The two types of functional dependencies are:
1. Trivial Functional Dependencies
2. Non-trivial Functional Dependencies

1. Trivial Functional Dependencies

 Definition :A functional dependency X → Y is said to be trivial if and only if Y is a


subset of X (Y ⊆ X).
 Thus, if RHS of a functional dependency is a subset of LHS, then it is called as a
trivial functional dependency.

Note : A subset is a set whose all elements are members of another set. The symbol
"⊆" means "is a subset of". The symbol "⊂" means "is a proper subset of". Since all of
the members of set A are members of set B, A is a subset of B. Symbolically this is
represented as A ⊆ B. Example : A={1,2,3,4,5} B={1,2,3} C={1,2,4} .Here B⊆A but
C⊄A
Trivial means waste or disadvantage.
Non-trivial means useful.

DBMS P a g e 9 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Examples:
The examples of trivial functional dependencies are:
Example 1:
 FD 1 : AB → A
 FD 2 : AB → B
 FD 3: AB → AB

Example 2:
Consider a table with two columns Student_id and Student_Name.
FD 1: {Student_Id, Student_Name} ->Student_Id is a trivial functional
dependency as Student_Id is a subset of {Student_Id, Student_Name}. That makes sense
because if we know the values of Student_Id and Student_Name then the value of Student_Id
can be uniquely determined.
Also, FD 2: Student_Id ->Student_Id &

FD 3: Student_Name ->Student_Name are trivial dependencies too.

Example 3:
Consider a table with two columns Employee_Id and Employee_Name.
FD 1: {Employee_id, Employee_Name} → Employee_Id is a trivial
functional dependency as Employee_Id is a subset of {Employee_Id, Employee_Name}.
Also, FD 2: Employee_Id → Employee_Id and

FD 3: Employee_Name → Employee_Name are


trivial dependencies too.

2. Non-Trivial Functional Dependencies


 A functional dependency X → Y is said to be non-trivial if and only if Y is not a
subset of X (Y ⊄ X).
 Thus, if there exists at least one attribute in the RHS of a functional dependency
that is not a part of LHS, then it is called as a non-trivial functional dependency.

DBMS P a g e 10 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

 Examples:The examples of non-trivial functional dependencies are-


Example 1:
 FD 1: AB → BC
 FD 2: AB → CD
 FD 3: A → D

Example 2:
An employee table with three attributes: emp_id, emp_name,
emp_address. The following functional dependencies are non-trivial:
FD 1: emp_id ->emp_name (emp_name is not a subset of emp_id)
FD 2: emp_id ->emp_address (emp_address is not a subset of emp_id)
On the other hand, the following dependencies are trivial:{emp_id, emp_name}
-> emp_name [emp_name is a subset of {emp_id, emp_name}]

Example 3:
FD 1: ID → Name
FD 2: Name → DOB

DBMS P a g e 11 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Closure of Functiona Dependency


Introduction
 Definition :The Closure of Functional Dependency means the complete set of all
possible attributes that can be functionally derived from given functional dependency
using the inference rules known as Armstrong’s Rules.
 If “F” is a functional dependency then closure of functional dependency can be denoted
using “{F}+”.
 There are three steps to calculate closure of functional dependency. These are:
 Step-1 : Add the attributes which are present on Left Hand Side in the original
functional dependency.
 Step-2 : Now, add the attributes present on the Right Hand Side of the functional
dependency.
 Step-3 : With the help of attributes present on Right Hand Side, check the other
attributes that can be derived from the other given functional dependencies. Repeat
this process until all the possible attributes which can be derived are added in the
closure.
 Seems difficult? Check out the example explained below and it will surely clear your
doubt on how to calculate closure of functional dependency.

Examples
Example-1 : Consider the table student_details having (Roll_No, Name,Marks,
Location) as the attributes and having two functional dependencies.

FD1 : Roll_No  Name, Marks

FD2 : Name  Marks, Location

Now, We will calculate the closure of all the attributes present in the relation using the
three steps mentioned below.

Step-1 : Add attributes present on the LHS of the first functional dependency to the closure.

{Roll_no}+ = {Roll_No}

DBMS P a g e 12 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Step-2 : Add attributes present on the RHS of the original functional dependency to the
closure.

{Roll_no}+ = {Roll_No, Marks}

Step-3 : Add the other possible attributes which can be derived using attributes present on the
RHS of the closure. So Roll_No attribute cannot functionally determine any attribute but Name
attribute can determine other attributes such as Marks and Location using 2 nd Functional
Dependency(Name  Marks, Location).

Therefore, complete closure of Roll_No will be :

{Roll_no}+ = {Roll_No, Marks, Name, Location}

Similarly, we can calculate closure for other attributes too i.e “Name”.

Step-1 : Add attributes present on the LHS of the functional dependency to the closure.

{Name}+ = {Name}

Step-2 : Add the attributes present on the RHS of the functional dependency to the closure.

{Name}+ = {Name, Marks, Location}

Step-3 : Since, we don’t have any functional dependency where “Marks or Location” attribute
is functionally determining any other attribute , we cannot add more attributes to the closure.
Hence complete closure of Name would be :

{Name}+ = {Name, Marks, Location}

NOTE : We don’t have any Functional dependency where marks and location can functionally
determine any attribute. Hence, for those attributes we can only add the attributes themselves
in their closures. Therefore,

{Marks}+ = {Marks}

DBMS P a g e 13 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

and

{Location}+ = { Location}

Example-2 : Consider a relation R(A,B,C,D,E) having below mentioned functional


dependencies.

FD1 : A  BC

FD2 : C B

FD3 : D E

FD4 : E D

Now, we need to calculate the closure of attributes of the relation R. The closures will be:

{A}+ = {A, B, C}
{B}+ = {B}
{C}+ = {B, C}
{D}+ = {D, E}
{E}+ = {E}

Closure Of Functional Dependency : Calculating Candidate Key

“A Candidate Key of a relation is an attribute or set of attributes that can determine the
whole relation or contains all the attributes in its closure."

Let’s try to understand how to calculate candidate keys.

Example-1 : Consider the relation R(A,B,C) with given functional dependencies :

FD1 : A  B

FD2 : B  C

Now, calculating the closure of the attributes as :

{A}+ = {A, B, C}

{B}+ = {B, C}

{C}+ = {C}

DBMS P a g e 14 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Clearly, “A” is the candidate key as, its closure contains all the attributes present in the
relation “R”.

Example-2 : Consider another relation R(A, B, C, D, E) having the Functional


dependencies :

FD1 : A  BC

FD2 : C B

FD3 : D E

FD4 : E  D

Now, calculating the closure of the attributes as :

{A}+ = {A, B, C}

{B}+ = {B}

{C}+ = {C, B}

{D}+ = {E, D}

{E}+ = {E, D}

In this case, a single attribute is unable to determine all the attribute on its own like in
previous example. Here, we need to combine two or more attributes to determine the
candidate keys.

{A, D}+ = {A, B, C, D, E}

{A, E}+ = {A, B, C, D, E}

Hence, "AD" and "AE" are the two possible keys of the given relation “R”. Any other
combination other than these two would have acted as extraneous attributes.

NOTE : Any relation “R” can have either single or multiple candidate keys.

DBMS P a g e 15 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

Closure Of Functional Dependency : Key Definitions

Prime Attributes : Attributes which are indispensable part of candidate keys. For
example : “A, D, E” attributes are prime attributes in above example-2.

Non-Prime Attributes : Attributes other than prime attributes which does not take part
in formation of candidate keys.

Extraneous Attributes : Attributes which does not make any effect on removal from
candidate key.

For example : Consider the relation R(A, B, C, D) with functional dependencies :

FD1 : A  BC

FD2 : B C

FD3 : D C

Here, Candidate key can be “AD” only. Hence,

Prime Attributes : A, D.

Non-Prime Attributes : B, C

Extraneous Attributes : B, C(As if we add any of the to the candidate key, it will remain
unaffected). Those attributes, which if removed does not affect closure of that set.

Closure of an Attribute Set


 The set of all those attributes which can be functionally determined from an attribute
set is called as a closure of that attribute set.
 Closure of attribute set {X} is denoted as {X}+.
Example:Consider a relation R ( A , B , C , D , E , F , G ) with the functional dependencies-

A → BC

DBMS P a g e 16 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

BC → DE
D→F

CF → G

Now, let us find the closure of some attributes and attribute sets-

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 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 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 }
Inference Rule (IR):
 The Armstrong's axioms are the basic inference rule.
 Armstrong's axioms are used to conclude functional dependencies on a relational
database.
 The inference rule is a type of assertion. It can apply to a set of FD(functional
dependency) to derive other FD.

DBMS P a g e 17 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

 Using the inference rule, we can derive additional functional dependency from the
initial set.
 The Functional dependency has 6 types of inference rule:

1. Reflexive Rule (IR1)

 In the reflexive rule, if Y is a subset of X, then X determines Y.

Mathematically, If X ⊇ Y then X → Y

 Example:

X = {a, b, c, d, e}

Y = {a, b, c}

2. Augmentation Rule (IR2)

 The augmentation is also called as a partial dependency. In augmentation, if X


determines Y, then XZ determines YZ for any Z.

Mathematically ,If X → Y then XZ → YZ

 Example:

For R(ABCD), if A → B then AC → BC

3. Transitive Rule (IR3)

 In the transitive rule, if X determines Y and Y determine Z, then X must also determine
Z.

Mathematically,If X → Y and Y → Z then X → Z

4. Union Rule (IR4)

 Union rule says, if X determines Y and X determines Z, then X must also determine Y
and Z.

Mathematically, If X → Y and X → Z then X → YZ

Proof:
DBMS P a g e 18 | 19
LectureNotes Unit-4.1 :Functional Dependencies Er.Sushil Yadav

1. X → Y (given)
2. X → Z (given)
3. X → XY (using IR2 on 1 by augmentation with X. Where XX = X)
4. XY → YZ (using IR2 on 2 by augmentation with Y)
5. X → YZ (using IR3 on 3 and 4)

5. Decomposition Rule (IR5)

 Decomposition rule is also known as project rule. It is the reverse of union rule.
 This Rule says, if X determines Y and Z, then X determines Y and X determines Z
separately.

Mathematically, If X → YZ then X → Y and X → Z

Proof:
1. X → YZ (given)
2. YZ → Y (using IR1 Rule)
3. X → Y (using IR3 on 1 and 2)
6. Pseudo transitive Rule (IR6)

 In Pseudo transitive Rule, if X determines Y and YZ determines W, then XZ determines


W.

Mathematically, If X → Y and YZ → W then XZ → W

Proof:

1. X → Y (given)
2. WY → Z (given)
3. WX → WY (using IR2 on 1 by augmenting with W)
4. WX → Z (using IR3 on 3 and 2)

DBMS P a g e 19 | 19
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Contents
4. DataBase Design
4.1 Functional Dependencies:
Introduction,
Trivial and Non-trivial Dependencies,
Closure of a Set of Dependencies,
Closure of a Set of Attributes.
4.2 Data Normalization:
Introduction,
Decomposition and
Functional Dependencies INF, 2NF and 3NF

References
https://minigranth.in/dbms-tutorial/database-normalization-dbms
https://www.gatevidyalay.com/database-normalization/

DBMS P a g e 1 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

4.2 Normalization
Till now we are familiar with the concept of functional dependency and closure of attributes that
helps in database normalization. What does database normalization means? Let’s discuss.

Introduction

 Definition : “Database Normalization” is a process or technique to reduce the attribute


redundancy and functional dependency within the set of tables present in any database.
 Redundancy needs to be eliminated because of its undesirable ability to generate multiple
issues in the whole database.
 The database normalization concept is implied with the help of normal forms to remove
redundancy from the database.
 The database normalization concept is implied with the help of normal forms to remove
redundancy from the database.
 Redundancy can be a major cause of concern while Inserting, Deleting and Updating
the data in the tables and these issues are commonly known as “Anomalies” i.e.
“Insertion Anomaly, Deletion Anomaly and Updation Anomaly”. “Anomaly” means
“Inconsistency” in data. We will try to discuss all of these in detail below.
 Definition : Normalization is the process of minimizing redundancy from a relation or set
of relations. Redundancy in relation may cause insertion, deletion and updation anomalies.
So, it helps to minimize the redundancy in relations. Normal forms are used to eliminate
or reduce redundancy in database tables.

Database Normalization : Why it is Required?


 Since, databases contains a hell lot of data in the form of tables, it is very difficult or almost
impossible to manage data if any of the anomaly occur i.e. either Insertion, Deletion or
Updation. Hence, removal of redundant data is very necessary.
 Consider the example of a company having table “company_details” of accounts
department.

DBMS P a g e 2 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Table : company_details

 Here in this table Department, Dept_ID and Manager_Name are the attributes which are
same for all the employees. This type of repetition of data is called as redundancy. Let’s
see the anomalies now:
1. Insertion Anomaly :
If we want to add more employees to the above table, with insertion of every new employee
detail attributes such as Department, Dept_ID and Manager_Name will also be
inserted, which will ultimately result in higher memory usage and redundancy in data. This
is called Insertion Anomaly.
2. Deletion Anomaly :
If we want to delete details of an employee with respect to above table, all the other details
such as Department, Dept_ID and Manager_Name will also get deleted. Similarly, if we
need to delete all the other E_ID, all other details will also be deleted and the table will
become blank. This is called as Deletion Anomaly where any change in one attribute
causes the change in whole table.
3. Updation Anomaly :
If the manager of the department gets promoted and new manager comes in place of him,
then we need to update all the entries present in the table. If in case for a particular
employees the manager name is not updated, the whole data will become inconsistent. This
is called as Updation Anomaly.

DBMS P a g e 3 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Anomalies In Database Normalization : Is There Any Solution?

 Yes, there is a solution to above anomalies we has discussed. Without applying any
solution to anomalies, database normalization cannot be achieved. For this, we can split or
decompose the whole Company_Details table into Employee_Details (E_ID, Name,
Dept_ID) and Department_details(Dept_ID, Dept_Name, Manager_Name).

 If in case we want to update the name of manager, we will be required to update value of
just an attribute only once in the Department_Details table.
 By splitting the table into two, it will enable us to remove all the anomalies which will
further result in implementation of database normalization through normal forms.

DBMS P a g e 4 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Types of Normal Forms


 The three types of Normal Forms are given below:

1.First Normal Form (1NF)


2. Second Normal Form (2NF)
3.Third Normal Form (3NF)

Normal Description

Form

1NF A relation is in 1NF if it contains an atomic value.

2NF A relation will be in 2NF if it is in 1NF and no partial dependency exists.

3NF A relation will be in 3NF if it is in 2NF and no transitive dependency exists.

First Normal Form(1NF)


 A given relation is called in First Normal Form (1NF) if each cell of the table contains only
an atomic value.
OR
 A given relation is called in First Normal Form (1NF) if the attribute of every tuple is either
single valued or a null value.

Note : Atomic values are those which cannot be decomposed further.

DBMS P a g e 5 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Example :

The given relation is not in 1NF-

Student_id Name Subjects

100 Akshay Computer Networks, Designing

101 Aman Database Management System

102 Anjali Automata, Compiler Design

Relation is not in First Normal form (1NF)

However,
 This relation can be brought into 1NF.
 This can be done by rewriting the relation such that each cell of the table contains only
one value.

Student_id Name Subjects

100 Akshay Computer Networks

100 Akshay Designing

101 Aman Database Management System

102 Anjali Automata

102 Anjali Compiler Design

Relation is in 1NF
This relation is in First Normal Form (1NF).

NOTE
By default, every relation is in 1NF.

This is because formal definition of a relation states that value of all the attributes must be atomic.

DBMS P a g e 6 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Second Normal Form (2NF)


 A given relation is called in Second Normal Form (2NF) if and only if-
a. Relation already exists in 1NF.
b. No partial dependency exists in the relation.

Partial Dependency
 A partial dependency is a dependency where few attributes of the candidate key
determines non-prime attribute(s).
OR
 A partial dependency is a dependency where a portion of the candidate key or incomplete
candidate key determines non-prime attribute(s).

In other words,
 A → B is called a partial dependency if and only if-
a. A is a subset of some candidate key
b. B is a non-prime attribute.
If any one condition fails, then it will not be a partial dependency.

OR
 Partial Dependency occurs when a non-prime attribute is functionally dependent on part
of a candidate key.
 The 2nd Normal Form (2NF) eliminates the Partial Dependency.

NOTE-
To avoid partial dependency, incomplete candidate key must not determine any non-prime
attribute.
However, incomplete candidate key can determine prime attributes.

DBMS P a g e 7 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Example
<StudentProject>
StudentID ProjectNo StudentName ProjectName

S01 199 Katie Geo Location

S02 120 Ollie Cluster Exploration

 In the above table, we have partial dependency; let us see how −


 The prime key attributes are StudentID and ProjectNo, and

StudentID = Unique ID of the student


StudentName = Name of the student
ProjectNo = Unique ID of the project
ProjectName = Name of the project

 As stated, the non-prime attributes i.e. StudentName and ProjectName should be


functionally dependent on part of a candidate key, to be Partial Dependent.

 The StudentName can be determined by StudentID, which makes the relation Partial
Dependent.

 The ProjectName can be determined by ProjectNo, which makes the relation Partial
Dependent.

 Therefore, the <StudentProject> relation violates the 2NF in Normalization and is


considered a bad database design.

 To remove Partial Dependency and violation on 2NF, decompose the tables −

<StudentInfo>
StudentID ProjectNo StudentName

S01 199 Katie

S02 120 Ollie

DBMS P a g e 8 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

<ProjectInfo>
ProjectNo ProjectName

199 Geo Location

120 Cluster Exploration

Example 2:

 The above table is not in 2NF


 To remove Partial Dependency and violation on 2NF, decompose the tables −

DBMS P a g e 9 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

Now, The above table is in 2NF

Third Normal Form

 A given relation is called in Third Normal Form (3NF) if and only if-
a. Relation already exists in 2NF.
b. No transitive dependency exists for non-prime attributes.

Transitive Dependency
 When an indirect relationship causes functional dependency it is called Transitive
Dependency.
 If P -> Q and Q -> R is true, then P-> R is a transitive dependency.

DBMS P a g e 10 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

 To achieve 3NF, eliminate the Transitive Dependency.


OR
 A → B is called a transitive dependency if and only if-
a. A is not a super key.
b. B is a non-prime attribute.
If any one condition fails, then it is not a transitive dependency.

NOTE-
Transitive dependency must not exist for non-prime attributes.
However, transitive dependency can exist for prime attributes.

Example:
The given relation is not in 3NF

DBMS P a g e 11 | 12
LectureNotes Unit-4.2 : Data Normalization Er.sushil Yadav

 We find that in the above Student_detail relation, Stu_ID is the key and only prime key
attribute. We find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is
a superkey nor is City a prime attribute. Additionally, Stu_ID → Zip→ City, so there exists
transitive dependency.
 To bring this relation into third normal form, we break the relation into two relations as
follows –

DBMS P a g e 12 | 12
Unit-4:SQL

Unit-4:SQL Overview (Structured Query Language)


Contents
4.1:Introduction
4.2:Types of SQL
1.Data Definition Language(DDL)(Commands:CREATE,ALTER,DROP,RENAME)
2.Data Manipulation Language(DML)(Commands:SELECT,INSERT,UPDATE,DELETE)
3.Data Control Language(DCL)(Commands:GRANT,REVOKE)
4.3:SQL Clause(WHERE,AND,OR,WITH,ORDER BY)
4.4:SQL Joins
4.4.1:Inner Join
4.4.3:Natural Join
4.4.2:Left Outer Join
4.4.4:Right Outer Join
4.4.5:Full Outer Join
4.5:SQL view

4.1:Introduction to SQL

Introduction
 Definition:SQL stands for Structured Query Language used for storing and managing
data in Relational Database Management System (RDBMS).
 It is a standard language for Relational Database System. It enables a user to create, read,
update and delete relational databases and tables.
 SQL is the programming language for relational databases like MySQL, Oracle, Sybase,
SQL Server, Postgre, etc.
 Non-relational databases (also called NoSQL) like MongoDB, DynamoDB, etc. do not
use SQL.
 All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as
their standard database language.

DBMS Page 1
Unit-4:SQL

 SQL allows users to query the database in a number of ways, using English-like
statements.

Rules
 SQL follows the following rules:
a. Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
b. Statements of SQL are dependent on text lines. We can use a single SQL
statement on one or multiple text line.
c. Using the SQL statements, you can perform most of the actions in a database.
d. SQL depends on tuple relational calculus and relational algebra.

Characteristics of SQL

a. SQL is easy to learn.


b. SQL is used to access data from relational database management systems.
c. SQL can execute queries against the database.
d. SQL is used to describe the data.
e. SQL is used to define the data in the database and manipulate it when needed.
f. SQL is used to create and drop the database and table.
g. SQL is used to create a view, stored procedure, function in a database.
h. SQL allows users to set permissions on tables, procedures, and views.

Advantages of SQL

 There are the following advantages of SQL:


a. High speeUsing the SQL queries, the user can quickly and efficiently retrieve a large
amount of records from a database.
b. No coding needed:
In the standard SQL, it is very easy to manage the database system. It doesn't require a
substantial amount of code to manage the database system.
c. Well defined standards:
Long established are used by the SQL databases that are being used by ISO and ANSI.
d. Portability:
SQL can be used in laptop, PCs, server and even some mobile phones.

DBMS Page 2
Unit-4:SQL

e. Interactive language:
SQL is a domain language used to communicate with the database. It is also used to
receive answers to the complex questions in seconds.
f. Multiple data view:
Using the SQL language, the users can make different views of the database structure.
SQL Datatype

 SQL Datatype is used to define the values that a column can contain.
 Every column is required to have a name and data type in the database table.

Datatype of SQL:

DBMS Page 3
Unit-4:SQL

1. Binary Datatypes:

 There are Three types of binary Datatypes which are given below:
Data Type Description

binary It has a maximum length of 8000 bytes. It contains fixed-length


binary data.
varbinary It has a maximum length of 8000 bytes. It contains variable-length
binary data.
image It has a maximum length of 2,147,483,647 bytes. It contains
variable-length binary data.

2. Approximate Numeric Datatype :


 The subtypes are given below:
Data Type From To Description

float -1.79E + 308 1.79E + 308 It is used to specify a floating-point


value e.g. 6.2, 2.9 etc.
real -3.40e + 38 3.40E + 38 It specifies a single precision
floating point number
3. Exact Numeric Datatype:
 The subtypes are given below:
Data Type Description
int It is used to specify an integer value.
smallint It is used to specify small integer value.
bit It has the number of bits to store.
decimal It specifies a numeric value that can have a decimal number.
numeric It is used to specify a numeric value.
4. Character String Datatype:
The subtypes are given below:
Data Type Description
char It has a maximum length of 8000 characters. It
contains Fixed-length non-unicode characters.
varchar It has a maximum length of 8000 characters. It

DBMS Page 4
Unit-4:SQL

contains variable-length non-unicode


characters.
text It has a maximum length of 2,147,483,647
characters. It contains variable-length non-
unicode characters.
5. Date and time Datatypes:
The subtypes are given below:
Data Type Description
date It is used to store the year, month, and days
value.
time It is used to store the hour, minute, and second
values.
timestamp It stores the year, month, day, hour, minute,
and the second value.

4.2:SQL Commands
 SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
 SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
Types of SQL(or SQL) Commands
There are five types of SQL commands:

1. Data Definition Language (DDL)


2. Data Manipulation Language (DML)
3. Data Control Language(DCL)
4. Transaction Control Language(TCL)
5. Data Query Language (DQL)

DBMS Page 5
Unit-4:SQL

1. Data Definition Language (DDL)


 DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
 All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
 Here are some commands that come under DDL:
 CREATE
 ALTER
 DROP
 TRUNCATE

DBMS Page 6
Unit-4:SQL

a.CREATE:

 It is used to create a new table in the database.

Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:

CREATE TABLE EMPLOYEE (Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

b. DROP:

 It is used to delete both the structure and record stored in the table.

Syntax:

DROP TABLE TABLE_NAME ;

Example:

DROP TABLE EMPLOYEE;

C. ALTER:
 It is used to alter the structure of the database. This change could be either to modify the
characteristics of an existing attribute or probably to add a new attribute.

Syntax:
To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;

To modify existing column in the table:

ALTER TABLE table_name MODIFY(COLUMN DEFINITION....);

DBMS Page 7
Unit-4:SQL

Example:

ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));

ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

d. TRUNCATE:

It is used to delete all the rows from the table and free the space containing the table.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE EMPLOYEE;

2. Data Manipulation Language:


 DML commands are used to modify the database. It is responsible for all form of changes
in the database.
 The command of DML is not auto-committed that means it can't permanently save all the
changes in the database. They can be rollback.
 Here are some commands that come under DML:
 INSERT
 UPDATE
 DELETE

DBMS Page 8
Unit-4:SQL

a. INSERT:
 The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:

INSERT INTO TABLE_NAME


(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);

Or

INSERT INTO TABLE_NAME


VALUES (value1, value2, value3, .... valueN);

For example:

INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");

b. UPDATE:
 This command is used to update or modify the value of a column in the table.
Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]

For example:

UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'

DBMS Page 9
Unit-4:SQL

C.DELETE:
 It is used to remove one or more row from a table.
Syntax:

DELETE FROM table_name [WHERE condition];

For example:

DELETE FROM javatpoint


WHERE Author="Sonoo";

3. Data Control Language:

 DCL commands are used to grant and take back authority from any database user.
 Here are some commands that come under DCL:
 Grant
 Revoke

a. Grant: It is used to give user access privileges to a database.

Example:

GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

b. Revoke: It is used to take back permissions from the user.

Example:

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4. Transaction Control Language:


 TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.
 These operations are automatically committed in the database that's why they cannot be
used while creating tables or dropping them.

DBMS Page 10
Unit-4:SQL

 Here are some commands that come under TCL:


 COMMIT
 ROLLBACK
 SAVEPOINT

a.Commit:
Commit command is used to save all the transactions to the database.
Syntax:

COMMIT;
Example:

DELETE FROM CUSTOMERS


WHERE AGE = 25;
COMMIT;

b. Rollback:
Rollback command is used to undo transactions that have not already been saved to the database.
Syntax:

ROLLBACK;

Example:

DELETE FROM CUSTOMERS


WHERE AGE = 25;
ROLLBACK;
c. SAVEPOINT:
 It is used to roll the transaction back to a certain point without rolling back the entire
transaction.

DBMS Page 11
Unit-4:SQL

Syntax:

SAVEPOINT SAVEPOINT_NAME;

5. Data Query Language:


 DQL is used to fetch the data from the database.
 It uses only one command:
 SELECT
a. SELECT:
This is the same as the projection operation of relational algebra. It is used to select the attribute
based on the condition described by WHERE clause.
Syntax:

SELECT expressions
FROM TABLES
WHERE conditions;

For example:

SELECT emp_name
FROM employee
WHERE age > 20;

SQL Table
 SQL Table is a collection of data which is organized in terms of rows and columns. In
DBMS, the table is known as relation(Entity) and row as a tuple and column header
is called an attribute.
 Table is a simple form of data storage. A table is also considered as a convenient
representation of relations.
 Let's see an example of the EMPLOYEE table:

DBMS Page 12
Unit-4:SQL

EMP_ID EMP_NAME CITY PHONE_NO


1 Kristen Washington 7289201223
2 Anna Franklin 9378282882
3 Jackson Bristol 9264783838
4 Kellan California 7254728346
5 Ashley Hawaii 9638482678
In the above table, "EMPLOYEE" is the table name, "EMP_ID", "EMP_NAME",
"CITY", "PHONE_NO" are the column names. The combination of data of multiple
columns forms a row, e.g., 1, "Kristen", "Washington" and 7289201223 are the data of
one row.

Operation on Table:
1. Create table
2. Drop table
3. Delete table
4. Rename table
SQL Create Table:
 SQL create table is used to create a table in the database. To define the table, you should
define the name of the table and also define its columns and column's data type.
Syntax:
Create table "table_name"
("column1" "data type",
"column2" "data type",
"column3" "data type",
...
"columnN" "data type");

Example:

DBMS Page 13
Unit-4:SQL

SQL> CREATE TABLE EMPLOYEE (


EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (25) NOT NULL,
PHONE_NO INT NOT NULL,
ADDRESS CHAR (30),
PRIMARY KEY (ID)
);

If you create the table successfully, you can verify the table by looking at the message by
the SQL server. Else you can use DESC command as follows:

SQL> DESC EMPLOYEE;


Field Type Null key Default Extra
EMP_ID int(11) NO PRI NULL
EMP_NAME varchar(25) NO NULL
PHONE_NO NO int(11) NULL
ADDRESS YES NULL char(30)
4 rows in set (0.35 sec)
Now you have an EMPLOYEE table in the database, and you can use the stored
information related to the employees.
Drop table:
 A SQL drop table is used to delete a table definition and all the data from a table. When
this command is executed, all the information available in the table is lost forever, so you
have to very careful while using this command.
Syntax:

DROP TABLE "table_name";


Firstly, you need to verify the EMPLOYEE table using the following command:

SQL> DESC EMPLOYEE;

DBMS Page 14
Unit-4:SQL

Field Type Null key Default Extra


EMP_ID int(11) NO PRI NULL
EMP_NAME varchar(25) NO NULL
PHONE_NO NO int(11) NULL
ADDRESS YES NULL char(30)
 4 rows in set (0.35 sec)
This table shows that EMPLOYEE table is available in the database, so we can drop it as
follows:

SQL>DROP TABLE EMPLOYEE;

Now, we can check whether the table exists or not using the following command:

Query OK, 0 rows affected (0.01 sec)

As this shows that the table is dropped, so it doesn't display it.


SQL DELETE table:
 In SQL, DELETE statement is used to delete rows from a table. We can use WHERE
condition to delete a specific row from a table. If you want to delete all the records from
the table, then you don't need to use the WHERE clause.
Syntax;

DELETE FROM table_name WHERE condition;

Example:

Suppose, the EMPLOYEE table having the following records:

EMP_ID EMP_NAME CITY PHONE_NO SALARY


1 Kristen Chicago 9737287378 150000
2 Russell Austin 9262738271 200000
3 Denzel Boston 7353662627 100000

DBMS Page 15
Unit-4:SQL

4 Angelina Denver 9232673822 600000


5 Robert Washington 9367238263 350000
6 Christian Los angels 7253847382 260000
The following query will DELETE an employee whose ID is 2.

SQL> DELETE FROM EMPLOYEE


WHERE EMP_ID = 3;

Now, the EMPLOYEE table would have the following records.

EMP_ID EMP_NAME CITY PHONE_NO SALARY


1 Kristen Chicago 9737287378 150000
2 Russell Austin 9262738271 200000
4 Angelina Denver 9232673822 600000
5 Robert Washington 9367238263 350000
6 Christian Los angels 7253847382 260000
If you don't specify the WHERE condition, it will remove all the rows from the table.

DELETE FROM EMPLOYEE;

Now, the EMPLOYEE table would not have any records.


SQL SELECT Statement
 In SQL, the SELECT statement is used to query or retrieve data from a table in the
database. The returns data is stored in a table, and the result table is known as result-set.
Syntax:
SELECT column1, column2, ...
FROM table_name;

Here, the expression is the field name of the table that you want to select data from.
 Use the following syntax to select all the fields available in the table:

DBMS Page 16
Unit-4:SQL

SELECT * FROM table_name;

Example:
EMPLOYEE

EMP_ID EMP_NAME CITY PHONE_NO SALARY


1 Kristen Chicago 9737287378 150000
2 Russell Austin 9262738271 200000
3 Angelina Denver 9232673822 600000
4 Robert Washington 9367238263 350000
5 Christian Los angels 7253847382 260000
 To fetch the EMP_ID of all the employees, use the following query:

SELECT EMP_ID FROM EMPLOYEE;

Output:
EMP_ID
1
2
3
4
5

 To fetch the EMP_NAME and SALARY, use the following query:

SELECT EMP_NAME, SALARY FROM EMPLOYEE;

EMP_NAME SALARY
Kristen 150000
Russell 200000
Angelina 600000
Robert 350000
Christian 260000

DBMS Page 17
Unit-4:SQL

To fetch all the fields from the EMPLOYEE table, use the following query:

SELECT * FROM EMPLOYEE

EMP_ID EMP_NAME CITY PHONE_NO SALARY


1 Kristen Chicago 9737287378 150000
2 Russell Austin 9262738271 200000
3 Angelina Denver 9232673822 600000
4 Robert Washington 9367238263 350000
5 Christian Los angels 7253847382 260000

SQL INSERT Statement


 The SQL INSERT statement is used to insert a single or multiple data in a table. In SQL,
You can insert the data in two ways:
1. Without specifying column name
2. By specifying column name
Sample table:
EMPLOYEE

EMP_ID EMP_NAME CITY SALARY AGE


1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36

1. Without specifying column name:


 If you want to specify all column values, you can specify or ignore the column values.

DBMS Page 18
Unit-4:SQL

Syntax:

INSERT INTO TABLE_NAME


VALUES (value1, value2, value 3, .... Value N);

Query:

INSERT INTO EMPLOYEE VALUES (6, 'Marry', 'Canada', 600000, 48);

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36

6 Marry Canada 600000 48

2. By specifying column name:


 To insert partial column values, you must have to specify the column names.
Syntax:

INSERT INTO TABLE_NAME


[(col1, col2, col3,.... col N)]
VALUES (value1, value2, value 3, .... Value N);

Query:

INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME, AGE) VALUES (7, 'Jack', 40);

DBMS Page 19
Unit-4:SQL

Output: After executing this query, the table will look like:
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36

6 Marry Canada 600000 48


7 Jack null null 40
Note: In SQL INSERT query, if you add values for all columns then there is no need to specify
the column name. But, you must be sure that you are entering the values in the same order as the
column exists.

SQL update statement


The SQL UPDATE statement is used to modify the data that is already in the database. The
condition in the WHERE clause decides that which row is to be updated.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Sample Table:
EMPLOYEE
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36

DBMS Page 20
Unit-4:SQL

6 Marry Canada 600000 48

Updating single record:


 Update the column EMP_NAME and set the value to 'Emma' in the row where SALARY
is 500000.
Syntax:

UPDATE table_name
SET column_name = value
WHERE condition;

Query:
UPDATE EMPLOYEE
SET EMP_NAME = 'Emma'
WHERE SALARY = 500000;

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Emma Washington 500000 29
5 Russell Los angels 200000 36

6 Marry Canada 600000 48


Updating multiple records:
 If you want to update multiple columns, you should separate each field assigned with a
comma. In the EMPLOYEE table, update the column EMP_NAME to 'Kevin' and CITY
to 'Boston' where EMP_ID is 5.
Syntax:

DBMS Page 21
Unit-4:SQL

UPDATE table_name
SET column_name = value1, column_name2 = value2
WHERE condition;

Query:
UPDATE EMPLOYEE
SET EMP_NAME = 'Kevin', City = 'Boston'
WHERE EMP_ID = 5;

EMP_ID EMP_NAME CITY SALARY AGE


1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Kevin Boston 200000 36

6 Marry Canada 600000 48

Without use of WHERE clause:


 If you want to update all row from a table, then you don't need to use the WHERE clause.
In the EMPLOYEE table, update the column EMP_NAME as 'Harry'.
Syntax:

UPDATE table_name
SET column_name = value1;

Query:

DBMS Page 22
Unit-4:SQL

UPDATE EMPLOYEE
SET EMP_NAME = 'Harry';

EMP_ID EMP_NAME CITY SALARY AGE


1 Hary Chicago 200000 30
2 Hary Austin 300000 26
3 Hary Denver 100000 42
4 Hary Washington 500000 29
5 Hary Los angels 200000 36

6 Hary Canada 600000 48

SQL DELETE Statement:


The SQL DELETE statement is used to delete rows from a table. Generally, DELETE statement
removes one or more records form a table.
Syntax:

DELETE FROM table_name WHERE some_condition;

Sample Table:
EMLOYEE
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36

6 Marry Canada 600000 48


Deleting Single Record
 Delete the row from the table EMPLOYEE where EMP_NAME = 'Kristen'. This will
delete only the fourth row.
Query

DBMS Page 23
Unit-4:SQL

DELETE FROM EMPLOYEE


WHERE EMP_NAME = 'Kristen';

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
5 Russell Los angels 200000 36
6 Marry Canada 600000 48
Deleting Multiple Record
 Delete the row from the EMPLOYEE table where AGE is 30. This will delete two
rows(first and third row).
Query

DELETE FROM EMPLOYEE WHERE AGE= 30;

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE
2 Robert Austin 300000 26
3 Christian Denver 100000 42
5 Russell Los angels 200000 36
6 Marry Canada 600000 48
Delete all of the records

 Delete all the row from the EMPLOYEE table. After this, no records left to display. The
EMPLOYEE table will become empty.
Syntax

DBMS Page 24
Unit-4:SQL

DELETE * FROM table_name;


or
DELETE FROM table_name;

Query

DELETE FROM EMPLOYEE;

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE

Note: Using the condition in the WHERE clause, we can delete single as well as multiple
records. If you want to delete all the records from the table, then you don't need to use the
WHERE clause.

4.5:Views in SQL

 Views in SQL are considered as a virtual table. A view also contains rows and columns.
 To create the view, we can select the fields from one or more tables present in the
database.
 A view can either have specific rows based on certain condition or all the rows of a table.

Sample table:

Student_Detail

STU_ID NAME ADDRESS


1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram

Student_Marks

DBMS Page 25
Unit-4:SQL

STU_ID NAME MARKS AGE


1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1.Creating view

 A view can be created using the CREATE VIEW statement. We can create a view from
a single table or multiple tables.
Syntax:

CREATE VIEW view_name AS

SELECT column1, column2.....

FROM table_name

WHERE condition;

2.Creating View from a single table


 In this example, we create a View named DetailsView from the table Student_Detail.
Query:

CREATE VIEW DetailsView AS

SELECT NAME, ADDRESS

FROM Student_Details

WHERE STU_ID < 4;

Just like table query, we can query the view to view the data.

SELECT * FROM DetailsView;

Output:

DBMS Page 26
Unit-4:SQL

NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
3.Creating View from multiple tables

 View from multiple tables can be created by simply include multiple tables in the
SELECT statement.
 In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.

Query:

CREATE VIEW MarksView AS


SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS
FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
To display data of View MarksView:

SELECT * FROM MarksView;

NAME ADDRESS MARKS


Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90

4. Deleting View
 A view can be deleted using the Drop View statement.
Syntax

DROP VIEW view_name;

DBMS Page 27
Unit-4:SQL

Example:

If we want to delete the View MarksView, we can do this as:

DROP VIEW MarksView;

4.3 : SQL Clause(WHERE,AND,OR,WITH,ORDER BY)

What is Clause in SQL?

https://www.scaler.com/topics/clause-in-sql/

 SQL clause helps us to retrieve a set or bundles of records from the table.

 SQL clause helps us to specify a condition on the columns or the records of a table.

 A clause in SQL is a built-in function that helps to fetch the required records from a

database table.

 A clause receives a conditional expression, i.e. a column name or some terms involving

the columns.

 The clause calculates the result based on the given statements in the expression.

 When a large amount of data is stored in the database, clauses are helpful to filter and

analyze the queries.

What are the Types of Clauses in SQL?

 There are various types of clauses available in SQL, and some of them are listed below:
Clause Description
HAVING HAVING clause can be used in a GROUP BY clause. It is used to specify a
search condition for a group in the database tables.
WHERE The WHERE clause in SQL is used to retrieve the specific data from the database
that specifies the conditions exactly that are given in the UPDATE, DELETE, etc.

DBMS Page 28
Unit-4:SQL

statements.
ORDER BY The ORDER BY clause in SQL is used for sorting the records of the database
tables.
GROUP BY To group the result set of the rows that have the same values in the result set from
the database tables, the GROUP BY clause is used.
TOP This clause is used when the database has many records. It is used to specify the
total number of records to be fetched or returned.
WITH WITH clause acts as a temporary view as it is available only during the execution
of SELECT, UPDATE, INSERT, DELETE, etc. statements. It is used to
simplify complex and long queries.
LIKE The SQL LIKE clause compares a value to similar values using wildcard
operators, i.e. per cent sign ( % ) and the underscore operator ( _ ).
FROM The FROM clause in SQL is used to select the database tables, which are
manipulated using the SELECT, DELETE, and UPDATE statements.
LIMIT The LIMIT clause is used when you are dealing with large databases. It is used to
specify the maximum number of rows to be retrieved from the table.
AND The AND clause is used when multiple conditions are specified in a query and
returns a dataset when all the conditions given in the AND clause meet the
requirements.
OR The OR clause is used when multiple conditions are specified in a query and
returns a dataset when one of those conditions gets satisfied.
What are the Uses of SQL Clause?
There are various uses of clauses in SQL based on the type of clause. The uses of some of the
clauses in SQL are mentioned below:
ORDER BY

 To sort the database records, you can use the ORDER BY clause in SQL. This clause in
SQL is used to arrange the fetched data in ascending or descending order based on the
requirements.

WHERE

DBMS Page 29
Unit-4:SQL

 The WHERE clause in SQL is used to fetch the data or certain records that match the
specified condition in the SELECT statement. SQL's WHERE clause is also used with
the DELETE, UPDATE, etc. statements.
GROUP BY

 Another usage of the clause in SQL is to group the rows that have the same values in the
result set, and this can be achieved by using the GROUP BY clause in SQL.

TOP

 If you want to determine the total number of record rows in the result then you can use
the TOP clause in SQL.

AND

 The AND clause is used with the UPDATE and DELETE statements and returns the
resultant dataset only when all the conditions given with the AND clause are satisfied.

OR

 The OR clause is also used with the UPDATE and DELETE statements and returns the
resultant dataset when one or more than one condition is satisfied.

LIMIT

 When the amount of data in the database is very large, the LIMIT clause is used to
restrict the number of rows from the database records.

Examples of Clause in SQL


Consider the below Students table, which is used as a reference for all the examples that are
mentioned below.
Students
stu_id stu_name stu_fees stu_subject stu_age stu_class

DBMS Page 30
Unit-4:SQL

1 Divyesha Patil 3000 Maths 16 10

2 Mayra Pandit 2000 Social Science 15 10

3 Kunal Purohit 4500 Chemistry 17 11

4 Manvi Tyagi 2000 Social Science 16 9

5 Joy Yadav 3000 Maths 16 9

6 Tisha Shah 2500 Science 15 9

7 Surbhi Soni 4000 Chemistry 17 10

Example: WHERE clause


In this example, the below SQL query is used along with the WHERE clause in SQL to retrieve
all the records of a student from the Students table whose fees is less than 3500.
SELECT * FROM Students
WHERE stu_fees < 3500;

Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
1 Divyesha Patil 3000 Maths 16 10
2 Mayra Pandit 2000 Social Science 15 10
4 Manvi Tyagi 2000 Social Science 16 9
5 Joy Yadav 3000 Maths 16 9
6 Tisha Shah 2500 Science 15 9
As shown in the output, the WHERE clause in SQL fetches the records of those students whose
fee is less than 3500.
Example: GROUP BY clause
The following query uses the GROUP BY clause to fetch the total fees in the students'
individual classes. This can be easily done by grouping of the rows from the Students table.
SELECT SUM(stu_fees), stu_class
FROM Students
GROUP BY stu_class;

DBMS Page 31
Unit-4:SQL

Output:
stu_fees stu_class
9000 10
4500 11
7500 9
As shown in the output above, the GROUP BY clause is used to group the rows of the students
based on the student class column. The total fees in an individual class are summed up, and the
grouped rows are displayed in the table.

Example: ORDER BY clause


Let's take another example which includes the ORDER BY clause in SQL. The below query is
used to order the students based on the fees of the individual students.
SELECT *
FROM Students
ORDER BY stu_fees;

Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
2 Mayra Pandit 2000 Social Science 16 10
4 Manvi Tyagi 2000 Social Science 16 9
6 Tisha Shah 2500 Science 15 9
1 Divyesha Patil 3000 Maths 16 10
5 Joy Yadav 3000 Maths 16 9
7 Surbhi Soni 4000 Chemistry 17 10
3 Kunal Purohit 4500 Chemistry 17 11
In the above example, the ORDER BY clause is applied to the column stu_fees to sort the final
result based on the fees of the students.

Example: HAVING clause


Consider another example which explains the HAVING clause in SQL. The following query
returns the details of all the students having an age less than 17 after grouping the records based
on stu_id.
SELECT *

DBMS Page 32
Unit-4:SQL

FROM Students
GROUP BY stu_id
HAVING stu_age < 17;

Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
1 Divyesha Patil 3000 Maths 16 10
2 Mayra Pandit 2000 Social Science 15 9
4 Manvi Tyagi 2000 Social Science 16 9
5 Joy Yadav 3000 Maths 16 10
6 Tisha Shah 2500 Science 15 9
In the above output, you can see that the HAVING clause is used to fetch the records of students
under 17. Also, note that the GROUP BY clause is mandatory if you are using
the HAVING clause in SQL.

https://beginnersbook.com/2014/05/order-by-clause-in-sql/

SQL WHERE Clause


 Where clause is used to fetch a particular row or set of rows from a table. This clause
filters records based on given conditions and only those row(s) comes out as result that
satisfies the condition defined in WHERE clause of the SQL query.
SQL Where Clause Syntax
SELECT Column_name1, Column_name2, ....
FROM Table_name
WHERE Condition;
Here we have used the where clause with the SQL SELECT statement, however we can use this
clause with other SQL statements as well such as UPDATE, DELETE etc.
Example – SQL WHERE Clause
We have a table EMPLOYEES:
+------+----------+---------+----------+
|SSN | EMP_NAME | EMP_AGE |EMP_SALARY|
+------+----------+---------+----------+
| 101 | Steve | 23 | 9000.00 |

DBMS Page 33
Unit-4:SQL

| 223 | Peter | 24 | 2550.00 |


| 388 | Shubham | 19 | 2444.00 |
| 499 | Chaitanya| 29 | 6588.00 |
| 589 | Apoorv | 21 | 1400.00 |
| 689 | Rajat | 24 | 8900.00 |
| 700 | Ajeet | 20 | 18300.00 |
+------+----------+---------+----------+
Lets say we want to fetch the name of the those employees who are more than 23 years old. The
SQL statement would look like this –

Query:
SELECT EMP_NAME
FROM EMPLOYEES
WHERE EMP_AGE > 23;
Result:
+----------+
| EMP_NAME |
+----------+
| Peter |
| Chaitanya|
| Rajat |
+----------+
Fetch all the details of employees having salary greater than 6000.
Query:
SELECT *
FROM EMPLOYEES
WHERE EMP_SALARY > 6000;

Result:
+------+----------+---------+----------+
|SSN | EMP_NAME | EMP_AGE |EMP_SALARY|
+------+----------+---------+----------+
| 101 | Steve | 23 | 9000.00 |
| 499 | Chaitanya| 29 | 6588.00 |
| 689 | Rajat | 24 | 8900.00 |
| 700 | Ajeet | 20 | 18300.00 |

Operators allowed in The WHERE Clause conditions


In the above examples we have seen that where clause allowed operators as > & =. Lets see the
complete list of operators that we can use in the where clause.
Operators List:
DBMS Page 34
Unit-4:SQL

a. Greater than operator


b. < Less than operator
c. = Equal operator
d. >= Greater than or equal
e. <= Less than or equal
f. <> Not equal.
g. IN To specify the set of values
h. BETWEEN To specify the range of values
i. LIKE To specify the pattern
AND clause

 The AND clause is used with the UPDATE and DELETE statements and returns the
resultant dataset only when all the conditions given with the AND clause are satisfied.
 When multiple conditions are joined using AND operator, only those rows will be
fetched from the database which meets all the conditions

AND Operator or AND Clause Syntax


SELECT column_name1, column_name2, ...
FROM table_name
WHERE condition_1 AND condition_2 ...;

SQL AND Example


Table: STUDENT
+----+--------------+-----+-----------+----------+
| ID | STU_NAME | AGE | ADDRESS | BRANCH |
+----+--------------+-----+-----------+----------+
| 11 | Carl | 22 | Agra | ECE |
| 12 | Rick | 23 | Delhi | CSE |
| 13 | Maggie | 23 | Gurgaon | CL |
| 14 | Carol | 24 | Noida | ME |
| 15 | Negan | 26 | Delhi | EE |
+----+--------------+-----+-----------+----------+
Lets write an SQL statements to fetch the name of the students where student age is “greater than
23” and address is “Delhi”
Query:
SELECT STU_NAME
FROM STUDENT
WHERE AGE > 23 AND ADDRESS = 'Delhi';

DBMS Page 35
Unit-4:SQL

Result:
+----------+
| STU_NAME |
+----------+
| Negan |
+----------+

OR Operator or OR Clause
 The OR clause is also used with the UPDATE and DELETE statements and returns the
resultant dataset when one or more than one condition is satisfied
 When multiple conditions are joined using OR operator, all those rows will be fetched
from the database which meet any of the given conditions.

OR Operator (Clause) Syntax


SELECT column_name1, column_name2, ...
FROM table_name
WHERE condition_1 OR condition_2 ...;
SQL OR Example
Table: EMPLOYEE

+----+--------------+-----+-----------+----------+
| ID | EMP_NAME | AGE | ADDRESS | SALARY |
+----+--------------+-----+-----------+----------+
| 90 | David | 30 | Agra | 10000 |
| 91 | Steve | 31 | Delhi | 9000 |
| 92 | Ajeet | 29 | Gurgaon | 11000 |
| 93 | Rahul | 33 | Noida | 19000 |
| 94 | Pappu | 35 | Agra | 9900 |
+----+--------------+-----+-----------+----------+
Lets write an SQL statements to fetch the details of the employees, where either employee age is
“greater than 30” or address is “Agra”.
Query:
SELECT *
FROM EMPLOYEE
WHERE AGE > 30 OR ADDRESS = 'Agra';

DBMS Page 36
Unit-4:SQL

Result:
+----+--------------+-----+-----------+----------+
| ID | EMP_NAME | AGE | ADDRESS | SALARY |
+----+--------------+-----+-----------+----------+
| 90 | David | 30 | Agra | 10000 |
| 91 | Steve | 31 | Delhi | 9000 |
| 93 | Rahul | 33 | Noida | 19000 |
| 94 | Pappu | 35 | Delhi | 9900 |
+----+--------------+-----+-----------+----------+

Explanation:
Row number 1 is in the output because it meets the second condition (ADDRESS = ‘Agra’).
Row number 2nd and 4th are in the output because they meet the first condition (AGE > 30).
Row number 5th is in the output because it meets both the conditions.

SQL ORDER BY Clause

 ORDER BY clause is used to sort the returned records in an order. By using ORDER

BY clause, we can sort the result in ascending or descending order. This clause can be

used with multiple columns as well.

ORDER BY Syntax
SELECT column_name1, column_name2, column_name3....
FROM table_name
WHERE Condition
ORDER BY column_name1, column_name2, .... (ASC OR DESC);
Here ASC is used for sorting in ascending order and DESC for descending order .
SQL ORDER BY clause example

Lets say we have a table “STUDENT” with the following records.

+---------+----------+-----+-----------+----------+
| ROLL_NO | NAME | AGE | BRANCH | CITY |
+---------+----------+-----+-----------+----------+
| 10001 | Kate | 22 | CSE | Mumbai |
| 10002 | Richard | 21 | ECE | Delhi |
| 10003 | Rick | 33 | ME | Chennai |
| 10004 | Peter | 26 | CSE | Delhi |

DBMS Page 37
Unit-4:SQL

| 10005 | Steve | 22 | CSE | Noida |


| 10006 | Mark | 22 | ECE | Jaipur |
| 10007 | Brad | 23 | ME | Rampur |
+---------+----------+-----+-----------+----------+
The following SQL statement will fetch all the records from the table “STUDENT” and the
returned rows will be in ascending order of student age.

SELECT *
FROM STUDENT
ORDER BY AGE;
Note: The default order of sorting is ascending so the rows will be sorted based on the column
“AGE”, even though we have not used the ASC keyword in order by clause. So it is safe to say
that the following query is same as the above query and would fetch the same result.
SELECT *
FROM STUDENT
ORDER BY AGE ASC;
Result:
+---------+----------+-----+-----------+----------+
| ROLL_NO | NAME | AGE | BRANCH | CITY |
+---------+----------+-----+-----------+----------+
| 10002 | Richard | 21 | ECE | Delhi |
| 10001 | Kate | 22 | CSE | Mumbai |
| 10005 | Steve | 22 | CSE | Noida |
| 10006 | Mark | 22 | ECE | Jaipur |
| 10007 | Brad | 23 | ME | Rampur |
| 10004 | Peter | 26 | CSE | Delhi |
| 10003 | Rick | 33 | ME | Chennai |
+---------+----------+-----+-----------+----------+
SQL ORDER BY DESC example
Table: STUDENT
+---------+----------+-----+-----------+----------+
| ROLL_NO | NAME | AGE | BRANCH | CITY |
+---------+----------+-----+-----------+----------+
| 10001 | Kate | 22 | CSE | Mumbai |
| 10002 | Richard | 21 | ECE | Delhi |
| 10003 | Rick | 33 | ME | Chennai |
| 10004 | Peter | 26 | CSE | Delhi |
| 10005 | Steve | 22 | CSE | Noida |
| 10006 | Mark | 22 | ECE | Jaipur |
| 10007 | Brad | 23 | ME | Rampur |
+---------+----------+-----+-----------+----------+

DBMS Page 38
Unit-4:SQL

The following SQL statement will fetch the student names from the table “STUDENT” and the
returned names will be sorted in descending order (we have used DESC for descending order in
the ORDER BY clause).
SELECT NAME
FROM STUDENT
ORDER BY NAME DESC;

Result:
+----------+
| NAME |
+----------+
| Steve |
| Rick |
| Richard |
| Peter |
| Mark |
| Kate |
| Brad |
+----------+
As you can see, we have got a list of student names sorted in the descending order.

DBMS Page 39
Unit-4:SQL

4.4:SQL JOIN
https://www.studytonight.com/dbms/joining-in-sql.php

 As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".
 SQL Join is an operation in DBMS(DataBase Management System) that combines the
row of two or more tables based on related(matching) columns between them.
 SQL Join is used to fetch data from two or more tables, which is joined to appear as
single set of data. It is used for combining column from two or more tables by using
values common to both tables.

 JOIN Keyword is used in SQL queries for joining two or more tables.
Types of SQL JOIN
Different types of Joins are as follows:
1. INNER JOIN
2. NATURAL JOIN
3. OUTER JOIN
a. LEFT OUTER JOIN
b. RIGHT OUTER JOIN
c. FULL OUTER JOIN
1.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 SQL 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;
Diagram

DBMS Page 40
Unit-4:SQL

Example of INNER JOIN


Consider a class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu

and class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI

Inner JOIN query will be,


SELECT * from class INNER JOIN class_info where class.id = class_info.id;

The result set table will look like,


ID NAME ID Address

1 abhi 1 DELHI

2 adam 2 MUMBAI

3 alex 3 CHENNAI

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

DBMS Page 41
Unit-4:SQL

The syntax for Natural Join is,


SELECT * FROM
table-name1 NATURAL JOIN table-name2;
Example of Natural JOIN
class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
and the
class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
Natural join query will be,

SELECT * from class NATURAL JOIN class_info;

The resultset table will look like,


ID NAME Address

1 abhi DELHI

2 adam MUMBAI

3 alex CHENNAI

In the above example, both the tables being joined have ID column(same name and same
datatype), hence the records for which value of ID matches in both the tables will be the result of
Natural Join of these two tables.

DBMS Page 42
Unit-4:SQL

3.OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
a. Left Outer Join
b. Right Outer Join
c. Full Outer Join
a.LEFT Outer Join
 The left outer join returns a result set table with the matched data from the two tables and
then the remaining rows of the left table and null from the right table's columns.
Syntax for Left Outer Join is,

SELECT column-name-list FROM


table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Diagram

Example of Left Outer Join


Here is the class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
and the class_info table,

DBMS Page 43
Unit-4:SQL

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

Left Outer Join query will be,


SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
b.RIGHT Outer Join
 The right outer join returns a resultset table with the matched data from the two tables
being joined, then the remaining rows of the right table and null for the
remaining left table's columns.
Syntax for Right Outer Join is,
SELECT column-name-list FROM
table-name1 RIGHT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column

DBMS Page 44
Unit-4:SQL

Diagram

-Example of Right Outer Join


Once again the class table,
NAME
ID

1 abhi

2 adam

3 alex

4 anu

5 ashish

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

DBMS Page 45
Unit-4:SQL

Right Outer Join query will be,


SELECT * FROM class RIGHT OUTER JOIN class_info ON (class.id = class_info.id);

The resultant table will look like,


NAME ID Address
ID

1 abhi 1 DELHI

2 adam 2 MUMBAI

3 alex 3 CHENNAI

null null 7 NOIDA

null null 8 PANIPAT

c.Full Outer Join


The full outer join returns a resultset table with the matched data of two table then remaining
rows of both left table and then the right table.
Syntax of Full Outer Join is,
SELECT column-name-list FROM
table-name1 FULL OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Diagram

Example of Full outer join is,


The class table,

DBMS Page 46
Unit-4:SQL

ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
Full Outer Join query will be like,

SELECT * FROM class FULL OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,


ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
null null 7 NOIDA
null null 8 PANIPAT

DBMS Page 47
Unit-4:SQL

Assignment
1.Define SQL .Write the characteristics of SQL.
2.List and explain different types of SQL or SQL commands .(VVI)
3.List and explain different types of SQL Join . .(VVI)
4.Explain SQL View.
5.Explain SQL Clause.

DBMS Page 48
Unit-6:DataBase Transaction

Unit-6: DataBase Transaction

6.1:Introduction to Transaction
6.2:Concurrency in Transaction
6.3:ACID properties
6.4:State of Transaction
6.1:Introduction to Transaction

Introduction
 The transaction is a set of logically related operation. It contains a group of tasks.
 A transaction is an action or series of actions. It is performed by a single user to perform operations
for accessing the contents of the database.
 Set of operation or group of task is called Transaction.
 Example:Deposit Money,Withdraw Money,Transfer Money,Balance enquiry,print statement,Ticket
Booking,etc.
 The transaction is a process in which
1.Input is chosen
2.Desired operations are performed on it.
3.Output is generated.

Begin Select Input Apply Output End


Operation Generated
Data

fig:Database Transaction

 Example: Suppose an employee of bank transfers Rs 800 from X's account to Y's account. This
small transaction contains several low-level tasks:
X's Account
1. Open_Account(X)
2. Old_Balance = X.balance
3. New_Balance = Old_Balance - 800
4. X.balance = New_Balance
5. Close_Account(X)

Y's Account
1. Open_Account(Y)
2. Old_Balance = Y.balance
3. New_Balance = Old_Balance + 800

DBMS-2080 Notes Page 1


Unit-6:DataBase Transaction

4. Y.balance = New_Balance
5. Close_Account(Y)

Note: A data buffer (or just buffer) is a region of a memory used to store data temporarily while it is
being moved from one place to another.

Operations of Transaction:
Following are the main operations of transaction:
a. Read(X): Read operation is used to read the value of X from the database and stores it in a buffer in
main memory.
b. Write(X): Write operation is used to write the value back to the database from the buffer.
Let's take an example to debit transaction from an account which consists of following operations:

1. R(X);
2. X = X - 500;
3. W(X);
Let's assume the value of X before starting of the transaction is 4000.
a. The first operation reads X's value from database and stores it in a buffer.
b. The second operation will decrease the value of X by 500. So buffer will contain 3500.
c. The third operation will write the buffer's value to the database. So X's final value will be 3500.

DBMS-2080 Notes Page 2


Unit-6:DataBase Transaction

But it may be possible that because of the failure of hardware, software or power, etc. that transaction may
fail before finished all the operations in the set.
 For example: If in the above transaction, the debit transaction fails after executing operation 2 then
X's value will remain 4000 in the database which is not acceptable by the bank.
To solve this problem, we have two important operations:
1.Commit: It is used to save the work done permanently.
2. Rollback: It is used to undo the work done

6.2:Concurrency in Transaction
concurrency -occurrring or operating system at the same time

 Concurrency can be simply defined as executing multiple transactions at a time.


 It is required to increase time efficiency. If many transactions try to access the same data, then
inconsistency arises. Concurrency control required to maintain consistency data.
 Concurrency Control is a procedure in DBMS which helps us for the management of two
simultaneous processes to execute without conflicts between each other, these conflicts occur in
multi user systems.
 For example, if we take ATM machines and do not use concurrency, multiple persons cannot draw
money at a time in different places. This is where we need concurrency.
Advantages
The advantages of concurrency control are as follows −
a. Waiting time will be decreased.
b. Response time will decrease.
c. Resource utilization will increase.
d. System performance & Efficiency is increased.
Main problems in using Concurrency
The problems which arise while using concurrency are as follows −
a. Updates will be lost − One transaction does some changes and another transaction deletes that
change. One transaction nullifies the updates of another transaction.
b. Uncommitted Dependency or dirty read problem − On variable has updated in one transaction, at
the same time another transaction has started and deleted the value of the variable there the variable
is not getting updated or committed that has been done on the first transaction this gives us false
values or the previous values of the variables this is a major problem.

DBMS-2080 Notes Page 3


Unit-6:DataBase Transaction

c. Inconsistent retrievals − One transaction is updating multiple different variables, another


transaction is in a process to update those variables, and the problem occurs is inconsistency of the
same variablee in different instances.
Concurrency control techniques
The concurrency control techniques are as follows −
1. Locking
2. Time Stamping
3. Optimistic

6.3:ACID properties
 A transaction is a very small unit of a program and it may contain several low level tasks. A
solation, and Durability −
transaction in a database system must maintain Atomicity, Consistency, Isolation,
commonly known as ACID properties − in order to ensure accuracy, completeness, and data
integrity.
Property of Transaction
1. Atomicity
2. Consistency
3. Isolation
4. Durability

DBMS-2080 Notes Page 4


Unit-6:DataBase Transaction

1.Atomicity

 This property states that a transaction must be treated as an atomic unit, that is, either all of its
operations are executed or none.
 There must be no state in a database where a transaction is left partially completed.
 States should be defined either before the execution of the transaction or after the
execution/abortion/failure of the transaction.
 Atomicity involves the following two operations:
a.Abort: If a transaction aborts then all the changes made are not visible.
b.Commit: If a transaction commits then all the changes made are visible.

2.Consistency

 The database must remain in a consistent state after any transaction. No transaction should
have any adverse effect on the data residing in the database. If the database was in a
consistent state before the execution of a transaction, it must remain consistent after the
execution of the transaction as well.
For example: The total amount must be maintained before or after the transaction.

 Total before T occurs = 600+300=900


 Total after T occurs= 500+400=900
Therefore, the database is consistent. In the case when T1 is completed but T2 fails, then inconsistency will
occur.
Note:
Data consistency is the accuracy, completeness, and correctness of data stored in a database. The same data
across all related systems, applications, and databases is when we say that data is consistent. Inconsistent
data can lead to incorrect analysis, decision-making, and outcomes.
3.Durability

 The database should be durable enough to hold all its latest updates even if the system fails or
restarts.
 The durability property is used to indicate the performance of the database's consistent state. It states
that the transaction made the permanent changes.

DBMS-2080 Notes Page 5


Unit-6:DataBase Transaction

 They cannot be lost by the erroneous operation of a faulty transaction or by the system failure. When
a transaction is completed, then the datab
database
ase reaches a state known as the consistent state. That
consistent state cannot be lost, even in the event of a system's failure.
 The recovery subsystem of the DBMS has the responsibility of Durability property.
4.Isolation

 It shows that the data which is used at the time of execution of a transaction cannot be used by the
second transaction until the first one is completed.
 In isolation, if the transaction T1 is being executed and using the data item X, then that data item
can't be accessed by any other transaction
ransaction T2 until the transaction T1 ends.
 The concurrency control subsystem of the DBMS enforced the isolation property.

6.4:State of Transaction
In a database, the transaction can be in one of the following states -

Active State
 The active state is the first state of every transaction. In this state, the transaction is being executed.
 For example: Insertion or deletion or updating a record is done here. But all the records are still not
saved to the database.

DBMS-2080 Notes Page 6


Unit-6:DataBase Transaction

Partially committed
 In the partially committed state, a transaction executes its final operation, but the data is still not
saved to the database.
 In the total mark calculation example, a final display of the total marks step is executed in this state.
Committed
 A transaction is said to be in a committed state if it executes all its operations successfully. In this
state, all the effects are now permanently saved on the database system.

Failed state
 If any of the checks made by the database recovery system fails, then the transaction is said to be in
the failed state.
 In the example of total mark calculation, if the database is not able to fire a query to fetch the marks,
then the transaction will fail to execute.
Aborted
 If any of the checks fail and the transaction has reached a failed state then the database recovery
system will make sure that the database is in its previous consistent state. If not then it will abort or
roll back the transaction to bring the database into a consistent state.
 If the transaction fails in the middle of the transaction then before executing the transaction, all the
executed transactions are rolled back to its consistent state.
 After aborting the transaction, the database recovery module will select one of the two operations:
a.Re-start the transaction
b. Kill the transaction
Assignments
1.Define Transaction.Explain the operations of DatatBaseTransaction.
2.What do you mean by ACID Properties.Explain the ACID properties of Transaction. (VVI)
3.Explain the state of Transaction. (VVI)
4.Explain Concurrency in Transaction.

DBMS-2080 Notes Page 7


Unit-7: Database Backup , Recovery & Security

Unit-7: Database Backup , Recovery & Security


Contents
7.1: Introduction to backup
7.2:Types of Backup
7.2.1:Physical Backup
7.2.2:Logical Backup
7.3:Reason for Database failure
7.4:Methods of Database backup
7.5:Concept of recovery,redo/undo
7.6:Introduction to Database Security
7.7:Common Threats in Database

7.1:Introduction to Data Backup


Introduction
 Data backup is making a copy of, or duplicating important data files and storing it on
another media, different from the original media it is native to. This ensures that even if
your original files are lost, damaged, or stolen, you can still access them from another location.
 Data backup is essential for both individuals and businesses as it helps protect against data loss
due to hardware failure, human error, cyber-attacks, and natural disasters. All of which happen
more often than we think.
 There are different types of data backups available as well that needs to be considered, such
as full backups where all data is backed up every time, incremental backups which only back
up new or changed information since the last backup was taken, and differential
backups which back up all changes made since the last full backup.
 Additionally, there are different platforms for backing up data such as cloud-based
services or physical devices like external hard drives, or NAS (Network Attached
Storage) devices.
 Having a reliable data backup system in place can save you significant amounts of time and
money in the event of an unexpected loss.
Advantages
There are many advantages to backing up your data. The most obvious one is that you get to restore
your data in the event something bad happens to your computer or server. It’s a form of insurance
that you can rely on.
But I’ll break this down into 3 main categories that have the most positive impact.
1.Security

DBMS-2080 Page 1
Unit-7: Database Backup , Recovery & Security

 By regularly backing up your data, you can ensure that even if your system is compromised or
suffers a hardware failure, you won’t lose any critical information. This can be especially
important for businesses that deal with sensitive customer information or financial records.
 Additionally, many backup solutions offer encryption options that help keep your data safe
from prying eyes. What this means is that no one else can access your data when it’s backed
up to the cloud, for instance, unless they have the appropriate credentials to access it.
 The industry standard for cloud backup encryption is 256-bit.
2.Versatility
 One of the biggest advantages of data backup is its versatility. With a good backup system, you
can store any kind of data without worrying about losing it.
 A versatile data backup system allows you to store all kinds of files such as documents, pictures,
videos, and audio files in one place. This means that you no longer have to worry about
managing different types of storage devices or services for your various file formats.
 Whether you are an individual who needs to keep important documents safe or a business that
needs to store large amounts of customer data securely, a versatile backup system can help.
 Versatility can also come in various platforms. As I’ve mentioned before, your source for data
backup could be via the cloud, like Google Drive, Dropbox, or Carbonite. It can also be local to
your internal network either with a server, or a NAS (Network Attached Storage). And if you
truly want to keep things local, your data backup could be as simple as an external hard drive.
 Many external hard drives come with backup software loaded on them, so it’s not a bad place to
start.
3.Reliability
 Once you have decided what platform you’re going to use to back up your data, once it’s in place
and consistently backing up your data, you know you can rely on it to restore your data if need
be.
 Data loss prevention is one of the most significant advantages of backing up your files.
Prevention is always better than cure, is what I always say, and that applies to data backup.
 In this digital age, losing important information due to hardware failure or cyber-attacks can have
devastating consequences for businesses. However, with a reliable data backup system, you
never have to worry about losing your valuable data, regardless of what may occur.
 If anything happens to your primary storage devices, you can easily restore all your files from
your backups without any hassle. Moreover, another benefit of backing up your files is that it
enables you to recover lost or deleted data quickly and easily.

DBMS-2080 Page 2
Unit-7: Database Backup , Recovery & Security

Disadvantages of Data Backup


While data backup can provide many benefits, including protection against data loss and increased
productivity, there are also some disadvantages to consider.
 One of the biggest disadvantages of data backup is the cost.
Depending on the size of your business and the volume of data you need to back up, the costs can
quickly escalate.
 Another disadvantage of data backup is that it requires time and resources. Backing up your
data can be a time-consuming process that takes away from other important tasks, such as
customer service or product development. This is especially true at the beginning when you’re
first setting up your data backup system.

Selecting the Right Backup Method or Technique


With so many backup methods available, it can be overwhelming to choose the right one for your needs.
But let’s look at the different options to see what would be best for you.
1.Cloud Storage

This is one of the easiest and most convenient backup systems to set in place. Cloud storage falls under the
SaaS (Software as a Service) category, and it’s the method by which your data to stored on a remote server,
owned by a third party. This remote server is what’s referred to as “the cloud”.
The advantage of using cloud storage is that it offers unlimited space and convenient access from any device
with an internet connection. This makes cloud storage a very attractive method for data backup. And there
are many vendors(seller) to choose from. Others that you can choose from for both individual and business
usage are:
 Microsoft OneDrive
 Google Drive
 Dropbox
 Amazon Web Services
 Carbonite
 Acronis True Image
 Idrive
There are many more to choose from of course, but these options are a great place to start.

DBMS-2080 Page 3
Unit-7: Database Backup , Recovery & Security

2.External Hard Drives

 Using external hard drives as a backup resource is as basic as you can get. Many people start
here because it’s the most economical way of getting started with data backup and it’s the most
simple. All you need is to purchase an external with a large enough storage size.
 As I mentioned before, many external hard drives come with backup software installed to help
you automatically do your backups, daily and weekly.
 The main drawback of an external backup is that you are limited to that device. So if that hard
drive gets lost or stolen, so does your data. Security is very limited in this area unless you decide
to be proactive and store your external hard drive in a secured location.
 This is a great place to start your backup system, but I recommend upgrading to a more secure
and advanced backup system, as business improves for you.
3.NAS (Network Attached Storage)

 One popular option for backing up data is Network Attached Storage (NAS), which provides a
centralized and secure location for storing and accessing files.
 There are several options to choose from when it comes to NAS devices.
 NAS refers to a type of storage device that connects to your network and allows multiple
devices to access its contents. The device acts as a central location where you can store and

DBMS-2080 Page 4
Unit-7: Database Backup , Recovery & Security

share files, making it an ideal solution for backing up your data. Many NAS devices also allow
for some cloud features, where your data gets stored in the cloud using the manufacturer’s cloud
platform.
 Unlike traditional external hard drives or USBs, which are limited by their capacity and
portability, NAS provides ample (Sufficient) storage space with added flexibility.
 Another advantage of using NAS as your backup method is its ability to automate backups.
Sure you can do this with an external hard drive, as I’ve mentioned before. But a NAS device is
more centralized and accessible by several other devices from that network.
When selecting a NAS backup method, there are several factors to consider.
 First, you must choose the right hardware that suits your needs. This includes things like storage
capacity, connectivity options, and speed.
 Another key consideration when selecting a NAS backup method is security. You want to ensure
that your data is protected from unauthorized access or theft at all times.
Here are some top NAS brands for you to choose from:
 Buffalo TeraStation
 Western Digital My Cloud
 Synology NAS Diskstation

What Are the Benefits of Data Backups?


Data backups provide a safety net in case of any disruptions to your business data. They protect you against
potential risks to reputation and bottom line. Consider these database backup benefits:
1. Inexpensive insurance against business interruptions
2. Ability to restore missing or corrupted data
3. Security against cyberattacks
4. Bottom line protection
5. Peace of mind
What Are the Disadvantages of Database Backup?
Database backup requires time and labor costs associated with establishing procedures, defining schedules,
and monitoring the related processes. This is because databases are typically used to hold critical business
data, like customer records or product inventory, and require clear processes.
Cloud backup comes with many pros, but also cons, such as:
 You can’t access your data if you don’t have Internet access.
 You have to deal with bandwidth issues to back up large chunks of data.
 While cloud recovery of small quantities of data is fast and easy, full data recovery could take some
time.
DBMS-2080 Page 5
Unit-7: Database Backup , Recovery & Security

 Likewise, cloud entry level with a small amount of data is inexpensive, while large quantities of data
can steadily increase costs.
What Are the Main Disadvantages of Backing Up Data Locally?
If hackers and cybercriminals can access your live operational files, they may also try to corrupt your
database backups.It’s much safer to back up in the cloud or some other remote location than on-premises.
The drawbacks of a local backup are:
 Higher costs compared to cloud backup
 Scalability concerns
 Increased maintenance
 Enhanced cybersecurity issues
 More vulnerable to events like fires and floods

Difference between Backup and Recovery


1. Backup: Backup refers to storing a copy of original data which can be used in case of data loss.
Backup is considered one of the approaches to data protection. Important data of the organization
needs to be kept in backup efficiently for protecting valuable data. Backup can be achieved by
storing a copy of the original data separately or in a database on storage devices. There are various
types of backups are available like full backup, incremental backup, Local backup, mirror backup,
etc. An example of a Backup can be SnapManager which makes a backup of everything in the
database.
2. Recovery: Recovery refers to restoring lost data by following some processes. Even if the data was
backed up still lost so it can be recovered by using/implementing some recovery techniques. When
a database fails due to any reason then there is a chance of data loss, so in that case recovery
process helps in improve the reliability of the database. An example of Recover can be
SnapManager which recovers the data from the last transaction.

Difference between Backup and Recovery:


S. No. BACKUP RECOVERY
01. Backup refers to storing a copy of original Recovery refers to restoring the lost data in case
data separately. of failure.
02. So we can say Backup is a copy of data So we can say Recovery is a process of
which is used to restore original data after a retrieving lost, corrupted or damaged data to its
data loss/damage occurs. original state.

DBMS-2080 Page 6
Unit-7: Database Backup , Recovery & Security

03. In simple backup is the replication of data. In simple recovery is the process to store the
database.
04. The prior goal of backup is just to keep one The prior goal of recovery is retrieve original
extra copy to refer in case of original data data in case of original data failure.
loss.
05. It helps in improving data protection. It helps in improving the reliability of the
database.
06. Backup makes the recovery process more Recovery has no role in data backup.
easier.
07. The cost of backup is affordable. The cost of recovery is expensive.
08. It’s production usage is very common. It’s production usage is very rare.
09. Example of Backup can be SnapManager Example of Recover can be SnapManager
makes a backup of everything in database. recovers the data to the last transaction.
10. Backup is not created automatically. There is automatic generation of restore points
by your computer.
11. A backup stores copies of the files in a A restore is carried out internally on your
location that is external to it. computer.
12. Backup requires extra storage space. Restore is internal so it does not require extra
external storage space.
13. Backup offers a means of recovery. Recovery aims to guarantee the atomicity of the
transaction and data.

7.2:Type of Database Backup – Physical and Logical


A database backup is a copy of storage that is stored on a server. Backup is used to prevent unexpected
data loss. If original data gets lost, then with the help of a backup, it is easy to gain access to the data
again.
There are two types of database backup.
1. Physical backup
2. Logical backup

1.Physical Backup:
Physical database backups are backups of physical files that are used to store and recover databases.
These include different data files, control files, archived redo logs, and many more. Typically, physical
backup data is kept in the cloud, offline storage, magnetic tape, or on a disc.

DBMS-2080 Page 7
Unit-7: Database Backup , Recovery & Security

There are two methods to perform a physical backup :


1. Operating system utilities
2. Recovery manager
This type of backup is useful when the user needs to restore the complete database in a short period. It is
beneficial to provide details of transactions and changes made in databases. It is considered the
foundation of the recovery mechanism. This form of backup has the drawback of slowing down database
operations.
Advantages:
a. It is useful when the user needs to restore the complete database in a short period.
b. They provide details of transactions and changes made in databases.
Disadvantage:
a. This slows down database operations.
2.Logical Backup:
It contains logical data which is retrieved from the database. It contains a view, procedure, function, and
table. This is useful When users want to restore or transfer a copy of the database to a different location.
Logical backups are not as secure as physical backups in preventing data loss. It only provides structural
details. Every week, complete logical backups should be performed. Logical backups are used as a
supplement to a physical backup.

Advantages:
a. This is useful when the user needs to restore the complete database to the last time.
b. It was more complex and provides granular recovery capabilities.
Disadvantages:
a. Critical for recovery of special components.
b. less secure compared to physical backup.
c. It only provides structural details.
Physical Backup Vs Logical Backup:
Physical Backup Logical Backup
Physical database backups are backups of Logical database backups are backups of logical
physical files that are used to store and files that are retrieved from the database.
recover databases.
It contains data files, control files, and archived It contains a view, a procedure, a function, and a
redo logs. table.
It copies data files when data is running or Using the EXPORT keyword Logical backup is

DBMS-2080 Page 8
Unit-7: Database Backup , Recovery & Security

stopped. done
A user needs to restore the complete database in a This is useful when users want to restore or
short period of time. transfer a copy of the database to a different
location.
More secure than logical backup. Less secure as compared to Physical backup.

7.3:Reasons for Database Failure


Definition
 Failure in terms of a database can be defined as its inability to execute the specified
transaction or loss of data from the database.
 A DBMS is vulnerable to several kinds of failures and each of these failures needs to be
managed differently.
 There are many reasons that can cause database failures such as network failure, system
crash, natural disasters, carelessness, sabotage(corrupting the data intentionally),
software errors, etc.
Failure Classification in DBMS
A failure in DBMS can be classified as:

Transaction Failure:
If a transaction is not able to execute or it comes to a point from where the transaction becomes
incapable of executing further then it is termed as a failure in a transaction.
Reason for a transaction failure in DBMS:

DBMS-2080 Page 9
Unit-7: Database Backup , Recovery & Security

1. Logical error: A logical error occurs if a transaction is unable to execute because of some
mistakes in the code or due to the presence of some internal faults.
2. System error: Where the termination of an active transaction is done by the database system
itself due to some system issue or because the database management system is unable to proceed
with the transaction. For example– The system ends an operating transaction if it reaches a
deadlock condition or if there is an unavailability of resources.
System Crash:
 A system crash usually occurs when there is some sort of hardware or software breakdown. Some
other problems which are external to the system and cause the system to abruptly stop or
eventually crash include failure of the transaction, operating system errors, power cuts, main
memory crash, etc.
 These types of failures are often termed soft failures and are responsible for the data losses in the
volatile memory. It is assumed that a system crash does not have any effect on the data stored in
the non-volatile storage and this is known as the fail-stop assumption.
Data-transfer Failure:
 When a disk failure occurs amid data-transfer operation resulting in loss of content from disk
storage then such failures are categorized as data-transfer failures. Some other reason for disk
failures includes disk head crash, disk unreachability, formation of bad sectors, read-write errors
on the disk, etc.

DATABASE FAILURE | CAUSES OR REASONS OF DATABASE FAILURE


 Failure in terms of a database can be defined as its inability to execute the specified
transaction or loss of data from the database.
 Database failure can be resulted due to a variety of reasons such as disk-crash, power failure,
software error or even sabotage.
The Causes (Reasons) of Database failure are:
a. System Crashes
b. User Error
c. Carelessness
d. Sabotage (intentional corruption of data)
e. Statement Failure
f. Application software errors
g. Network Failure
h. Media Failure

DBMS-2080 Page 10
Unit-7:
7: Database Backup , Recovery & Security

i. Natural Physical Disasters


Explanation
a. System Crash: System crash can result in loss of memory due to hardware or a software error.
b. User Errors: A user error occurs, for examples, when a user unintentionally deletes a row or
drops a table.
c. Carelessness: It is damage done to data or facilities by operators
operators or users due to their lack of
concentration on the task at hand.

d. Sabotage: It is the intentional damage done to data, hardware or software facilities.


e. Application Software Errors: It include logical errors in the program that is accessing the
database, which causes one or more transaction to fail.Network
fail. Failure: Network failures such
as communication software failures will interrupt the normal operation of the database system.
f. Media Failure: Media failure are the most dangerous failure. A typical example of a media
failure is a disk controller failure or disk head crash, which causes all databases residing on that
disk/disks to be lost.
g. Natural Disasters: Natural disasters are the damage caused to data, hardware and software due to
natural disasters like fire, floods, earthquakes, power failure etc.
7.4:
7.4:Database Backup Methods
 A full backup (or Image), differential and incremental are the three methods for backing up
data.
1.Full Database Backups
 A full backup (as its name implies) is a method to copy all data including user data, system files, and

DBMS-2080 Page 11
Unit-7: Database Backup , Recovery & Security

configuration files from one location to another. Since it makes a copy of everything, it’s a time
consuming process.
 With a full backup, all data is backed up to a target drive or disk with each backup. This means that
all documents and files are stored in one file, which makes working with the backups and managing
them simple.
Pros:
Creating such a backup is quicker than a differential or incremental backup.
Managing them is easier as only one file needs to be restored.
Cons:
A regular full backup requires much more space than a differential or incremental backup.

2.Incremental Database Backups


 Incremental backups make a copy of updated or newly created files since its last normal backup
in an iterative manner. This type of backup compares the state change since the last incremental
backup. This type of backup is best when the restoration requires you to store recent changes in
smaller chunks.
 Incremental means the backup is done in a shorter time with a smaller amount of data, hence this
backup method is faster and requires less space. If the entire data set is lost, both backups (i.e full
and incremental backup data) are required for a full database restoration.
 Pros:
A regular differential backup requires much less space than a full backup or differential backup.
 Cons:
Restoring such a backup is slower than a full backup or differential backup.
Managing them is more complex as all the files from a backup “chain” are required for a
restoration.
3.Differential Database Backups
 With a differential backup, only the changed or new data since the last full backup will be backed
up. This means of course that an initial full backup is required first so that the software knows
which documents are new or changed. When restoring such a backup both the “base” backup and
the differential backup files need to be restored.
Pros:
A regular differential backup requires much less space than a full backup.
Cons:
Restoring such a backup is slower than a full backup.
Managing them is harder as two files are required.

DBMS-2080 Page 12
Unit-7: Database Backup , Recovery & Security

7.6 : Database security

https://www.w3schools.in/dbms/database-security
https://phoenixnap.com/kb/database-security
https://www.javatpoint.com/database-security
https://intellipaat.com/blog/database-security/

What Is Database Security?


 Database security is the practice of protecting files within a database from accidental and
malicious threats.
Why Is Database Security Important?
Here's why maintaining high levels of database security is a priority for companies:
a. Keep sensitive data safe. Database security protects valuable files (PII, customer records,
financial data, business transactions, trade secrets, proprietary practices, etc.) from unauthorized
modification or deletion.
b. Maintain data availability. If someone takes down a database (willingly or accidentally), all
systems that rely on the database stop operating. Such an event often disrupts business
continuity and causes downtime, impacting your ability to generate revenue.
c. Meet compliance obligations. Many industries have regulatory requirements for database
security. Failure to comply with regulations results in penalties and legal consequences. The
strictest regulations (such as SAO, PCI, HIPAA, or GDPR) come with fines that can exceed
several million dollars per violation.
d. Prevent data breaches. Database security reduces the risk of breaches and their consequences.
A failure to maintain the confidentiality of data often leads to a loss of customer trust, high
recovery costs, and legal problems.
Best Practices for Database Security or Database Security Technique
 Businesses and organizations heavily rely on databases, which store sensitive and confidential
information requiring protection against unauthorized access and data breaches. Implementing
robust security measures becomes imperative to ensure the safety of this valuable data and
mitigate potential threats effectively.

DBMS-2080 Page 13
Unit-7: Database Backup , Recovery & Security

Numerous techniques are employed to safeguard databases against diverse threats, such as the
following:
Or
Here are the data security best practices that will help you to know how to secure your database:

1.Use Strong Passwords


Passwords serve as the initial barrier against unauthorized entry into databases. Using weak
passwords makes it easier for hackers to gain access to sensitive information. It is crucial to use
strong passwords that are complex and difficult to guess. A robust password comprises a blend of
uppercase and lowercase letters, numbers, and special characters. It is also essential to change
passwords frequently, especially when an employee leaves the company. This ensures the previous
employee cannot access the database using their old credentials.

Moreover, it is advisable to use two-factor authentication to add an extra layer of security. Two-
factor authentication necessitates users to present two types of identification, such as a password
combined with either a security token or biometric authentication. This reduces the risk of
unauthorized access to databases, even if the password is compromised.
2.Limit Access
Limiting access to databases is an effective way to prevent unauthorized access to sensitive data. Not
all employees or users require access to all the data stored in the database. It is important to restrict
access based on the principle of least privilege, which means granting only the minimum access
required to perform a particular task. For example, if an employee requires access to customer data,
they should only have access to that section of the database.

DBMS-2080 Page 14
Unit-7: Database Backup , Recovery & Security

Monitoring and tracking user activity within the database is crucial to promptly identifying and
reporting unauthorized access or suspicious behavior. By diligently monitoring and auditing user
activity, potential security gaps or vulnerabilities within the database can be detected, allowing for
timely remediation and fortification of the system.
3.Update and Patch Regularly

Keeping the database software up-to-date is crucial to preventing any potential security
vulnerabilities. Regular updates and patches ensure that any known security vulnerabilities are fixed.
It shows that the database is protected against potential threats. Failure to apply updates and patches
can result in security breaches that compromise sensitive data.
Maintaining a backup of the database is vital to guaranteeing the ability to restore data in the event of
data loss or corruption. It is essential to schedule regular backups and securely store them in a
protected location to prevent unauthorized access and ensure data integrity.
4.Monitor for Anomalies
Detecting and preventing potential security threats can be effectively achieved through the monitoring
of database anomalies. Anomaly detection entails the continuous observation of database activities to
identify any unusual or abnormal behavior that deviates from the expected norms. For instance, a
sudden surge in database traffic or an unauthorized attempt to access restricted data can be flagged as
anomalous activity. This will trigger appropriate actions for investigation and mitigation.

There is a wide range of tools accessible for monitoring and identifying irregularities in
databases.These tools include intrusion detection systems, network traffic analyzers, and log analysis
tools. These tools play a crucial role in recognizing potential data security risks and implementing
suitable measures

5.Data Encryption:
Data encryption is a technique used to protect data stored in a database from unauthorized access by
encrypting it using encryption algorithms. Encryption ensures that even if an unauthorized individual
gains access to the data, they cannot read or use it.

Data encryption is important as it ensures that sensitive data is protected even if it falls into the
wrong hands. Encryption also helps prevent data breaches and unauthorized access to the database.

DBMS-2080 Page 15
Unit-7: Database Backup , Recovery & Security

6.PhysicalSecurity:
Physical security safeguards the database against unauthorized access or theft through tangible
means. Measures such as securing the server room, implementing access controls for the data center,
and employing security cameras and alarms are integral components of physical security.

The significance of physical security lies in its ability to safeguard the database hardware from
physical harm or theft, ensuring its integrity and availability. Additionally, physical security plays a
vital role in preventing unauthorized individuals from gaining access to the database servers or
storage devices, thereby fortifying overall data security.

7.Network Security:
Network security pertains to safeguarding the database against unauthorized network access.
Network security measures include using firewalls, intrusion detection systems, and encryption.

Network security is important as it ensures that data is transmitted securely over the network.
Unauthorized individuals cannot intercept or modify the data in transit. Network security also helps
protect the database from external attacks such as hacking and malware.

7.7:Common Threats in Database


What is threat ?
 A threat, in the context of computer security, refers to anything that has the potential (capacity) to
cause serious harm to a computer system.
 A threat is something that may or may not happen, but has the potential to cause serious damage.
Threats can lead to attacks on computer systems, networks and more.
 Threats can include everything from viruses, Trojans, back doors to outright attacks from
hackers.
 According to technology vendor Application Security, Inc., the following are the top 10 threats
related to databases:
1. Default or weak passwords
2. SQL injection
3. Excessive user and group privileges
4. Unnecessary DBMS features enabled
5. Broken configuration management
6. Buffer overflows
7. Privilege escalation
8. Denial of service
DBMS-2080 Page 16
Unit-7: Database Backup , Recovery & Security

9. Un-patched RDBMS
10. Unencrypted data

Database Security Threats Explanation


Below are the most common and dangerous types of threats you must account for when creating a
database security strategy.
SQL Injections
 SQL injections are a type of cyberattack in which a threat actor inserts malicious code into a
Structured Query Language (SQL) statement through an input field. If there's a
vulnerability(weakness) in the database code, the hacker bypasses the authentication and gains
the ability to issue commands to the database.
 SQL injections enable intruders to perform one (or several) of the following activities:
 Modify, steal, or delete data.
 Create new vulnerabilities to set up more advanced threats (e.g., an APT attack).
 Gain root access to the system.
 Compromise backend infrastructure.
Malware
Malware (short for malicious software) is an umbrella term for software-based threats
(ransomware, spyware, adware, trojans, worms, viruses, etc.). Criminals introduce malware to databases in
various ways, such as through:

 Infected software.
 An endpoint device connected to the database's network.
 Drive-by downloads.
 Trojans hidden within email attachments.
 Unauthorized on-site access to the database server.

Malware causes damage to the database in different ways. Malicious programs enable hackers to perform
one (or several) of the following actions:
 Steal sensitive data.
 Destroy or modify files (e.g., a ransomware attack that encrypts all files within the database).
 Disrupt associated systems by crashing the database server.
 Create backdoors in the database.
 Gain remote access.
 Spy on data or keyboard inputs.

Human Error
Here are a few mistakes that that has been done by human :

 Using and reusing weak passwords.


 Sharing passwords with other employees.
 Installing infected software on a BYOD device.
DBMS-2080 Page 17
Unit-7: Database Backup , Recovery & Security

 Granting access to the wrong person (within or outside the organization).


 Sending messages with sensitive data to private social media or email accounts.
 Using shadow IT devices to transfer data from or to the database.
 Accidental erasure or corruption of data.

DMS Vulnerabilities
Criminals continually search for vulnerabilities in database management software (DMS). While companies
behind both commercial and open-source database platforms release patches regularly, many users fail to
install updates quickly enough to stop hackers from exploiting flaws.

There's also the danger of so-called zero-day attacks. These incidents occur when hackers discover an
exploitable vulnerability in the DMS before the database vendor has a chance to patch the issue.

Password Attacks

Most databases use passwords as the main method of user authentication. Criminals often use bots to
attempt to guess passwords, an approach that has several strategies:

 Brute-force attacks (systematically attempting to guess a password by trying different combinations


of characters).
 Dictionary attacks (going through a list of commonly used words and phrases to attempt to guess the
password).
 Password spraying attacks (using a list of commonly used credentials).
 Credential stuffing attacks (using a set of stolen usernames and passwords from other websites or
databases to attempt to gain access).

Some criminals prefer stealing passwords instead of guessing them. Phishing, a form of social engineering,
is the go-to technique for stealing credentials. Phishing attackers typically try to trick employees into typing
in usernames and passwords in a fake login form.

Denial of Service (DoS) Attacks

A Denial of Service (DoS) occurs when an attacker overwhelms the database server with fake requests. The
server then becomes unable to fulfill legitimate requests, causing the device to crash or become unstable.

DoS attacks typically have one of two goals:

 Halt business operations by causing resource exhaustion.


 Distract the security team while attackers perform other tasks (such as exfiltrating data or setting up
malware).

A more challenging form of DoS is the distributed denial of service (DDoS) attack. In a DDoS, the fake
traffic arrives from multiple servers (often malware-infected devices from unsuspecting users), making it
significantly more difficult for the security team to stop the attack.

DBMS-2080 Page 18
Unit-7: Database Backup , Recovery & Security

7.5:Concept of Recovery

Introduction
 Data recovery is the process of restoring lost, corrupted, accidentally deleted, or otherwise
inaccessible data to its server, computer, mobile device, or storage device.
 Database recovery is the process of restoring the database to a correct (consistent) state in the
event of a failure.
Types of Recovery Techniques in DBMS
 Database recovery techniques are used in database management systems (DBMS) to restore a
database to a consistent state after a failure or error has occurred. The main goal of recovery
techniques is to ensure data integrity and consistency and prevent data loss.
 There are mainly two types of recovery techniques used in DBMS
1. Rollback/Undo Recovery Technique
2. Commit/Redo Recovery Technique
1.Rollback/Undo Recovery Technique
 The rollback/undo recovery technique is based on the principle of backing out or undoing the
effects of a transaction that has not been completed successfully due to a system failure or error.
 This technique is accomplished by undoing the changes made by the transaction using the log
records stored in the transaction log.
 The transaction log contains a record of all the transactions that have been performed on the
database. The system uses the log records to undo the changes made by the failed transaction and
restore the database to its previous state.

2.Commit/Redo Recovery Technique


 The commit/redo recovery technique is based on the principle of reapplying the changes made by a
transaction that has been completed successfully to the database.
 This technique is accomplished by using the log records stored in the transaction log to redo the
changes made by the transaction that was in progress at the time of the failure or error.
 The system uses the log records to reapply the changes made by the transaction and restore the
database to its most recent consistent state.

DBMS-2080 Page 19
Unit-7: Database Backup , Recovery & Security

Assignment
1.Define backup.Write the advantages and disadvantages of backup.Differentiate between backup and
recovery.
3.List and explain the different the different types of Database Backup . Differentiate between physical
and logical backup. (VVI)
4.List and explain the reasons for Database Failure.
5.List and Explain the different methods of Database backup. (VVI)
6.Define Database Security.Explain,How can you secure the database from threats.
Or
Explain Database Security Techniques.
7.What is Threat ? List and explain the some common threats in database. (VVI)
8.Define database recovery .Explain database recovery technique

DBMS-2080 Page 20

You might also like