0% found this document useful (0 votes)
7 views135 pages

DBMS Unit 2

The document outlines a course on Database Management Systems (DBMS) taught by Dr. Gitanjali, focusing on data structures, integrity, and SQL query construction. It details course outcomes and objectives, including understanding database concepts, using DDL, DML, and DCL commands, and analyzing transaction processing techniques. Key topics include functional dependencies, normalization, and various types of dependencies, emphasizing their importance in maintaining data integrity and optimizing database performance.

Uploaded by

JAYESH SAINI
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)
7 views135 pages

DBMS Unit 2

The document outlines a course on Database Management Systems (DBMS) taught by Dr. Gitanjali, focusing on data structures, integrity, and SQL query construction. It details course outcomes and objectives, including understanding database concepts, using DDL, DML, and DCL commands, and analyzing transaction processing techniques. Key topics include functional dependencies, normalization, and various types of dependencies, emphasizing their importance in maintaining data integrity and optimizing database performance.

Uploaded by

JAYESH SAINI
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/ 135

UNIVERSITY INSTITUTE OF

ENGINEERING

Department of Computer Science & Engineering

Databases Management System


(23CSH-205/23ITH-205)
DR. GITANJALI
E16525
ASSISTANT PROFESSOR DISCOVER . LEARN . EMPOWER
CSE
ABOUT COURSE
• The main DBMS course, we learn about the structural formation of data, maintain data
integrity, multitasking with concurrent access and recovery without occurring crashes,
data structures, data models etc. and their working in which every organization is based.
• At the end of the course, the students may understand the concepts of data structures,
data models and design, construction of queries by using SQL, uses and applications of
database design etc

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OUTCOMES

CO No. Title BT Level Will be covered in


this lecture
Students will be able to understand the database system
CO1
concepts and design databases for different applications.
Memorize

Students will be able to identify different types of DDL, DML,


CO2 DCL and TCL commands and their usage. Understanding

Students will be able to classifying Normalization,


CO3 Dependencies and Denormalization along with their Apply
requirements.
Students will be able to analyse and Compare Transaction
CO4
Processing techniques and Recovery techniques.
Analyze
Students will be able to evaluate database performance after
CO5 implementing Triggers, procedures, packages, cursors and Evaluate
views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OBJECTIVES

This course will enable students to,

1. To have good understanding of database system concepts and design databases for
different applications.
2. To learn how to use a DBMS and RDBMS.
3. To implement and understand different types of DDL, DML and DCL statements.
4. To understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024
Topics covered

Functional dependencies and Normalization:


Functional dependencies, Decomposition, Full Functional Dependency (FFD),
Transitive Dependency (TD), Join Dependency (JD), Multi-valued Dependency
(MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Key terms
Here, are some key terms for functional dependency:

Key Terms Description


Axiom Axioms is a set of inference rules used to infer all the functional
dependencies on a relational database.

Decomposition It is a rule that suggests if you have a table that appears to


contain two entities which are determined by the same primary
key then you should consider breaking them up into two
different tables.

Dependent It is displayed on the right side of the functional dependency


diagram.
Determinant It is displayed on the left side of the functional dependency
Diagram.
Union It suggests that if two tables are separate, and the PK is the
same, you should consider putting them. together

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Types of Dependency

▪ Functional Dependency
▪ Partial Dependency
▪ Fully Functional Dependency
▪ Multivalued dependency
▪ Transitive dependency

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency
A dependency function (FD) is a database constraint
that determines the relationship of one attribute to
another in a database management system
(DBMS). Functional dependencies help maintain the
quality of data in the database. Functional
dependence is a relationship that exists between
two attributes. It usually exists between the primary
key and non-prime attributes in the table.
Example: X -> Y
In this case, the left side of the arrow is the
determinant and the right of the arrow is dependent.
X will be the primary attribute and Y will be a non-
prime attribute of the table. It shows that the
attributes of column X uniquely identify the attribute
of column Y to satisfy this functional dependency.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Functional Dependency
How to represent functional dependency in DBMS?
Functional Dependency is expressed in the form
of equations. Here you have an array of objects
(A, B, C, etc.) and an arrow (->) that represents
the dependency. For example, if we have an
employee record with fields "EmployeeID",
"FirstName" and "LastName", we can specify the
function as follows:

EmployeeID -> FirstName, LastName


To represent functional dependency in DBMS
has two main features: left (LHS) and right
(RHS) of the arrow (->).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency
Types of Functionality in DBMS
Trivial Functional Dependency: Trivial functional dependency occurs when an
attribute or attributes (columns) appear in the DBMS. When the (->) function is
present, the left element (LHS) already determines the right element (RHS)
without additional information.
Example: Suppose we have a student table with attributes "EmployeeID" and
"EmployeeName". In this case, if we declare the dependency function as
EmployeeID -> EmployeeName
It will be a rivial functional dependency. Because there is only one
"EmployeeName" in "EmployeeID". That is, the value of "EmployeeID"
determines the value of "EmployeeName" without the need for any additional
information or conditions.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency
Non-trivial Functional Dependency
Non-trivial functional dependencies are a type of dependency between attributes
(columns) in a table. Here the relationship is neither obvious nor important. It
provides valuable information about how values ​in one feature set determine
values ​in another feature set.
Example: Here is the functional dependency based on the employee's date of
birth (EmployeeDOB) and Department. In this case the non-trivial dependency
will be
EmployeeDOB, Department -> EmployeeName.
In this functional dependency given combination of employee's date of birth and
class can uniquely determines employee names. It is important because it
provides important information about the relationship between attributes in the
language.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency
Properties of Functional Dependency in DBMS
• Reflexivity: If A is a set of attributes and B is a part of A, then the function
A -> B is valid.
• Augmentation: If the A -> B dependency is valid, adding multiple elements
to either side of the dependency will not affect the dependency.
• Transitivity: If the functions X → Y and Y → Z are both valid, then X → Z
is also valid according to the transitivity rule.
Advantages of functional dependency in DBMS
• Data Integrity
• Normalization
• Effective Storage
• Easy to maintain

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Decomposition
What is Decomposition in DBMS?
When we divide a table into multiple tables or divide a relation into multiple
relations, then this process is termed Decomposition in DBMS. We perform
decomposition in DBMS when we want to process a particular data set. It is
performed in a database management system when we need to ensure
consistency and remove anomalies and duplicate data present in the
database. When we perform decomposition in DBMS, we must try to ensure
that no information or data is lost.
Types of Decomposition
There are two types of Decomposition:
• Lossless Decomposition
• Lossy Decomposition

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Decomposition
Properties of Decomposition
• Lossless: All the decomposition that we perform in Database
management system should be lossless. All the information should not be
lost while performing the join on the sub-relation to get back the original
relation. It helps to remove the redundant data from the database.
• Dependency Preservation: Dependency Preservation is an important
technique in database management system. It ensures that the functional
dependencies between the entities is maintained while performing
decomposition. It helps to improve the database efficiency, maintain
consistency and integrity.
• Lack of Data Redundancy: Data Redundancy is generally termed as
duplicate data or repeated data. This property states that the
decomposition performed should not suffer redundant data. It will help us
to get rid of unwanted data and focus only on the useful data or
information.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Fully Functional Dependency
What is Fully Functional Dependency?
Real-life dependencies emerge when, within one relationship, the value of
one of the attributes is capable of identifying the value of another attribute.
Simply put, if A fully determines B without any proper subset of A giving full
cause to B, then A is said to fully depend on B. In other words, you cannot
derive B from a smaller part of A.
An Example of Fully Functional Dependency
To visualize a relation called "Employee” with
the attributes {Employee_ID, Employee_Name,
Department, Salary}, use the following diagram:
Basically, if that the Employee_ID attribute
figures out the other attributes in the table, such
as the Employee_Name, Department, and
Salary, the Employee_ID attribute fully
functionally depends on all these attributes.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Fully Functional Dependency
Importance of Fully Functional Dependency
• Data Integrity: Instantaneous reference derives ones and only one value from
others on the elements, which provides data integrity.
• Normalization: Finding and ensuring all the relationships are incomplete for
one (and only one) of the attributes is the central part of the normalization
process. It plays a role in recording large tables into smaller, structured tables,
which reduces redundancy and helps people to handle transactions
successfully.
• Efficient Queries: Databases with well-thought-out dependencies that provide
maximum functionality are usually the ones that tend to outperform in query
response. In the data has a logical structure, the search process becomes
faster as time goes by.
• Update Anomalies: All major operations will be enabled by this through
reduced anomalies in updates. An update anomaly happens when performing
an action on one attribute results in the ratification other attributes or errors.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Functional Dependency

Types of Dependencies
• Partial Dependency
• Full Dependency
• Transitive Dependency
A Partial Dependency occurs when a non-prime attribute (an attribute that is
not part of any candidate key) depends on part of a composite primary key
rather than the whole key.
OrderID, ProductID, ProductName, Quantity
OrderID and ProductID together form the composite primary key.
ProductName depends only on ProductID (not on the whole composite key).
This is a Partial Dependency because ProductName is determined by a part of
the composite key (ProductID), not the entire key.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency

A Full Dependency occurs when a non-prime attribute depends on all parts of


a composite primary key and not just a subset.
Example:
• OrderID, ProductID, Quantity
Quantity depends on both OrderID and ProductID.
This is a Full Dependency because Quantity depends on the entire composite
primary key.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency
Transitive Dependency
• If the value of a non-primary attribute can be defined using another non-
primary attribute, then it is called a transitive dependency.
• When any attribute does not require primary key and can easily get value
using another non-primary attribute then it is called as Transitive
Dependency.

Example
Let's take an example, we have a table where we have
columns of student roll number, name, city where student live,
and zip-code of city .

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Functional Dependency

• Here the primary key is roll_no but we can


identify the city using zip-code where city
and zip-code both are the primary key
• So here roll_no → city and city→zip-code
eventually resulting into roll_no →zip-code.
so, we can find a non-primary attribute
using another non-primary attribute. For
example, roll-no = 1 has city=pune and
city=pune will have zip-code=411044.So
wherever city is pune , zip-code will be
411044
• This type of functional Dependency is
known as Transitive Dependency.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Join Dependency
• Join Dependency (JD) can be illustrated as when the relation R is equal to
the join of the sub-relations R1, R2,..., and Rn are present in the database.
Join Dependency arises when the attributes in one relation are dependent
on attributes in another relation, which means certain rows will exist in the
table if there is the same row in another table. Multiple tables are joined to
create a single table where one of the attributes is common in the sub-
tables. We can also relate the join dependency to the 5th Normal Form. A
join dependency is said to be not that important if any relational schemas in
the join dependency are equivalent to the original relation R.
• Join dependency on a database is denoted by:
R1 ⨝ R2 ⨝ R3 ⨝ ..... ⨝ Rn ;

where R1 , R2, ... , Rn are the relations and ⨝ represents the natural join
operator.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Join Dependency
Types of Join Dependency
There are two types of Join Dependencies:
• Lossless Join Dependency: It means that whenever the join occurs between the tables,
then no information should be lost, the new table must have all the content in the original
table.
• Lossy Join Dependency: In this type of join dependency, data loss may occur at some
point in time which includes the absence of a tuple from the original table or duplicate
tuples within the database.
Importance of Join Dependencies
Data Integrity: Join Dependency helps maintain data integrity in a database. Database
designers can make sure that the queries are consistent after checking for the dependencies.
This will remove the data that is not accurate.
Query Optimization: Query optimization leads to improving the performance of the database
system. The database designers can choose the best join order to execute the queries which
in turn reduces the computational costs, memory utilization and i/o operations to get the
queries executed quickly.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Multivalued Dependency
Multivalued Dependency
When one attribute in a database depends on another attribute and has many
independent values, it is said to have multivalued dependency (MVD). It
supports maintaining data accuracy and managing intricate data interactions.
Example of Multivalued Dependency:

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Introduction to Normalization
• Normalization: Process of decomposing unsatisfactory "bad"
relations by breaking up their attributes into smaller relations
• Normalization is the process of removing redundant data from your
tables in order to improve storage efficiency, data integrity and
scalability.
• This improvement is balanced against an increase in complexity and
potential performance losses from the joining of the normalized tables
at query-time.
• There are two goals of the normalization process: eliminating
redundant data (for example, storing the same data in more than one
table) and ensuring data dependencies make sense (only storing
related data in a table). Both of these are worthy goals as they reduce
the amount of space a database consumes and ensure that data is
logically stored.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Introduction to Normalization
Normal form: Condition using keys and FDs of a relation to certify
whether a relation schema is in a particular normal form
– 2NF, 3NF, BCNF based on keys and FDs of a relation schema
– 4NF based on keys, multi-valued dependencies
There is a sequence to normal forms:
– 1NF is considered the weakest,
– 2NF is stronger than 1NF,
– 3NF is stronger than 2NF, and
– BCNF is considered the strongest

Also,
– any relation that is in BCNF, is in 3NF;
– any relation in 3NF is in 2NF; and
– any relation in 2NF is in 1NF.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
WHY WE NEED NORMALIZATION?
Normalization is the aim of well design Relational Database Management
System (RDBMS). It is step by step set of rules by which data is put in its
simplest forms. We normalize the relational database management
system because of the following reasons:

❑ Minimize data redundancy i.e. no unnecessarily duplication of data.


❑ To make database structure flexible i.e. it should be possible to add
new data values and rows without reorganizing the database
structure.
❑ Data should be consistent throughout the database i.e. it should not
suffer from following anomalies.
❑ Complex queries required by the user should be easy to handle.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Types of Normalization

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


1 Normal Form
As per the rule of first normal form, an attribute (column) of a table cannot
hold multiple values. It should hold only atomic values.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


First Normal Form
• Disallows composite attributes, multivalued attributes, and nested
relations; attributes whose values for an individual tuple are non-
atomic.

• We say a relation is in 1NF if all values stored in the relation are


single-valued and atomic.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Normal Forms in DBMS

Example 1 – Relation STUDENT in table 1 is not in 1NF because of multi-


valued attribute STUD_PHONE. Its decomposition into 1NF has been shown in
table 2.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


First Normal Form (1NF)

• An outer join between Employee and EmployeeDegree will produce


the information we saw before

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Implication of INF

❖ You must have single value in a single column, null value can be

present.

❖ Order of row and order of column is insignificant.

❖ Every value must belong to same domain.

❖ Every column should have unique name.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Second Normal Forms
A relation is in 2NF if it is in 1NF and any non-prime attribute (attributes which
are not part of any candidate key) is not partially dependent on any proper
subset of any candidate key of the table. In other words, we can say that, every
non-prime attribute must be fully dependent on each candidate key.
A functional dependency X->Y (where X and Y
are set of attributes) is said to be in partial Second Normal Form
dependency, if Y can be determined by any
proper subset of X.
However, in 2NF it is possible for a prime
attribute to be partially dependent on any
candidate key, but every non-prime attribute
must be fully dependent(or not partially
dependent) on each candidate key of the table.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Example 2 – Consider following functional dependencies in relation R (A, B , C,
D)

In the above relation, AB is the only candidate key and there is no partial
dependency, i.e., any proper subset of AB doesn’t determine any non-prime
attribute.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Second Normal Form

• A relation is in 2NF if it is in 1NF, and every non-key attribute


is fully dependent on each candidate key. (That is, we don’t
have any partial functional dependency.)

• 2NF (and 3NF) both involve the concepts of key and non-key
attributes.

• A key attribute is any attribute that is part of a key; any


attribute that is not a key attribute, is a non-key attribute .

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Second Normal Form

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Third Normal Form

A relation is in the third normal form, if there is no transitive dependency for


non-prime attributes as well as it is in the second normal form. A relation is in
3NF if at least one of the following conditions holds in every non-trivial function
dependency X –> Y.
X is a super key.
Y is a prime attribute (each element of Y is part of some candidate key).

In other words,
A relation that is in First and Second Third Normal Form
Normal Form and in which no non-primary-
key attribute is transitively dependent on the
primary key, then it is in Third Normal Form
(3NF).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Third Normal Form
The normalization of 2NF relations to 3NF involves the removal of transitive
dependencies. If a transitive dependency exists, we remove the transitively
dependent attribute(s) from the relation by placing the attribute(s) in a new
relation along with a copy of the determinant. Consider the examples given
below. Y is a prime attribute (each element of Y is part of some candidate key).

Example 2: Consider Relation R(A, B, C, D, E)

All possible candidate keys in above relation are


{A, E, CD, BC} All attribute are on right sides of
all functional dependencies are prime.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Third Normal Form
• A database is in third normal form if it satisfies the following
conditions:
• It is in second normal form
• There is no transitive functional dependency

Definition
• Transitive functional dependency – if there a set of atribute
Z that are neither a primary or candidate key and both X –> Z
and Y –> Z holds.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Third Normal Form
• In the table able, [Book ID] determines [Genre ID], and [Genre ID]
determines [Genre Type]. Therefore, [Book ID] determines [Genre
Type] via [Genre ID] and we have transitive functional dependency,
and this structure does not satisfy third normal form.
• To bring this table to third normal form, we split the table into two as
follows:

Now all non-key attributes are fully functional dependent only on the primary
key. In [TABLE_BOOK], both [Genre ID] and [Price] are only dependent on
[Book ID]. In [TABLE_GENRE], [Genre Type] is only dependent on [Genre ID].

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


The difference between 3NF and BCNF is that for a functional dependency A🡪 B,
3NF allows this dependency in a relation if B is a primary-key attribute and A is not
a candidate key, Whereas BCNF insists that for this dependency to remain in a
relation, A must be a candidate key.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


EXAMPLE
Consider this table:

FD set:
{STUD_NO -> STUD_NAME, STUD_NO -> STUD_STATE, STUD_STATE -
> STUD_COUNTRY, STUD_NO -> STUD_AGE}

Candidate Key:
{STUD_NO}

For this relation in table , STUD_NO -> STUD_STATE and STUD_STATE ->
STUD_COUNTRY are true. So STUD_COUNTRY is transitively dependent
on STUD_NO. It violates the third normal form. To convert it in third normal
form, we will decompose the relation STUDENT (STUD_NO, STUD_NAME,
STUD_PHONE, STUD_STATE, STUD_COUNTRY_STUD_AGE) as:

STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,


STUD_AGE) STATE_COUNTRY (STATE, COUNTRY)

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


BCNF (Boyce-Codd Normal Form)
For a table to be in BCNF, following conditions must be satisfied:
• R must be in 3rd Normal Form
• and, for each functional dependency ( X -> Y ), X should be a
super Key.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Rules for BCNF
Rule 1: The table should be in the 3rd Normal Form.
Rule 2: X should be a superkey for every functional dependency (FD) X−>Y in a
given relation.

Example
Find the highest normal form of a relation R(A, B, C, D, E) with FD set as:
Explanation:
Step-1: As we can see, (AC)+ ={A, C, B, E, D} but none of its subsets can
determine all attributes of the relation, So AC will be the candidate key. A or C
can’t be derived from any other attribute of the relation, so there will be only 1
candidate key {AC}.
Step-2: Prime attributes are those attributes that are part of candidate key {A, C}
in this example and others will be non-prime {B, D, E} in this example.
Step-3: The relation R is in 1st normal form as a relational DBMS does not allow
multi-valued or composite attributes.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Boyce-Codd Normal Form (BCNF)
Although, 3NF is an adequate normal form for relational databases, this (3NF)
normal form may not remove 100% redundancy because of X−>Y functional
dependency if X is not a candidate key of the given relation. This can be solved
by the Boyce-Codd Normal Form (BCNF).

Boyce-Codd Normal Form (BCNF) An


advanced version of the third normal form,
which is a little more strict. This is a tougher
criterion that helps eliminate redundancy and
anomalies from your Database. BCNF
(Boyce-Codd Normal Form) is a further
refinement of the third normal form which
simplifies our database design.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Example:

For example consider relation R(A, B, C)


A -> BC, B -> A
A and B both are super keys so above relation is in BCNF.

Note –

BCNF decomposition may always not possible with dependency


preserving, however, it always satisfies lossless join condition. For
example, relation R (V, W, X, Y, Z), with functional dependencies:
V, W -> X Y, Z -> X W -> Y
It would not satisfy dependency preserving BCNF decomposition.

Note -:Redundancies are sometimes still present in a BCNF relation as it


is not always possible to eliminate them completely.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Boyce Codd Normal Form
In BCNF:
❖ When a relation has more than one candidate key, anomalies may result even though the
relation is in 3NF.
❖ 3NF does not deal satisfactorily with the case of a relation with overlapping candidate keys
❖ i.e. composite candidate keys with at least one attribute in common.
❖ BCNF is based on the concept of a determinant.
❖ A determinant is any attribute (simple or composite) on which some other attribute is fully
functionally dependent.
❖ A relation is in BCNF is, and only if, every determinant is a candidate key.
❖ Definition: A relation is in Boyce-Codd Normal Form (BCNF) if every determinant is a
candidate key.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


EXAMPLE
It (like, chomsky hierarchy) can be inferred
that every relation in BCNF is also in 3NF. To put it
another way, a relation in 3NF need not to be in
BCNF. Ponder over this statement for a while.
To determine the highest normal form of a given
relation R with functional dependencies, the first
step is to check whether the BCNF condition holds.
If R is found to be in BCNF, it can be safely
deduced that the relation is also in 3NF, 2NF and
1NF as the hierarchy shows. The 1NF has the
least restrictive constraint – it only requires a
relation R to have atomic values in each tuple. The
2NF has a slightly more restrictive constraint. The
3NF has more restrictive constraint than the first
two normal forms but is less restrictive than the
BCNF. In this manner, the restriction increases as
we traverse down the hierarchy.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Fourth Normal Form

• A relation will be in 4NF if it is in Boyce Codd normal form


and has no multi-valued dependency.
• For a dependency A → B, if for a single value of A, multiple
values of B exists, then the relation will be a multi-valued
dependency.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


4th Normal Form
An entity is in Fourth Normal Form (4NF) when it meets the requirement of being in
Third Normal Form (3NF) and additionally: Has no multiple sets of multi-valued
dependencies.

In other words, 4NF states that no entity can have more than a single one-to-many
relationship within an entity if the one-to-many attributes are independent of each
other.

Fourth Normal Form applies to situations involving many-to-many relationships.

In relational databases, many-to-many relationships are expressed through cross-


reference tables.

Definition: A table is in fourth normal form (4NF) if and only if it is in BCNF and
contains no more than one multi-valued dependency.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Example: 4th NF
Consider the database table of a class which has two relations R1 contains
student ID(SID) and student name (SNAME) and R2 contains course id(CID)
and course name (CNAME).

Table – R1(SID, SNAME) Table – R2(CID, CNAME) Table – R1 X R2

SID SNAME CID CNAME SNA CNA


SID CID
ME ME
S1 A C1 C
S1 A C1 C
S2 B C2 D
S1 A C2 D
S2 B C1 C
When there cross product is done it
resulted in multivalued dependencies: S2 B C2 D

Multivalued dependencies (MVD) are:


SID->->CID; SID->->CNAME; SNAME->->CNAME

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Fourth Normal Form
Let us see an example −
<Movie>
Movie_Name Shooting_Loc Listing
The above is not in 4NF,
ation
since
• Many shooting
MovieOne UK Comedy
locations can have the
same movie
MovieOne UK Thriller
• More than one movie
can have the same MovieTwo Australia Action
listing
MovieTwo Australia Crime

MovieThree India Drama

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Fourth Normal Form
Movie_Name Shooting_Location

MovieOne UK

MovieOne UK
Let us convert the
above table in 4NF MovieTwo Australia

− MovieTwo Australia
<Movie_Shooting> MovieThree India
<Movie_Listing>
Now the violation is
Movie_Name Listing
removed and the
tables are in 4NF. MovieOne Comedy

MovieOne Thriller

MovieTwo Action

MovieTwo Crime

MovieThree Drama

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Fifth Normal Form
The 5NF (Fifth Normal Form) is also known as
project-join normal form. A relation is in Fifth EmpName EmpSkills EmpJob (Assig
Normal Form (5NF), if it is in 4NF, and won’t have ned Work)
lossless decomposition into smaller tables.
You can also consider that a relation is in 5NF, if David Java E145
the candidate key implies every join dependency in
it. John JavaScript E146
Example
The below relation violates the Fifth Normal Form Jamie jQuery E146
(5NF) of Normalization −
<Employee> Emma Java E147
The above relation can be decomposed into the
following three tables; therefore, it is not in 5NF −

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Fifth Normal Form
EmployeeSkills> EmpName EmpSkills
The following is the <EmployeeJob> relation that displays
the jobs assigned to each employee − David Java
<EmployeeJob>
Here is the skills that are related to the assigned jobs −
<JobSkills> John JavaScript
Our Join Dependency −
The above relations have join dependency, so they are Jamie jQuery
not in 5NF. That would mean that a join relation of the
above three relations is equal to our original
Emma Java
relation <Employee>.

EmpName EmpJob

David E145
EmpSkills EmpJob
John E146
Java E145
Jamie E146
JavaScript E146
Emma E147
jQuery E146

Java E147

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


5th Normal Form
In 5th NF:

A relation that has a join dependency cannot be decomposed by a projection into


other relations without spurious results
A relation is in 5NF when its information content cannot be reconstructed from
several smaller relations i.e. from relations having fewer attributes than the original
relation
Definition: A table is in fifth normal form (5NF) or Project-Join Normal Form (PJNF)
if it is in 4NF and it cannot have a lossless decomposition into any number of
smaller tables.

Fifth normal form, also known as join-projection normal form (JPNF), states that no
non-trivial join dependencies exist. 5NF states that any fact should be able to be
reconstructed without any anomalous results in any case, regardless of the number
of tables being joined. A 5NF table should have only candidate keys and it's
primary key should consist of only a single column.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


A relation R is in 5NF if and only if every join dependency in R is implied by
the candidate keys of R. A relation decomposed into two relations must
have loss-less join Property, which ensures that no spurious or extra tuples
are generated, when relations are reunited through a natural join.

Properties – A relation R is in 5NF if and only if it satisfies following


conditions:
❖ R should be already in 4NF.
❖ It cannot be further non loss decomposed (join dependency)
Example – Consider the AGENT COMPANY PRODUCT
schema, with a case as “if
A1 PQR Nut
a company makes a
product and an agent is an A1 PQR Bolt
agent for that company,
A1 XYZ Nut
then he always sells that
product for the company”. A1 XYZ Bolt
Under these
A2 PQR Nut
circumstances, the ACP
table is shown as: Table –
ACP
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
The relation ACP is again decompose into 3 relations. Now, the natural Join of all
the three relations will be shown as:
Table – Table – Table –
R1 R2 R3
AGENT COMPANY AGENT PRODUCT COMPANY PRODUCT
A1 PQR A1 Nut PQR Nut
A1 XYZ A1 Bolt PQR Bolt

A2 PQR XYZ Nut


A2 Nut
XYZ Bolt

Result of Natural Join of R1 and R3 over ‘Company’ and then Natural Join of R13
and R2 over ‘Agent’and ‘Product’ will be table ACP.

Hence, in this example, all the redundancies are eliminated, and the
decomposition of ACP is a lossless join decomposition. Therefore, the relation is in
5NF as it does not violate the property of lossless join.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Denormalization in Databases
Denormalization is a database optimization technique in which we add redundant
data to one or more tables. This can help us avoid costly joins in a relational
database. Note that denormalization does not mean ‘reversing normalization’ or
‘not to normalize’. It is an optimization technique that is applied after normalization.
Basically, The process of taking a normalized schema and making it non-
normalized is called denormalization, and designers use it to tune the performance
of systems to support time-critical operations.
How is Denormalization Different From Normalization ?
One side normalization is used for reduce or removing the redundancy which
means there will be no duplicate data or entries in the same table and also
optimizes for data integrity and efficient storage, while Denormalization is used for
add the redundancy into normalized table so that enhance the functionality and
minimize the running time of database queries (like joins operation ) and optimizes
for performance and query simplicity.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Denormalization in Databases
Advantages of Denormalization
• Improved Query Performance: Denormalization can
improve query performance by reducing the number of joins
required to retrieve data.
• Reduced Complexity: By combining related data into fewer
tables, denormalization can simplify the database schema
and make it easier to manage.
• Easier Maintenance and Updates: Denormalization can
make it easier to update and maintain the database by
reducing the number of tables.
• Improved Read Performance: Denormalization can improve
read performance by making it easier to access data.
• Better Scalability: Denormalization can improve the
scalability of a database system by reducing the number of
tables and improving the overall performance.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Denormalization in Databases
Disadvantages of Denormalization
• Reduced Data Integrity: By adding redundant data,
denormalization can reduce data integrity and increase the
risk of inconsistencies.
• Increased Complexity: While denormalization can simplify
the database schema in some cases, it can also increase
complexity by introducing redundant data.
• Increased Storage Requirements: By adding redundant
data, denormalization can increase storage requirements
and increase the cost of maintaining the database.
• Increased Update and Maintenance Complexity:
Denormalization can increase the complexity of updating
and maintaining the database by introducing redundant
data.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


THANK YOU

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


UNIVERSITY INSTITUTE OF
ENGINEERING

Department of Computer Science & Engineering

Databases Management System


(23CSH-205/23ITH-205)
DR. GITANJALI
E16525
ASSISTANT PROFESSOR DISCOVER . LEARN . EMPOWER
CSE
ABOUT COURSE
• The main DBMS course, we learn about the structural formation of data, maintain data
integrity, multitasking with concurrent access and recovery without occurring crashes,
data structures, data models etc. and their working in which every organization is based.
• At the end of the course, the students may understand the concepts of data structures,
data models and design, construction of queries by using SQL, uses and applications of
database design etc

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OUTCOMES

CO No. Title BT Level Will be covered in


this lecture
Students will be able to understand the database system
CO1
concepts and design databases for different applications.
Memorize

Students will be able to identify different types of DDL, DML,


CO2 DCL and TCL commands and their usage. Understanding

Students will be able to classifying Normalization,


CO3 Dependencies and Denormalization along with their Apply
requirements.
Students will be able to analyse and Compare Transaction
CO4
Processing techniques and Recovery techniques.
Analyze
Students will be able to evaluate database performance after
CO5 implementing Triggers, procedures, packages, cursors and Evaluate
views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OBJECTIVES

This course will enable students to,

1. To have good understanding of database system concepts and design databases for
different applications.
2. To learn how to use a DBMS and RDBMS.
3. To implement and understand different types of DDL, DML and DCL statements.
4. To understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024
Topics covered

Database Security:
Introduction, Threats, Counter Measures.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Database Security

• Database Security - protection from malicious attempts to steal (view)


or modify data.
Database Security
Security of databases refers to the array of controls, tools, and procedures
designed to ensure and safeguard confidentiality, integrity, and accessibility.

Security for databases must cover and safeguard


the following aspects:
• The database containing data.
• Database management systems (DBMS)
• Any applications that are associated with it.
• Physical database servers or the database
server virtual, and the hardware that runs it.
• The infrastructure for computing or network
that is used to connect to the database.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Database Security
Security of databases is a complicated and challenging task that requires all
aspects of security practices and technologies. This is inherently at odds with
the accessibility of databases. The more usable and accessible the database
is, the more susceptible we are to threats from security. The more vulnerable it
is to attacks and threats, the more difficult it is to access and utilize.

• Database security ensures that sensitive and


critical data stored in a database is secure from
unauthorized access, misuse, or corruption.
• Security measures include access controls,
encryption, authentication, and auditing
mechanisms.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Why Database Security is Important?
Intellectual property that is compromised: Our intellectual property--trade
secrets, inventions, or proprietary methods -- could be vital for our ability to
maintain an advantage in our industry. If our intellectual property has been
stolen or disclosed and our competitive advantage is lost, it could be difficult to
keep or recover.
The damage to our brand's reputation: Customers or partners may not want
to purchase goods or services from us (or deal with our business) If they do not
feel they can trust our company to protect their data or their own.
The concept of business continuity (or lack of it): Some businesses cannot
continue to function until a breach has been resolved.
Costs for repairing breaches and notifying consumers about them:
Alongside notifying customers of a breach, the company that has been
breached is required to cover the investigation and forensic services such as
crisis management, triage repairs to the affected systems, and much more.
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
Types of Security
Database security is a broad area that addresses many issues, including the following:

■ Various legal and ethical issues regarding the right to access certain information—
for example, some information may be deemed to be private and cannot be accessed
legally by unauthorized organizations or persons.

■ Policy issues at the governmental, institutional, or corporate level as to what kinds of


information should not be made publicly available—for example, credit ratings and
personal medical records.

■ System-related issues such as the system levels at which various security functions
should be enforced—for example, whether a security function should be handled at the
physical hardware level, the operating system level, or the DBMS level.

■ The need in some organizations to identify multiple security levels and to categorize
the data and users based on these classifications—for example, top secret, secret,
confidential, and unclassified.
Levels of Data Security

• Human level: Corrupt/careless User


• Network/User Interface
• Database application program
• Database system
• Operating System
• Physical level

79
Physical/OS Security
• Physical level
– Traditional lock-and-key security
– Protection from floods, fire, etc.
• E.g. WTC (9/11), fires in IITM, WWW conf website, etc.
– Protection from administrator error
• E.g. delete critical files
– Solution
• Remote backup for disaster recovery
• Plus archival backup (e.g. DVDs/tapes)

• Operating system level

– Protection from virus/worm attacks critical


Database Encryption

• E.g. What if a laptop/disk/USB key with critical data is lost?

• Partial solution: encrypt the database at storage level, transparent to


application

– Main issue: key management


• E.g. user provides decryption key (password) when database is
started up

– Supported by many database systems


• Standard practice now to encrypt credit card information, and
other sensitive information
Database/Application Program

• Authentication and authorization


mechanisms to allow specific
users access only to required data

• Authentication:
– who are you? Prove it!

• Authorization:
– what you are allowed to do
Network Security

• Network level: must use encryption to prevent


– Eavesdropping: unauthorized reading of messages
– Masquerading:
• pretending to be an authorized user or legitimate site, or
• sending messages supposedly from authorized users
– Handled by secure http - https://

• Must prevent person-in-the-middle attacks


– E.g. someone impersonates seller or bank/credit card company
and fool's buyer into revealing information
Human level /User Authentication
• Password
– Most users abuse passwords. For e.g.
• Easy to guess password
• Share passwords with others
• Smartcards
– Need smartcard
Bill Gates
– + a PIN or password
Threats

Threat is any intentional or accidental event that may adversely affect the system.

Examples of threats:

- Using another person’s log-in name to access data


- Unauthorized copying data
- Program/Data alteration
- Illegal entry by hacker
- Viruses
- Etc.
Threats to databases
• Loss of integrity. Database integrity refers to the requirement that information
be protected from improper modification. Modification of data includes creation,
insertion, updating, changing the status of data, and deletion. Integrity is lost if
unauthorized changes are made to the data by either intentional or accidental
acts. If the loss of system or data integrity is not corrected, continued use of the
contaminated system or corrupted data could result in inaccuracy, fraud, or
erroneous decisions.

• Loss of availability. Database availability refers to making objects available to


a human user or a program to which they have a legitimate right.

• Loss of confidentiality. Database confidentiality refers to the protection of


data from unauthorized disclosure. The impact of unauthorized disclosure of
confidential information can range from violation of the Data Privacy Act to the
jeopardization of national security. Unauthorized, unanticipated, or unintentional
disclosure could result in loss of public confidence, embarrassment, or legal
action against the organization.

Slide 23- 86
Control methods of Database Security

The following are the main control measures are used to provide security of
data in databases:
1. Authentication
2. Access control
3. Inference control
4. Flow control
5. Database Security applying Statistical Method
6. Encryption

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Authentication:
Authentication is the process of confirmation that whether the user log in only according to the
rights provided to him to perform the activities of data base. A particular user can login only up
to his privilege but he can’t access the other sensitive data. The privilege of accessing
sensitive data is restricted by using Authentication.
By using these authentication tools for biometrics such as retina and figure prints can prevent
the data base from unauthorized/malicious users.

Access Control:
The security mechanism of DBMS must include some provisions for restricting access to the
data base by unauthorized users. Access control is done by creating user accounts and to
control login process by the DBMS. So, that database access of sensitive data is possible only
to those people (database users) who are allowed to access such data and to restrict access
to unauthorized persons.
The database system must also keep the track of all operations performed by certain user
throughout the entire login time.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Access Control Models

1. Discretionary Access Control (DAC)


• Users have control over their own data
• Permissions granted using Access Control Lists (ACLs)

2. Mandatory Access Control (MAC)


• Access decisions based on security labels (classification levels)
• Used in military and government systems

3. Role-Based Access Control (RBAC)


• Permissions assigned based on job roles (e.g., admin, user, guest)
• Reduces administrative overhead
Discretionary Access Control

• The typical method of enforcing discretionary access control in a


database system is based on the granting and revoking privileges.
These are used to grant privileges to users, including the capability to
access specific data files, records, or fields in a specified mode (such
as read, insert, delete, or update).
Mandatory Access Control

• Mandatory Access Control (MAC) in a database system is based on a


central authority assigning security labels to data and users. These
labels define access levels, ensuring that users can only access data
classified within their clearance level. MAC is commonly used in
government and military applications, where strict control over data
access is required.
Role Based Access Control

• Role-Based Access Control (RBAC) in a database system is based


on predefined roles assigned to users. Each role has specific
privileges, including the ability to read, insert, delete, or update
data. Instead of granting permissions to individual users, RBAC
assigns access based on job responsibilities, making it efficient
for large organizations.
Differences between Authentication and Authorization.
Authentication Authorization
It helps decide whether users are what they claim they It helps to decide which user is allowed to access what.
are.
User identity has to be verified via a username and It verifies whether the given access is allowed by following
password or answering a security question. rules and policies.
Typically, this process takes place before Authorization. It takes place once Authentication is completed.

Different ways of Authentication are: Other forms of Authorization are:

Captcha test Permissions – Read & Write access to files and allowing
Biometric Authentication access to the database.
Passwords Deciding the roles of users for accessing data

It’s controlled through the server for finding out who’s The server helps to decide that client has the authority to
accessing what data or site. access a resource or not.
Example: Employees authenticating through the network Example: Once an employee is authenticated, the different
before opening the company mails. system decides which information will be accessed by
which employee.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Inference Control:
This method is known as the countermeasures to
statistical database security problem. It is used to
prevent the user from completing any inference
channel. This method protect sensitive
information from indirect disclosure.
Inferences are of two types, identity disclosure or
attribute disclosure.

Flow Control:
This prevents information from flowing in a way
that it reaches unauthorized users. Channels are
the pathways for information to flow implicitly in
ways that violate the privacy policy of a company
are called convert channels.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Database Security applying Statistical Method:

Statistical database security focuses on the protection of


confidential individual values stored in and used for statistical
purposes and used to retrieve the summaries of values based on
categories. They do not permit to retrieve the individual
information.
This allows to access the database to get statistical information
about the number of employees in the company but not to access
the detailed confidential/personal information about the specific
individual employee.

Encryption:
This method is mainly used to protect sensitive data (such as
credit card numbers, OTP numbers) and other sensitive
numbers. The data is encoded using some encoding algorithms.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Database Security and DBA

• The database administrator (DBA) is the central authority for managing a database
system.

– The DBA’s responsibilities include

• granting privileges to users who need to use the system


• classifying users and data in accordance with the policy of the organization

• The DBA is responsible for the overall security of the database system.
Database Security and DBA

• The DBA has a DBA account in the DBMS


– Sometimes these are called a system or superuser account

– These accounts provide powerful capabilities such as:


• 1. Account creation
• 2. Privilege granting
• 3. Privilege revocation
• 4. Security level assignment

– Action 1 is access control, whereas 2 and 3 are discretionary


access and 4 is used to control mandatory authorization
THANK YOU

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


UNIVERSITY INSTITUTE OF
ENGINEERING

Department of Computer Science & Engineering

Databases Management System


(23CSH-205/23ITH-205)
DR. GITANJALI
E16525
ASSISTANT PROFESSOR DISCOVER . LEARN . EMPOWER
CSE
ABOUT COURSE
• The main DBMS course, we learn about the structural formation of data, maintain data
integrity, multitasking with concurrent access and recovery without occurring crashes,
data structures, data models etc. and their working in which every organization is based.
• At the end of the course, the students may understand the concepts of data structures,
data models and design, construction of queries by using SQL, uses and applications of
database design etc

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OUTCOMES

CO No. Title BT Level Will be covered in


this lecture
Students will be able to understand the database system
CO1
concepts and design databases for different applications.
Memorize

Students will be able to identify different types of DDL, DML,


CO2 DCL and TCL commands and their usage. Understanding

Students will be able to classifying Normalization,


CO3 Dependencies and Denormalization along with their Apply
requirements.
Students will be able to analyse and Compare Transaction
CO4
Processing techniques and Recovery techniques.
Analyze
Students will be able to evaluate database performance after
CO5 implementing Triggers, procedures, packages, cursors and Evaluate
views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


COURSE OBJECTIVES

This course will enable students to,

1. To have good understanding of database system concepts and design databases for
different applications.
2. To learn how to use a DBMS and RDBMS.
3. To implement and understand different types of DDL, DML and DCL statements.
4. To understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


12/20/2024
Topics covered

Control Structures :

Introduction to conditional control, Iterative


control and sequential control statements,
Cursors, Views.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
PL/SQL (Procedural Language/Structured Query Language) is an extension of
SQL used in Oracle databases to write procedural code. It includes various
conditional statements that allow developers to execute different blocks of code
based on specific conditions. Decision-making statements in programming
languages decide the direction of the flow of program execution. Conditional
Statements available in PL/SQL are defined below:
• IF THEN
• IF THEN ELSE
• NESTED-IF-THEN
• IF THEN ELSIF-THEN-ELSE Ladder

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
1. IF THEN
If then the statement is the simplest decision-making statement. It is used
to decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of statement is
executed otherwise not.
Syntax:
if condition then
-- do something
end if;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
As the condition present in the if
statement is false. So, the block
below the if statement is not
executed.
Output:
I am Not in if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
2. IF THEN ELSE
The if statement alone tells us that if a condition is true, it will execute a block of statements
and if the condition is false, it won’t. But what if we want to do something else if the
condition is false. Here comes the else statement. We can use the else statement with if
statement to execute a block of code when the condition is false.
Syntax:-
if (condition) then
-- Executes this block if
-- condition is true
else
-- Executes this block if
-- condition is false
if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
i'm in if Block
i'm not in if and not in else Block
The block of code following the else
statement is executed as the condition
present in the if statement is false after
calling the statement which is not in
block(without spaces).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
3. NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if
then statement. Syntax:-
if (condition1) then
-- Executes when condition1 is true
if (condition2) then
-- Executes when condition2 is true
end if;
end if;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
num1 small num2
num1 small num3 also
after end if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
4. IF THEN ELSIF-THEN-ELSE Ladder
Here, a user can decide among multiple options. The if then statements are executed from the top
down. As soon as one of the conditions controlling the if is true, the statement associated with that if
is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final
else statement will be executed. Syntax:-
if (condition) then
--statement
elsif (condition) then
--statement
.
.
else
--statement
endif

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Conditional Statements
Output:-
num1 small
after end if

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Iterative control indicates the ability to repeat or skip sections of a code block.
A loop marks a sequence of statements that has to be repeated. The keyword loop has to be placed
before the first statement in the sequence of statements to be repeated, while the keyword end loop
is placed immediately after the last statement in the sequence.
Once a loop begins to execute, it will go on forever. Hence a conditional statement that controls the
number of times a loop is executed always accompanies loops.
PL/SQL supports the following structures for iterative control:
• Loop
• WHILE loop
• FOR loop

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Simple loop:
In simple loop, the key word loop should be placed before the first statement in the sequence
and the keyword end loop should be written at the end of the sequence to end the loop.
Syntax:
Loop
< Sequence of statements >
End loop;

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Code Output:
: i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i = 10
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
PL/SQL Iterative Control
WHILE loop
The while loop executes commands in its body as long as the condition remains true

Syntax :

WHILE < condition >


LOOP
< Action >
END LOOP

Output :
reverse of given number is:
321

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
The FOR Loop
The FOR loop can be used when the number of iterations to be executed are known.
Syntax :
FOR variable IN [REVERSE] start..end
LOOP
< Action >
END LOOP;
The variable in the For Loop need not be declared. Also, the increment value cannot be
specified. The For Loop variable is always incremented by 1.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Iterative Control
Output :
Code: i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i = 10
12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University
PL/SQL Sequential Control
The GOTO Statement
The GOTO statement changes the flow of control within a PL/SQL block.
This statement allows execution of a section of code, which is not in the
normal flow of control. The entry point into such a block of code is marked
using the tags «userdefined name». The GOTO statement can then make
use of this user-defined name to jump into that block of code for execution.

Syntax :
GOTO jump;
....
<<jump>>

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


PL/SQL Sequential Control:
Code: Output:

code starts
before gotostatements
flow of execution jumped here.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Whenever DML statements are executed, a temporary work area is created in the system
memory and it is called a cursor. A cursor can have more than one row, but processing wise
only 1 row is taken into account. Cursors are very helpful in all kinds of databases like
Oracle, SQL Server, MySQL, etc. They can be used well with DML statements like Update,
Insert and Delete. Especially Implicit cursors are there with these operations. From time to
time, it changes the values and hence the implicit cursor attribute values need to be
assigned in a local variable for further use. In PL/SQL, two different types of cursors are
available.

1.Implicit cursors
2.Explicit cursors

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Implicit Cursors
An Implicit Cursor is a cursor that is automatically created by the database management
system to handle single-row queries or DML operations (INSERT, UPDATE, DELETE). The
database automatically manages the lifecycle of an implicit cursor, so the programmer does
not need to explicitly declare, open, fetch, or close it.

%ROWCOUNT
It displays the number of rows produced by a SELECT INTO command or affected by DML
operations like INSERT, DELETE, and UPDATE.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
%FOUND
Its return result is TRUE if a SELECT INTO command produced one or more rows, or
if DML actions like INSERT, DELETE, and UPDATE influence at least one row or
more rows. Otherwise, FALSE is returned.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
%NOTFOUND
Its return result is TRUE if a SELECT INTO command produced one or more rows, or if
DML actions like INSERT, DELETE, and UPDATE influence at least one row or more
rows. Otherwise, FALSE is returned.
%NOTFOUND
Its return result is TRUE if no rows are affected by DML operations like INSERT,
DELETE, and UPDATE, or if a SELECT INTO command returns no rows. Otherwise,
FALSE is returned. It is directly opposed to %FOUND.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Example of an Implicit Cursor:

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Advantages of Implicit Cursors:
Simplifies coding for basic queries and DML operations.
Automatically managed by the DBMS, reducing programmer
effort.
Suitable for operations involving single rows or simple data
manipulation.

Disadvantages of Implicit Cursors:


Limited to single-row queries or operations.
Lack of control for handling multi-row query results (use explicit
cursors for this).

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
An Explicit Cursor is a database object that allows programmers to handle multi-row query
results in a controlled manner, processing each row one at a time. Unlike implicit cursors (which
are automatically created by SQL for single-row operations), explicit cursors must be explicitly
declared, opened, fetched, and closed by the user.

Steps for Using an Explicit Cursor:


• Declare the Cursor: Define the cursor and associate it with a SELECT query

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
• Open the Cursor: Execute the query and make the result set available for processing.

• Fetch Data: Retrieve one row at a time from the result set.

• Close the Cursor: Release the resources associated with the cursor.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors
Example of an Explicit Cursor:

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Cursors

Advantages of Explicit Cursors:


• Provides control over row-by-row processing.
• Enables complex operations on individual rows of a
query result set.

Disadvantages:
• Slower performance compared to set-based
operations.
• Increases code complexity.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Views in SQL are a type of virtual table that simplifies how users interact with data across
one or more tables. Unlike traditional tables, a view in SQL does not store data on disk;
instead, it dynamically retrieves data based on a pre-defined query each time it’s accessed.
SQL views are particularly useful for managing complex queries, enhancing security, and
presenting data in a simplified format

How Views Work in Cursors


View Definition: A view is created as a virtual table that combines data from one or more
tables. You can create a cursor to process rows from a view instead of directly querying the
underlying tables.
Cursor Usage: The cursor can reference the view in its SELECT query to retrieve and
process data row by row.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Steps to Use a Cursor with a View
• Create the View: Define the view using a CREATE VIEW statement.

• Declare the Cursor: Use the view in the SELECT query for the cursor declaration.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
• Open the Cursor: Execute the query to make the result set available for processing.

• Fetch Rows from the Cursor: Retrieve rows one at a time from the cursor.

• Process Each Row: Apply desired logic (e.g., calculations, updates) to the fetched data.
• Close the Cursor: Release resources after completing processing.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


Views
Advantages of Using Views in Cursors

1.Simplicity:
⚬ Views can encapsulate complex queries,
making the cursor's logic simpler.
2.Reusability:
⚬ A view can be reused across different queries or
cursors.
3.Data Abstraction:
⚬ Views abstract the underlying table structure,
providing a layer of security and modularity.

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University


THANK YOU

12/20/2024 Dr. Gitanjali , Asst. Prof., UIE-CSE, Chandigarh University

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy