0% found this document useful (0 votes)
11 views

Rdbms Js Book

Uploaded by

testorretest
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Rdbms Js Book

Uploaded by

testorretest
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 342

THE ART OF RDBMS

With
Lab Manual
2 Mark (Q & A)
5 Mark and 10 Mark
(Assessment Questions)
Sample Programs

In Accordance with the University Syllabus


For BCA, BSc Students

Prof. JAYALAKSHMI V, M.Sc., MBA(ISM)., M.Phil., NET., (Ph.D).,


Prof. SURESH R, M.Sc., M.Phil., B.Ed., (Ph.D).,
Prof. JAYALAKSHMI V, M.Sc., MBA(ISM).,
M.Phil., NET., (Ph.D).,
Assistant Professor,
Department of Computer Applications,
DRBCCC Hindu College,
Pattabiram, Chennai-600072.

Prof. SURESH R, M.Sc., M.Phil., B.Ed.,


(Ph.D).,
Assistant Professor,
Department of Computer Applications,
DRBCCC Hindu College,
Pattabiram, Chennai-600072.
Published by,

ONE TO ONE PUBLICATIONS


Plot No. 1703, TNHB,
Avadi, Chennai – 600 054.

The Art of RDBMS


Book No. : AC2312
ISBN : 978-81-962733-4-7
MRP : 296/-

Copyright @ 2023, One to One Publications


The content of a book that is protected by copyright includes the written
text, diagrams, sample programs with screenshots and any other
creative elements that are original to the author. The author and the
publisher have the exclusive right to control how their work is used and
distributed, and anyone else who wishes to use or reproduce the content
must obtain permission from the copyright owner.

For Publishing your Manuscripts contact

ONE TO ONE PUBLICATIONS


+91 9360850725/+91 9345384214
[email protected]
www.onetooneacademy.com

“Transfer your dreams into a book with ISBN”


ONE TO ONE ACADEMY

ONE TO ONE HOME/ONLINE TUITIONS


(for all Subjects, Boards, Schools, Universities, Colleges)

ONE TO ONE PUBLICATIONS


(All types of Books with ISBN)

ONE TO ONE PATENT PUBLICATION


(Including Patent File, Grant, Publications)

ONE TO ONE FINE ARTS


(Vocals, Dance, Piano, Drawing)

ONE TO ONE DESIGN & DEVELOPMENT


(Website & Mobile Application)

ONE TO ONE RESEARCH PROJECTS SUPPORT & SOLUTIONS


(A Complete Research Assistance, T to T along with Publications)

ONE TO ONE COLLEGE/SCHOOL PROJECTS


(All Models will be done including Chart Work, Working Models, Non –
Working Models, Art & Craft Works, Assignments & Record Writing,
Drawings done for All Courses| Departments)

ONE TO ONE TECHYWAVE SOLUTIONS


(Internship, Mini Projects, Major Projects, E Projects, E Learning |
along with Certificates)

For Further Details Contact:


+91 9360850725/+91 9345384214
[email protected]
www.onetooneacademy.com
Our Other Titles

S.No. Book Name Author Name


Object Oriented Programming Concepts G. Deepa
AC2301
Using C++ A. Chitra
V. Jayalakshmi
AC2302 Mobile Application Development
A. Chitra
Dr. S. DilliArasu
Relational Database Management System
AC2303 Dr. G. Saranya
(Inclusive with PL/SQL Lab)
A. Chitra
AC2304 Information Technology for Business G. Deepa
A. Chitra
AC2305 Java Programming
G. Deepa
A. Chitra
AC2306 Computer Organization B. Gayathri
B. Jansi
AC2307 Operating Systems A. Chitra
Dr. G. Kalpana
AC2308 Python Programming
G. Deepa
A. Chitra
AC2309 Data Structures
V. Jayalashmi
A Practical Guide for Python Learning
A. Chitra
AC2310 Python Programming (Cracker for the
G. Deepa
Technical Round)
The Power of Multimedia: Exploring R. Suresh
AC2311
Application and Innovations V. Jayalakshmi
V. Jayalakshmi
AC2312 The Art of RDBMS
R. Suresh
Dr. N. Venkataramanan
AC2313 C Programming from Beginner to Expert
J. Seetha Lakshmi
M. Vedhapriya
AC2314 Blockchain Technology S. Yasmin
V. Dhivya
C++ Programming Manual to Accompany A. Chitra
AC2315
You (Cracker for the Technical Round) G. Deepa
Learn Yourself Java Programming (Cracker G. Deepa
AC2316
for the Technical Round) A. Chitra
Dr. R. Latha
AC2317 Problem Solving Using Python S. Mahalakshmi
B.Gayathri
PREFACE
Relational Database Management Systems (RDBMS) are the
backbone of modern enterprise applications. They store and manage
vast amounts of data, providing a secure and reliable foundation for
business-critical applications.

The Art of RDBMS is a comprehensive guide to design,


implementation, and administration of RDBMSs. The book covers the
basics of RDBMSs, contains the relational model, SQL, security,
disaster recovery and database administration. It also delves into more
advanced topics, such as performance tuning, security, and disaster
recovery.

The book is written for Students of Computer Applications,


Computer Science, Computer Technology, Information Technology and
Information System Management, developers, database administrators,
and anyone else who needs to work with RDBMS. It is packed with
practical advice and real-world examples.

To improve the book's approach, send

[email protected] your insightful comments and

ideas.

- Authors
ACKNOWLEDGEMENT
My Heartful Thanks to God who has given his blessings to
write this book on“The Art of RDBMS”.
My sincere thanks and gratitude to Dharmamurthi Rao
Bahadur Calavala Cunnan Chetty Hindu Charities, Trust members,
Secretary, Principal, and Director of DRBCCC Hindu College.
As Author, I (Mrs. V. Jayalakshmi) thank my husband Mr. S.
Sundar for his unwavering support and continuous encouragement
has helped me in writing this book.
My Sincere thanks to our Secretary Shri.Venkatesa perumal,
Principal Dr. G. Kalvikkarasi, Director Dr. N. RajendraNaidu, andour
HOD Dr. N. Venkataramanan of DRBCCC Hindu College for their
constant guidance.
I am proud of the work that accomplished together, and I am
grateful for the opportunity to have collaborated with my co-author
and friend Prof. R. Suresh who is talented and committed in the
work.
Special thanks to my friends, fellow colleagues and my
students for their constant support.

With sincere regards,


V. Jayalakshmi.
ACKNOWLEDGEMENT
I thank almighty for giving this opportunity.

My sincere thanks and gratitude to Dharmamurthi Rao


Bahadur Calavala Cunnan Chetty Hindu Charities, Trust members,
Secretary, Principal, and Director of DRBCCC Hindu College.

My Sincere thanks to our Secretary Shri. Venkatesaperumal,


Principal Dr. G. Kalvikkarasi, Director Dr. N. Rajendra Naidu, and
our HOD Dr. N. Venkataramanan of DRBCCC Hindu College for
their constant guidance.

As Author, I (Mr. R. Suresh) thank my wife Mrs. K. Anusuya


for her great support, kindness, and uninterrupted cheer has helped
me in writing this book.

My hearty wishes and thanks to my co-author and friend Prof.


V. Jayalakshmi who is sincere and always helping towards success.

Special thanks to my friends, fellow colleagues and my


students for their constant support.

With sincere regards,


R. Suresh.
Page
Table of Contents
No.
UNIT I
1. Introduction 1
1.1.Introduction to Database 1
1.2.Database Management System 2
1.3.Advantages of DBMS 4
1.4.Disadvantages of DBMS 4
1.5.Types of Database 5
1.5.1.Centralised Database 5
1.5.2.Distributed Database 6
1.5.3.Relational Database 8
1.5.4.NoSQL Database 9
1.5.5.Cloud Database 11
1.5.6.Object-oriented Databases 11
1.5.7.Hierarchical Databases 12
1.5.8.Network Databases 12
1.5.9.Personal Database 13
1.6 DBMS Architecture 13
1.6.1. Types of DBMS Architecture 13
1.7 Introduction of ER Model 19
1.7.1. Why Use ER Diagrams In DBMS? 20
1.7.2. Symbols Used in ER Model 20
1.7.3. Components of ER Diagram 21
1.7.4.ER Diagram To Table Conversion 27
1.7.5. Degree of a relationship 31
1.7.6. Constraints of ER Diagrams 31
1.8 Aggregation in DBMS 35
1.8.1. Types of Aggregations 36
1.8.2. Data aggregation techniques in DBMS 36

UNIT II
2.1. Relational Model 39
2.1.1. Properties of Relations 39
2.2. Codd's Rules 41
2.3. Relational Algebra 45
2.3.1. Types of Relational operation 46
2.4. Advantages of Relational Algebra 64
2.5. Integrity Constraints 65
2.5.1. Domain constraints 66
2.5.2. Entityintegrityconstraints 67
2.5.3. Referential Integrity Constraints 67
2.5.4. Key constraints 68
2.6. Relational Calculus 70
2.6.1. Types of Relational calculus 70
2.6.2 Tuple Relational Calculus (TRC) 70
2.6.3. Domain Relational Calculus (DRC) 72
2.7. Query By Example (QBE): 73

UNIT III
3.0 Relational Database(RDBMS) 78
3.1. The relational model 79
3.2. ACID properties and RDBMS 80
3.3. Database locking and concurrency 81
3.4. Brief History of RDBMS 81
3.4.1. What is Table/Relation? 82
3.4.2. What is a row or record? 83
3.4.3. What is a column/attribute? 84
3.4.5. What is data item/Cells? 85
3.4.6. Cardinality 87
3.4.7. Data Integrity 88
3.5. RDBMS Architecture 89
3.6. RDBMS Tools 91
3.7. Anomalies in DBMS 94
3.7.1. Types of an anomaly in the database 96
3.8. Redundancy in DBMS 98
3.9. Functional Dependency 103
3.9.1. Types of Functional dependency 106
3.10. Normalization 112
3.10.1. Types of Normal Forms 114
3.10.2. First Normal Form (1NF) 116
3.10.3. Second Normal Form (2NF) 119
3.10.4. Third Normal Form (3NF) 122
3.11. Transactions 133
3.12. Database Security 139
3.12.1. Why Database Security is Important? 140
3.12.2. Common Threats and Challenges 142
3.12.3. Best use of Database Security 145
3.12.4. Data protection tools and platforms 147
3.12.5. Computer Based Controls 149
3.12.6. Access Controls 150

UNIT IV
4.1. SQL 156
4.1.1. Why SQL 156
4.1.2. History of SQL 157
4.1.3. Process of SQL 158
4.1.4. SQL vs NO-SQL 160
4.1.5. Advantages of SQL 162
4.1.6. Disadvantages of SQL 163
4.1.7. Most Important SQL Commands and
164
Statements
4.2. SQL Data types 179
4.2.1. My SQL String Data types 180
4.2.2. My SQL Numeric Data types 181
4.2.3. My SQL Date and Time Data types 183
4.2.4. SQL Server String Data types 184
4.2.5. SQL Server Numeric Data types 185
4.2.6. SQL Server Date and Time Data types 186
4.2.7. SQL Server Other Data types 187
4.3. DDL Commands in SQL 188
4.3.1. Create Command 188
4.3.2. Drop Command 192
4.3.3. Alter Command 193
4.3.4. Truncate Command 194
4.3.5. Rename Command 195
4.4. DML Commands in SQL 197
4.4.1. Select Command 197
4.4.2. Insert Command 201
4.4.3. Update Command 202
4.4.4. Delete Command 205
4.5. SQL Join 207
4.5.1. Inner Join 209
4.5.2. Left Join 210
4.5.3. Right Join 212
4.5.4. Full Join 214
4.5.5. Natural Join 217
4.6. SQL Set Operations 219
4.6.1. Types of Set Operations 219
4.7. SQL Sub Queries 226
4.7.1. Sub Queries with Select Statement 227
4.7.2. Sub Queries with Insert Statement 229
4.7.3. Sub Queries with Update Statement 230
4.7.4. Sub Queries with Delete Statement 232

UNIT V
5.1. PL/SQL Introduction 235
5.1.1. Features and Advantages PL/SQL 235
5.2. Structure of PL/SQL 237
5.3. PL/SQL Variables 239
5.4. PL/SQL Constant 248
5.5. PL/SQL Literals 250
5.6. PL/SQL Operators 251
5.6.1. Arithmetic Operators 251
5.6.2. Relational Operators 252
5.6.3. Comparison Operators 253
5.6.4. Logical Operators 255
5.6.5. PL/SQL Operator Precedence 256
5.7. PL/SQL Control Statements 257
5.7.1. Conditional Statements 257
5.7.2. PL/SQL Case Statement 260
5.7.3. PL/SQL Looping Statements 262
5.7.4. Continue Statement 268
5.7.5. PL/SQL GOTO Statement 270
5.8. PL/SQL Procedure 273
5.9. PL/SQL Function 276
5.10. PL/SQL Cursor 282
5.11. PL/SQL Exception Handling 290
5.12. PL/SQL Trigger 300
Relational Database Management System
UNIT 1
1.1 Introduction to Database:

What is data?:
The terms ―data‖ and ―information‖ are used interchangeably.
However, there is a subtle difference between the two.

In a nutshell, data can be a number, symbol, character, word, codes,


graphs, etc. On the other hand, information is data put into context.
Information is utilized by humans in some significant way (such as to
make decisions, forecasts etc).

The term Data is defined as a raw and unstructured fact that needs to
be processed to make it meaningful. Data can be simple and
unstructured at the same time until it is structured. Usually data
contains facts, numbers, symbols, image, observations, perceptions,
characters, etc.

What is Information?

The term Information is defined as a set of data that is processed


according to the given requirement in a meaningful way. To make
the information useful and meaningful, it must be processed,
presented and structured in a given context.

Information is processed from data and possess context, purpose and


relevance. It also includes raw data manipulation.

1
Relational Database Management System

What is Database?

The database is a collection of inter-related data which is used to


retrieve, insert and delete the data efficiently. It is also used to
organize the data in the form of a table, schema, views, and reports,
etc. For example: The college Database organizes the data about the
admin, staff, students and faculty etc. Using the database, you can
easily retrieve, insert, and delete the information.

1.2 Database Management System

Database management system is a software which is used to manage


the database. For example: MySQL, Oracle, etc are a very popular
commercial database which is used in different applications.

DBMS provides an interface to perform various operations like


database creation, storing data in it, updating data, creating a table in
the database and a lot more.

It provides protection and security to the database. In the case of


multiple users, it also maintains data consistency.

DBMS allows users the following tasks:

Data Definition: It is used for creation, modification, and removal


of definition that defines the organization of data in the database.

2
Relational Database Management System
Data Updation: It is used for the insertion, modification, and
deletion of the actual data in the database.
Data Retrieval: It is used to retrieve the data from the database
which can be used by applications for various purposes.
User Administration: It is used for registering and monitoring
users, maintain data integrity, enforcing data security, dealing with
concurrency control, monitoring performance and recovering
information corrupted by unexpected failure.

Characteristics of DBMS

 It uses a digital repository established on a server to store and


manage the information.
 It can provide a clear and logical view of the process that
manipulates data.
 DBMS contains automatic backup and recovery procedures.
 It contains ACID properties which maintain data in a healthy
state in case of failure.
 It can reduce the complex relationship between data.
 It is used to support manipulation and processing of data.
 It is used to provide security of data.
 It can view the database from different viewpoints according to
the requirements of the user.

3
Relational Database Management System
1.3 Advantages of DBMS

Controls database redundancy: It can control data redundancy


because it stores all the data in one single database file and that
recorded data is placed in the database.

1. Data sharing: In DBMS, the authorized users of an


organization can share the data among multiple users.
2. Easily Maintenance: It can be easily maintainable due to the
centralized nature of the database system.
3. Reduce time: It reduces development time and maintenance
need.
4. Backup: It provides backup and recovery subsystems which
create automatic backup of data from hardware and software
failures and restores the data if required.
5. Multiple user interface: It provides different types of user
interfaces like graphical user interfaces, application program
interfaces

1.4 Disadvantages of DBMS

 Cost of Hardware and Software: It requires a high speed of data


processor and large memory size to run DBMS software.
 Size: It occupies a large space of disks and large memory to run
them efficiently.

4
Relational Database Management System
 Complexity: Database system creates additional complexity and
requirements.
 Higher impact of failure: Failure is highly impacted the database
because in most of the organization, all the data stored in a
single database and if the database is damaged due to electric
failure or database corruption then the data may be lost forever.

1.5 Types of Databases:

Fig.1.1.DB Types

1.5.1. Centralized Database

It is the type of database that stores data at a centralized database


system. It comforts the users to access the stored data from different
locations through several applications. These applications contain
the authentication process to let users access data securely. An
example of a Centralized database can be Central Library that
carries a central database of each library in a college/university.

5
Relational Database Management System

Advantages of Centralized Database

 It has decreased the risk of data management, i.e., manipulation


of data will not affect the core data.
 Data consistency is maintained as it manages data in a central
repository.
 It provides better data quality, which enables organizations to
establish data standards.
 It is less costly because fewer vendors are required to handle the
data sets.

Disadvantages of Centralized Database

 The size of the centralized database is large, which increases the


response time for fetching the data.
 It is not easy to update such an extensive database system.
 If any server failure occurs, entire data will be lost, which could
be a huge loss.

1.5.2. Distributed Database

Unlike a centralized database system, in distributed systems, data is


distributed among different database systems of an organization.
These database systems are connected via communication links.
Such links help the end-users to access the data easily. Examples of
the Distributed database are Apache Cassandra, HBase, Ignite, etc.

6
Relational Database Management System

We can further divide a distributed database system into:

Fig.1.2.Distributed DB
Homogeneous DDB: Those database systems which execute on the
same operating system and use the same application process and
carry the same hardware devices.
Heterogeneous DDB: Those database systems which execute on
different operating systems under different application procedures,
and carries different hardware devices.

Advantages of Distributed Database:

 Modular development is possible in a distributed database, i.e.,


the system can be expanded by including new computers and
connecting them to the distributed system.
 One server failure will not affect the entire data set.

7
Relational Database Management System
1.5.3. Relational Database

This database is based on the relational data model, which stores


data in the form of rows(tuple) and columns(attributes), and together
forms a table(relation). A relational database uses SQL for storing,
manipulating, as well as maintaining the data. E.F. Codd invented
the database in 1970. Each table in the database carries a key that
makes the data unique from others. Examples of Relational
databases are MySQL, Microsoft SQL Server, Oracle, etc.

Properties of Relational Database:

There are following four commonly known properties of a relational


model known as ACID properties, where:
 A means Atomicity: This ensures the data operation will
complete either with success or with failure. It follows the 'all or
nothing' strategy. For example, a transaction will either be
committed or will abort.
 C means Consistency: If we perform any operation over the data,
its value before and after the operation should be preserved. For
example, the account balance before and after the transaction
should be correct, i.e., it should remain conserved.
 I means Isolation: There can be concurrent users for accessing
data at the same time from the database. Thus, isolation between
the data should remain isolated. For example, when multiple

8
Relational Database Management System
transactions occur at the same time, one transaction effects
should not be visible to the other transactions in the database.
 D means Durability: It ensures that once it completes the
operation and commits the data, data changes should remain
permanent.

1.5.4. NoSQL Database

Non-SQL/Not Only SQL is a type of database that is used for


storing a wide range of data sets. It is not a relational database as it
stores data not only in tabular form but in several different ways. It
came into existence when the demand for building modern
applications increased. Thus, NoSQL presented a wide variety of
database technologies in response to the demands. We can further
divide a NoSQL database into the following four types:

Fig.1.3.NoSQL DB Types

9
Relational Database Management System
Key-value storage: It is the simplest type of database storage where
it stores every single item as a key (or attribute name) holding its
value, together.
Document-oriented Database: A type of database used to store
data as JSON-like document. It helps developers in storing data by
using the same document-model format as used in the application
code.
Graph Databases: It is used for storing vast amounts of data in a
graph-like structure. Most commonly, social networking websites
use the graph database.
Wide-column stores: It is similar to the data represented in
relational databases. Here, data is stored in large columns together,
instead of storing in rows.

Advantages of NoSQL Database:


 It enables good productivity in the application development as it
is not required to store data in a structured format.
 It is a better option for managing and handling large data sets.
 It provides high scalability.
 Users can quickly access data from the database through key-
value.

10
Relational Database Management System
1.5.5. Cloud Database

A type of database where data is stored in a virtual environment and


executes over the cloud computing platform. It provides users with
various cloud computing services (SaaS, PaaS, IaaS, etc.) for
accessing the database. There are numerous cloud platforms, but the
best options are:
 Amazon Web Services(AWS)
 Microsoft Azure
 Kamatera
 PhonixNAP
 ScienceSoft
 Google Cloud SQL, etc.

1.5.6. Object-oriented Databases

The type of database that uses the object-based data model approach
for storing data in the database system. The data is represented and
stored as objects which are similar to the objects used in the object-
oriented programming language.

11
Relational Database Management System
1.5.7. Hierarchical Databases

It is the type of database that stores data in the form of parent-


children relationship nodes. Here, it organizes data in a tree-like
structure.

Fig1.4.Hierarchial DB
Data get stored in the form of records that are connected via links.
Each child record in the tree will contain only one parent. On the
other hand, each parent record can have multiple child records.

1.5.8. Network Databases

It is the database that typically follows the network data model. Here,
the representation of data is in the form of nodes connected via links
between them. Unlike the hierarchical database, it allows each
record to have multiple children and parent nodes to form a
generalized graph structure.

12
Relational Database Management System

1.5.9. Personal Database

Collecting and storing data on the user's system defines a Personal


Database. This database is basically designed for a single user.

1.6 DBMS Architecture

A Database store a lot of critical information to access data quickly


and securely. Hence it is important to select the correct architecture
for efficient data management. DBMS Architecture helps users to
get their requests done while connecting to the database. We choose
database architecture depending on several factors like the size of
the database, number of users, and relationships between the users.
There are two types of database models that we generally use, are
logical model and physical model.

1.6.1. Types of DBMS Architecture:

There are several types of DBMS Architecture that we use according


to the usage requirements. Types of DBMS Architecture are
discussed here.
1-Tier Architecture
2-Tier Architecture
3-Tier Architecture

13
Relational Database Management System

Fig1.5.DBMS Architectures
1-Tier Architecture

In 1-Tier Architecture the database is directly available to the user,


the user can directly sit on the DBMS and use it that is, the client,
server, and Database are all present on the same machine.

For Example: to learn SQL we set up an SQL server and the


database on the local system. This enables us to directly interact
with the relational database and execute operations. The industry
won‘t use this architecture they logically go for 2-Tier and 3-Tier
Architecture.

14
Relational Database Management System

Fig1.5.1. 1 Tier Architecture

Advantages of 1-Tier Architecture


 Simple Architecture: 1-Tier Architecture is the most simple
architecture to set up, as only a single machine is required to
maintain it.
 Cost-Effective: No additional hardware is required for
implementing 1-Tier Architecture, which makes it cost-effective.
 Easy to Implement: 1-Tier Architecture can be easily deployed,
and hence it is mostly used in small projects.

2-Tier Architecture

The 2-tier architecture is similar to a basic client-server model. The


application at the client end directly communicates with the database
on the server side. APIs like ODBC and JDBC are used for this
interaction. The server side is responsible for providing query
processing and transaction management functionalities. On the client
side, the user interfaces and application programs are run. The

15
Relational Database Management System
application on the client side establishes a connection with the server
side in order to communicate with the DBMS.

An advantage of this type is that maintenance and understanding are


easier, and compatible with existing systems. However, this model
gives poor performance when there are a large number of users.

Fig1.5.2. 2 Tier Architecture

Advantages of 2-Tier Architecture:


 Easy to Access: 2-Tier Architecture makes easy access to the
database, which makes fast retrieval.
 Scalable: We can scale the database easily, by adding clients or
by upgrading hardware.
 Low Cost: 2-Tier Architecture is cheaper than 3-Tier
Architecture and Multi-Tier Architecture.

16
Relational Database Management System
 Easy Deployment: 2-Tier Architecture is easy to deploy than 3-
Tier Architecture.
 Simple: 2-Tier Architecture is easily understandable as well as
simple because of only two components.

3-Tier Architecture

In 3-Tier Architecture, there is another layer between the client and


the server. The client does not directly communicate with the server.
Instead, it interacts with an application server which further
communicates with the database system and then the query
processing and transaction management takes place.

This intermediate layer acts as a medium for the exchange of


partially processed data between the server and the client. This type
of architecture is used in the case of large web applications.

17
Relational Database Management System

Fig1.5.3. 3 Tier Architecture


Advantages of 3-Tier Architecture:

 Enhanced scalability: Scalability is enhanced due to distributed


deployment of application servers. Now, individual connections
need not be made between the client and server.
 Data Integrity: 3-Tier Architecture maintains Data Integrity.
Since there is a middle layer between the client and the server,
data corruption can be avoided/removed.
 Security: 3-Tier Architecture Improves Security. This type of
model prevents direct interaction of the client with the server
thereby reducing access to unauthorized data.

18
Relational Database Management System
Disadvantages of 3-Tier Architecture:
 More Complex: 3-Tier Architecture is more complex in
comparison to 2-Tier Architecture. Communication Points are
also doubled in 3-Tier Architecture.
 Difficult to Interact: It becomes difficult for this sort of
interaction to take place due to the presence of middle layers.

1.7 Introduction of ER Model:

The Entity Relational Model is a model for identifying entities to be


represented in the database and representation of how those entities
are related. The ER data model specifies enterprise schema that
represents the overall logical structure of a database graphically.

The Entity Relationship Diagram explains the relationship among


the entities present in the database. ER models are used to model
real-world objects like a person, a car, or a company and the relation
between these real-world objects. In short, ER Diagram is the
structural format of the database.

19
Relational Database Management System
1.7.1. Why Use ER Diagrams In DBMS?
 ER diagrams are used to represent the E-R model in a database,
which makes them easy to be converted into relations (tables).
 ER diagrams provide the purpose of real-world modeling of
objects which makes them intently useful.
 ER diagrams require no technical knowledge and no hardware
support.
 These diagrams are very easy to understand and easy to create
even for a naive user.
 It gives a standard solution for visualizing the data logically.

1.7.2. Symbols Used in ER Model:

ER Model is used to model the logical view of the system from a


data perspective which consists of these symbols:

 Rectangles: Rectangles represent Entities in ER Model.


 Ellipses: Ellipses represent Attributes in ER Model.
 Diamond: Diamonds represent Relationships among Entities.
 Lines: Lines represent attributes to entities and entity sets with
other relationship types.
 Double Ellipse: Double Ellipses represent Multi-Valued
Attributes.
 Double Rectangle: Double Rectangle represents a Weak Entity.

20
Relational Database Management System

Fig.1.6.Symbols in ER Diagram

1.7.3. Components of ER Diagram:


ER Model consists of Entities, Attributes, and Relationships among
Entities in a Database System.

Fig.1.7.ER Diagram Components

21
Relational Database Management System
A). Entity:

An entity may be any object, class, person or place. In the ER


diagram, an entity can be represented as rectangles.

Consider an organization as an example- manager, product,


employee, department etc. can be taken as an entity.

Fig.1.8a.Entity

Weak Entity: An entity that depends on another entity called a weak


entity. The weak entity doesn't contain any key attribute of its own.
The weak entity is represented by a double rectangle.

Fig.1.8b.Weak Entity

B). Attribute: The attribute is used to describe the property of an


entity. Eclipse is used to represent an attribute.

For example, id, age, contact number, name, etc. can be attributes of
a student.

22
Relational Database Management System

Fig.1.8c.Attribute

Key Attribute: The key attribute is used to represent the main


characteristics of an entity. It
represents a primary key. The key
attribute is represented by an ellipse
with the text underlined.

Fig.1.8d.Key Attribute

Composite Attribute: An attribute that composed of many other


attributes is known as a composite attribute. The composite attribute
is represented by an ellipse, and those ellipses are connected with an
ellipse.

23
Relational Database Management System

Fig.1.8.e. Composite Attribute

Multivalued Attribute: An attribute can have more than one value.


These attributes are known as a multivalued attribute. The double
oval is used to represent multivalued attribute.

For example, a student can have more than one phone number.

Fig.1.8f.Multivalued Atribute

Derived Attribute: An attribute that can be derived from other


attribute is known as a derived attribute. It can be represented by a
dashed ellipse.

For example, A person's age changes over time and can be derived
from another attribute like Date of birth.

24
Relational Database Management System

Fig.1.8g.Derived Attribute

C). Relationship

A relationship is used to describe the relation between entities.


Diamond or rhombus is used to represent the relationship.

Fig.1.9.Relationship

Types of relationship are as follows:

One-to-One Relationship: When only one instance of an entity is


associated with the relationship, then it is known as one to one
relationship.

For example, A female can marry to one male, and a male can marry
to one female.

25
Relational Database Management System

Fig1.9.1.One-one Relationship

One-to-many relationship: When only one instance of the entity on


the left, and more than one instance of an entity on the right
associates with the relationship then this is known as a one-to-many
relationship.

For example, Scientist can invent many inventions, but the invention
is done by the only specific scientist.

Fig.1.9.2.Ont to Many Relationship

Many-to-one relationship: When more than one instance of the


entity on the left, and only one instance of an entity on the right
associates with the relationship then it is known as a many-to-one
relationship.

For example, Student enrolls for only one course, but a course can
have many students.

26
Relational Database Management System
Fig.1.9.3.Many to one Relationship

Many-to-many relationship: When more than one instance of the


entity on the left, and more than one instance of an entity on the right
associates with the relationship then it is known as a many-to-many
relationship.

For example, Employee can assign by many projects and project can
have many employees.

Fig.1.9.4.Many to many Relationship

1.7.4. ER Diagram To Table Conversion


The databases are represented by the notations, and the following
notations are reduced to the collection of tables.
In the databases, every relationship or entity set can be denoted in
the tabular forms.

There are various points for the conversion of the ER diagram to the
table:

 The respective Entity type becomes a table


 According to the following ER diagram, COURSE, LECTURE,
SUBJECT, and STUDENT forms their tables.

27
Relational Database Management System
 Every attribute of a single value converts into a column for the
table
 In the entity of the STUDENT, the STUDENT _ID and
STUDENT_NAME form the columns of the table of the
STUDENT.
 In the same way, COURSE_ID and COURSE_NAME structure
the columns of the COURSE table and so on.

 A separate table represents the multivalued attribute

In the table of STUDENT, a Hobby is considered as a multivalued


attribute. Hence, it is impossible to describe the multiple values in
just a single column of the table of STUDENT. Therefore, we can
make a table STUD_HOBBY under the name of HOBBY and
STUDENT_ID. We can create a composite key by using both
columns.

28
Relational Database Management System

Fig.1.10.Multivalued Variable

 The primary key represents the critical attribute of the


entity type

In the following ER diagram, LECTURE_ID, STUDENT_ID,


COURSE_ID, and SUBJECT_ID are considered as the key attributes
of the entity.

 The components stand for the composite attribute

In the following ER Diagram, the student address stands for a


composite attribute. It consists of PIN, STATE, STREET, CITY, and
DOOR#. In the table of the STUDENT, the respective attributes can
be merged in an individual column.

29
Relational Database Management System
 The derived attributes are not included in the table

In the table of STUDENT, Age is considered as the derived attribute.


The age can be calculated anytime by using the differences of the
Date of Birth and the current date.

By applying these rules, you can easily assign mapping in the tables
and change the ER diagram to columns and tables.

Following is the structure of the table for the ER Diagram:

Fig.1.11.structure of the table for the ER Diagram

30
Relational Database Management System
1.7.5. Degree of a relationship :
This is used to demonstrate the number of entities which are a part of
the given relationship. Degree 2 is called binary relationship and it
means that 2 entities are participating in the relationship. It is the
most commonly used relationship.

Degree 3 is called ternary relationship. Degree n is called n-ary


relationship. The tuples should have components which are
equivalent to the degree of the relationship.

1.7.6. Constraints of ER Diagrams:

Relationships : The entities are denoted by rectangle and


relationshipsbydiamond.

There are numbers (represented by M and N) written above the lines


which connect relationships and entities. These are called cardinality
ratios. These represent the maximum number of entities that can be
associated with each other through relationship, R.

Types of Cardinality:

 One-to-one (1:1)

31
Relational Database Management System
 One-to-many (1: N)
 Many-to-one (N:1)
 Many-to-many (N: N)

There can be 4 types of cardinality –

Fig.1.12.Types of Cardinality

One-to-one (1:1) – When one entity in each entity set takes part at
most once in the relationship, the cardinality is one-to-one.

One-to-many (1: N) – If entities in the first entity set take part in the
relationship set at most once and entities in the second entity set take

32
Relational Database Management System
part many times (at least twice), the cardinality is said to be one-to-
many.

Many-to-one (N:1) – If entities in the first entity set take part in the
relationship set many times (at least twice), while entities in the
second entity set take part at most once, the cardinality is said to be
many-to-one.

Many-to-many (N: N) – The cardinality is said to be many to many if


entities in both the entity sets take part many times (at least twice) in
the relationship set.

Fig1.13.Participation Constrains

Participation Constraints : Participation Constraints tell us that the


participation in a relationship can either be total or partial. When
each entity in an entity set participates in a relation, it is called Total

33
Relational Database Management System
Participation. However, when all entities in the given entity set do
not participate in a relation, it is called Partial
Participation. Structural Constraints : Structural Constraints are also
called Structural properties of a database management system
(DBMS). Cardinality Ratios and Participation Constraints taken
together are called Structural Constraints. The name constraints refer
to the fact that such limitations must be imposed on the data, for the
DBMS system to be consistent with the requirements. The Structural
constraints are represented by Min-Max notation. This is a pair of
numbers(m, n) that appear on the connecting line between the entities
and their relationships.

The minimum number of times an entity can appear in a relation is


represented by m whereas, the maximum time it is available is
denoted by n. If m is 0 it signifies that the entity is participating in
the relation partially, whereas, if m is either greater than or equal to 1,
it denotes total participation of the entity. Note – Number of times an
entity participates in a relationship is same as the number appearance
of the entity in the tuples.

34
Relational Database Management System
1.8 Aggregation in DBMS:

Aggregation can be defined as a procedure for combining multiple


entities into a single one. In database management, it is a design
system performed to model relationships between a group of entities
and another relationship. Its main motive is treating these
relationships as a single one. This process is mainly performed when
the entities don‘t look good and need to be aggregated to get desired
results.

The main motive of Aggregation is to summarize the data results for


database management. major jobs are performed by Aggregation in
DBMS are below:

 It is very helpful for maintaining the collection, processing, and


presentation of data making this tool an essential component of
the database management system.

 Aggregation assists in briefing data from various resources to


boost the value of information available in it.

 It is useful for finding the origin of data and creating an audit


trail.

 This technique works in situations where the entry model


relationship can‘t be applied adequately.

35
Relational Database Management System
 It ensures all the entities in the database management system are
utilized according to their full potential.

1.8.1. Types of Aggregations:

The data aggregation can be done according to date that let the
characteristics display over a period of months, years, etc. Check out
the major kinds of data aggregation in detail below:
 Sum: This tool is used for combining the various specified data
for creating a total value.
 Average: The average value of the particular data is fully
computed by this command.
 Max: It is used for showing the highest value for every category
in the database management system.
 Min: It is used for providing the lowest value for every category
in the database management system.
 Count: The total number of data entries is counted for all the
categories available in the database management system.

1.8.2. Data aggregation techniques in DBMS:

1. In-Network Aggregation
It is a general procedure where the information is gathered and
routed using a multi-hop network. This impressive technique is the
cornerstone of the database management system.

36
Relational Database Management System
2. Tree-Based Approach
The Aggregation from developing an aggregation tree is defined with
the help of a tree-based approach. It is a very important technique for
performing database management procedures brilliantly.

3. Multi-Path Approach
The partially aggregated data is transferred to a particular parent
mode during the multi-path approach. All the nodes are sent to many
different inputs in this type of Aggregation.

4. Cluster-Based Approach
This technique is utilized for collating a great sort of data on the
complete network. The cluster-based approach is equipped with a
cluster head chosen from various cluster members.

Examples of Aggregation:

SELECT c1, aggregate_function(c2)


FROM table
GROUP BY c1;

The following are the commonly used SQL aggregate functions:


 AVG() – returns the average of a set.
 COUNT() – returns the number of items in a set.
 MAX() – returns the maximum value in a set.

37
Relational Database Management System
 MIN() – returns the minimum value in a set
 SUM() – returns the sum of all or distinct values in a set

Questions
2 Mark Questions:
1. Define Database.
2. What is DBMS?
3. Write a short note on RDBMS.
4. Define cardinality.
5. Define relationship.
6. What is aggregation?
7. Define E-R diagram.
8. What is entity?
9. Write a note on Attributes.
10. Define constraints.

5 Mark / 10 Mark Questions:


11. Discuss the advantages and disadvantages of DBMS.
12. Explain the database types in detail.
13. Illustrate the DBMS Architecture in detail.
14. Write a brief notes on ER diagram.
15. What is a degree of relationship? Explain.
16. What are the constrains of ER Diagram?
17. What is data aggregation? Explain in detail.

38
Relational Database Management System
UNIT II
2.1. Relational Model

Relational model can represent as a table with columns and rows.


Each row is known as a tuple. Each table of the column has a name
or attribute.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table.


Each attribute Ai must have a domain, dom(Ai)

Relational instance: In the relational database system, the relational


instance is represented by a finite set of tuples. Relation instances do
not have duplicate tuples.

Relational schema: A relational schema contains the name of the


relation and name of all columns or attributes.

Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.

Constraints

Every relation has some conditions that must hold for it to be a


validrelation. These conditions are called Relational Integrity

Constraints. There are three main integrity constraints −

39
Relational Database Management System
 Key constraints
 Domain constraints
 Referential integrity constraints
Key Constraints

There must be at least one minimal subset of attributes in the relation,


which can identify a tuple uniquely. This minimal subset of attributes
is called key for that relation. If there are more than one such
minimal subsets, these are called candidate keys.

Key constraints force that −

 in a relation with a key attribute, no two tuples can have


identical values for key attributes.
 a key attribute can not have NULL values.

Key constraints are also referred to as Entity Constraints.

Domain Constraints

Attributes have specific values in real-world scenario. For example,


age can only be a positive integer. The same constraints have been
tried to employ on the attributes of a relation. Every attribute is
bound to have a specific range of values. For example, age cannot be
less than zero and telephone numbers cannot contain a digit outside
0-9.

40
Relational Database Management System

Referential integrity Constraints

Referential integrity constraints work on the concept of Foreign Keys.


A foreign key is a key attribute of a relation that can be referred in
other relation.

Referential integrity constraint states that if a relation refers to a key


attribute of a different or same relation, then that key element must
exist.

2.1.1. Properties of Relations

 Name of the relation is distinct from all other relations.


 Each relation cell contains exactly one atomic (single) value
 Each attribute contains a distinct name
 Attribute domain has no significance
 tuple has no duplicate value
 Order of tuple can have a different sequence

2.2. Codd's Rules

Every database has tables, and constraints cannot be referred to as a


rational database system. And if any database has only relational data
model, it cannot be a Relational Database System (RDBMS). So,
some rules define a database to be the correct RDBMS. These rules
were developed by Dr. Edgar F. Codd (E.F. Codd) in 1985, who

41
Relational Database Management System
has vast research knowledge on the Relational Model of database
Systems. Codd presents his 13 rules for a database to test the concept
of DBMS against his relational model, and if a database follows the
rule, it is called a true relational database (RDBMS). These 13
rules are popular in RDBMS, known as Codd's 12 rules.

Rule 0: The Foundation Rule

The database must be in relational form. So that the system can


handle the database through its relational capabilities.

Rule 1: Information Rule

A database contains various information, and this information must


be stored in each cell of a table in the form of rows and columns.

Rule 2: Guaranteed Access Rule

Every single or precise data (atomic value) may be accessed logically


from a relational database using the combination of primary key
value, table name, and column name.

Rule 3: Systematic Treatment of Null Values

This rule defines the systematic treatment of Null values in database


records. The null value has various meanings in the database, like

42
Relational Database Management System
missing the data, no value in a cell, inappropriate information,
unknown data and the primary key should not be null.

Rule 4: Active/Dynamic Online Catalog based on the relational


model

It represents the entire logical structure of the descriptive database


that must be stored online and is known as a database dictionary. It
authorizes users to access the database and implement a similar
query language to access the database.

Rule 5: Comprehensive Data SubLanguage Rule

The relational database supports various languages, and if we want to


access the database, the language must be the explicit, linear or well-
defined syntax, character strings and supports the comprehensive:
data definition, view definition, data manipulation, integrity
constraints, and limit transaction management operations. If the
database allows access to the data without any language, it is
considered a violation of the database.

Rule 6: View Updating Rule

All views table can be theoretically updated and must be practically


updated by the database systems.

43
Relational Database Management System
Rule 7: Relational Level Operation (High-Level Insert, Update
and delete) Rule

A database system should follow high-level relational operations


such as insert, update, and delete in each level or a single row. It also
supports union, intersection and minus operation in the database
system.

Rule 8: Physical Data Independence Rule

All stored data in a database or an application must be physically


independent to access the database. Each data should not depend on
other data or an application. If data is updated or the physical
structure of the database is changed, it will not show any effect on
external applications that are accessing the data from the database.

Rule 9: Logical Data Independence Rule

It is similar to physical data independence. It means, if any changes


occurred to the logical level (table structures), it should not affect the
user's view (application). For example, suppose a table either split
into two tables, or two table joins to create a single table, these
changes should not be impacted on the user view application.

Rule 10: Integrity Independence Rule

44
Relational Database Management System
A database must maintain integrity independence when inserting data
into table's cells using the SQL query language. All entered values
should not be changed or rely on any external factor or application to
maintain integrity. It is also helpful in making the database-
independent for each front-end application.

Rule 11: Distribution Independence Rule

The distribution independence rule represents a database that must


work properly, even if it is stored in different locations and used by
different end-users. Suppose a user accesses the database through an
application; in that case, they should not be aware that another user
uses particular data, and the data they always get is only located on
one site. The end users can access the database, and these access data
should be independent for every user to perform the SQL queries.

Rule 12: Non Subversion Rule

The non-submersion rule defines RDBMS as a SQL language to


store and manipulate the data in the database. If a system has a low-
level or separate language other than SQL to access the database
system, it should not subvert or bypass integrity to transform data.

2.3. Relational Algebra

45
Relational Database Management System
Relational algebra is a procedural query language. It gives a step by
step process to obtain the result of the query. It uses operators to
perform queries.

2.3.1. Types of Relational operation

Fig.2.1.Relation Operation Types

I. Select Operation:

o The select operation selects tuples that satisfy a given


predicate.
o It is denoted by sigma (σ).

Notation: σ p(r)

Where:

46
Relational Database Management System
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors
like: AND OR and NOT. These relational can use as relational
operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

47
Relational Database Management System
Perryride L-15 1500

Perryride L-16 1300

II. Project Operation:

o This operation shows the list of those attributes that we wish


to appear in the result. Rest of the attributes are eliminated
from the table.
o It is denoted by ∏.

Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

48
Relational Database Management System
Input:

∏ NAME, CITY (CUSTOMER)

Output:

NAME CITY

Jones Harrison

Smith Rye

Hays Harrison

Curry Rye

Johnson Brooklyn

Brooks Brooklyn

III. Union Operation:

o Suppose there are two tuples R and S. The union operation


contains all the tuples that are either in R or S or both in R &
S.
o It eliminates the duplicate tuples. It is denoted by ∪.

Notation: R ∪ S

A union operation must hold the following condition:

o R and S must have the attribute of the same number.

49
Relational Database Management System
o Duplicate tuples are eliminated automatically.

Example:

DEPOSITOR RELATION

CUSTOMER_NAME ACCOUNT_NO

Johnson A-101

Smith A-121

Mayes A-321

Turner A-176

Johnson A-273

Jones A-472

BORROW RELATION

CUSTOMER_NAME LOAN_NO

Jones L-17

Smith L-23

Hayes L-15

Jackson L-14

50
Relational Database Management System
Curry L-93

Smith L-11

Williams L-17

Input:

∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (D


EPOSITOR)

Output:

CUSTOMER_NAME

Johnson

Smith

Hayes

Turner

Jones

Lindsay

Jackson

Curry

Williams

Mayes

51
Relational Database Management System

IV. Set Intersection:

o Suppose there are two tuples R and S. The set intersection


operation contains all tuples that are in both R & S.
o It is denoted by intersection ∩.

Notation: R ∩ S

Example: Using the above DEPOSITOR table and BORROW table

Input:

∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (D


EPOSITOR)

Output:

CUSTOMER_NAME

Smith

Jones

V. Set Difference:

52
Relational Database Management System
o Suppose there are two tuples R and S. The set intersection
operation contains all tuples that are in R but not in S.
o It is denoted by intersection minus (-).

Notation: R - S

Example: Using the above DEPOSITOR table and BORROW table

Input:

∏ CUSTOMER_NAME (BORROW) -
∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Jackson

Hayes

Willians

Curry

VI. Cartesian product

o The Cartesian product is used to combine each row in one


table with each row in the other table. It is also known as a
cross product.
o It is denoted by X.

53
Relational Database Management System
Notation: E X D

Example:

EMPLOYEE

EMP_ID EMP_NAME EMP_DEPT

1 Smith A

2 Harry C

3 John B

DEPARTMENT

DEPT_NO DEPT_NAME

A Marketing

B Sales

C Legal

Input:

EMPLOYEE X DEPARTMENT

54
Relational Database Management System

Output:

EMP_I EMP_NA EMP_DE DEPT_N DEPT_NA


D ME PT O ME

1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Legal

VII.Rename Operation:

The rename operation is used to rename the output relation. It is


denoted by rho (ρ).

Example: We can use the rename operator to rename STUDENT


relation to STUDENT1.

55
Relational Database Management System
ρ(STUDENT1, STUDENT)

VIII. Join Operations:

A Join operation combines related tuples from different relations, if


and only if a given join condition is satisfied. It is denoted by ⋈.

Example:

EMPLOYEE

EMP_CODE EMP_NAME

101 Stephan

102 Jack

103 Harry

SALARY

EMP_CODE SALARY

101 50000
102 30000
103 25000

Operation: (EMPLOYEE ⋈ SALARY)

56
Relational Database Management System

Result:

EMP_CODE EMP_NAME SALARY

101 Stephan 50000

102 Jack 30000

103 Harry 25000

Types of Join operations:

57
Relational Database Management System
Fig.2.2. Join Operations

1. Natural Join:

o A natural join is the set of tuples of all combinations in R and


S that are equal on their common attribute names.
o It is denoted by ⋈.

Example: Let's use the above EMPLOYEE table and SALARY table:

Input:

∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)

Output:

EMP_NAME SALARY

Stephan 50000

Jack 30000

Harry 25000

2. Outer Join:

The outer join operation is an extension of the join operation. It is


used to deal with missing information.

58
Relational Database Management System

Example:

EMPLOYEE

EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

Input:

(EMPLOYEE ⋈ FACT_WORKERS)

59
Relational Database Management System

Output:

EMP_NAM STREE CITY BRANC SALAR


E T H Y

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru Hyderaba TCS 50000


nagar d

An outer join is basically of three types:

1. Left outer join


2. Right outer join
3. Full outer join

a. Left outer join:

o Left outer join contains the set of tuples of all combinations


in R and S that are equal on their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.

Example: Using the above EMPLOYEE table and


FACT_WORKERS table

60
Relational Database Management System
Input:

EMPLOYEE ⟕FACT_WORKERS

EMP_NAM STREE CITY BRANC SALAR


E T H Y

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru Hyderaba TCS 50000


street d

Ravi M.G. Delhi NULL NULL


Street

b. Right outer join:

o Right outer join contains the set of tuples of all combinations


in R and S that are equal on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.

Example: Using the above EMPLOYEE table and


FACT_WORKERS Relation

Input:

EMPLOYEE ⟖FACT_WORKERS

61
Relational Database Management System

Output:

EMP_NAM BRANC SALAR STREE CITY


E H Y T

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru Hyderaba


street d

Kuber HCL 30000 NULL NULL

c. Full outer join:

o Full outer join is like a left or right join except that it contains
all rows from both tables.
o In full outer join, tuples in R that have no matching tuples in
S and tuples in S that have no matching tuples in R in their
common attribute name.
o It is denoted by ⟗.

Example: Using the above EMPLOYEE table and


FACT_WORKERS table

Input:

62
Relational Database Management System
EMPLOYEE ⟗FACT_WORKERS

Output:

EMP_NAM STREE CITY BRANC SALAR


E T H Y

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru Hyderaba TCS 50000


street d

Ravi M.G. Delhi NULL NULL


Street

Kuber NULL NULL HCL 30000

3. Equi join:

It is also known as an inner join. It is the most common join. It is


based on matched data as per the equality condition. The equi join
uses the comparison operator(=).

Example:

CUSTOMER RELATION

CLASS_ID NAME

1 John

63
Relational Database Management System
2 Harry

3 Jackson

PRODUCT

PRODUCT_ID CITY

1 Delhi

2 Mumbai

3 Noida

Input:

CUSTOMER ⋈ PRODUCT

Output:

CLASS_ID NAME PRODUCT_ID CITY

1 John 1 Delhi

2 Harry 2 Mumbai

3 Harry 3 Noida

2.4. Advantages of Relational Algebra:

There are several advantages to using relational algebra:

64
Relational Database Management System
 Efficient execution: Relational algebra queries can be efficiently
executed using a variety of optimization techniques, including
indexing and query planning.
 Formal foundation: Relational algebra provides a formal
foundation for the design and implementation of relational
databases and query languages.
 Portable: Relational algebra queries can be easily translated and
executed on different database systems, as long as they support
the same set of operations.

Disadvantages:

However, there are also some potential drawbacks to using relational


algebra, including:

 Complexity: The use of relational algebra can add an additional


layer of complexity to the development and execution of queries,
as it requires a thorough understanding of the different
operations and their semantics.
 Limited expressiveness: Relational algebra has a limited set of
operations and does not support advanced features such as
aggregation, ranking, and window functions.

2.5. Integrity Constraints

65
Relational Database Management System
 Integrity constraints are a set of rules. It is used to maintain the
quality of information.
 Integrity constraints ensure that the data insertion, updating, and
other processes have to be performed in such a way that data
integrity is not affected.
 Thus, integrity constraint is used to guard against accidental
damage to the database.

Types of Integrity Constraint

2.5.1. Domain constraints

 Domain constraints can be defined as the definition of a valid set


of values for an attribute.
 The data type of domain includes string, character, integer, time,
date, currency, etc. The value of the attribute must be available
in the corresponding domain.

Example:

66
Relational Database Management System

2.5.2. Entity integrity constraints

o The entity integrity constraint states that primary key value


can't be null.
o This is because the primary key value is used to identify
individual rows in relation and if the primary key has a null
value, then we can't identify those rows.
o A table can contain a null value other than the primary key
field.

Example:

2.5.3. Referential Integrity Constraints

 A referential integrity constraint is specified between two tables.

67
Relational Database Management System
 In the Referential integrity constraints, if a foreign key in Table
1 refers to the Primary Key of Table 2, then every value of the
Foreign Key in Table 1 must be null or be available in Table 2.

Example:

2.5.4. Key constraints


o Keys are the entity set that is used to identify an entity within
its entity set uniquely.
o An entity set can have multiple keys, but out of which one
key will be the primary key. A primary key can contain a
unique and null value in the relational table.

Example:

68
Relational Database Management System

2.6. Relational Calculus

There is an alternate way of formulating queries known as Relational


Calculus. Relational calculus is a non-procedural query language. In
the non-procedural query language, the user is concerned with the
details of how to obtain the end results. The relational calculus tells
what to do but never explains how to do. Most commercial relational
languages are based on aspects of relational calculus including SQL-
QBE and QUEL.

Why it is called Relational Calculus?

It is based on Predicate calculus, a name derived from branch of


symbolic language. A predicate is a truth-valued function with
arguments. On substituting values for the arguments, the function
result in an expression called a proposition. It can be either true or
false. It is a tailored version of a subset of the Predicate Calculus to
communicate with the relational database.

Many of the calculus expressions involves the use of Quantifiers.


There are two types of quantifiers:

69
Relational Database Management System
o Universal Quantifiers: The universal quantifier denoted by
∀ is read as for all which means that in a given set of tuples
exactly all tuples satisfy a given condition.
o Existential Quantifiers: The existential quantifier denoted
by ∃ is read as for all which means that in a given set of
tuples there is at least one occurrences whose value satisfy a
given condition.

Before using the concept of quantifiers in formulas, we need to know


the concept of Free and Bound Variables.

A tuple variable t is bound if it is quantified which means that if it


appears in any occurrences a variable that is not bound is said to be
free.

Free and bound variables may be compared with global and local
variable of programming languages.

2.6.1. Types of Relational calculus:

Fig.2.3.Relational Calculus Types

2.6.2 Tuple Relational Calculus (TRC)

70
Relational Database Management System
It is a non-procedural query language which is based on finding a
number of tuple variables also known as range variable for which
predicate holds true. It describes the desired information without
giving a specific procedure for obtaining that information. The tuple
relational calculus is specified to select the tuples in a relation. In
TRC, filtering variable uses the tuples of a relation. The result of the
relation can have one or more tuples.

Notation:

A Query in the tuple relational calculus is expressed as following


notation

{T | P (T)} or {T | Condition (T)}

Where

T is the resulting tuples

P(T) is the condition used to fetch T.

For example:

{ T.name | Author(T) AND T.article = 'database' }

Output: This query selects the tuples from the AUTHOR relation. It
returns a tuple with 'name' from Author who has written an article o

n 'database'.

71
Relational Database Management System
TRC (tuple relation calculus) can be quantified. In TRC, we can use
Existential (∃ ) and Universal Quantifiers (∀ ).

For example:

{ R| ∃ T ∈ Authors(T.article='database' AND R.name=T.name)}

Output: This query will yield the same result as the previous one.

2.6.3. Domain Relational Calculus (DRC)

The second form of relation is known as Domain relational calculus.


In domain relational calculus, filtering variable uses the domain of
attributes. Domain relational calculus uses the same operators as
tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓
(not). It uses Existential (∃ ) and Universal Quantifiers (∀ ) to bind
the variable. The QBE or Query by example is a query language
related to domain relational calculus.

Notation:

{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

Where

a1, a2 are attributes


P stands for formula built by inner attributes

For example:

{< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}

Output: This query will yield the article, page, and subject from the
relational javatpoint, where the subject is a database.

72
Relational Database Management System
In the tuple relational calculus, you have use variables that have a
series of tuples in a relation. In the domain relational calculus, you
will also use variables, but in this case, the variables take their values
from domains of attributes rather than tuples of relations. A domain
relational calculus expression has the following general format:

{d1, d2, . . . , dn | F(d1, d2, . . . , dm)} m ≥ n


where d1, d2, . . . , dn, . . . , dm stand for domain variables and F(d1,
d2, . . . , dm) stands for a formula composed of atoms.

select TCHR_ID and TCHR_NAME of teachers who work for


department 8, (where suppose - dept. 8 is Computer Application
Department)

{<tchr_id, tchr_name=""> | <tchr_id, tchr_name=""> ? TEACHER Λ


DEPT_ID = 10}
Get the name of the department name where Karlos works:

{DEPT_NAME |< DEPT_NAME > ? DEPT Λ ? DEPT_ID ( ?


TEACHER Λ TCHR_NAME = Karlos)}

It is to be noted that these queries are safe. The use domain relational
calculus is restricted to safe expressions; moreover, it is equivalent to

73
Relational Database Management System
the tuple relational calculus, which in turn is similar to the relational
algebra.

2.7. Query By Example (QBE):

Query-by-Example (QBE) is another language for querying (and,


like SQL, for creating and modifying) relational data. It is different
from SQL, and from most other database query languages, in having
a graphical user interface that allows users to write queries by
creating example tables on the screen. A user needs minimal
information to get started and the whole language contains relatively
few concepts. QBE is especially suited for queries that are not too
complex and can be expressed in terms of a few tables.

QBE, like SQL, was developed at IBM and QBE is an IBM


trademark, but a number of other companies sell QBE-like interfaces,
including Paradox.

Some systems, such as Microsoft Access, offer partial support for


form-based queries and reflect the influence of QBE. Often a QBE-
like interface is offered in addition to SQL, with QBE serving as a
more intuitive user-interface for simpler queries and the full power of
SQL available for more complex queries. An appreciation of the
features of QBE offers insight into the more general, and widely used,
paradigm of tabular query interfaces for relational databases.

74
Relational Database Management System
A user writes queries by creating example tables. QBE uses domain
variables, as in the DRC, to create example tables. The domain of a
variable is determined by the column in which it appears, and
variable symbols are prefixed with underscore ( ) to distinguish them
from constants. Constants, including strings, appear unquoted, in
contrast to SQL. The fields that should appear in the answer are
specified by using the command P., which stands for print. The fields
containing this command are analogous to the target-list in the
SELECT clause of an SQL query.

We introduce QBE through example queries involving just one


relation. To print the names and ages of all sailors, we would create
the following example table:

Sailors sid sname rating age


P.N P.A

A variable that appears only once can be omitted; QBE supplies a


unique new name internally. Thus the previous query could also be
written by omitting the variables N and A, leaving just P. in the
sname and age columns. The query corresponds to the following
DRC query, obtained from the QBE query by introducing
existentially quantified domain variables for each field.
{⟨ N, A⟩ | ∃ I, T (⟨ I, N, T, A⟩ ∈ Sailors)}
A convenient shorthand notation is that if we want to print all fields
in some relation, we can place P. under the name of the relation. This

75
Relational Database Management System
notation is like the SELECT * convention in SQL. It is equivalent to
placing a P. in every field:

Sailors sid sname rating age


P.

Selections are expressed by placing a constant in some field:

Sailor sid sname rating age


s
P. 10

Placing a constant, say 10, in a column is the same as placing the


condition =10. This query is very similar in form to the equivalent
DRC query
{⟨ I, N, 10, A⟩ | ⟨ I, N, 10, A⟩ ∈ Sailors}
We can use other comparison operations (<, >, <=, >=, ¬) as well.
For example, we could say < 10 to retrieve sailors with a rating less
than 10 or say ¬10 to retrieve sailors whose rating is not equal to 10.
The expression ¬10 in an attribute column is the same as /= 10. As
we will see shortly, ¬ under the relation name denotes (a limited
form of) ¬∃ in the relational calculus sense.

Questions:

2 Mark Questions
1. Define relational model.
2. List down CODD‘s rule.

76
Relational Database Management System
3. Define keys.
4. What is integrity?
5. List down the types of integrity.
6. Define relational algebra.
7. What is mean by relational calculus?
8. Define QBE.

5 Mark / 10 Mark Questions

9. What is relational Database? Explain.


10. Explain in detail about Codd‘s Rules.
11. List down the advantages and disadvantages of relational
algebra.
12. What are the types of Relational DB?
13. Elucidate the Integrity Constraints in detail.
14. Write a short notes on Referential Integrity Constraints.
15. Illustrate the Relational calculus in detail with it‘s types.

77
Relational Database Management System
UNIT III
3. The Relational Database (RDBMS)

A relational database is a type of database that stores and provides


access to data points that are related to one another. Relational
databases are based on the relational model, an intuitive,
straightforward way of representing data in tables. In a relational
database, each row in the table is a record with a unique ID called the
key. The columns of the table hold attributes of the data, and each
record usually has a value for each attribute, making it easy to
establish the relationships among data points.

Here‘s a simple example of two tables a small business might use to


process orders for its products. The first table is a customer info table,
so each record includes a customer‘s name, address, shipping and
billing information, phone number, and other contact information.
Each bit of information (each attribute) is in its own column, and the
database assigns a unique ID (a key) to each row. In the second
table—a customer order table—each record includes the ID of the
customer that placed the order, the product ordered, the quantity, the
selected size and color, and so on—but not the customer‘s name or
contact information.

These two tables have only one thing in common: the ID column (the
key). But because of that common column, the relational database

78
Relational Database Management System
can create a relationship between the two tables. Then, when the
company‘s order processing application submits an order to the
database, the database can go to the customer order table, pull the
correct information about the product order, and use the customer ID
from that table to look up the customer‘s billing and shipping
information in the customer info table. The warehouse can then pull
the correct product, the customer can receive timely delivery of the
order, and the company can get paid.

3.1. The relational model

In the early years of databases, every application stored data in its


own unique structure. When developers wanted to build applications
to use that data, they had to know a lot about the particular data
structure to find the data they needed. These data structures were
inefficient, hard to maintain, and hard to optimize for delivering
good application performance. The relational database model was
designed to solve the problem of multiple arbitrary data structures.

The relational data model provided a standard way of representing


and querying data that could be used by any application. From the
beginning, developers recognized that the chief strength of the
relational database model was in its use of tables, which were an
intuitive, efficient, and flexible way to store and access structured
information.

79
Relational Database Management System

Over time, another strength of the relational model emerged as


developers began to use structured query language (SQL) to write
and query data in a database. For many years, SQL has been widely
used as the language for database queries. Based on relational
algebra, SQL provides an internally consistent mathematical
language that makes it easier to improve the performance of all
database queries. In comparison, other approaches must define
individual queries.

3.2. ACID properties and RDBMS

Four crucial properties define relational database transactions:


atomicity, consistency, isolation, and durability—typically referred
to as ACID.

 Atomicity defines all the elements that make up a complete


database transaction.

 Consistency defines the rules for maintaining data points in a


correct state after a transaction.

 Isolation keeps the effect of a transaction invisible to others


until it is committed, to avoid confusion.

 Durability ensures that data changes become permanent once


the transaction is committed.

80
Relational Database Management System
3.3. Database locking and concurrency

Conflicts can arise in a database when multiple users or applications


attempt to change the same data at the same time. Locking and
concurrency techniques reduce the potential for conflicts while
maintaining the integrity of the data.

Locking prevents other users and applications from accessing data


while it is being updated. In some databases, locking applies to the
entire table, which creates a negative impact on application
performance. Other databases, such as Oracle relational databases,
apply locks at the record level, leaving the other records within the
table available, helping ensure better application performance.

Concurrency manages the activity when multiple users or


applications invoke queries at the same time on the same database.
This capability provides the right access to users and applications
according to policies defined for data control.

3.4. Brief History of RDBMS

From 1970 to 1972, E.F. Codd published a paper to propose using a


relational database model.

RDBMS is originally based on E.F. Codd's relational model


invention.

81
Relational Database Management System
Following are the various terminologies of RDBMS:

Fig.3.1.RDBMS Key Terms

3.4.1. What is Table/Relation?

Everything in a relational database is stored in the form of relations.


The RDBMS database uses tables to store data. A table is a
collection of related data entries and contains rows and columns to
store data. Each table represents some real-world objects such as
person, place, or event about which information is collected. The
organized collection of data into a relational table is known as the
logical view of the database.

Properties of a Relation:

o Each relation has a unique name by which it is identified in


the database.

82
Relational Database Management System
o Relation does not contain duplicate tuples.
o The tuples of a relation have no specific order.
o All attributes in a relation are atomic, i.e., each cell of a
relation contains exactly one value.

A table is the simplest example of data stored in RDBMS.

Let's see the example of the student table.

ID Name AGE COURSE

1 Ajeet 24 B.Tech

2 aryan 20 C.A

3 Mahesh 21 BCA

4 Ratan 22 MCA

5 Vimal 26 BSC

3.4.2. What is a row or record?

A row of a table is also called a record or tuple. It contains the


specific information of each entry in the table. It is a horizontal entity
in the table. For example, The above table contains 5 records.

83
Relational Database Management System
Properties of a row:

o No two tuples are identical to each other in all their entries.


o All tuples of the relation have the same format and the same
number of entries.
o The order of the tuple is irrelevant. They are identified by
their content, not by their position.

Let's see one record/row in the table.

ID Name AGE COURSE

1 Ajeet 24 B.Tech

3.4.3. What is a column/attribute?

A column is a vertical entity in the table which contains all


information associated with a specific field in a table. For example,
"name" is a column in the above table which contains all information
about a student's name.

Properties of an Attribute:

o Every attribute of a relation must have a name.


o Null values are permitted for the attributes.

84
Relational Database Management System
o Default values can be specified for an attribute automatically
inserted if no other value is specified for an attribute.
o Attributes that uniquely identify each tuple of a relation are
the primary key.

Name

Ajeet

Aryan

Mahesh

Ratan

Vimal

3.4.5. What is data item/Cells?

The smallest unit of data in the table is the individual data item. It is
stored at the intersection of tuples and attributes.

Properties of data items:

o Data items are atomic.


o The data items for an attribute should be drawn from the
same domain.

85
Relational Database Management System
In the below example, the data item in the student table consists of
Ajeet, 24 and Btech, etc.

ID Name AGE COURSE

1 Ajeet 24 B.Tech

Degree:

The total number of attributes that comprise a relation is known as


the degree of the table.

For example, the student table has 4 attributes, and its degree is
4.

ID Name AGE COURSE

1 Ajeet 24 B.Tech

2 aryan 20 C.A

3 Mahesh 21 BCA

4 Ratan 22 MCA

5 Vimal 26 BSC

86
Relational Database Management System
3.4.6. Cardinality:

The total number of tuples at any one time in a relation is known as


the table's cardinality. The relation whose cardinality is 0 is called an
empty table.

For example, the student table has 5 rows, and its cardinality is 5.

ID Name AGE COURSE

1 Ajeet 24 B.Tech

2 aryan 20 C.A

3 Mahesh 21 BCA

4 Ratan 22 MCA

5 Vimal 26 BSC

Domain:

The domain refers to the possible values each attribute can contain. It
can be specified using standard data types such as integers, floating
numbers, etc. For example, An attribute entitled Marital_Status may
be limited to married or unmarried values.

87
Relational Database Management System
NULL Values

The NULL value of the table specifies that the field has been left
blank during record creation. It is different from the value filled with
zero or a field that contains space

3.4.7. Data Integrity

There are the following categories of data integrity exist with each
RDBMS:

Entity integrity: It specifies that there should be no duplicate rows


in a table.

Domain integrity: It enforces valid entries for a given column by


restricting the type, the format, or the range of values.

Referential integrity specifies that rows cannot be deleted, which


are used by other records.

User-defined integrity: It enforces some specific business rules


defined by users. These rules are different from the entity, domain, or
referential integrity.

88
Relational Database Management System
3.5. RDBMS Architecture

Fig.3.2.RDBMS Architecture

1) All data, data about data (metadata) and logs are stored in the
Secondary Storage devices (SSD), such as Disks and Tapes.The
programs that are used to do the day-to-day tasks of an
enterprise are called Application programs. These programs
provide the functionality for the day-to-day operations of the
enterprise. They are written in high level languages (HLL) like
Java, C etc, which along with the SQL, are used to communicate
with the databases.
2) RDBMS has a compiler that converts the SQL commands to
lower level language, processes it and stores it into the
secondary storage device.

89
Relational Database Management System
3) It is the job of Database Administrator (DBA) to set up the
structure of the database using command processor. The DDL
stands for Data Definition Language and is used by the DBA to
create or drop tables, add columns etc. The DBA also uses other
commands which are used to set constraints and access controls.
4) Application Programmers compile the applications using a
compiler and create executable files (compiled application
programs) and then store the data on the secondary storage
device.
5) Job of Data Analyst is to use the Query Compiler and Query
Optimizer (uses relational properties for executing queries) to
manipulate the data in the database.
6) RDBMS Run Time System executes the compiled queries and
application programs and also interacts with the transaction
manager and buffer manager.
7) Buffer Manager temporarily stores the data of the database in the
main memory and uses paging algorithm so that operations can
be performed faster and the disk space can be managed.
8) Transaction Manager deals with the principle of either
completely doing a task or not doing it at all (Atomicity
property). E.g. Suppose a person named Geeks, wants to send
money to his sister. He sends the money and system crashes in
between. In no case should it happen that he has sent money but
his sister has not received it. This is handled by the transaction

90
Relational Database Management System
manager. The transaction manager would either refund the
money to Geeks or transfer it to his sister.
9) Log is a system, which records the information about all the
transactions, so that whenever a system failure (disk failure,
system shut down due to no power etc.) arises, the partial
transactions can be undone.
10) Recovery Manager takes control of the system so that it reaches
a steady state after failure. The Recovery Manager takes into
account the log files and undoes the partial transactions and
reflects the complete transaction in the database.

3.6. RDBMS Tools


The software used to store, manage, query, and retrieve data stored in
a relational database is called a relational database management
system (RDBMS). The RDBMS provides an interface between users
and applications and the database, as well as administrative functions
for managing data storage, access, and performance.

Several factors can guide your decision when choosing among


database types and relational database products. The RDBMS you
choose will depend on your business needs. Ask yourself the
following questions:

 What are our data accuracy requirements? Will data storage and
accuracy rely on business logic? Does our data have stringent

91
Relational Database Management System
requirements for accuracy (for example, financial data and
government reports)?
 Do we need scalability? What is the scale of the data to be
managed, and what is its anticipated growth? Will the database
model need to support mirrored database copies (as separate
instances) for scalability? If so, can it maintain data consistency
across those instances?
 How important is concurrency? Will multiple users and
applications need simultaneous data access? Does the database
software support concurrency while protecting the data?
 What are our performance and reliability needs? Do we need a
high-performance, high-reliability product? What are the
requirements for query-response performance? What are the
vendor‘s commitments for service level agreements (SLAs) or
unplanned downtime?

All modern database management systems like SQL, MS SQL


Server, IBM DB2, ORACLE, My-SQL, and Microsoft Access are
based on RDBMS.

Relational Database Management System (RDBMS) tools are


software applications and utilities designed to assist in the
management, development, and administration of relational
databases. These tools are essential for database professionals and

92
Relational Database Management System
administrators to streamline tasks such as data modeling, schema
design, querying, data manipulation, and database maintenance.
There are various RDBMS tools available, and their selection often
depends on the specific needs and preferences of the users. Some
popular RDBMS tools include:

1. Microsoft SQL Server Management Studio (SSMS): A


comprehensive tool for managing Microsoft SQL Server
databases. It offers a wide range of features for database
design, administration, and querying.
2. MySQL Workbench: A visual database design tool and
administration tool for MySQL databases. It allows you to
create and manage databases, design schemas, and run SQL
queries.
3. Oracle SQL Developer: A free integrated development
environment (IDE) for Oracle databases. It offers tools for
database development, administration, and querying.
4. PostgreSQL: PostgreSQL comes with its own set of
command-line tools for database management and
administration. Additionally, various GUI tools like pgAdmin
are available for PostgreSQL users.
5. IBM Data Studio: An integrated development environment
for IBM Db2 and other IBM database products. It offers tools
for database design, development, and administration.

93
Relational Database Management System
6. Toad for Oracle: A popular tool for Oracle database
development and administration. It provides features for
coding, debugging, and performance tuning.
7. SQLiteStudio: A cross-platform SQLite database manager
that allows you to create, modify, and manage SQLite
databases.
8. DBVisualizer: A universal database tool that supports
multiple database management systems, including Oracle,
MySQL, PostgreSQL, and more. It offers a single interface
for various databases.
9. Navicat: A database management and development tool that
supports a wide range of databases, including MySQL,
PostgreSQL, Oracle, and SQL Server.
10. SQLyog: A graphical MySQL management and development
tool for database administrators and developers.

3.7. Anomalies in DBMS

What is Anomaly?

Anomaly means inconsistency in the pattern from the normal form.


In Database Management System (DBMS), anomaly means the
inconsistency occurred in the relational table during the operations
performed on the relational table.

94
Relational Database Management System
There can be various reasons for anomalies to occur in the database.
For example, if there is a lot of redundant data present in our
database then DBMS anomalies can occur. If a table is constructed in
a very poor manner then there is a chance of database anomaly. Due
to database anomalies, the integrity of the database suffers.

The other reason for the database anomalies is that all the data is
stored in a single table. So, to remove the anomalies of the database,
normalization is the process which is done where the splitting of the
table and joining of the table (different types of join) occurs.

Example 1:

Worker_id Worker_name Worker_dept Worker_address

65 Ramesh ECT001 Jaipur

65 Ramesh ECT002 Jaipur

73 Amit ECT002 Delhi

76 Vikas ECT501 Pune

76 Vikas ECT502 Pune

79 Rajesh ECT669 Mumbai

95
Relational Database Management System
In the above table, we have four columns which describe the details
about the workers like their name, address, department and their id.
The above table is not normalized, and there is definitely a chance of

anomalies present in the table.

3.7.1. There can be three types of an anomaly in the database:

A). Updation / Update Anomaly

When we update some rows in the table, and if it leads to the


inconsistency of the table then this anomaly occurs. This type of
anomaly is known as an updation anomaly. In the above table, if we
want to update the address of Ramesh then we will have to update all
the rows where Ramesh is present. If during the update we miss any
single row, then there will be two addresses of Ramesh, which will
lead to inconsistent and wrong databases.

B). Insertion Anomaly

If there is a new row inserted in the table and it creates the


inconsistency in the table then it is called the insertion anomaly. For
example, if in the above table, we create a new row of a worker, and
if it is not allocated to any department then we cannot insert it in the

table so, it will create an insertion anomaly.

96
Relational Database Management System
C). Deletion Anomaly

If we delete some rows from the table and if any other information or
data which is required is also deleted from the database, this is called
the deletion anomaly in the database. For example, in the above table,
if we want to delete the department number ECT669 then the details
of Rajesh will also be deleted since Rajesh's details are dependent on
the row of ECT669. So, there will be deletion anomalies in the table.
To remove this type of anomalies, we will normalize the table or split
the table or join the tables. There can be various normalized forms of
a table like 1NF, 2NF, 3NF, BCNF etc. we will apply the different
normalization schemes according to the current form of the table.

Example 2:

Stu_id Stu_name Stu_branch Stu_club

2018nk01 Shivani Computer science literature

2018nk01 Shivani Computer science dancing

2018nk02 Ayush Electronics Videography

2018nk03 Mansi Electrical dancing

2018nk03 Mansi Electrical singing

2018nk04 Gopal Mechanical Photography

In the above table, we have listed students with their name, id,
branch and their respective clubs.

97
Relational Database Management System
Updation / Update Anomaly

In the above table, if Shivani changes her branch from Computer


Science to Electronics, then we will have to update all the rows. If
we miss any row, then Shivani will have more than one branch,
which will create the update anomaly in the table.

Insertion Anomaly

If we add a new row for student Ankit who is not a part of any club,
we cannot insert the row into the table as we cannot insert null in the
column of stu_club. This is called insertion anomaly.

Deletion Anomaly

If we remove the photography club from the college, then we will


have to delete its row from the table. But it will also delete the table
of Gopal and his details. So, this is called deletion anomaly and it
will make the database inconsistent.

3.8. Redundancy in DBMS

Data redundancy means the occurrence of duplicate copies of similar


data. It is done intentionally to keep the same piece of data at
different places, or it occurs accidentally.

98
Relational Database Management System
What is Data redundancy in the database management system?

In DBMS, when the same data is stored in different tables, it causes


data redundancy.

Sometimes, it is done on purpose for recovery or backup of data,


faster access of data, or updating data easily. Redundant data costs
extra money, demands higher storage capacity, and requires extra
effort to keep all the files up to date. Sometimes, unintentional
duplicity of data causes a problem for the database to work properly,
or it may become harder for the end user to access data. Redundant
data unnecessarily occupy space in the database to save identical
copies, which leads to space constraints, which is one of the major
problems.

Let us understand redundancy in DBMS properly with the help of an


example.

Student_id Name Course Session Fee Department

101 Devi B. Tech 2022 90,000 CS

102 Sona B. Tech 2022 90,000 CS

103 Varun B. Tech 2022 90,000 CS

104 Satish B. Tech 2022 90,000 CS

99
Relational Database Management System
105 Amisha B. Tech 2022 90,000 CS

In the above example, there is a "Student" table that contains data


such as "Student_id", "Name", "Course", "Session", "Fee", and

"Department". As you can see, some data is repeated in the table,


which causes redundancy.

Problems that are caused due to redundancy in the database

Redundancy in DBMS gives rise to anomalies, and we will study it


further. In a database management system, the problems that occur
while working on data include inserting, deleting, and updating data
in the database.

We will understand these anomalies with the help of the following


student table:

student_id student_name student_age dept_id dept_name dept_head

1 Shiva 19 104 Information Jaspreet


Technology Kaur

2 Khushi 18 102 Electronics Avni Singh

3 Harsh 19 104 Information Jaspreet


Technology Kaur

100
Relational Database Management System
1. Insertion Anomaly:

Insertion anomaly arises when you are trying to insert some data into
the database, but you are not able to insert it.

Example: If you want to add the details of the student in the above
table, then you must know the details of the department; otherwise,
you will not be able to add the details because student details are
dependent on department details.

2. Deletion Anomaly:

Deletion anomaly arises when you delete some data from the
database, but some unrelated data is also deleted; that is, there will be
a loss of data due to deletion anomaly.

Example: If we want to delete the student detail, which has


student_id 2, we will also lose the unrelated data, i.e., department_id
102, from the above table.

3. Updating Anomaly:

An update anomaly arises when you update some data in the


database, but the data is partially updated, which causes data
inconsistency.

101
Relational Database Management System
Example: If we want to update the details of dept_head from
Jaspreet Kaur to Ankit Goyal for Dept_id 104, then we have to
update it everywhere else; otherwise, the data will get partially
updated, which causes data inconsistency.

Advantages of data redundancy in DBMS

 Provides Data Security: Data redundancy can enhance data


security as it is difficult for cyber attackers to attack data that are
in different locations.
 Provides Data Reliability: Reliable data improves accuracy
because organizations can check and confirm whether data is
correct.
 Create Data Backup: Data redundancy helps in backing up the
data.

Disadvantages of data redundancy in DBMS

 Data corruption: Redundant data leads to high chances of data


corruption.
 Wastage of storage: Redundant data requires more space,
leading to a need for more storage space.
 High cost: Large storage is required to store and maintain
redundant data, which is costly.

102
Relational Database Management System
How to reduce data redundancy in DBMS?

We can reduce data redundancy using the following methods:

 Database Normalization: We can normalize the data using the


normalization method. In this method, the data is broken down
into pieces, which means a large table is divided into two or
more small tables to remove redundancy. Normalization
removes insert anomaly, update anomaly, and delete anomaly.
 Deleting Unused Data: It is important to remove redundant data
from the database as it generates data redundancy in the DBMS.
It is a good practice to remove unwanted data to reduce
redundancy.
 Master Data: The data administrator shares master data across
multiple systems. Although it does not remove data redundancy,
but it updates the redundant data whenever the data is changed.

3.9. Functional Dependency

The functional dependency is a relationship that exists between two


attributes. It typically exists between the primary key and non-key
attribute within a table.

X → Y

103
Relational Database Management System
The left side of FD is known as a determinant, the right side of the
production is known as a dependent.

For example:

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.

Example:

roll_no name dept_name dept_building

42 abc CO A4

43 pqr IT A3

44 xyz CO A4

45 xyz IT A3

104
Relational Database Management System
roll_no name dept_name dept_building

46 mno EC B2

47 jkl ME B2

From the above table we can conclude some valid functional


dependencies:

 roll_no → { name, dept_name, dept_building },→ Here,


roll_no can determine values of fields name, dept_name and
dept_building, hence a valid Functional dependency
 roll_no → dept_name , Since, roll_no can determine whole set
of {name, dept_name, dept_building}, it can determine its
subset dept_name also.
 dept_name → dept_building , Dept_name can identify the
dept_building accurately, since departments with different
dept_name will also have a different dept_building
 More valid functional dependencies: roll_no → name, {roll_no,
name} ⇢ {dept_name, dept_building}, etc.

Here are some invalid functional dependencies:

 name → dept_name Students with the same name can have


different dept_name, hence this is not a valid functional
dependency.

105
Relational Database Management System
 dept_building → dept_name There can be multiple
departments in the same building. Example, in the above table
departments ME and EC are in the same building B2, hence
dept_building → dept_name is an invalid functional
dependency.
 More invalid functional dependencies: name → roll_no, {name,
dept_name} → roll_no, dept_building → roll_no, etc.
Armstrong’s axioms/properties of functional dependencies:

1. Reflexivity: If Y is a subset of X, then X→Y holds by


reflexivity rule
Example, {roll_no, name} → name is valid.
2. Augmentation: If X → Y is a valid dependency, then XZ →
YZ is also valid by the augmentation rule.
Example, {roll_no, name} → dept_building is valid, hence
{roll_no, name, dept_name} → {dept_building, dept_name} is
also valid.
3. Transitivity: If X → Y and Y → Z are both valid
dependencies, then X→Z is also valid by the Transitivity rule.
Example, roll_no → dept_name & dept_name → dept_building,
then roll_no → dept_building is also valid.

3.9.1. Types of Functional dependency

 Trivial functional dependency


 Non-Trivial functional dependency

106
Relational Database Management System
 Multivalued functional dependency
 Transitive functional dependency

1. Trivial Functional Dependency

In Trivial Functional Dependency, a dependent is always a subset


of the determinant. i.e. If X → Y and Y is the subset of X, then it is
called trivial functional dependency

Example:

roll_no name Age

42 Abc 17

43 Pqr 18

44 xyz 18

Here, {roll_no, name} → name is a trivial functional dependency,


since the dependent name is a subset of determinant set {roll_no,
name}. Similarly, roll_no → roll_no is also an example of trivial
functional dependency.

107
Relational Database Management System
2. Non-trivial Functional Dependency

In Non-trivial functional dependency, the dependent is strictly not


a subset of the determinant. i.e. If X → Y and Y is not a subset of
X, then it is called Non-trivial functional dependency.

Example:

roll_no name age

42 abc 17

43 pqr 18

44 xyz 18

Here, roll_no → name is a non-trivial functional dependency, since


the dependent name is not a subset
of determinant roll_no. Similarly, {roll_no, name} → age is also a
non-trivial functional dependency, since age is not a subset of
{roll_no, name}

3. Multivalued Functional Dependency

In Multivalued functional dependency, entities of the dependent


set are not dependent on each other. i.e. If a → {b, c} and there

108
Relational Database Management System
exists no functional dependency between b and c, then it is called
a multivalued functional dependency.

For example,

roll_no name age

42 abc 17

43 pqr 18

44 xyz 18

45 abc 19

Here, roll_no → {name, age} is a multivalued functional


dependency, since the dependents name & age are not
dependent on each other(i.e. name → age or age → name doesn’t
exist !)

4. Transitive Functional Dependency

In transitive functional dependency, dependent is indirectly


dependent on determinant. i.e. If a → b & b → c, then according to
axiom of transitivity, a → c. This is a transitive functional
dependency.

109
Relational Database Management System
For example,

enrol_no name deptbuilding_no

42 abc CO 4

43 pqr EC 2

44 xyz IT 1

45 abc EC 2

Here, enrol_no → dept and dept → building_no. Hence,


according to the axiom of transitivity, enrol_no → building_no is
a valid functional dependency. This is an indirect functional
dependency, hence called Transitive functional dependency.

5. Fully Functional Dependency

In full functional dependency an attribute or a set of attributes


uniquely determines another attribute or set of attributes. If a
relation R has attributes X, Y, Z with the dependencies X->Y and
X->Z which states that those dependencies are fully functional.

6. Partial Functional Dependency

In partial functional dependency a non key attribute depends on a


part of the composite key, rather than the whole key. If a relation R

110
Relational Database Management System
has attributes X, Y, Z where X and Y are the composite key and Z
is non key attribute. Then X->Z is a partial functional dependency
in RBDMS.

Advantages of Functional Dependencies

Functional dependencies having numerous applications in the field


of database management system. Here are some applications listed
below:

1. Data Normalization

Data normalization is the process of organizing data in a database in


order to minimize redundancy and increase data integrity.
Functional dependencies play an important part in data
normalization.

With the help of functional dependencies we are able to identify the


primary key, candidate key in a table which in turns helps in
normalization.

2. Query Optimization

With the help of functional dependencies we are able to decide the


connectivity between the tables and the necessary attributes need to
be projected to retrieve the required data from the tables. This helps
in query optimization and improves performance.

111
Relational Database Management System
3. Consistency of Data

Functional dependencies ensures the consistency of the data


by removing any redundancies or inconsistencies that may exist in
the data. Functional dependency ensures that the changes made in
one attribute does not affect inconsistency in another set of
attributes thus it maintains the consistency of the data in database.

4. Data Quality Improvement

Functional dependencies ensure that the data in the database to be


accurate, complete and updated. This helps to improve the overall
quality of the data, as well as it eliminates errors and inaccuracies
that might occur during data analysis and decision making, thus
functional dependency helps in improving the quality of data in
database.

3.10. Normalization

A large database defined as a single relation may result in data


duplication. This repetition of data may result in:

 Making relations very large.


 It isn't easy to maintain and update data as it would involve
searching many records in relation.
 Wastage and poor utilization of disk space and resources.

112
Relational Database Management System
 The likelihood of errors and inconsistencies increases.

So to handle these problems, we should analyze and decompose the


relations with redundant data into smaller, simpler, and well-
structured relations that are satisfy desirable properties.
Normalization is a process of decomposing the relations into
relations with fewer attributes.

What is Normalization:

o Normalization is the process of organizing the data in the


database.
o Normalization is used to minimize the redundancy from a
relation or set of relations. It is also used to eliminate
undesirable characteristics like Insertion, Update, and
Deletion Anomalies.
o Normalization divides the larger table into smaller and links
them using relationships.
o The normal form is used to reduce redundancy from the
database table.

The main reason for normalizing the relations is removing these


anomalies. Failure to eliminate anomalies leads to data redundancy
and can cause data integrity and other problems as the database
grows. Normalization consists of a series of guidelines that helps to
guide you in creating a good database structure.

113
Relational Database Management System
3.10.1. Types of Normal Forms

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 all non-key


attributes are fully functional dependent on the primary
key.

3NF A relation will be in 3NF if it is in 2NF and no


transition dependency exists.

BCNF A stronger definition of 3NF is known as Boyce Codd's


normal form.

4NF A relation will be in 4NF if it is in Boyce Codd's


normal form and has no multi-valued dependency.

5NF A relation is in 5NF. If it is in 4NF and does not contain


any join dependency, joining should be lossless.

Advantages of Normalization

 Normalization helps to minimize data redundancy.


 Greater overall database organization.
 Data consistency within the database.
 Much more flexible database design.
 Enforces the concept of relational integrity.

114
Relational Database Management System
Disadvantages of Normalization

 You cannot start building the database before knowing what the
user needs.
 The performance degrades when normalizing the relations to
higher normal forms, i.e., 4NF, 5NF.
 It is very time-consuming and difficult to normalize relations of
a higher degree.
 Careless decomposition may lead to a bad database design,
leading to serious problems.

Before we proceed let‘s understand a few things —

What is a KEY in SQL?

A KEY in SQL is a value used to identify records in a table uniquely.


An SQL KEY is a single column or combination of multiple columns
used to uniquely identify rows or tuples in the table. SQL Key is
used to identify duplicate information, and it also helps establish a
relationship between multiple tables in the database.

Note: Columns in a table that are NOT used to identify a record


uniquely are called non-key columns.

115
Relational Database Management System
What is a Primary Key?

A primary is a single column value used to identify a database record


uniquely.

It has following attributes

 A primary key cannot be NULL


 A primary key value must be unique
 The primary key values should rarely be changed
 The primary key must be given a value when a new record is
inserted.

What is Composite Key?

A composite key is a primary key composed of multiple columns


used to identify a record uniquely.

3.10.2. First Normal Form (1NF)

If a relation contains a composite or multi-valued attribute, it violates


the first normal form, or the relation is in first normal form if it does
not contain any composite or multi-valued attribute. A relation is in
first normal form if every attribute in that relation is singled valued
attribute.

116
Relational Database Management System
A table is in 1 NF if:

 There are only Single Valued Attributes.


 Attribute Domain does not change.
 There is a unique name for every Attribute/Column.
 The order in which data is stored does not matter.

Consider the examples given below.

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.

117
Relational Database Management System

Example-2:
ID Name Courses

------------------

1 A c1, c2

2 E c3

3 M C2, c3

In the above table, Course is a multi-valued attribute so it is not in


1NF.

Below Table is in 1NF as there is no multi-valued attribute:

ID Name Course

------------------

1 A c1

1 A c2

2 E c3

3 M c2

3 M c3

Note: A database design is considered as bad if it is not even in the


First Normal Form (1NF).

118
Relational Database Management System
3.10.3. Second Normal Form (2NF)

First Normal Form (1NF) does not eliminate redundancy, but rather,
it‘s that it eliminates repeating groups. Instead of having multiple
columns of the same kind of data in a record, (0NF or Unnormalized
form) you remove the repeated information into a separate relation
and represent them as rows. This is what constitutes 1NF.

Second Normal Form (2NF) is based on the concept of full


functional dependency. Second Normal Form applies to relations
with composite keys, that is, relations with a primary key composed
of two or more attributes. A relation with a single-attribute primary
key is automatically in at least 2NF. A relation that is not in 2NF
may suffer from the update anomalies. To be in second normal form,
a relation must be in first normal form and relation must not contain
any partial dependency. A relation is in 2NF if it has No Partial
Dependency, i.e., no non-prime attribute (attributes which are not
part of any candidate key) is dependent on any proper subset of any
candidate key of the table. In other words,

A relation that is in First Normal Form and every non-primary-key


attribute is fully functionally dependent on the primary key, then the
relation is in Second Normal Form (2NF).

Note – If the proper subset of candidate key determines non-prime


attribute, it is called partial dependency. The normalization of 1NF

119
Relational Database Management System
relations to 2NF involves the removal of partial dependencies. If a
partial dependency exists, we remove the partially dependent
attribute(s) from the relation by placing them in a new relation along
with a copy of their determinant. Consider the examples given below.
Example-1: Consider table as following below.

STUD_NO COURSE_NO COURSE_FEE


1 C1 1000
2 C2 1500
1 C4 2000
4 C3 1000
4 C1 1000
2 C5 2000
{Note that, there are many courses having the same course fee. }

Here, COURSE_FEE cannot alone decide the value of


COURSE_NO or STUD_NO; COURSE_FEE together with
STUD_NO cannot decide the value of COURSE_NO;
COURSE_FEE together with COURSE_NO cannot decide the value
of STUD_NO;

Hence, COURSE_FEE would be a non-prime attribute, as it does not


belong to the one only candidate key {STUD_NO, COURSE_NO} ;

But, COURSE_NO -> COURSE_FEE, i.e., COURSE_FEE is


dependent on COURSE_NO, which is a proper subset of the

120
Relational Database Management System
candidate key. Non-prime attribute COURSE_FEE is dependent on a
proper subset of the candidate key, which is a partial dependency and
so this relation is not in 2NF.

To convert the above relation to 2NF, we need to split the table into
two tables such as : Table 1: STUD_NO, COURSE_NO

Table 2: COURSE_NO, COURSE_FEE

Table 1 Table 2
STUD_NO COURSE_NO COURSE_NO COURSE_FEE
1 C1 C1 1000
2 C2 C2 1500
1 C4 C3 1000
4 C3 C4 2000
4 C1 C5 2000
2 C5
Note – 2NF tries to reduce the redundant data getting stored in
memory. For instance, if there are 100 students taking C1 course, we
dont need to store its Fee as 1000 for all the 100 records, instead
once we can store it in the second table as the course fee for C1 is
1000.

Example-2: Consider following functional dependencies in relation


R (A, B, C, D )

121
Relational Database Management System
AB ->C [A and B together determine C]
BC ->D [B and C together determine D]

In this case, we can see that the relation R has a composite candidate
key {A,B} as AB->C. Therefore, A and B together uniquely
determine the value of C. Similarly, BC -> D shows that B and C
together uniquely determine the value of D.

The relation R is already in 1NF because it does not have any


repeating groups or nested relations.

However, we can see that the non-prime attribute D is functionally


dependent on only part of a candidate key, BC. This violates the 2NF
condition.

3.10.4. Third Normal Form (3NF)


Third Normal Form (3NF):
A relation is in third normal form, if there is no transitive
dependency for non-prime attributes as well as it is in second normal
form.

A relation is in 3NF if at least one of the following condition holds in


every non-trivial function dependency X –> Y:

1. X is a super key.
2. Y is a prime attribute (each element of Y is part of some
candidate key).

122
Relational Database Management System
In other words,

A relation that is in First and Second 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).

Note – If A->B and B->C are two FDs then A->C is called transitive
dependency.

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.

Example-1:
In relation STUDENT given in Table ,

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

123
Relational Database Management System
Candidate Key:
{STUD_NO}

For this relation in table 4, 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)

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

A -> BC,

CD -> E,

B -> D,

E -> A

All possible candidate keys in above relation are {A, E, CD, BC} All
attribute are on right sides of all functional dependencies are prime.

124
Relational Database Management System
Note –
Third Normal Form (3NF) is considered adequate for normal
relational database design because most of the 3NF tables are free of
insertion, update, and deletion anomalies. Moreover, 3NF always
ensures functional dependency preserving and lossless.

Boyce-Codd Normal Form (BCNF)

Application of the general definitions of 2NF and 3NF may identify


additional redundancy caused by dependencies that violate one or
more candidate keys. However, despite these additional constraints,
dependencies can still exist that will cause redundancy to be present
in 3NF relations. This weakness in 3NF resulted in the presentation
of a stronger normal form called the Boyce-Codd Normal Form
(Codd, 1974).

Although, 3NF is an adequate normal form for relational databases,


still, 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 Boyce-Codd Normal
Form (BCNF).

Boyce–Codd Normal Form (BCNF) is based on functional


dependencies that take into account all candidate keys in a relation;
however, BCNF also has additional constraints compared with the
general definition of 3NF.

125
Relational Database Management System
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.

Note: To test whether a relation is in BCNF, we identify all the


determinants and make sure that they are candidate keys.

Fig.3.1. BCNF in DBMS

You came across a similar hierarchy known as the Chomsky


Normal Form in the Theory of Computation. Now, carefully study
the hierarchy above. It can be inferred that every relation in BCNF
is also in 3NF. To put it another way, a relation in 3NF need not 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

126
Relational Database Management System
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 a 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.

Examples
Here, we are going to discuss some basic examples which let you
understand the properties of BCNF. We will discuss multiple
examples here.

Example 1

Let us consider the student database, in which data of the student


are mentioned.

Stu_ID Stu_Branch Stu_Course Branch_Number Stu_Course_No

Computer Science &


101 DBMS B_001 201
Engineering

Computer Science Computer


101 B_001 202
&Engineering Networks

127
Relational Database Management System
Stu_ID Stu_Branch Stu_Course Branch_Number Stu_Course_No

Electronics &
VLSI
102 Communication B_003 401
Technology
Engineering

Electronics &
Mobile
102 Communication B_003 402
Communication
Engineering

Functional Dependency of the above is as mentioned:

Stu_ID −> Stu_Branch

Stu_Course −> {Branch_Number, Stu_Course_No}

Candidate Keys of the above table are: {Stu_ID, Stu_Course}

Why this Table is Not in BCNF?


The table present above is not in BCNF, because as we can see that
neither Stu_ID nor Stu_Course is a Super Key. As the rules
mentioned above clearly tell that for a table to be in BCNF, it must
follow the property that for functional dependency X−>Y, X must
be in Super Key and here this property fails, that‘s why this table is
not in BCNF.

128
Relational Database Management System
How to Satisfy BCNF?

For satisfying this table in BCNF, we have to decompose it into


further tables. Here is the full procedure through which we
transform this table into BCNF. Let us first divide this main table
into two tables Stu_Branch and Stu_Course Table.

Stu_Branch Table

Stu_ID Stu_Branch

101 Computer Science & Engineering

102 Electronics & Communication Engineering

Candidate Key for this table: Stu_ID.

Stu_Course Table

Stu_Course Branch_Number Stu_Course_No

DBMS B_001 201

Computer Networks B_001 202

VLSI Technology B_003 401

Mobile Communication B_003 402

Candidate Key for this table: Stu_Course.

129
Relational Database Management System
Stu_ID to Stu_Course_No Table

Stu_ID Stu_Course_No

101 201

101 202

102 401

102 402

Candidate Key for this table: {Stu_ID, Stu_Course_No}.

After decomposing into further tables, now it is in BCNF, as it is


passing the condition of Super Key, that in functional dependency
X−>Y, X is a Super Key.

Example 2

Find the highest normal form of a relation R(A, B, C, D, E) with


FD set as:

{ BC->D, AC->BE, B->E }

130
Relational Database Management System
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.

The relation is in 2nd normal form because BC->D is in 2nd normal


form (BC is not a proper subset of candidate key AC) and AC->BE
is in 2nd normal form (AC is candidate key) and B->E is in 2nd
normal form (B is not a proper subset of candidate key AC).

The relation is not in 3rd normal form because in BC->D (neither


BC is a super key nor D is a prime attribute) and in B->E (neither B
is a super key nor E is a prime attribute) but to satisfy 3rd normal
for, either LHS of an FD should be super key or RHS should be a
prime attribute. So the highest normal form of relation will be the
2nd Normal form.

131
Relational Database Management System
Note: A prime attribute cannot be transitively dependent on a key
in BCNF relation.

Consider these functional dependencies of some relation R

AB ->C

C ->B

AB ->B

Suppose, it is known that the only candidate key of R is AB. A


careful observation is required to conclude that the above
dependency is a Transitive Dependency as the prime attribute B
transitively depends on the key AB through C. Now, the first and
the third FD are in BCNF as they both contain the candidate key (or
simply KEY) on their left sides. The second dependency, however,
is not in BCNF but is definitely in 3NF due to the presence of the
prime attribute on the right side. So, the highest normal form of R is
3NF as all three FDs satisfy the necessary conditions to be in 3NF.

Example 3

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

A -> BC,

B -> A

A and B both are super keys so the above relation is in BCNF.

132
Relational Database Management System
Note: BCNF decomposition may always not be possible
with dependency preserving, however, it always satisfies
the 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.

There are also some higher-order normal forms, like the 4th Normal
Form and the 5th Normal Form.

3.11. Transactions

A transaction can be defined as a group of tasks. A single task is the


minimum processing unit which cannot be divided further.

Let‘s take an example of a simple transaction. Suppose a bank


employee transfers Rs 500 from A's account to B's account. This
very simple and small transaction involves several low-level tasks.

A’s Account

Open_Account(A)
Old_Balance = A.balance

133
Relational Database Management System
New_Balance = Old_Balance - 500
A.balance = New_Balance
Close_Account(A)

B’s Account

Open_Account(B)
Old_Balance = B.balance
New_Balance = Old_Balance + 500
B.balance = New_Balance
Close_Account(B)

ACID Properties

A transaction is a very small unit of a program and it may contain


several lowlevel tasks. A transaction in a database system must
maintain Atomicity, Consistency, Isolation, and Durability −
commonly known as ACID properties − in order to ensure accuracy,
completeness, and data integrity.

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.

134
Relational Database Management System
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.

Durability − The database should be durable enough to hold all its


latest updates even if the system fails or restarts. If a transaction
updates a chunk of data in a database and commits, then the database
will hold the modified data. If a transaction commits but the system
fails before the data could be written on to the disk, then that data
will be updated once the system springs back into action.

Isolation − In a database system where more than one transaction are


being executed simultaneously and in parallel, the property of
isolation states that all the transactions will be carried out and
executed as if it is the only transaction in the system. No transaction
will affect the existence of any other transaction.

Serializability

When multiple transactions are being executed by the operating


system in a multiprogramming environment, there are possibilities
that instructions of one transactions are interleaved with some other
transaction.

135
Relational Database Management System
 Schedule − A chronological execution sequence of a transaction
is called a schedule. A schedule can have many transactions in it,
each comprising of a number of instructions/tasks.
 Serial Schedule − It is a schedule in which transactions are
aligned in such a way that one transaction is executed first.
When the first transaction completes its cycle, then the next
transaction is executed. Transactions are ordered one after the
other. This type of schedule is called a serial schedule, as
transactions are executed in a serial manner.

In a multi-transaction environment, serial schedules are considered as


a benchmark. The execution sequence of an instruction in a
transaction cannot be changed, but two transactions can have their
instructions executed in a random fashion. This execution does no
harm if two transactions are mutually independent and working on
different segments of data; but in case these two transactions are
working on the same data, then the results may vary. This ever-
varying result may bring the database to an inconsistent state.

To resolve this problem, we allow parallel execution of a transaction


schedule, if its transactions are either serializable or have some
equivalence relation among them.

Equivalence Schedules

An equivalence schedule can be of the following types −

136
Relational Database Management System
Result Equivalence

If two schedules produce the same result after execution, they are
said to be result equivalent. They may yield the same result for some
value and different results for another set of values. That's why this
equivalence is not generally considered significant.

View Equivalence

Two schedules would be view equivalence if the transactions in both


the schedules perform similar actions in a similar manner.

For example −

 If T reads the initial data in S1, then it also reads the initial data
in S2.
 If T reads the value written by J in S1, then it also reads the
value written by J in S2.
 If T performs the final write on the data value in S1, then it also
performs the final write on the data value in S2.

Conflict Equivalence

Two schedules would be conflicting if they have the following


properties −

 Both belong to separate transactions.


 Both accesses the same data item.
 At least one of them is "write" operation.

137
Relational Database Management System
Two schedules having multiple transactions with conflicting
operations are said to be conflict equivalent if and only if −

 Both the schedules contain the same set of Transactions.


 The order of conflicting pairs of operation is maintained in both
the schedules.

Note − View equivalent schedules are view serializable and conflict


equivalent schedules are conflict serializable. All conflict serializable
schedules are view serializable too.

States of Transactions

A transaction in a database can be in one of the following states −

 Active − In this state, the transaction is being executed. This is


the initial state of every transaction.
 Partially Committed − When a transaction executes its final
operation, it is said to be in a partially committed state.

138
Relational Database Management System
 Failed − A transaction is said to be in a failed state if any of the
checks made by the database recovery system fails. A failed
transaction can no longer proceed further.
 Aborted − If any of the checks fails and the transaction has
reached a failed state, then the recovery manager rolls back all
its write operations on the database to bring the database back to
its original state where it was prior to the execution of the
transaction. Transactions in this state are called aborted. The
database recovery module can select one of the two operations
after a transaction aborts −
 Re-start the transaction
 Kill the transaction

Committed − If a transaction executes all its operations successfully,


it is said to be committed. All its effects are now permanently
established on the database system.

3.12. Database Security

Security of databases refers to the array of controls, tools, and


procedures designed to ensure and safeguard confidentiality,
integrity, and accessibility. This tutorial will concentrate on
confidentiality because it's a component that is most at risk in data
security breaches.

139
Relational Database Management System
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.

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.

3.12.1. Why Database Security is Important?

According to the definition, a data breach refers to a breach of data


integrity in databases. The amount of damage an incident like a data
breach can cause our business is contingent on various consequences
or elements.

140
Relational Database Management System
 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.
 Penalties or fines to be paid for not complying: The cost of
not complying with international regulations like the Sarbanes-
Oxley Act (SAO) or Payment Card Industry Data Security
Standard (PCI DSS) specific to industry regulations on data
privacy, like HIPAA or regional privacy laws like the European
Union's General Data Protection Regulation (GDPR) could be a
major problem with fines in worst cases in excess of many
million dollars for each violation.
 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

141
Relational Database Management System
forensic services such as crisis management, triage repairs to the
affected systems, and much more.

3.11.2. Common Threats and Challenges

Numerous software configurations that are not correct, weaknesses,


or patterns of carelessness or abuse can lead to a breach of security.
Here are some of the most prevalent kinds of reasons for security
attacks and the reasons.

Insider Dangers

An insider threat can be an attack on security from any three sources


having an access privilege to the database.

 A malicious insider who wants to cause harm


 An insider who is negligent and makes mistakes that expose the
database to attack. vulnerable to attacks
 An infiltrator is an outsider who acquires credentials by using a
method like phishing or accessing the database of credential
information in the database itself.

Insider dangers are among the most frequent sources of security


breaches to databases. They often occur as a consequence of the
inability of employees to have access to privileged user credentials.

142
Relational Database Management System
Human Error

The unintentional mistakes, weak passwords or sharing passwords,


and other negligent or uninformed behaviours of users remain the
root causes of almost half (49 percent) of all data security breaches.

Database Software Vulnerabilities can be Exploited

Hackers earn their money by identifying and exploiting


vulnerabilities in software such as databases management software.
The major database software companies and open-source databases
management platforms release regular security patches to fix these
weaknesses. However, failing to implement the patches on time
could increase the risk of being hacked.

SQL/NoSQL Injection Attacks

A specific threat to databases is the infusing of untrue SQL as well as


other non-SQL string attacks in queries for databases delivered by
web-based apps and HTTP headers. Companies that do not follow
the safe coding practices for web applications and conduct regular
vulnerability tests are susceptible to attacks using these.

Buffer Overflow is a way to Exploit Buffers

Buffer overflow happens when a program seeks to copy more data


into the memory block with a certain length than it can accommodate.

143
Relational Database Management System
The attackers may make use of the extra data, which is stored in
adjacent memory addresses, to establish a basis for they can begin
attacks.

DDoS (DoS/DDoS) Attacks

In a denial-of-service (DoS) attack in which the attacker overwhelms


the targeted server -- in this case, the database server with such a
large volume of requests that the server is unable to meet no longer
legitimate requests made by actual users. In most cases, the server is
unstable or even fails to function.

Malware

Malware is software designed to exploit vulnerabilities or cause harm


to databases. Malware can be accessed via any device that connects
to the databases network.

Attacks on Backups

Companies that do not protect backup data using the same rigorous
controls employed to protect databases themselves are at risk of
cyberattacks on backups.

The following factors amplify the threats:

144
Relational Database Management System
 Data volumes are growing: Data capture, storage, and
processing continue to increase exponentially in almost all
organizations. Any tools or methods must be highly flexible to
meet current as well as far-off needs.
 The infrastructure is sprawling: Network environments are
becoming more complicated, especially as companies shift their
workloads into multiple clouds and hybrid cloud architectures
and make the selection of deployment, management, and
administration of security solutions more difficult.
 More stringent requirements for regulatory compliance: The
worldwide regulatory compliance landscape continues to
increase by complexity. This makes the compliance of every
mandate more challenging.

3.12.3. Best use of Database Security

As databases are almost always accessible via the network, any


security risk to any component or part of the infrastructure can
threaten the database. Likewise, any security attack that impacts a
device or workstation could endanger the database. Therefore,
security for databases must go beyond the limits of the database.

In evaluating the security of databases in our workplace to determine


our organization's top priorities, look at each of these areas.

145
Relational Database Management System
 Security for physical security: If the database servers are on-
premises or the cloud data centre, they should be placed in a
secure, controlled climate. (If our server for database is located
in a cloud-based data centre, the cloud provider will handle the
security on our behalf.)
 Access to the network and administrative restrictions: The
practical minimum number of users granted access to the
database and their access rights should be restricted to the
minimum level required to fulfil their tasks. Additionally, access
to the network is limited to the minimum permissions needed.
 End security of the user account or device: Be aware of who
has access to the database and when and how data is used.
Monitoring tools for data can notify you of data-related activities
that are uncommon or seem to be dangerous. Any device that
connects to the network hosting the database must be physically
secured (in the sole control of the appropriate person) and be
subject to security checks throughout the day.
 Security: ALL data--including data stored in databases, as well
as credential information should be secured using the highest-
quality encryption when in storage and while in transport. All
encryption keys must be used in accordance with the best
practices guidelines.

146
Relational Database Management System
 Security of databases using software: Always use the most
current version of our software to manage databases and apply
any patches immediately after they're released.
 Security for web server applications and websites: Any
application or web server that connects to the database could be
a target and should be subjected to periodic security testing and
best practices management.
 Security of backups: All backups, images, or copies of the
database should have the identical (or equally rigorous) security
procedures as the database itself.
 Auditing: Audits of security standards for databases should be
conducted every few months. Record all the logins on the server
as well as the operating system. Also, record any operations that
are made on sensitive data, too.

3.12.4. Data protection tools and platforms

Today, a variety of companies provide data protection platforms and


tools. A comprehensive solution should have all of the following
features:

 Discovery: The ability to discover is often needed to meet


regulatory compliance requirements. Look for a tool that can
detect and categorize weaknesses across our databases, whether
they're hosted in the cloud or on-premises. It will also provide

147
Relational Database Management System
recommendations to address any vulnerabilities that are
discovered.
 Monitoring of Data Activity: The solution should be capable of
monitoring and analysing the entire data activity in all databases,
whether our application is on-premises, in the cloud, or inside a
container. It will alert us to suspicious activity in real-time to
allow us to respond more quickly to threats. It also provides
visibility into the state of our information through an integrated
and comprehensive user interface. It is also important to choose
a system that enforces rules that govern policies, procedures, and
the separation of duties. Be sure that the solution we select is
able to generate the reports we need to comply with the
regulations.
 The ability to Tokenize and Encrypt Data: In case of an
incident, encryption is an additional line of protection against
any compromise. Any software we choose to use must have the
flexibility to protect data cloud, on-premises hybrid, or multi-
cloud environments. Find a tool with volume, file, and
application encryption features that meet our company's
regulations for compliance. This could require tokenization (data
concealing) or advanced key management of security keys.
 Optimization of Data Security and Risk Analysis: An
application that will provide contextual insights through the
combination of security data with advanced analytics will allow

148
Relational Database Management System
users to perform optimizing, risk assessment, and reporting in a
breeze. Select a tool that is able to keep and combine large
amounts of recent and historical data about the security and state
of your databases. Also, choose a solution that provides data
exploration, auditing, and reporting capabilities via an extensive
but user-friendly self-service dashboard.

3.12.5. Computer Based Controls

The different forms of countermeasure to threats on computer


systems range from physical controls to managerial procedures. In
spite of the range of computer-based controls that are preexisting, it
is worth noting that, usually, the security of a DBMS is merely as
good as that of the operating system, due to the close association
among them.

Most of the computer-based database security are listed below:

 Access authorization.
 Access controls.
 Views.
 Backup and recovery of data.
 Data integrity.
 Encryption of data.
 RAID technology

149
Relational Database Management System
3.12.6. Access Controls

The usual way of supplying access controls to a database system is


dependent on the granting and revoking of privileges within the
database. A privilege allows a user to create or access some database
object or to run some specific DBMS utilities. Privileges are granted
users to achieve the tasks required for those jobs.

The database provides various types of access controls:

 Discretionary Access Control (DAC)


 Mandatory Access Control (MAC)

Database Access Control Systems work on three sides: the user, the
administrator, and the infrastructure.

 The User: When an employee wishes to enter a restricted area,


they must provide their credentials. An unlock request gets made
at a card reader, which sends the information to an Access
Control Unit, subsequently authorizing the user and opening the
door.
 The Administrator: An access control system has a management
dashboard or portal on the administrative side. Office
administrators, IT managers, and security chiefs can use the
control portal to specify who has access to the premises and
under what conditions.

150
Relational Database Management System
 The System Infrastructure: An access control system‘s
infrastructure includes electric locks, card readers, door status
for traffic monitoring, and requests to exit devices, all of which
report to the control panel and the server.

1. DAC :
DAC is identity-based access control. DAC mechanisms will be
controlled by user identification such as username and password.
DAC is discretionary because the owners can transfer objects or any
authenticated information to other users. In simple words, the owner
can determine the access privileges.

Attributes of DAC –

 Users can transfer their object ownership to another user.


 The access type of other users can be determined by the user.
 Authorization failure can restrict the user access after several
failed attempts.
 Unauthorized users will be blind to object characteristics called
file size, directory path, and file name.
Examples- Permitting the Linux file operating system is an example
of DAC.

2. MAC :
The operating system in MAC will provide access to the user based
on their identities and data. For gaining access, the user has to submit
their personal information. It is very secure because the rules and

151
Relational Database Management System
restrictions are imposed by the admin and will be strictly followed.
MAC settings and policy management will be established in a secure
network and are limited to system administrators.

Attributes of MAC –

MAC policies can help to reduce system errors.

It has tighter security because only the administrator can access or


alter controls.

MAC has an enforced operating system that can label and


delineate incoming application data.

Maintenance will be difficult because only the administrator can


have access to the database.

Examples- Access level of windows for ordinary users, admins, and


guests are some of the examples of MAC.

Differences between DAC and MAC :

DAC MAC

DAC is easier to implement. MAC is difficult to implement.

152
Relational Database Management System

DAC is less secure to use. MAC is more secure to use.

In DAC, the owner can In MAC, the system only


determine the access and determines the access and the
privileges and can restrict the resources will be restricted based
resources based on the identity on the clearance of the subjects.
of the users.

DAC has extra labor-intensive MAC has no labor-intensive


properties. property.

Users will be provided access Users will be restricted based on


based on their identity and not their power and level of
using levels. hierarchy.

DAC has high flexibility with MAC is not flexible as it contains


no rules and regulations. lots of strict rules and regulations.

DAC has complete trust in MAC has trust only in


users. administrators.

Decisions will be based only on Decisions will be based on


user ID and ownership. objects and tasks, and they can
have their own ids.

153
Relational Database Management System

Information flow is impossible Information flow can be easily


to control. controlled.

DAC is supported by MAC is not supported by


commercial DBMSs. commercial DBMSs.

DAC can be applied in all MAC can be applied in the


domains. military, government, and
intelligence.

DAC is vulnerable to trojan MAC prevents virus flow from a


horses. higher level to a lower level.

Questions:

2 Mark Questions

1. Define relational database.


2. List down the objectives of relational database.
3. What are the tools used in RDBMS.
4. Define redundancy.
5. What is data anomaly?
6. List down the types of data anomaly.
7. Define functional dependency.
8. What is normalization?

154
Relational Database Management System
9. Define transaction processing.
10. What is database security?

5 Mark / 10 Mark Questions

11. What are the ACID Properties of RDBMS?


12. Explain i). Table ii) Row iii)Column iv) Cells
13. Explain briefly about Cardinality and Integrity.
14. Write a short notes on Anomalies in DBMS.
15. Explain about data redundancy in DBMS.
16. Explain Normalization and it‘s types in detail.
17. What is access control? Explain.
18. What are the common threats and challenges in database
management?

155
Relational Database Management System
UNIT IV

4.1 SQL

SQL is a short-form of the structured query language. This database


language is mainly designed for maintaining the data in relational
database management systems. It is a special tool used by data
professionals for handling structured data (data which is stored in the
form of tables). It is also designed for stream processing in RDSMS.

You can easily create and manipulate the database, access and
modify the table rows and columns, etc. This query language became
the standard of ANSI in the year of 1986 and ISO in the year of
1987.

If you want to get a job in the field of data science, then it is the most
important query language to learn. Big enterprises like Facebook,
Instagram, and LinkedIn, use SQL for storing the data in the back-
end.

4.1.1 Why SQL?

Nowadays, SQL is widely used in data science and analytics.


Following are the reasons which explain why it is widely used:

156
Relational Database Management System
 The basic use of SQL for data professionals and SQL
users is to insert, update, and delete the data from the
relational database.

 SQL allows the data professionals and users to retrieve


the data from the relational database management
systems.

 It also helps them to describe the structured data.

 It allows SQL users to create, drop, and manipulate the


database and its tables.

 It also helps in creating the view, stored procedure, and


functions in the relational database.

 It allows you to define the data and modify that stored


data in the relational database.

 It also allows SQL users to set the permissions or


constraints on table columns, views, and stored
procedures.

4.1.2 History of SQL

"A Relational Model of Data for Large Shared Data Banks" was a
paper which was published by the great computer scientist "E.F.
Codd" in 1970.

157
Relational Database Management System
The IBM researchers Raymond Boyce and Donald Chamberlin
originally developed the SEQUEL (Structured English Query
Language) after learning from the paper given by E.F. Codd. They
both developed the SQL at the San Jose Research laboratory of IBM
Corporation in 1970.

At the end of the 1970s, relational software Inc. developed their own
first SQL using the concepts of E.F. Codd, Raymond Boyce, and
Donald Chamberlin. This SQL was totally based on RDBMS.
Relational Software Inc., which is now known as Oracle
Corporation, introduced the Oracle V2 in June 1979, which is the
first implementation of SQL language. This Oracle V2 version
operates on VAX computers.

4.1.3 Process of SQL

When we are executing the command of SQL on any Relational


database management system, then the system automatically finds
the best routine to carry out our request, and the SQL engine
determines how to interpret that particular command.

Structured Query Language contains the following four components


in its process:

o Query Dispatcher
o Optimization Engines

158
Relational Database Management System
o Classic Query Engine
o SQL Query Engine, etc.

A classic query engine allows data professionals and users to


maintain non-SQL queries. The architecture of SQL is shown in the
following diagram:

159
Relational Database Management System
4.1.4 SQL vs No-SQL

SQL No-SQL

1. SQL is a relational database 1. While No-SQL is a non-


management system. relational or distributed database
management system.

2. The query language used in 2. The query language used in the


this database system is a No-SQL database systems is a non-
structured query language. declarative query language.

3. The schema of SQL 3. The schema of No-SQL


databases is predefined, fixed, databases is a dynamic schema for
and static. unstructured data.

4. These databases are vertically 4. These databases are horizontally


scalable. scalable.

5. The database type of SQL is 5. The database type of No-SQL is

160
Relational Database Management System
in the form of tables, i.e., in the in the form of documents, key-
form of rows and columns. value, and graphs.

6. It follows the ACID model. 6. It follows the BASE model.

7. Complex queries are easily 7. NoSQL databases cannot handle


managed in the SQL database. complex queries.

8. This database is not the best 8. While No-SQL database is a


choice for storing hierarchical perfect option for storing
data. hierarchical data.

9. All SQL databases require 9. Many No-SQL databases do not


object-relational mapping. require object-relational mapping.

10. Gauges, CircleCI, 10. Airbnb, Uber, and Kickstarter


Hootsuite, etc., are the top are the top enterprises that are
enterprises that are using this using this query language.
query language.

11. SQLite, Ms-SQL, Oracle, 11. Redis, MongoDB, Hbase,


PostgreSQL, and MySQL are BigTable, CouchDB, and
examples of SQL database Cassandra are examples of NoSQL
systems. database systems.

161
Relational Database Management System
4.1.5 Advantages of SQL

1. No programming needed

SQL does not require a large number of coding lines for managing
the database systems. We can easily access and maintain the database
by using simple SQL syntactical rules. These simple rules make the
SQL user-friendly.

2. High-Speed Query Processing

A large amount of data is accessed quickly and efficiently from the


database by using SQL queries. Insertion, deletion, and updation
operations on data are also performed in less time.

3. Standardized Language

SQL follows the long-established standards of ISO and ANSI, which


offer a uniform platform across the globe to all its users.

4. Portability

The structured query language can be easily used in desktop


computers, laptops, tablets, and even smartphones. It can also be
used with other applications according to the user's requirements.

162
Relational Database Management System
5. Interactive language

We can easily learn and understand the SQL language. We can also
use this language for communicating with the database because it is a
simple query language. This language is also used for receiving the
answers to complex queries in a few seconds.

6. More than one Data View

The SQL language also helps in making the multiple views of the
database structure for the different database users.

4.1.6 Disadvantages of SQL

With the advantages of SQL, it also has some disadvantages, which


are as follows:

1. Cost

The operation cost of some SQL versions is high. That's why some
programmers cannot use the Structured Query Language.

2. Interface is Complex

Another big disadvantage is that the interface of Structured query


language is difficult, which makes it difficult for SQL users to use
and manage it.

163
Relational Database Management System
3. Partial Database control

The business rules are hidden. So, the data professionals and users
who are using this query language cannot have full database control.

4.1.7 Most Important SQL Commands and Statements

1. Select Statement
2. Update Statement
3. Delete Statement
4. Create Table Statement
5. Alter Table Statement
6. Drop Table Statement
7. Create Database Statement
8. Drop Database Statement
9. Insert Into Statement
10. Truncate Table Statement
11. Describe Statement
12. Distinct Clause
13. Commit Statement
14. Rollback Statement
15. Create Index Statement
16. Drop Index Statement

164
Relational Database Management System
17. Use Statement

Let's discuss each statement in short one by one with syntax and
one example:

1. SELECT Statement

This SQL statement reads the data from the SQL database and shows
it as the output to the database user.

Syntax of SELECT Statement:

SELECT column_name1, column_name2, .…, column_nameN


[ FROM table_name ]
[ WHERE condition ]
[ ORDER BY order_column_name1 [ ASC | DESC ], .... ];

Example of SELECT Statement:

SELECT Emp_ID, First_Name, Last_Name, Salary, City


FROM Employee_details
WHERE Salary = 100000
ORDER BY Last_Name

This example shows the Emp_ID, First_Name, Last_Name,


Salary, and City of those employees from
the Employee_details table whose Salary is 100000. The output

165
Relational Database Management System
shows all the specified details according to the ascending
alphabetical order of Last_Name.

2. UPDATE Statement

This SQL statement changes or modifies the stored data in the SQL
database.

Syntax of UPDATE Statement:

UPDATE table_name
SET column_name1 = new_value_1, column_name2 = new_valu
e_2, ...., column_nameN = new_value_N
[ WHERE CONDITION ];

Example of UPDATE Statement:

UPDATE Employee_details
SET Salary = 100000
WHERE Emp_ID = 10;

This example changes the Salary of those employees of


the Employee_details table whose Emp_ID is 10 in the table.

3. DELETE Statement

This SQL statement deletes the stored data from the SQL database.

166
Relational Database Management System
Syntax of DELETE Statement:

DELETE FROM table_name


[ WHERE CONDITION ];

Example of DELETE Statement:

DELETE FROM Employee_details


WHERE First_Name = 'Sumit';

This example deletes the record of those employees from


the Employee_details table whose First_Name is Sumit in the
table.

4. CREATE TABLE Statement

This SQL statement creates the new table in the SQL database.

Syntax of CREATE TABLE Statement:

CREATE TABLE table_name


(
column_name1 data_type [column1 constraint(s)],
column_name2 data_type [column2 constraint(s)],
.....
.....,
column_nameN data_type [columnN constraint(s)],

167
Relational Database Management System
PRIMARY KEY(one or more col)
);

Example of CREATE TABLE Statement:

CREATE TABLE Employee_details(


Emp_Id NUMBER(4) NOT NULL,
First_name VARCHAR(30),
Last_name VARCHAR(30),
Salary Money,
City VARCHAR(30),
PRIMARY KEY (Emp_Id)
);

This example creates the table Employee_details with five columns


or fields in the SQL database. The fields in the table are Emp_Id,
First_Name, Last_Name, Salary, and City. The Emp_Id column in
the table acts as a primary key, which means that the Emp_Id
column cannot contain duplicate values and null values.

5. ALTER TABLE Statement

This SQL statement adds, deletes, and modifies the columns of the
table in the SQL database.

168
Relational Database Management System
Syntax of ALTER TABLE Statement:

1. ALTER TABLE table_name ADD column_name datatype[(


size)];

The above SQL alter statement adds the column with its datatype in
the existing database table.

ALTER TABLE table_name MODIFY column_name colu


mn_datatype[(size)];

The above 'SQL alter statement' renames the old column name to the
new column name of the existing database table.

ALTER TABLE table_name DROP COLUMN column_na


me;

The above SQL alter statement deletes the column of the existing
database table.

Example of ALTER TABLE Statement:

ALTER TABLE Employee_details


ADD Designation VARCHAR(18);

This example adds the new field whose name is Designation with
size 18 in the Employee_details table of the SQL database.

169
Relational Database Management System
6. DROP TABLE Statement

This SQL statement deletes or removes the table and the structure,
views, permissions, and triggers associated with that table.

Syntax of DROP TABLE Statement:

DROP TABLE [ IF EXISTS ]


table_name1, table_name2, ……, table_nameN;

The above syntax of the drop statement deletes specified tables


completely if they exist in the database.

Example of DROP TABLE Statement:

DROP TABLE Employee_details;

This example drops the Employee_details table if it exists in the


SQL database. This removes the complete information if available in
the table.

7. CREATE DATABASE Statement

This SQL statement creates the new database in the database


management system.

170
Relational Database Management System
Syntax of CREATE DATABASE Statement:

CREATE DATABASE database_name;

Example of CREATE DATABASE Statement:

CREATE DATABASE Company;

The above example creates the company database in the system.

8. DROP DATABASE Statement

This SQL statement deletes the existing database with all the data
tables and views from the database management system.

Syntax of DROP DATABASE Statement:

DROP DATABASE database_name;

Example of DROP DATABASE Statement:

DROP DATABASE Company;

The above example deletes the company database from the system.

171
Relational Database Management System
9. INSERT INTO Statement

This SQL statement inserts the data or records in the existing table of
the SQL database. This statement can easily insert single and
multiple records in a single query statement.

Syntax of insert a single record:

INSERT INTO table_name


(
column_name1,
column_name2, .…,
column_nameN
)
VALUES
(value_1,
value_2, ..…,
value_N
);

Example of insert a single record:

INSERT INTO Employee_details


(
Emp_ID,
First_name,

172
Relational Database Management System
Last_name,
Salary,
City
)
VALUES
(101,
Akhil,
Sharma,
40000,
Bangalore
);

This example inserts 101 in the first column, Akhil in the second
column, Sharma in the third column, 40000 in the fourth column,
and Bangalore in the last column of the table Employee_details.

Syntax of inserting a multiple records in a single query:

INSERT INTO table_name


( column_name1, column_name2, .…, column_nameN)
VALUES (value_1, value_2, ..…, value_N), (value_1, value_2, ..
…, value_N),….;

Example of inserting multiple records in a single query:

INSERT INTO Employee_details

173
Relational Database Management System
( Emp_ID, First_name, Last_name, Salary, City )
VALUES (101, Amit, Gupta, 50000, Mumbai), (101, John, Agg
arwal, 45000, Calcutta), (101, Sidhu, Arora, 55000, Mumbai);

This example inserts the records of three employees in


the Employee_details table in the single query statement.

10. TRUNCATE TABLE Statement

This SQL statement deletes all the stored records from the table of
the SQL database.

Syntax of TRUNCATE TABLE Statement:

TRUNCATE TABLE table_name;

Example of TRUNCATE TABLE Statement:

TRUNCATE TABLE Employee_details;

This example deletes the record of all employees from the


Employee_details table of the database.

11. DESCRIBE Statement

This SQL statement tells something about the specified table or view
in the query.

174
Relational Database Management System
Syntax of DESCRIBE Statement:

DESCRIBE table_name | view_name;

Example of DESCRIBE Statement:

DESCRIBE Employee_details;

This example explains the structure and other details about


the Employee_details table.

12. DISTINCT Clause

This SQL statement shows the distinct values from the specified
columns of the database table. This statement is used with
the SELECT keyword.

Syntax of DISTINCT Clause:

SELECT DISTINCT column_name1, column_name2, ...


FROM table_name;

Example of DISTINCT Clause:

SELECT DISTINCT City, Salary


FROM Employee_details;

175
Relational Database Management System
This example shows the distinct values of
the City and Salary column from the Employee_details table.

13. COMMIT Statement

This SQL statement saves the changes permanently, which are done
in the transaction of the SQL database.

Syntax of COMMIT Statement:

COMMIT

Example of COMMIT Statement:

DELETE FROM Employee_details


WHERE salary = 30000;
COMMIT;

This example deletes the records of those employees


whose Salary is 30000 and then saves the changes permanently in
the database.

14. ROLLBACK Statement

This SQL statement undo the transactions and operations which are
not yet saved to the SQL database.

176
Relational Database Management System
Syntax of ROLLBACK Statement:

ROLLBACK

Example of ROLLBACK Statement:

DELETE FROM Employee_details


WHERE City = Mumbai;
ROLLBACK;

This example deletes the records of those employees


whose City is Mumbai and then undo the changes in the database.

15. CREATE INDEX Statement

This SQL statement creates the new index in the SQL database table.

Syntax of CREATE INDEX Statement:

CREATE INDEX index_name


ON table_name ( column_name1, column_name2, …, colum
n_nameN );

Example of CREATE INDEX Statement:

CREATE INDEX idx_First_Name


ON employee_details (First_Name);

177
Relational Database Management System
This example creates an index idx_First_Name on
the First_Name column of the Employee_details table.

16. DROP INDEX Statement

This SQL statement deletes the existing index of the SQL database
table.

Syntax of DROP INDEX Statement:

DROP INDEX index_name;

Example of DROP INDEX Statement:

DROP INDEX idx_First_Name;

This example deletes the index idx_First_Name from the SQL


database.

17. USE Statement

This SQL statement selects the existing SQL database. Before


performing the operations on the database table, you have to select
the database from the multiple existing databases.

Syntax of USE Statement:

USE database_name;

178
Relational Database Management System
Example of USE DATABASE Statement:

USE Company;

This example uses the company database.

4.2 SQL Data Types

Data types are used to represent the nature of the data that can be
stored in the database table. For example, in a particular column of a
table, if we want to store a string type of data then we will have to
declare a string data type of this column.

Data types mainly classified into three categories for every database.

o String Data types


o Numeric Data types
o Date and time Data types

Data Types in MySQL, SQL Server and Oracle Databases

MySQL Data Types

A list of data types used in MySQL database. This is based on


MySQL 8.0.

179
Relational Database Management System
4.2.1 MySQL String Data Types

CHAR(Size) It is used to specify a fixed length string that


can contain numbers, letters, and special
characters. Its size can be 0 to 255 characters.
Default is 1.

VARCHAR(Size) It is used to specify a variable length string


that can contain numbers, letters, and special
characters. Its size can be from 0 to 65535
characters.

BINARY(Size) It is equal to CHAR() but stores binary byte


strings. Its size parameter specifies the
column length in the bytes. Default is 1.

VARBINARY(Size) It is equal to VARCHAR() but stores binary


byte strings. Its size parameter specifies the
maximum column length in bytes.

TEXT(Size) It holds a string that can contain a maximum


length of 255 characters.

TINYTEXT It holds a string with a maximum length of


255 characters.

180
Relational Database Management System
MEDIUMTEXT It holds a string with a maximum length of
16,777,215.

LONGTEXT It holds a string with a maximum length of


4,294,967,295 characters.

ENUM(val1, val2, It is used when a string object having only


val3,...) one value, chosen from a list of possible
values. It contains 65535 values in an ENUM
list. If you insert a value that is not in the list,
a blank value will be inserted.

SET( It is used to specify a string that can have 0 or


val1,val2,val3,....) more values, chosen from a list of possible
values. You can list up to 64 values at one
time in a SET list.

BLOB(size) It is used for BLOBs (Binary Large Objects).


It can hold up to 65,535 bytes.

4.2.2 MySQL Numeric Data Types

BIT(Size) It is used for a bit-value type. The number of bits


per value is specified in size. Its size can be 1 to
64. The default value is 1.

181
Relational Database Management System
INT(size) It is used for the integer value. Its signed range
varies from -2147483648 to 2147483647 and
unsigned range varies from 0 to 4294967295.
The size parameter specifies the max display
width that is 255.

INTEGER(size) It is equal to INT(size).

FLOAT(size, d) It is used to specify a floating point number. Its


size parameter specifies the total number of
digits. The number of digits after the decimal
point is specified by d parameter.

FLOAT(p) It is used to specify a floating point number.


MySQL used p parameter to determine whether
to use FLOAT or DOUBLE. If p is between 0
to24, the data type becomes FLOAT (). If p is
from 25 to 53, the data type becomes
DOUBLE().

DOUBLE(size, It is a normal size floating point number. Its size


d) parameter specifies the total number of digits.
The number of digits after the decimal is
specified by d parameter.

182
Relational Database Management System
DECIMAL(size, It is used to specify a fixed point number. Its size
d) parameter specifies the total number of digits.
The number of digits after the decimal parameter
is specified by d parameter. The maximum value
for the size is 65, and the default value is 10. The
maximum value for d is 30, and the default value
is 0.

DEC(size, d) It is equal to DECIMAL(size, d).

BOOL It is used to specify Boolean values true and


false. Zero is considered as false, and nonzero
values are considered as true.

4.2.3 MySQL Date and Time Data Types

DATE It is used to specify date format YYYY-MM-


DD. Its supported range is from '1000-01-01'
to '9999-12-31'.

DATETIME(fsp) It is used to specify date and time


combination. Its format is YYYY-MM-DD
hh:mm:ss. Its supported range is from '1000-
01-01 00:00:00' to 9999-12-31 23:59:59'.

183
Relational Database Management System
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is
stored as the number of seconds since the
Unix epoch('1970-01-01 00:00:00' UTC). Its
format is YYYY-MM-DD hh:mm:ss. Its
supported range is from '1970-01-01 00:00:01'
UTC to '2038-01-09 03:14:07' UTC.

TIME(fsp) It is used to specify the time format. Its format


is hh:mm:ss. Its supported range is from '-
838:59:59' to '838:59:59'

YEAR It is used to specify a year in four-digit format.


Values allowed in four digit format from 1901
to 2155, and 0000.

4.2.4 SQL Server String Data Type

char(n) It is a fixed width character string data type. Its size


can be up to 8000 characters.

varchar(n) It is a variable width character string data type. Its


size can be up to 8000 characters.

varchar(max) It is a variable width character string data types. Its


size can be up to 1,073,741,824 characters.

184
Relational Database Management System
text It is a variable width character string data type. Its
size can be up to 2GB of text data.

nchar It is a fixed width Unicode string data type. Its size


can be up to 4000 characters.

nvarchar It is a variable width Unicode string data type. Its


size can be up to 4000 characters.

ntext It is a variable width Unicode string data type. Its


size can be up to 2GB of text data.

binary(n) It is a fixed width Binary string data type. Its size


can be up to 8000 bytes.

varbinary It is a variable width Binary string data type. Its size


can be up to 8000 bytes.

image It is also a variable width Binary string data type. Its


size can be up to 2GB.

4.2.5 SQL Server Numeric Data Types

bit It is an integer that can be 0, 1 or null.

tinyint It allows whole numbers from 0 to 255.

185
Relational Database Management System
Smallint It allows whole numbers between -32,768 and 32,767.

Int It allows whole numbers between -2,147,483,648 and


2,147,483,647.

bigint It allows whole numbers between -


9,223,372,036,854,775,808 and
9,223,372,036,854,775,807.

float(n) It is used to specify floating precision number data from -


1.79E+308 to 1.79E+308. The n parameter indicates
whether the field should hold the 4 or 8 bytes. Default
value of n is 53.

real It is a floating precision number data from -3.40E+38 to


3.40E+38.

money It is used to specify monetary data from -


922,337,233,685,477.5808 to 922,337,203,685,477.5807.

4.2.6 SQL Server Date and Time Data Types

datetime It is used to specify date and time combination. It


supports range from January 1, 1753, to December 31,
9999 with an accuracy of 3.33 milliseconds.

186
Relational Database Management System
datetime2 It is used to specify date and time combination. It
supports range from January 1, 0001 to December 31,
9999 with an accuracy of 100 nanoseconds

date It is used to store date only. It supports range from


January 1, 0001 to December 31, 9999

time It stores time only to an accuracy of 100 nanoseconds

timestamp It stores a unique number when a new row gets created


or modified. The time stamp value is based upon an
internal clock and does not correspond to real time.
Each table may contain only one-time stamp variable.

4.2.7 SQL Server Other Data Types

Sql_variant It is used for various data types except for text,


timestamp, and ntext. It stores up to 8000 bytes
of data.

XML It stores XML formatted data. Maximum 2GB.

cursor It stores a reference to a cursor used for database


operations.

table It stores result set for later processing.

187
Relational Database Management System
uniqueidentifier It stores GUID (Globally unique identifier).

4.3 DDL Commands in SQL

DDL is an abbreviation of Data Definition Language.

The DDL Commands in Structured Query Language are used to


create and modify the schema of the database and its objects. The
syntax of DDL commands is predefined for describing the data. The
commands of Data Definition Language deal with how the data
should exist in the database.

Following are the five DDL commands in SQL:

1. CREATE Command
2. DROP Command
3. ALTER Command
4. TRUNCATE Command
5. RENAME Command

4.3.1 CREATE Command

CREATE is a DDL command used to create databases, tables,


triggers and other database objects.

188
Relational Database Management System
Examples of CREATE Command in SQL

Example 1: This example describes how to create a new database


using the CREATE DDL command.

Syntax to Create a Database:

1. CREATE Database Database_Name;

Suppose, you want to create a Books database in the SQL database.


To do this, you have to write the following DDL Command:

Create Database Books;

Example 2: This example describes how to create a new table


using the CREATE DDL command.

Syntax to create a new table:

CREATE TABLE table_name


(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
);

189
Relational Database Management System
Suppose, you want to create a Student table with five columns in the
SQL database. To do this, you have to write the following DDL
command:

1. CREATE TABLE Student


2. (
3. Roll_No. Int ,
4. First_Name Varchar (20) ,
5. Last_Name Varchar (20) ,
6. Age Int ,
7. Marks Int ,
8. ) ;

Example 3: This example describes how to create a new index


using the CREATE DDL command.

Syntax to Create a new index:

1. CREATE INDEX Name_of_Index ON Name_of_Table (col


umn_name_1 , column_name_2 , … . , column_name_N);

Let's take the Student table:

Stu_Id Name Marks City State

100 Abhay 80 Noida U.P

190
Relational Database Management System
101 Sushil 75 Jaipur Rajasthan

102 Ankit 90 Gurgaon Haryana

103 Yogesh 93 Lucknow U.P

Suppose, you want to create an index on the combination of


the City and State field of the Student table. For this, we have to use
the following DDL command:

1. CREATE INDEX index_city_State ON Employee (Emp_Cit


y, Emp_State);

Example 4: This example describes how to create a trigger in the


SQL database using the DDL CREATE command.

Syntax to create a trigger:

1. CREATE TRIGGER [trigger_name]


2. [ BEFORE | AFTER ]
3. { INSERT | UPDATE | DELETE }
4. ON [table_name] ;

191
Relational Database Management System
4.3.2 DROP Command

DROP is a DDL command used to delete/remove the database


objects from the SQL database. We can easily remove the entire
table, view, or index from the database using this DDL command.

Example 1: This example describes how to remove a database


from the SQL database.

Syntax to remove a database:

1. DROP DATABASE Database_Name;

Suppose, you want to delete the Books database from the SQL
database. To do this, you have to write the following DDL command:

1. DROP DATABASE Books;

Example 2: This example describes how to remove the existing


table from the SQL database.

Syntax to remove a table:

1. DROP TABLE Table_Name;

Suppose, you want to delete the Student table from the SQL
database. To do this, you have to write the following DDL command:

192
Relational Database Management System
1. DROP TABLE Student;

Example 3: This example describes how to remove the existing


index from the SQL database.

Syntax to remove an index:

1. DROP INDEX Index_Name;

Suppose, you want to delete the index_city from the SQL database.
To do this, you have to write the following DDL command:

1. DROP INDEX Index_city;

4.3.3 ALTER Command

ALTER is a DDL command which changes or modifies the existing


structure of the database, and it also changes the schema of database
objects.

We can also add and drop constraints of the table using the ALTER
command.

Examples of ALTER Command in SQL

Example 1: This example shows how to add a new field to the


existing table.

193
Relational Database Management System
Syntax to add a newfield in the table:

1. ALTER TABLE name_of_table ADD column_name column


_definition;

Suppose, you want to add the 'Father's_Name' column in the existing


Student table. To do this, you have to write the following DDL
command:

1. ALTER TABLE Student ADD Father's_Name Varchar(60);

Example 2: This example describes how to remove the existing


column from the table.

Syntax to remove a column from the table:

1. ALTER TABLE name_of_table DROP Column_Name_1 ,


column_Name_2 , ….., column_Name_N;

Suppose, you want to remove the Age and Marks column from the
existing Student table. To do this, you have to write the following
DDL command:

1. ALTER TABLE StudentDROP Age, Marks;

194
Relational Database Management System
Example 3: This example describes how to modify the existing
column of the existing table.

Syntax to modify the column of the table:

1. ALTER TABLE table_name MODIFY ( column_name colu


mn_datatype(size));

Suppose, you want to change the character size of the


Last_Namefield of the Student table. To do this, you have to write
the following DDL command:

1. ALTER TABLE table_name MODIFY ( Last_Name varch


ar(25));

4.3.4 TRUNCATE Command

TRUNCATE is another DDL command which deletes or removes all


the records from the table.

This command also removes the space allocated for storing the table
records.

Syntax of TRUNCATE command

1. TRUNCATE TABLE Table_Name;

195
Relational Database Management System
Example

Suppose, you want to delete the record of the Student table. To do


this, you have to write the following TRUNCATE DDL command:

1. TRUNCATE TABLE Student;

The above query successfully removed all the records from the
student table. Let's verify it by using the following SELECT
statement:

1. SELECT * FROM Student;

4.3.5 RENAME Command

RENAME is a DDL command which is used to change the name of


the database table.

Syntax of RENAME command

1. RENAME TABLE Old_Table_Name TO New_Table_Name


;

Example

1. RENAME TABLE Student TO Student_Details ;

196
Relational Database Management System
This query changes the name of the table from Student to
Student_Details.

4.4 DML Commands in SQL

DML is an abbreviation of Data Manipulation Language.

The DML commands in Structured Query Language change the data


present in the SQL database. We can easily access, store, modify,
update and delete the existing records from the database using DML
commands.

Following are the four main DML commands in SQL:

1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command

4.4.1 SELECT Command

SELECT is the most important data manipulation command in


Structured Query Language. The SELECT command shows the
records of the specified table. It also shows the particular record of a
particular column by using the WHERE clause.

197
Relational Database Management System
Syntax of SELECT DML command

1. SELECT column_Name_1, column_Name_2, ….., column_


Name_N FROM Name_of_table;

Here, column_Name_1, column_Name_2, …..,


column_Name_N are the names of those columns whose data we
want to retrieve from the table.

If we want to retrieve the data from all the columns of the table, we
have to use the following SELECT command:

1. SELECT * FROM table_name;

Examples of SELECT Command

Example 1: This example shows all the values of every column


from the table.

1. SELECT * FROM Student;

This SQL statement displays the following values of the student


table:

Student_ID Student_Name Student_Marks

198
Relational Database Management System
BCA1001 Abhay 85

BCA1002 Anuj 75

BCA1003 Bheem 60

BCA1004 Ram 79

BCA1005 Sumit 80

Example 2: This example shows all the values of a specific


column from the table.

1. SELECT Emp_Id, Emp_Salary FROM Employee;

This SELECT statement displays all the values


of Emp_Salary and Emp_Id column of Employee table:

Emp_Id Emp_Salary

201 25000

202 45000

203 30000

204 29000

199
Relational Database Management System
205 40000

Example 3: This example describes how to use the WHERE


clause with the SELECT DML command.

Let's take the following Student table:

Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1002 Ankit 75

BCA1003 Bheem 80

BCA1004 Ram 79

BCA1005 Sumit 80

If you want to access all the records of those students whose marks is
80 from the above table, then you have to write the following DML
command in SQL:

1. SELECT * FROM Student WHERE Stu_Marks = 80;

The above SQL query shows the following table in result:

200
Relational Database Management System

Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1003 Bheem 80

BCA1005 Sumit 80

4.4.2 INSERT Command

INSERT is another most important data manipulation command in


Structured Query Language, which allows users to insert data in
database tables.

Syntax of INSERT Command

1. INSERT INTO TABLE_NAME ( column_Name1 , column


_Name2 , column_Name3 , .... column_NameN ) VALUES (
value_1, value_2, value_3, .... value_N ) ;

Examples of INSERT Command

Example 1: This example describes how to insert the record in


the database table.

Let's take the following student table, which consists of only 2


records of the student.

201
Relational Database Management System

Stu_Id Stu_Name Stu_Marks Stu_Age

101 Ramesh 92 20

201 Jatin 83 19

Suppose, you want to insert a new record into the student table. For
this, you have to write the following DML INSERT command:

1. INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu


_Age) VALUES (104, Anmol, 89, 19);

4.4.3 UPDATE Command

UPDATE is another most important data manipulation command in


Structured Query Language, which allows users to update or modify
the existing data in database tables.

Syntax of UPDATE Command

1. UPDATE Table_name SET [column_name1= value_1, …..,


column_nameN = value_N] WHERE CONDITION;

Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and
'Table_name' is the name of the table whose values you want to
update.

202
Relational Database Management System
Examples of the UPDATE command

Example 1: This example describes how to update the value of a


single field.

Let's take a Product table consisting of the following records:

Product_I Product_Na Product_Pri Product_Quanti


d me ce ty

P101 Chips 20 20

P102 Chocolates 60 40

P103 Maggi 75 5

P201 Biscuits 80 20

P203 Namkeen 40 50

Suppose, you want to update the Product_Price of the product whose


Product_Id is P102. To do this, you have to write the following DML
UPDATE command:

1. UPDATE Product SET Product_Price = 80 WHERE Produc


t_Id = 'P102' ;

203
Relational Database Management System
Example 2: This example describes how to update the value of
multiple fields of the database table.

Let's take a Student table consisting of the following records:

Stu_Id Stu_Name Stu_Marks Stu_Age

101 Ramesh 92 20

201 Jatin 83 19

202 Anuj 85 19

203 Monty 95 21

102 Saket 65 21

103 Sumit 78 19

104 Ashish 98 20

Suppose, you want to update Stu_Marks and Stu_Age of that student


whose Stu_Id is 103 and 202. To do this, you have to write the
following DML Update command:

1. UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WH


ERE Stu_Id = 103 AND Stu_Id = 202;

204
Relational Database Management System
4.4.4 DELETE Command

DELETE is a DML command which allows SQL users to remove


single or multiple existing records from the database tables.

This command of Data Manipulation Language does not delete the


stored data permanently from the database. We use the WHERE
clause with the DELETE command to select specific rows from the
table.

Syntax of DELETE Command

1. DELETE FROM Table_Name WHERE condition;

Examples of DELETE Command

Example 1: This example describes how to delete a single record


from the table.

Let's take a Product table consisting of the following records:

Product_I Product_Na Product_Pri Product_Quanti


d me ce ty

P101 Chips 20 20

205
Relational Database Management System
P102 Chocolates 60 40

P103 Maggi 75 5

P201 Biscuits 80 20

P203 Namkeen 40 50

Suppose, you want to delete that product from the Product table
whose Product_Id is P203. To do this, you have to write the
following DML DELETE command:

1. DELETE FROM Product WHERE Product_Id = 'P202' ;

Example 2: This example describes how to delete the multiple


records or rows from the database table.

Let's take a Student table consisting of the following records:

Stu_Id Stu_Name Stu_Marks Stu_Age

101 Ramesh 92 20

201 Jatin 83 19

202 Anuj 85 19

206
Relational Database Management System
203 Monty 95 21

102 Saket 65 21

103 Sumit 78 19

104 Ashish 98 20

Suppose, you want to delete the record of those students whose


Marks is greater than 70. To do this, you have to write the following
DML Update command:

1. DELETE FROM Student WHERE Stu_Marks > 70 ;

4.5 SQL Join


SQL Join statement is used to combine data or rows from two or
more tables based on a common field between them. Different types
of Joins are as follows:
 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL JOIN
 NATURAL JOIN
Consider the two tables below as follows:

207
Relational Database Management System
Student

StudentCourse

The simplest Join is INNER JOIN.

208
Relational Database Management System
4.5.1. INNER JOIN
The INNER JOIN keyword selects all rows from both the tables as
long as the condition is satisfied. This keyword will create the result-
set by combining all rows from both the tables where the condition
satisfies i.e value of the common field will be the same.

Syntax:
SELECT table1.column1,table1.column2,table2.column1,....

FROM table1

INNER JOIN table2

ON table1.matching_column = table2.matching_column;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.
Note: We can also write JOIN instead of INNER JOIN. JOIN is same
as INNER JOIN.

209
Relational Database Management System
Example Queries(INNER JOIN)
This query will show the names and age of students enrolled in
different courses.

SELECT StudentCourse.COURSE_ID, Student.NAME,


Student.AGE FROM Student

INNER JOIN StudentCourse

ON Student.ROLL_NO = StudentCourse.ROLL_NO;

Output:

4.5.2 LEFT JOIN


This join returns all the rows of the table on the left side of the join
and matches rows for the table on the right side of the join. For the
rows for which there is no matching row on the right side, the result-
set will contain null. LEFT JOIN is also known as LEFT OUTER
JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....

210
Relational Database Management System
FROM table1

LEFT JOIN table2

ON table1.matching_column = table2.matching_column;

table1: First table.

table2: Second table

matching_column: Column common to both the tables.

Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN,
both are the same.

Example Queries(LEFT JOIN):


SELECT Student.NAME,StudentCourse.COURSE_ID

FROM Student

LEFT JOIN StudentCourse

ON StudentCourse.ROLL_NO = Student.ROLL_NO;

211
Relational Database Management System
Output:

4.5.3 RIGHT JOIN


RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows
of the table on the right side of the join and matching rows for the
table on the left side of the join. For the rows for which there is no
matching row on the left side, the result-set will contain null. RIGHT
JOIN is also known as RIGHT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....

FROM table1

RIGHT JOIN table2

212
Relational Database Management System
ON table1.matching_column = table2.matching_column;

table1: First table.

table2: Second table

matching_column: Column common to both the tables.

Note: We can also use RIGHT OUTER JOIN instead of RIGHT


JOIN, both are the same.

Example Queries(RIGHT JOIN):


SELECT Student.NAME,StudentCourse.COURSE_ID

FROM Student

RIGHT JOIN StudentCourse

ON StudentCourse.ROLL_NO = Student.ROLL_NO;

213
Relational Database Management System
Output:

4.5.4 FULL JOIN


FULL JOIN creates the result-set by combining results of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from
both tables. For the rows for which there is no matching, the result-
set will contain NULL values.

214
Relational Database Management System

Syntax:
SELECT table1.column1,table1.column2,table2.column1,....

FROM table1

FULL JOIN table2

ON table1.matching_column = table2.matching_column;

table1: First table.

table2: Second table

matching_column: Column common to both the tables.

Example Queries(FULL JOIN):


SELECT Student.NAME,StudentCourse.COURSE_ID

FROM Student

FULL JOIN StudentCourse

ON StudentCourse.ROLL_NO = Student.ROLL_NO;

215
Relational Database Management System
Output:

NAME COURSE_ID

HARSH 1

PRATIK 2

RIYANKA 2

DEEP 3

SAPTARHI 1

DHANRAJ NULL

ROHIT NULL

NIRAJ NULL

NULL 4

216
Relational Database Management System
NAME COURSE_ID

NULL 5

NULL 4

4.5.5 Natural join (⋈)

Natural join can join tables based on the common columns in the
tables being joined. A natural join returns all rows by matching
values in common columns having same name and data type of
columns and that column should be present in both tables.

Both table must have at list one common column with same column
name and same data type.

The two table are joined using Cross join.

DBMS will look for a common column with same name and data
type Tuples having exactly same values in common columns are kept
in result.

217
Relational Database Management System
Example:

Employee

Emp_id Emp_name Dept_id

1 Ram 10

2 Jon 30

3 Bob 50

Department

Dept_id Dept_name

10 IT

30 HR

40 TIS
Query: Find all Employees and their respective departments.

Solution: (Employee) ⋈ (Department)

Emp_id Emp_name Dept_idDept_idDept_name

1 Ram 10 10 IT

2 Jon 30 30 HR

218
Relational Database Management System
Emp_id Emp_name Dept_idDept_idDept_name

1 Ram 10 10 IT

Employee data Department data

4.6 SQL Set Operation

The SQL Set operation is used to combine the two or more SQL
SELECT statements.

4.6.1Types of Set Operation

1. Union
2. UnionAll
3. Intersect
4. Minus

219
Relational Database Management System

1. Union

o The SQL Union operation is used to combine the result of


two or more SQL SELECT queries.
o In the union operation, all the number of datatype and
columns must be same in both the tables on which UNION
operation is being applied.
o The union operation eliminates the duplicate rows from its
resultset.

Syntax

1. SELECT column_name FROM table1

220
Relational Database Management System
2. UNION
3. SELECT column_name FROM table2;

Example:

The First table

ID NAME

1 Jack

2 Harry

3 Jackson

The Second table

ID NAME

3 Jackson

4 Stephan

5 David

Union SQL query will be:

221
Relational Database Management System
1. SELECT * FROM First
2. UNION
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson

4 Stephan

5 David

2. Union All

Union All operation is equal to the Union operation. It returns the set
without removing duplication and sorting the data.

Syntax:

1. SELECT column_name FROM table1


2. UNION ALL

222
Relational Database Management System
3. SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;

The result set table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson

3 Jackson

4 Stephan

5 David

223
Relational Database Management System
3. Intersect

o It is used to combine two SELECT statements. The Intersect


operation returns the common rows from both the SELECT
statements.
o In the Intersect operation, the number of datatype and
columns must be the same.
o It has no duplicates and it arranges the data in ascending
order by default.

Syntax

1. SELECT column_name FROM table1


2. INTERSECT
3. SELECT column_name FROM table2;

Example:

Using the above First and Second table.

Intersect query will be:

1. SELECT * FROM First


2. INTERSECT
3. SELECT * FROM Second;

The result set table will look like:

224
Relational Database Management System

ID NAME

3 Jackson

4. Minus

o It combines the result of two SELECT statements. Minus


operator is used to display the rows which are present in the
first query but absent in the second query.
o It has no duplicates and data arranged in ascending order by
default.

Syntax:

1. SELECT column_name FROM table1


2. MINUS
3. SELECT column_name FROM table2;

Example

Using the above First and Second table.

Minus query will be:

1. SELECT * FROM First


2. MINUS

225
Relational Database Management System
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

4.7 SQL - Sub Queries

A Subquery or Inner query or a Nested query is a query within


another SQL query and embedded within clauses, most commonly in
the WHERE clause. It is used to return data from a table, and this
data will be used in the main query as a condition to further restrict
the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and


DELETE statements along with the operators like =, <, >, >=, <=,
IN, BETWEEN, etc.

There are a few rules that subqueries must follow −

 Subqueries must be enclosed within parentheses.

226
Relational Database Management System
 A subquery can have only one column in the SELECT clause,
unless multiple columns are in the main query for the
subquery to compare its selected columns.
 An ORDER BY command cannot be used in a subquery,
although the main query can use an ORDER BY. The
GROUP BY command can be used to perform the same
function as the ORDER BY in a subquery.
 Subqueries that return more than one row can only be used
with multiple value operators such as the IN operator.
 The SELECT list cannot include any references to values that
evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
 A subquery cannot be immediately enclosed in a set function.
 The BETWEEN operator cannot be used with a subquery.
However, the BETWEEN operator can be used within the
subquery.

4.7.1. Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement.


The basic syntax is as follows −

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]

227
Relational Database Management System
FROM table1 [, table2 ]
[WHERE])

Example

Consider the CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Now, let us check the following subquery with a SELECT statement.

SQL> SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;

This would produce the following result.

228
Relational Database Management System
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+---------+----------+

4.7.2 Subqueries with the INSERT Statement

Subqueries also can be used with INSERT statements. The INSERT


statement uses the data returned from the subquery to insert into
another table. The selected data in the subquery can be modified with
any of the character, date or number functions.

The basic syntax is as follows.

INSERT INTO table_name [ (column1 [, column2 ]) ]


SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]

229
Relational Database Management System
Example

Consider a table CUSTOMERS_BKP with similar structure as


CUSTOMERS table. Now to copy the complete CUSTOMERS table
into the CUSTOMERS_BKP table, you can use the following syntax.

SQL> INSERT INTO CUSTOMERS_BKP


SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS) ;

4.7.3 Subqueries with the UPDATE Statement

The subquery can be used in conjunction with the UPDATE


statement. Either single or multiple columns in a table can be
updated when using a subquery with the UPDATE statement.

The basic syntax is as follows.

UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]

230
Relational Database Management System
Example

Assuming, we have CUSTOMERS_BKP table available which is


backup of CUSTOMERS table. The following example updates
SALARY by 0.25 times in the CUSTOMERS table for all the
customers whose AGE is greater than or equal to 27.

SQL> UPDATE CUSTOMERS


SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );

This would impact two rows and finally CUSTOMERS table would
have the following records.

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 125.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 2125.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

231
Relational Database Management System
4.7.4 Subqueries with the DELETE Statement

The subquery can be used in conjunction with the DELETE


statement like with any other statements mentioned above.

The basic syntax is as follows.

DELETE FROM TABLE_NAME


[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]

Example

Assuming, we have a CUSTOMERS_BKP table available which is a


backup of the CUSTOMERS table. The following example deletes
the records from the CUSTOMERS table for all the customers whose
AGE is greater than or equal to 27.

SQL> DELETE FROM CUSTOMERS


WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );

This would impact two rows and finally the CUSTOMERS table
would have the following records.

232
Relational Database Management System
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+---------+----------+

Questions

2 Mark Questions
1. Define SQL.
2. What is datatype?
3. List down few SQL datatypes.
4. List down DDL Commands.
5. Write a note on joins.
6. What are set operations?
7. Define aggregate functions.
8. What DML command in SQL?
9. Distinguish the difference between truncate, drop and
delete.
10. Write a brief note on subquery.

233
Relational Database Management System

5 Mark / 10 Mark Questions


11. Explain in detail about SQL datatypes with example.
12. Explain DDL Command with suitable example.
13. Explain DML command with suitable example.
14. List down Aggregate functions. Explain any 5 built in
functions.
15. Explain in detail about Join and its type in SQL
16. Explain in detail about set operations.
17. What is subquery? Explain in detail about any 5
subqueries.

234
Relational Database Management System

UNIT V
5.1. PL/SQL Introduction
The PL/SQL programming language was developed by Oracle
Corporation in the late 1980s as procedural extension language for
SQL and the Oracle relational database. Following are certain
notable facts about PL/SQL −

 PL/SQL is a completely portable, high-performance transaction-


processing language.
 PL/SQL provides a built-in, interpreted and OS independent
programming environment.
 PL/SQL can also directly be called from the command-line
SQL*Plus interface.
 Direct call can also be made from external programming
language calls to database.
 PL/SQL's general syntax is based on that of ADA and Pascal
programming language.

Apart from Oracle, PL/SQL is available in TimesTen in-memory


database and IBM DB2.

5.1.1 Features and Advantages PL/SQL


PL/SQL has the following features −

235
Relational Database Management System
 PL/SQL is tightly integrated with SQL.
 It offers extensive error checking.
 It offers numerous data types.
 It offers a variety of programming structures.
 It supports structured programming through functions and
procedures.
 It supports object-oriented programming.
 It supports the development of web applications and server
pages.

Advantages of PL/SQL
PL/SQL has the following advantages −
 SQL is the standard database language and PL/SQL is strongly
integrated with SQL. PL/SQL supports both static and dynamic
SQL. Static SQL supports DML operations and transaction
control from PL/SQL block. In Dynamic SQL, SQL allows
embedding DDL statements in PL/SQL blocks.
 PL/SQL allows sending an entire block of statements to the
database at one time. This reduces network traffic and provides
high performance for the applications.
 PL/SQL gives high productivity to programmers as it can query,
transform, and update data in a database.

236
Relational Database Management System
 PL/SQL saves time on design and debugging by strong features,
such as exception handling, encapsulation, data hiding, and
object-oriented data types.
 Applications written in PL/SQL are fully portable.
 PL/SQL provides high security level.
 PL/SQL provides access to predefined SQL packages.
 PL/SQL provides support for Object-Oriented Programming.
 PL/SQL provides support for developing Web Applications and
Server Pages.

5.2. Structure of PL/SQL


PL/SQL is a 4GL (fourth generation) programming language. It
offers all fea-tures of advanced programming language such as
portability, security, data encapsulation, information hiding,
etc.APL/SQL program may consist of more than one SQL statements,
while execution of a PL/SQL program makesonly one call to Oracle
engine, thus it helps in reducing the database over-heads. With
PL/SQL, one can use the SQL statements together with the control
structures (like if ... then) for data manipulation. Besides this, user
can define his/her own error messages to display. Thus we can say
that PL/SQL combines the data manipulation power of SQL with
data processing power of procedural language.

237
Relational Database Management System
PL/SQL is a block structured language. This means a PL/SQL
program is made up of blocks, where block is a smallest piece of
PL/SQL code having logically related statements and declarations. A
block consists of three section snamely: Declare, Begin and
Exception followed by an End statement. We will see the different
sections of PL/SQL block.

Declare Section
Declare section declares the variables, constants, processes, functions,
etc., to be used in the other parts of program. It is an optional section.

Begin Section
It is the executable section. It consists of a set of SQL and
PL/SQL statements, which is executed when PL/SQL block runs.
It is a compulsory section.

Exception Section
This section handles the errors, which occurs during execution of
the PL/SQL block. This section allows the user to define his/her
own error messages. This section executes only when an error
occurs. It is an optional section.

238
Relational Database Management System

DECLARE
Declarationsofvariables,constantset
c.tobeuseinPL/SQL.
BEGIN
PL/SQLandSQLExecutablest
atements
EXCEPTION
PL/SQLcodetohandleerrorsd
uringexecutionperiod.
END;

Fig.5.1.APL/SQLblock
End Section
This section indicates the end of PL/SQL block. Every PL/SQL
program must consist of atleast one block, which may consist of any
number of nested sub-blocks. Figure 5.1 shows a typical PL/SQL
block.

5.3 PL/SQL Variables


A variable is a meaningful name which facilitates a programmer to
store data temporarily during the execution of code. It helps you to
manipulate data in PL/SQL programs. It is nothing except a name
given to a storage area. Each variable in the PL/SQL has a specific
data type which defines the size and layout of the variable's memory.

239
Relational Database Management System
A variable should not exceed 30 characters. Its letter optionally
followed by more letters, dollar signs, numerals, underscore etc.

Syntax for declaring variable:


Following is the syntax for declaring variable:
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT i
nitial_value]
Here, variable_name is a valid identifier in PL/SQL and datatype
must be valid PL/SQL data type. A data type with size, scale or
precision limit is called a constrained declaration. The constrained
declaration needs less memory than unconstrained declaration.

Example:
Radius Number := 5;
Date_of_birth date;

Declaration Restrictions:
In PL/SQL while declaring the variable some restrictions hold.
o Forward references are not allowed i.e. you must declare a
constant or variable before referencing it in another statement
even if it is a declarative statement.
val number := Total - 200;
Total number := 1000;

240
Relational Database Management System
The first declaration is illegal because the TOTAL variable
must be declared before using it in an assignment expression.
o Variables belonging to the same datatype cannot be declared
in the same statement.
N1, N2, N3 Number;
It is an illegal declaration.

Naming rules for PL/SQL variables


The variable in PL/SQL must follow some naming rules like other
programming languages.
o The variable_name should not exceed 30 characters.
o Variable name should not be the same as the table table's
column of that block.
o The name of the variable must begin with ASCII letter. The
PL/SQL is not case sensitive so it could be either lowercase
or uppercase. For example: v_data and V_DATA refer to the
same variables.
o You should make your variable easy to read and understand,
after the first character, it may be any number, underscore (_)
or dollar sign ($).
o NOT NULL is an optional specification on the variable.

Initializing Variables in PL/SQL

241
Relational Database Management System
Evertime you declare a variable, PL/SQL defines a default value
NULL to it. If you want to initialize a variable with other value than
NULL value, you can do so during the declaration, by using any one
of the following methods.
o The DEFAULT keyword
o The assignment operator
counter binary_integer := 0;
greetings varchar2(20) DEFAULT 'Hello';
You can also specify NOT NULL constraint to avoid NULL
value. If you specify the NOT NULL constraint, you must assign
an initial value for that variable.
You must have a good programming skill to initialize variable
properly otherwise, sometimes program would produce unexpected
result.
Example of initializing variable
Let's take a simple example to explain it well:
DECLARE
a integer := 30;
b integer := 40;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);

242
Relational Database Management System
f := 100.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
After the execution, this will produce the following result:
Value of c: 70
Value of f: 33.333333333333333333

PL/SQL procedure successfully completed.

Variable Scope in PL/SQL:


PL/SQL allows nesting of blocks. A program block can contain
another inner block. If you declare a variable within an inner block, it
is not accessible to an outer block. There are two types of variable
scope:
o Local Variable: Local variables are the inner block variables
which are not accessible to outer blocks.
o Global Variable: Global variables are declared in outermost
block.

Example of Local and Global variables


Let's take an example to show the usage of Local and Global
variables in its simple form:
DECLARE
Global variables

243
Relational Database Management System
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
After the execution, this will produce the following result:
Outer Variable num1: 95
Outer Variable num2: 85
Inner Variable num1: 195
Inner Variable num2: 185

PL/SQL procedure successfully completed.

Variable Attributes:

244
Relational Database Management System
When you declare a PL/SQL variable to hold the column values, it
must be of correct data types and precision, otherwise error will
occur on execution. Rather than hard coding the data type and
precision of a variable. PL/SQL provides the facility to declare a
variable without having to specify a particular data type
using %TYPE and %ROWTYPE attributes. These two attributes
allow us to specify a variable and have that variable data type be
defined by a table/view column or a PL/SQL package variable.

A % sign servers as the attribute indicator. This method of declaring


variables has an advantage as the user is not concerned with writing
and maintaining code.

Following are the types of Variable Attributes in PL/SQL.


o %TYPE:
The %TYPE attribute is used to declare variables according to the
already declared variable or database column. It is used when you are
declaring an individual variable, not a record. The data type and
precision of the variable declared using %TYPE attribute is the same
as that of the column that is referred from a given table. This is
particularly useful when declaring variables that will hold database
values. When using the %TYPE keyword, the name of the columns
and the table to which the variable will correspond must be known to

245
Relational Database Management System
the user. These are then prefixed with the variable name. If some
previously declared variable is referred then prefix that variable
name to the %TYPE attribute.

The syntax for declaring a variable with %TYPE is:


<var_name> <tab_name>.<column_name>%TYPE;
Where <column_name> is the column defined in the <tab_name>.

Consider a declaration.
SALARY EMP.SAL % TYPE;
This declaration will declare a variable SALARY that has the same
data type as column SAL of the EMP table.
Example:
DECLARE
SALARY EMP.SAL % TYPE;
ECODE EMP.empno % TYPE;
BEGIN
Ecode :=&Ecode;
Select SAL into SALARY from EMP where EMPNO = ECOD
E;
dbms_output.put_line('Salary of ' || ECODE || 'is = || salary');
END;
After the execution, this will produce the following result:
Enter value for ecode: 7499

246
Relational Database Management System
Salary of 7499 is = 1600
PL/SQL procedure successfully completed.

%ROWTYPE:
The %ROWTYPE attribute is used to declare a record type that
represents a row in a table. The record can store an entire row or
some specific data selected from the table. A column in a row and
corresponding fields in a record have the same name and data types.

The syntax for declaring a variable with %ROWTYPE is:


<var_name> <tab_name>.ROW%TYPE;
Where <variable_name> is the variable defined in the <tab_name>.
Consider a declaration.
EMPLOYEE EMP. % ROW TYPE;
This declaration will declare a record named EMPLOYEE having
fields with the same name and data types as that of columns in the
EMP table. You can access the elements of EMPLOYEE record as
EMPLOYEE.SAL := 10000;
EMPLOYEE.ENAME := ‗KIRAN‘;
Example:
DECLARE
EMPLOYEE EMP. % ROW TYPE;
BEGIN
EMPLOYEE.EMPNO := 2092;

247
Relational Database Management System
5 EMPLOYEE.ENAME := 'Sanju';
Insert into EMP where (EMPNO, ENAME) Values (employee.
empno, employee.ename);
dbms_output.put_line('Row Inserted');
END;
After the execution, this will produce the following result:
Row Inserted
PL/SQL procedure successfully completed.
Advantages:
o If you don‘t know the data type at the time of declaration.
The data type assigned to the associated variables will be
determined dynamically at run time.
o If the data type of the variable you are referencing changes
the %TYPE or %ROWTYPE variable changes at run time
without having to rewrite variable declarations. For example:
if the ENAME column of an EMP table is changed from a
VARCHAR2(10) to VRACHAR2(15) then you don‘t need to
modify the PL/SQL code.

5.4 PL/SQL Constants


A constant is a value used in a PL/SQL block that remains
unchanged throughout the program. It is a user-defined literal value.
It can be declared and used instead of actual values.
Let's take an example to explain it well:

248
Relational Database Management System
Suppose, you have to write a program which will increase the salary
of the employees upto 30%, you can declare a constant and use it
throughout the program. Next time if you want to increase the salary
again you can change the value of constant than the actual value
throughout the program.
Syntax to declare a constant:
constant_name CONSTANT datatype := VALUE;
o Constant_name:it is the name of constant just like variable
name. The constant word is a reserved word and its value
does not change.
o VALUE: it is a value which is assigned to a constant when it
is declared. It can not be assigned later.
Example of PL/SQL constant
Let's take an example to explain it well:
DECLARE
constant declaration
pi constant number := 3.141592654;
other declarations
radius number(5,2);
dia number(5,2);
circumference number(7, 2);
area number (10, 2);
BEGIN
processing

249
Relational Database Management System
radius := 9.5;
dia := radius * 2;
circumference := 2.0 * pi * radius;
area := pi * radius * radius;
output
dbms_output.put_line('Radius: ' || radius);
dbms_output.put_line('Diameter: ' || dia);
dbms_output.put_line('Circumference: ' || circumference);
dbms_output.put_line('Area: ' || area);
END;
/
After the execution of the above code at SQL prompt, it will produce
the following result:.
Radius: 9.5
Diameter: 19
Circumference: 59.69
Area: 283.53
Pl/SQL procedure successfully completed.

5.5 PL/SQL Literals


Literals are the explicit numeric, character, string or boolean values
which are not represented by an identifier. For example: TRUE,
NULL, etc. are all literals of type boolean. PL/SQL literals are case-
sensitive. There are following kinds of literals in PL/SQL:

250
Relational Database Management System
o Numeric Literals
o Character Literals
o String Literals
o BOOLEAN Literals
o Date and Time Literals
Example of these different types of Literals:

Literals Examples

Numeric 75125, 3568, 33.3333333 etc.

Character 'A' '%' '9' ' ' 'z' '('

String Hello JavaTpoint!

Boolean TRUE, FALSE, NULL etc.

Date and Time '26-11-2002' , '2012-10-29 12:01:01'

5.6 PL/SQL Operators


Operators are used to do some operations with the help of
operands.
Types of operators −
 Arithmetic operators

251
Relational Database Management System
 Relational operators
 Comparison operators
 Logical operators

5.6.1 Arithmetic Operators


Following table shows all the arithmetic operators supported by
PL/SQL. Let us assume variable A holds 10 and variable B holds 5.

Operator Description Example

A + B will
+ Adds two operands
give 15

A - B will
- Subtracts second operand from the first
give 5

A * B will
* Multiplies both operands
give 50

A / B will
/ Divides numerator by de-numerator
give 2

Exponentiation operator, raises one A ** B will


**
operand to the power of other give 100000

252
Relational Database Management System
5.6.2 Relational Operators
Relational operators compare two expressions or values and return a
Boolean result. Following table shows all the relational operators
supported by PL/SQL.

Operator Description Example

Checks if the values of two operands are (A = B)


= equal or not, if yes then condition becomes is not
true. true.

!= Checks if the values of two operands are


(A != B)
<> equal or not, if values are not equal then
is true.
~= condition becomes true.

Checks if the value of left operand is (A > B)


> greater than the value of right operand, if is not
yes then condition becomes true. true.

Checks if the value of left operand is less


(A < B)
< than the value of right operand, if yes then
is true.
condition becomes true.

Checks if the value of left operand is (A >= B)


>=
greater than or equal to the value of right is not

253
Relational Database Management System
operand, if yes then condition becomes true.
true.

Checks if the value of left operand is less


(A <= B)
<= than or equal to the value of right operand,
is true
if yes then condition becomes true.

5.6.3 Comparison Operators


Comparison operators are used for comparing one expression to
another.

Operator Description Example

If 'Zara Ali' like


The LIKE operator compares
'Z% A_i' returns a
a character, string, or CLOB
Boolean true,
value to a pattern and returns
LIKE whereas, 'Nuha Ali'
TRUE if the value matches
like 'Z% A_i'
the pattern and FALSE if it
returns a Boolean
does not.
false.

The BETWEEN operator tests If x = 10 then, x


BETWEEN whether a value lies in a between 5 and 20
specified range. x BETWEEN returns true, x

254
Relational Database Management System
a AND b means that x >= a between 5 and 10
and x <= b. returns true, but x
between 11 and 20
returns false.

If x = 'm' then, x in
The IN operator tests set ('a', 'b', 'c') returns
membership. x IN (set) means Boolean false but x
IN
that x is equal to any member in ('m', 'n', 'o')
of set. returns Boolean
true.

The IS NULL operator returns


the BOOLEAN value TRUE
If x = 'm', then 'x is
if its operand is NULL or
IS NULL null' returns
FALSE if it is not NULL.
Boolean false.
Comparisons involving NULL
values always yield NULL.

5.6.4 Logical Operators


Following table shows the Logical operators supported by PL/SQL.
All these operators work on Boolean operands and produce Boolean
results.

255
Relational Database Management System
Operator Description Examples

Called the logical AND operator. If both


(A and B)
and the operands are true then condition
is false.
becomes true.

Called the logical OR Operator. If any of


(A or B)
or the two operands is true then condition
is true.
becomes true.

Called the logical NOT Operator. Used to


not (A
reverse the logical state of its operand. If
not and B) is
a condition is true then Logical NOT
true.
operator will make it false.

5.6.5 PL/SQL Operator Precedence


Operator precedence determines the grouping of terms in an
expression. This affects how an expression is evaluated. Certain
operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition
operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because
operator * has higher precedence than +, so it first gets multiplied
with 3*2 and then adds into 7.

256
Relational Database Management System
Here, operators with the highest precedence appear at the top of the
table, those with the lowest appear at the bottom. Within an
expression, higher precedence operators will be evaluated first.
The precedence of operators goes as follows: =, <, >, <=, >=, <>, !=,
~=, ^=, IS NULL, LIKE, BETWEEN, IN.

Operator Operation

** exponentiation

+, - identity, negation

*, / multiplication, division

+, -, || addition, subtraction, concatenation

comparison

NOT logical negation

AND conjunction

OR inclusion

5.7 PL/SQL Control Statements


257
Relational Database Management System
5.7.1 PL/SQL If (Conditional Statements)
PL/SQL supports the programming language features like
conditional statements and iterative statements. Its programming
constructs are similar to how you use in programming languages like
Java and C++.
Syntax for IF Statement:
There are different syntaxes for the IF-THEN-ELSE statement.
Syntax: (IF-THEN statement):
IF condition
THEN
Statement: {It is executed when condition is true}
END IF;
This syntax is used when you want to execute statements only when
condition is TRUE.
Syntax: (IF-THEN-ELSE statement):
IF condition
THEN
{...statements to execute when condition is TRUE...}
ELSE
{...statements to execute when condition is FALSE...}
END IF;
This syntax is used when you want to execute one set of statements
when condition is TRUE or a different set of statements when
condition is FALSE.

258
Relational Database Management System
Syntax: (IF-THEN-ELSIF statement):
IF condition1
THEN
{...statements to execute when condition1 is TRUE...}
ELSIF condition2
THEN
{...statements to execute when condition2 is TRUE...}
END IF;
This syntax is used when you want to execute one set of statements
when condition1 is TRUE or a different set of statements when
condition2 is TRUE.
Syntax: (IF-THEN-ELSIF-ELSE statement):
IF condition1
THEN
{...statements to execute when condition1 is TRUE...}
ELSIF condition2
THEN
{...statements to execute when condition2 is TRUE...}
ELSE
{...statements to execute when both condition1 and condition2 ar
e FALSE...}
END IF;
It is the most advance syntax and used if you want to execute one set
of statements when condition1 is TRUE, a different set of statement

259
Relational Database Management System
when condition2 is TRUE or a different set of statements when both
the condition1 and condition2 are FALSE.

Example of PL/SQL If Statement


Let's take an example to see the whole concept:
DECLARE
a number(3) := 500;
BEGIN
check the boolean condition using if statement
IF( a < 20 ) THEN
if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
ELSE
dbms_output.put_line('a is not less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
After the execution of the above code in SQL prompt, you will get
the following result:
a is not less than 20
value of a is : 500
PL/SQL procedure successfully completed.

260
Relational Database Management System

5.7.2 PL/SQL Case Statement


The PL/SQL CASE statement facilitates you to execute a sequence
of statements based on a selector. A selector can be anything such as
variable, function or an expression that the CASE statement checks
to a boolean value.
The CASE statement works like the IF statement, only using the
keyword WHEN. A CASE statement is evaluated from top to bottom.
If it get the condition TRUE, then the corresponding THEN calause
is executed and the execution goes to the END CASE clause.
Syntax for the CASE Statement:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
Example of PL/SQL case statement
Let's take an example to make it clear:
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade

261
Relational Database Management System
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
when 'D' then dbms_output.put_line('Average');
when 'F' then dbms_output.put_line('Passed with Grace');
else dbms_output.put_line('Failed');
END CASE;
END;
After the execution of above code, you will get the following result:
Excellent
PL/SQL procedure successfully completed.

5.7.3 PL/SQL Looping Statements


The PL/SQL loops are used to repeat the execution of one or more
statements for specified number of times. These are also known as
iterative control statements.
Syntax for a basic loop:
LOOP
Sequence of statements;
END LOOP;
Types of PL/SQL Loops
There are 4 types of PL/SQL Loops.
Basic Loop / Exit Loop
While Loop

262
Relational Database Management System
For Loop
Cursor For Loop

PL/SQL Exit Loop (Basic Loop)


PL/SQL exit loop is used when a set of statements is to be executed
at least once before the termination of the loop. There must be an
EXIT condition specified in the loop, otherwise the loop will get into
an infinite number of iterations. After the occurrence of EXIT
condition, the process exits the loop.
Syntax of basic loop:
LOOP
Sequence of statements;
END LOOP;
Syntax of exit loop:
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
Example of PL/SQL EXIT Loop
Let's take a simple example to explain it well:
DECLARE
i NUMBER := 1;
BEGIN

263
Relational Database Management System
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
After the execution of the above code, you will get the following
result:
1
2
3
4
5
6
7
8
9
10
Note: You must follow these steps while using PL/SQL Exit Loop.
o Initialize a variable before the loop body
o Increment the variable in the loop.
o You should use EXIT WHEN statement to exit from the
Loop. Otherwise the EXIT statement without WHEN
condition, the statements in the Loop is executed only once.

264
Relational Database Management System

PL/SQL While Loop


PL/SQL while loop is used when a set of statements has to be
executed as long as a condition is true, the While loop is used. The
condition is decided at the beginning of each iteration and continues
until the condition becomes false.
Syntax of while loop:
WHILE <condition>
LOOP statements;
END LOOP;
Example of PL/SQL While Loop
Let's see a simple example of PL/SQL WHILE loop.
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
After the execution of the above code, you will get the following
result:
1
2

265
Relational Database Management System
3
4
5
6
7
8
9
10
Note: You must follow these steps while using PL/SQL WHILE
Loop.
o Initialize a variable before the loop body.
o Increment the variable in the loop.
o You can use EXIT WHEN statements and EXIT statements
in While loop but it is not done often.

PL/SQL FOR Loop


PL/SQL for loop is used when when you want to execute a set of
statements for a predetermined number of times. The loop is iterated
between the start and end integer values. The counter is always
incremented by 1 and once the counter reaches the value of end
integer, the loop ends.
Syntax of for loop:
FOR counter IN initial_value .. final_value LOOP
LOOP statements;

266
Relational Database Management System
END LOOP;
o initial_value : Start integer value
o final_value : End integer value
PL/SQL For Loop Example 1
Let's see a simple example of PL/SQL FOR loop.
BEGIN
FOR k IN 1..10 LOOP
note that k was not declared
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;
After the execution of the above code, you will get the following
result:
1
2
3
4
5
6
7
8
9
10

267
Relational Database Management System
Note: You must follow these steps while using PL/SQL WHILE
Loop.
o You don't need to declare the counter variable explicitly
because it is declared implicitly in the declaration section.
o The counter variable is incremented by 1 and does not need to
be incremented explicitly.
o You can use EXIT WHEN statements and EXIT statements
in FOR Loops but it is not done often.

5.7.4 PL/SQL Continue Statement


The continue statement is used to exit the loop from the reminder if
its body either conditionally or unconditionally and forces the next
iteration of the loop to take place, skipping any codes in between.
The continue statement is not a keyword in Oracle 10g. It is a new
feature encorporated in oracle 11g.
For example: If a continue statement exits a cursor FOR LOOP
prematurely then it exits an inner loop and transfer control to the next
iteration of an outer loop, the cursor closes (in this context,
CONTINUE works like GOTO).
Syntax:
continue;
Example of PL/SQL continue statement
Let's take an example of PL/SQL continue statement.
DECLARE

268
Relational Database Management System
x NUMBER := 0;
BEGIN
LOOP -- After CONTINUE statement, control resumes here
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x
));
x := x + 1;
IF x < 3 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE
('Inside loop, after CONTINUE: x = ' || TO_CHAR(x));
EXIT WHEN x = 5;
END LOOP;

DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x


));
END;
/
After the execution of above code, you will get the following result:
Inside loop: x = 0
Inside loop: x = 1
Inside loop: x = 2
Inside loop, after CONTINUE: x = 3
Inside loop: x = 3

269
Relational Database Management System
Inside loop, after CONTINUE: x = 4
Inside loop: x = 4
Inside loop, after CONTINUE: x = 5
After loop: x = 5

5.7.5 PL/SQL GOTO Statement


In PL/SQL, GOTO statement makes you able to get an unconditional
jump from the GOTO to a specific executable statement label in the
same subprogram of the PL/SQL block.
Here the label declaration which contains the label_name
encapsulated within the << >> symbol and must be followed by at
least one statement to execute.

Syntax:
GOTO label_name;
Here the label declaration which contains the label_name
encapsulated within the << >> symbol and must be followed by
at least one statement to execute.
GOTO label_name;
..
..
<<label_name>>
Statement;
Example of PL/SQL GOTO statement

270
Relational Database Management System
Let's take an example of PL/SQL GOTO statement.
DECLARE
a number(2) := 30;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 50 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 35 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
/
After the execution of above code, you will get the following result:
value of a: 30
value of a: 31
value of a: 32
value of a: 33
value of a: 34
value of a: 36
value of a: 37

271
Relational Database Management System
value of a: 38
value of a: 39
value of a: 40
value of a: 41
value of a: 42
value of a: 43
value of a: 44
value of a: 45
value of a: 46
value of a: 47
value of a: 48
value of a: 49

Statement processed.
Restriction on GOTO statement
Following is a list of some restrictions imposed on GOTO statement.
o Cannot transfer control into an IF statement, CASE statement,
LOOP statement or sub-block.
o Cannot transfer control from one IF statement clause to
another or from one CASE statement WHEN clause to
another.
o Cannot transfer control from an outer block into a sub-block.
o Cannot transfer control out of a subprogram.
o Cannot transfer control into an exception handler.

272
Relational Database Management System

5.8 PL/SQL Procedure


The PL/SQL stored procedure or simply a procedure is a PL/SQL
block which performs one or more specific tasks. It is just like
procedures in other programming languages.
The procedure contains a header and a body.
o Header: The header contains the name of the procedure and
the parameters or variables passed to the procedure.
o Body: The body contains a declaration section, execution
section and exception section similar to a general PL/SQL
block.
How to pass parameters in procedure:
When you want to create a procedure or function, you have to define
parameters .There is three ways to pass parameters in procedure:
1. IN parameters: The IN parameter can be referenced by the
procedure or function. The value of the parameter cannot be
overwritten by the procedure or the function.
2. OUT parameters: The OUT parameter cannot be referenced
by the procedure or function, but the value of the parameter
can be overwritten by the procedure or function.
3. INOUT parameters: The INOUT parameter can be
referenced by the procedure or function and the value of the
parameter can be overwritten by the procedure or function.

273
Relational Database Management System
A procedure may or may not return any value.

PL/SQL Create Procedure


Syntax for creating procedure:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
Create procedure example
In this example, we are going to insert record in user table. So you
need to create user table first.
Table creation:
create table user(id number(10) primary key,name varchar2(100));

Now write the procedure code to insert record in user table.


Procedure Code:
create or replace procedure "INSERTUSER"
(id IN NUMBER,
name IN VARCHAR2)
is

274
Relational Database Management System
begin
insert into user values(id,name);
end;
/
Output:
Procedure created.

PL/SQL program to call procedure


Let's see the code to call above created procedure.
BEGIN
insertuser(101,'Rahul');
dbms_output.put_line('record inserted successfully');
END;
/
Now, see the "USER" table, you will see one record is inserted.

ID Name

101 Rahul

PL/SQL Drop Procedure


Syntax for drop procedure
DROP PROCEDURE procedure_name;
Example of drop procedure
DROP PROCEDURE pro1;

275
Relational Database Management System
5.9 PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The
main difference between procedure and a function is, a function must
always return a value, and on the other hand a procedure may or may
not return a value. Except this, all the other things of PL/SQL
procedure are true for PL/SQL function too.

Syntax to create a function:


CREATE [OR REPLACE] FUNCTION function_name [parame
ters]
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Here:
o Function_name: specifies the name of the function.
o [OR REPLACE] option allows modifying an existing
function.
o The optional parameter list contains name, mode and types
of the parameters.

276
Relational Database Management System
o IN represents that value will be passed from outside and OUT
represents that this parameter will be used to return a value
outside of the procedure.
The function must contain a return statement.
o RETURN clause specifies that data type you are going to
return from the function.
o Function_body contains the executable part.
o The AS keyword is used instead of the IS keyword for
creating a standalone function.

PL/SQL Function Example


Let's see a simple example to create a function.
create or replace function adder(n1 in number, n2 in number)
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
/
Now write another program to call the function.
DECLARE
n3 number(2);

277
Relational Database Management System
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
/
Output:
Addition is: 33
Statement processed.
0.05 seconds

PL/SQL function example using table


Let's take a customer table. This example illustrates creating and
calling a standalone function. This function will return the total
number of CUSTOMERS in the customers table.

Customers

Id Name Department Salary

1 alex web developer 35000

2 ricky program developer 45000

3 mohan web designer 35000

278
Relational Database Management System
4 dilshad database manager 44000

Create Function:
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;
RETURN total;
END;
/
After the execution of above code, you will get the following result.
Function created.

Calling PL/SQL Function:


While creating a function, you have to give a definition of what the
function has to do. To use a function, you will have to call that
function to perform the defined task. Once the function is called, the
program control is transferred to the called function. After the
successful completion of the defined task, the call function returns
program control back to the main program.

To call a function you have to pass the required parameters along


with function name and if function returns a value then you can store

279
Relational Database Management System
returned value. Following program calls the function totalCustomers
from an anonymous block:
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
After the execution of above code in SQL prompt, you will get the
following result.
Total no. of Customers: 4
PL/SQL procedure successfully completed.

PL/SQL Recursive Function


You already know that a program or a subprogram can call another
subprogram. When a subprogram calls itself, it is called recursive
call and the process is known as recursion.

Example to calculate the factorial of a number


DECLARE
num number;
factorial number;

280
Relational Database Management System
FUNCTION fact(x number)
RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;

BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
After the execution of above code at SQL prompt, it produces the
following result.
Factorial 6 is 720
PL/SQL procedure successfully completed.
PL/SQL Drop Function

281
Relational Database Management System
Syntax for removing your created function:
If you want to remove your created function from the database, you
should use the following syntax.
DROP FUNCTION function_name;

5.10 PL/SQL Cursor


When an SQL statement is processed, Oracle creates a memory area
known as context area. A cursor is a pointer to this context area. It
contains all information needed for processing the statement. In
PL/SQL, the context area is controlled by Cursor. A cursor contains
information on a select statement and the rows of data accessed by it.

A cursor is used to referred to a program to fetch and process the


rows returned by the SQL statement, one at a time. There are two
types of cursors:
o Implicit Cursors
o Explicit Cursors

1) PL/SQL Implicit Cursors


The implicit cursors are automatically generated by Oracle while an
SQL statement is executed, if you don't use an explicit cursor for the
statement. These are created by default to process the statements
when DML statements like INSERT, UPDATE, DELETE etc. are
executed. Orcale provides some attributes known as Implicit cursor's

282
Relational Database Management System
attributes to check the status of DML operations. Some of them
are: %FOUND, %NOTFOUND, %ROWCOUNT and %ISOPEN.

For example: When you execute the SQL statements like INSERT,
UPDATE, DELETE then the cursor attributes tell whether any rows
are affected and how many have been affected. If you run a SELECT
INTO statement in PL/SQL block, the implicit cursor attribute can be
used to find out whether any row has been returned by the SELECT
statement. It will return an error if there no data is selected.

The following table soecifies the status of the cursor with each of its
attribute.

Attribute Description

%FOUND Its return value is TRUE if DML statements like


INSERT, DELETE and UPDATE affect at least
one row or more rows or a SELECT INTO
statement returned one or more rows. Otherwise it
returns FALSE.

%NOTFOUND Its return value is TRUE if DML statements like


INSERT, DELETE and UPDATE affect no row, or
a SELECT INTO statement return no rows.
Otherwise it returns FALSE. It is a just opposite

283
Relational Database Management System
of %FOUND.

%ISOPEN It always returns FALSE for implicit cursors,


because the SQL cursor is automatically closed
after executing its associated SQL statements.

%ROWCOUNT It returns the number of rows affected by DML


statements like INSERT, DELETE, and UPDATE
or returned by a SELECT INTO statement.

PL/SQL Implicit Cursor Example


Create customers table and have records:

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

6 Sunita 20 Delhi 30000

284
Relational Database Management System
Let's execute the following program to update the table and increase
salary of each customer by 5000. Here, SQL%ROWCOUNT
attribute is used to determine the number of rows affected:
Create procedure:
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
Output:
6 customers updated
PL/SQL procedure successfully completed.
Now, if you check the records in customer table, you will find that
the rows are updated.
1. select * from customers;

285
Relational Database Management System

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 25000

2 Suresh 22 Kanpur 27000

3 Mahesh 24 Ghaziabad 29000

4 Chandan 25 Noida 31000

5 Alex 21 Paris 33000

6 Sunita 20 Delhi 35000

2) PL/SQL Explicit Cursors


The Explicit cursors are defined by the programmers to gain more
control over the context area. These cursors should be defined in the
declaration section of the PL/SQL block. It is created on a SELECT
statement which returns more than one row.
Following is the syntax to create an explicit cursor:
Syntax of explicit cursor
Following is the syntax to create an explicit cursor:
CURSOR cursor_name IS select_statement;;
Steps:
You must follow these steps while working with an explicit cursor.

286
Relational Database Management System
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.
1) Declare the cursor:
It defines the cursor with a name and the associated SELECT
statement.
Syntax for explicit cursor decleration
CURSOR name IS
SELECT statement;
2) Open the cursor:
It is used to allocate memory for the cursor and make it easy to fetch
the rows returned by the SQL statements into it.
Syntax for cursor open:
OPEN cursor_name;
3) Fetch the cursor:
It is used to access one row at a time. You can fetch rows from the
above-opened cursor as follows:

Syntax for cursor fetch:


FETCH cursor_name INTO variable_list;
4) Close the cursor:
It is used to release the allocated memory. The following syntax is
used to close the above-opened cursors.

287
Relational Database Management System

Syntax for cursor close:


Close cursor_name;
PL/SQL Explicit Cursor Example
Explicit cursors are defined by programmers to gain more control
over the context area. It is defined in the declaration section of the
PL/SQL block. It is created on a SELECT statement which returns
more than one row.
Let's take an example to demonstrate the use of explicit cursor. In
this example, we are using the already created CUSTOMERS table.

Create customers table and have records:

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

288
Relational Database Management System
6 Sunita 20 Delhi 30000

Create procedure:
Execute the following program to retrieve the customer name and
address.
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/
Output:
1 Ramesh Allahabad
2 Suresh Kanpur

289
Relational Database Management System
3 Mahesh Ghaziabad
4 Chandan Noida
5 Alex Paris
6 Sunita Delhi
PL/SQL procedure successfully completed.

5.11 PL/SQL Exception Handling


What is Exception?
An error occurs during the program execution is called Exception in
PL/SQL.
PL/SQL facilitates programmers to catch such conditions using
exception block in the program and an appropriate action is taken
against the error condition.
There are two type of exceptions:
o Built in Exceptions
o User-defined Exceptions

PL/SQL Exception Handling


Syntax for exception handling:
Following is a general syntax for exception handling:
DECLARE
<declarations section>
BEGIN
<executable command(s)>

290
Relational Database Management System
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
Example of exception handling
Let's take a simple example to demonstrate the concept of exception
handling. Here we are using the already created CUSTOMERS table.
SELECT* FROM COUSTOMERS;

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

291
Relational Database Management System
4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

6 Sunita 20 Delhi 30000

DECLARE
c_id customers.id%type := 8;
c_name customers.name%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/

292
Relational Database Management System
After the execution of above code at SQL Prompt, it produces the
following result:
No such customer!
PL/SQL procedure successfully completed.

The above program should show the name and address of a customer
as result whose ID is given. But there is no customer with ID value 8
in our database, so the program raises the run-time exception
NO_DATA_FOUND, which is captured in EXCEPTION block.
If you use the id defined in the above table (i.e. 1 to 6), you will get a
certain result. For a demo example: here, we are using the id 5.
DECLARE
c_id customers.id%type := 5;
c_name customers.name%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');

293
Relational Database Management System
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
After the execution of above code at SQL prompt, you will get the
following result:
Name: alex
Address: paris
PL/SQL procedure successfully completed.

Raising Exceptions
In the case of any internal database error, exceptions are raised by the
database server automatically. But it can also be raised explicitly by
programmer by using command RAISE.
Syntax for raising an exception:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;

294
Relational Database Management System
END;

PL/SQL User-defined Exceptions


PL/SQL facilitates their users to define their own exceptions
according to the need of the program. A user-defined exception can
be raised explicitly, using either a RAISE statement or the procedure
DBMS_STANDARD.RAISE_APPLICATION_ERROR.
Syntax for user define exceptions
DECLARE
my-exception EXCEPTION;
PL/SQL Pre-defined Exceptions
There are many pre-defined exception in PL/SQL which are executed
when any database rule is violated by the programs.
For example: NO_DATA_FOUND is a pre-defined exception
which is raised when a SELECT INTO statement returns no rows.
Following is a list of some important pre-defined exceptions:

Exception Oracle SQL Description


Error Code

ACCESS_IN 06530 -6530 It is raised when a


TO_NULL NULL object is
automatically
assigned a value.

295
Relational Database Management System
CASE_NOT 06592 -6592 It is raised when
_FOUND none of the choices
in the "WHEN"
clauses of a CASE
statement is selected,
and there is no else
clause.

COLLECTI 06531 -6531 It is raised when a


ON_IS_NUL program attempts to
L apply collection
methods other than
exists to an
uninitialized nested
table or varray, or
the program
attempts to assign
values to the
elements of an
uninitialized nested
table or varray.

DUP_VAL_ 00001 -1 It is raised when


ON_INDEX duplicate values are

296
Relational Database Management System
attempted to be
stored in a column
with unique index.

INVALID_C 01001 -1001 It is raised when


URSOR attempts are made to
make a cursor
operation that is not
allowed, such as
closing an unopened
cursor.

INVALID_N 01722 -1722 It is raised when the


UMBER conversion of a
character string into
a number fails
because the string
does not represent a
valid number.

LOGIN_DE 01017 -1017 It is raised when s


NIED program attempts to
log on to the
database with an

297
Relational Database Management System
invalid username or
password.

NO_DATA_ 01403 +100 It is raised when a


FOUND select into statement
returns no rows.

NOT_LOGG 01012 -1012 It is raised when a


ED_ON database call is
issued without being
connected to the
database.

PROGRAM 06501 -6501 It is raised when


_ERROR PL/SQL has an
internal problem.

ROWTYPE_ 06504 -6504 It is raised when a


MISMATCH cursor fetches value
in a variable having
incompatible data
type.

SELF_IS_N 30625 -30625 It is raised when a


ULL member method is

298
Relational Database Management System
invoked, but the
instance of the
object type was not
initialized.

STORAGE_ 06500 -6500 It is raised when


ERROR PL/SQL ran out of
memory or memory
was corrupted.

TOO_MAN 01422 -1422 It is raised when a


Y_ROWS SELECT INTO
statement returns
more than one row.

VALUE_ER 06502 -6502 It is raised when an


ROR arithmetic,
conversion,
truncation, or size-
constraint error
occurs.

ZERO_DIVI 01476 1476 It is raised when an


DE attempt is made to
divide a number by

299
Relational Database Management System
zero.

5.12 PL/SQL Trigger


Trigger is invoked by Oracle engine automatically whenever a
specified event occurs.Trigger is stored into database and invoked
repeatedly, when specific condition match.
Triggers are stored programs, which are automatically executed or
fired when some event occurs.
Triggers are written to be executed in response to any of the
following events.
o A database manipulation (DML) statement (DELETE,
INSERT, or UPDATE).
o A database definition (DDL) statement (CREATE, ALTER,
or DROP).
o A database operation (SERVERERROR, LOGON, LOGOFF,
STARTUP, or SHUTDOWN).
Triggers could be defined on the table, view, schema, or database
with which the event is associated.

Advantages of Triggers
These are the following advantages of Triggers:
o Trigger generates some derived column values automatically
o Enforces referential integrity

300
Relational Database Management System
o Event logging and storing information on table access
o Auditing
o Synchronous replication of tables
o Imposing security authorizations
o Preventing invalid transactions

Creating a trigger:
Syntax for creating trigger:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Here,

301
Relational Database Management System
o CREATE [OR REPLACE] TRIGGER trigger_name: It
creates or replaces an existing trigger with the trigger_name.
o {BEFORE | AFTER | INSTEAD OF} : This specifies when
the trigger would be executed. The INSTEAD OF clause is
used for creating trigger on a view.
o {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies
the DML operation.
o [OF col_name]: This specifies the column name that would
be updated.
o [ON table_name]: This specifies the name of the table
associated with the trigger.
o [REFERENCING OLD AS o NEW AS n]: This allows you
to refer new and old values for various DML statements, like
INSERT, UPDATE, and DELETE.
o [FOR EACH ROW]: This specifies a row level trigger, i.e.,
the trigger would be executed for each row being affected.
Otherwise the trigger will execute just once when the SQL
statement is executed, which is called a table level trigger.
o WHEN (condition): This provides a condition for rows for
which the trigger would fire. This clause is valid only for row
level triggers.
PL/SQL Trigger Example
Let's take a simple example to demonstrate the trigger. In this
example, we are using the following CUSTOMERS table:

302
Relational Database Management System

Create table and have records:

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

6 Sunita 20 Delhi 30000

Create trigger:
Let's take a program to create a row level trigger for the
CUSTOMERS table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old values and
new values:
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)

303
Relational Database Management System
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
After the execution of the above code at SQL Prompt, it produces the
following result.
Trigger created.
Check the salary difference by procedure:
Use the following code to get the old salary, new salary and salary
difference after the trigger created.
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;

304
Relational Database Management System
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
Output:
Old salary: 20000
New salary: 25000
Salary difference: 5000
Old salary: 22000
New salary: 27000
Salary difference: 5000
Old salary: 24000
New salary: 29000
Salary difference: 5000
Old salary: 26000
New salary: 31000
Salary difference: 5000
Old salary: 28000
New salary: 33000
Salary difference: 5000
Old salary: 30000
New salary: 35000
Salary difference: 5000
6 customers updated

305
Relational Database Management System
Note: As many times you executed this code, the old and new both
salary is incremented by 5000 and hence the salary difference is
always 5000.
After the execution of above code again, you will get the following
result.
Old salary: 25000
New salary: 30000
Salary difference: 5000
Old salary: 27000
New salary: 32000
Salary difference: 5000
Old salary: 29000
New salary: 34000
Salary difference: 5000
Old salary: 31000
New salary: 36000
Salary difference: 5000
Old salary: 33000
New salary: 38000
Salary difference: 5000
Old salary: 35000
New salary: 40000
Salary difference: 5000
6 customers updated

306
Relational Database Management System
Important Points
Following are the two very important point and should be noted
carefully.
o OLD and NEW references are used for record level triggers
these are not available for table level triggers.
o If you want to query the table in the same trigger, then you
should use the AFTER keyword, because triggers can query
the table or change it again only after the initial changes are
applied and the table is back in a consistent state.

Questions:
2 Mark Questions:
1. Define PL/SQL.
2. List down the advantages of PL/SQL.
3. Define PL/SQL Operators.
4. Define function.
5. Define package.
6. What is mean by procedure and procedure call?
7. Define exception handling.
8. List down some built in exceptions.
9. Define triggers.
10. What is cursor? What are the types of cursor?

307
Relational Database Management System
5 Mark / 10 Mark Questions:
11. Illustrate the structure of PL/SQL.
12. What are the PL/SQL Elements?
13. Explain about Datatypes.
14. What is operator precedence? Explain.
15. Explain IF control with suitable example.
16. Explain Loops in PL/SQL with suitable examples.
17. What is cursor? Explain in detail.
18. Elucidate the Functions in PL/SQL.
19. How PL/SQL handles the user defined exceptions?
20. Explain Triggers in detail.

308
Relational Database Management System

LAB PROGRAMS - SQL


1.DDL commands with constraints
Source code:
-- Create a database
CREATE DATABASE my_database;

-- Use the database


USE my_database;

-- Create a table with constraints


CREATE TABLE customers (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
-- Insert data into the table
INSERT INTO customers (name, email) VALUES ('Alice',
'[email protected]'), ('Bob', '[email protected]'), ('Carol',
'[email protected]');
-- Update a row in the table
UPDATE customers SET email = '[email protected]'
WHERE name = 'Alice';

309
Relational Database Management System
-- Truncate the table
TRUNCATE TABLE customers;
-- Drop the table
DROP TABLE customers;
OUTPUT:
The table is dropped

2.DML Commands with Constraints


Source Code:
-- Create a database
CREATE DATABASE my_Database;

-- Use the database


USE my_Database;

-- Create a table with constraints


CREATE TABLE products (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
price DECIMAL(10,2) NOT NULL CHECK (price >= 0),
PRIMARY KEY (id)
);

-- Insert data into the table

310
Relational Database Management System
INSERT INTO products (name, price) VALUES ('Product 1', 100),
('Product 2', 200), ('Product 3', 300);

-- Update a row in the table


UPDATE products SET price = 150 WHERE name = 'Product 1';

-- Try to update a row in the table with an invalid price


UPDATE products SET price = -100 WHERE name = 'Product 2';

-- Delete a row from the table


DELETE FROM products WHERE name = 'Product 3';

-- Try to delete a row from the table that is referenced by a foreign


key
DELETE FROM products WHERE name = 'Product 1';

--RETREIVE information from the table


SELECT * FROM products;

OUTPUT:
+----+-----------+--------+
| id | name | price |
+----+-----------+--------+
| 2 | Product 2 | 200.00 |

311
Relational Database Management System
+----+-----------+--------+
1 row in set (0.000 sec)

3 . SQL Queries: Queries, Sub Queries, Aggregate Function

Source code:
-- Create a table
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`age` INT NOT NULL,
PRIMARY KEY (`id`)
);
-- Insert some data into the table
INSERT INTO `users` (`name`, `age`) VALUES
('John Doe', 30),
('Jane Doe', 25),
('Peter Parker', 20),
('Mary Jane Watson', 19);
-- Simple query
SELECT * FROM `users`;
-- Query with a subquery
SELECT * FROM `users` WHERE `age` > (SELECT AVG(`age`)
FROM `users`);

312
Relational Database Management System
-- Query with an aggregate function
SELECT COUNT(`id`) AS `total_users` FROM `users`;

OUTPUT:
MariaDB [my_database]> -- Simple query
MariaDB [my_database]> SELECT * FROM `users`;
+----+------------------+-----+
| id | name | age |
+----+------------------+-----+
| 1 | John Doe | 30 |
| 2 | Jane Doe | 25 |
| 3 | Peter Parker | 20 |
| 4 | Mary Jane Watson | 19 |
+----+------------------+-----+
4 rows in set (0.001 sec)
MariaDB [my_database]> -- Query with a subquery
MariaDB [my_database]> SELECT * FROM `users` WHERE `age` >
(SELECT AVG(`age`) FROM `users`);
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | John Doe | 30 |
| 2 | Jane Doe | 25 |
+----+----------+-----+

313
Relational Database Management System
2 rows in set (0.004 sec)
MariaDB [my_database]> -- Query with an aggregate function
MariaDB [my_database]> SELECT COUNT(`id`) AS `total_users`
FROM `users`;
+-------------+
| total_users |
+-------------+
| 4|
+-------------+
1 row in set (0.000 sec)

4.DESIGN AND DEVELOP APPLICATION FOR LIBRARY


MANAGEMENT
-- Create a database for the library management system
CREATE DATABASE LibraryManagementSystem;

-- Use the created database


USE LibraryManagementSystem;

-- Create a table for books


CREATE TABLE Books (
BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
Author VARCHAR(255),

314
Relational Database Management System
ISBN VARCHAR(13) NOT NULL UNIQUE,
CopiesAvailable INT NOT NULL,
TotalCopies INT NOT NULL
);

-- Create a table for borrowers (library members)


CREATE TABLE Borrowers (
BorrowerID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
Phone VARCHAR(15),
Address TEXT
);

-- Create a table for transactions (checkouts and returns)


CREATE TABLE Transactions (
TransactionID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
BorrowerID INT,
TransactionType ENUM('Checkout', 'Return') NOT NULL,
TransactionDate DATE NOT NULL,
DueDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),

315
Relational Database Management System
FOREIGN KEY (BorrowerID) REFERENCES
Borrowers(BorrowerID)
);

-- Sample data insertion


INSERT INTO Books (Title, Author, ISBN, CopiesAvailable,
TotalCopies)
VALUES
('Introduction to SQL', 'John Smith', '1234567890', 5, 5),
('Python Programming', 'Jane Doe', '9876543210', 3, 3);

INSERT INTO Borrowers (FirstName, LastName, Email, Phone,


Address)
VALUES
('Alice', 'Johnson', '[email protected]', '+1234567890', '123
Main St'),
('Bob', 'Smith', '[email protected]', '+9876543210', '456 Elm St');

-- Query to check out a book


-- This would typically be done by the application logic
INSERT INTO Transactions (BookID, BorrowerID,
TransactionType, TransactionDate, DueDate)
VALUES (1, 1, 'Checkout', '2023-09-21', '2023-10-21');

316
Relational Database Management System

-- Query to return a book


-- This would typically be done by the application logic
INSERT INTO Transactions (BookID, BorrowerID,
TransactionType, TransactionDate)
VALUES (1, 1, 'Return', '2023-10-15');

-- Query to list books checked out by a specific borrower


SELECT Books.Title, Transactions.TransactionDate,
Transactions.DueDate
FROM Books
INNER JOIN Transactions ON Books.BookID =
Transactions.BookID
WHERE Transactions.TransactionType = 'Checkout' AND
Transactions.BorrowerID = 1;

-- Query to list available books


SELECT * FROM Books WHERE CopiesAvailable > 0;

OUTPUT:
+---------------------+-----------------+------------+
| Title | TransactionDate | DueDate |
+---------------------+-----------------+------------+
| Introduction to SQL | 2023-09-21 | 2023-10-21 |

317
Relational Database Management System
+---------------------+-----------------+------------+
1 row in set (0.016 sec)
+--------+---------------------+------------+------------+-----------------+---
----------+
| BookID | Title | Author | ISBN | CopiesAvailable |
TotalCopies |
+--------+---------------------+------------+------------+-----------------+---
----------+
| 1 | Introduction to SQL | John Smith | 1234567890 | 5|
5|
| 2 | Python Programming | Jane Doe | 9876543210 | 3|
3|
+--------+---------------------+------------+------------+-----------------+---
----------+
2 rows in set (0.000 sec)

5.DESIGN AND DEVELOP APPLICATION FOR STUDENT


MARK SHEET PROCESSING
CREATE DATABASE StudentDB;
USE StudentDB;

-- Table for storing student information


CREATE TABLE Students (
StudentID INT PRIMARY KEY AUTO_INCREMENT,

318
Relational Database Management System
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
Email VARCHAR(100)
);

-- Table for storing student marks


CREATE TABLE Marks (
MarkID INT PRIMARY KEY AUTO_INCREMENT,
StudentID INT,
Subject VARCHAR(50),
Mark INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);
-- Insert sample student data
INSERT INTO Students (FirstName, LastName, DateOfBirth, Email)
VALUES
('John', 'Doe', '2000-01-15', '[email protected]'),
('Jane', 'Smith', '2001-03-20', '[email protected]'),
('Bob', 'Johnson', '1999-08-10', '[email protected]');

-- Insert sample marks


INSERT INTO Marks (StudentID, Subject, Mark)
VALUES

319
Relational Database Management System
(1, 'Math', 90),
(1, 'Science', 85),
(2, 'Math', 92),
(2, 'Science', 88),
(3, 'Math', 78),
(3, 'Science', 80);
-- Retrieve all students and their marks
SELECT Students.FirstName, Students.LastName, Marks.Subject,
Marks.Mark
FROM Students
INNER JOIN Marks ON Students.StudentID = Marks.StudentID;

-- Calculate the average mark for each student


SELECT Students.FirstName, Students.LastName,
AVG(Marks.Mark) AS AverageMark
FROM Students
INNER JOIN Marks ON Students.StudentID = Marks.StudentID
GROUP BY Students.StudentID;

-- Retrieve students who scored above 85 in Math


SELECT Students.FirstName, Students.LastName
FROM Students
INNER JOIN Marks ON Students.StudentID = Marks.StudentID
WHERE Marks.Subject = 'Math' AND Marks.Mark > 85;

320
Relational Database Management System

OUTPUT:
+-----------+----------+---------+------+
| FirstName | LastName | Subject | Mark |
+-----------+----------+---------+------+
| John | Doe | Math | 90 |
| John | Doe | Science | 85 |
| Jane | Smith | Math | 92 |
| Jane | Smith | Science | 88 |
| Bob | Johnson | Math | 78 |
| Bob | Johnson | Science | 80 |
+-----------+----------+---------+------+
6 rows in set (0.019 sec)
+-----------+----------+-------------+
| FirstName | LastName | AverageMark |
+-----------+----------+-------------+
| John | Doe | 87.5000 |
| Jane | Smith | 90.0000 |
| Bob | Johnson | 79.0000 |
+-----------+----------+-------------+
3 rows in set (0.000 sec)
+-----------+----------+
| FirstName | LastName |
+-----------+----------+

321
Relational Database Management System
| John | Doe |
| Jane | Smith |
+-----------+----------+
2 rows in set (0.001 sec)

6.DESIGN AND DEVELOP APPLICATION FOR PAYROLL


PROCESSING
-- Create a database for the payroll system
CREATE DATABASE PayrollSystem;
USE PayrollSystem;

-- Create tables for employee information and payroll data


CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
Email VARCHAR(100),
Phone VARCHAR(15),
Address VARCHAR(200)
);

CREATE TABLE Payroll (


PayrollID INT PRIMARY KEY,

322
Relational Database Management System
EmployeeID INT,
Salary DECIMAL(10, 2),
HoursWorked DECIMAL(5, 2),
OvertimeHours DECIMAL(5, 2),
Deductions DECIMAL(10, 2),
GrossPay DECIMAL(10, 2),
NetPay DECIMAL(10, 2),
PayrollDate DATE,
FOREIGN KEY (EmployeeID) REFERENCES
Employees(EmployeeID)
);

-- Insert sample employee data


INSERT INTO Employees (EmployeeID, FirstName, LastName,
DateOfBirth, Email, Phone, Address)
VALUES
(1, 'John', 'Doe', '1990-05-15', '[email protected]', '555-123-
4567', '123 Main St'),
(2, 'Jane', 'Smith', '1985-12-10', '[email protected]', '555-
987-6543', '456 Elm St');

-- Calculate and insert sample payroll data (simplified)

323
Relational Database Management System
INSERT INTO Payroll (PayrollID, EmployeeID, Salary,
HoursWorked, OvertimeHours, Deductions, GrossPay, NetPay,
PayrollDate)
VALUES
(1, 1, 5000.00, 160.00, 10.00, 200.00, 7000.00, 6800.00, '2023-09-
15'),
(2, 2, 6000.00, 175.00, 15.00, 250.00, 8000.00, 7750.00, '2023-09-
15');

-- Query to retrieve employee payroll information


SELECT E.FirstName, E.LastName, P.Salary, P.HoursWorked,
P.OvertimeHours, P.Deductions, P.GrossPay, P.NetPay,
P.PayrollDate
FROM Employees E
JOIN Payroll P ON E.EmployeeID = P.EmployeeID;

OUTPUT:

+-----------+----------+---------+-------------+---------------+------------+-
---------+---------+-------------+
| FirstName | LastName | Salary | HoursWorked | OvertimeHours |
Deductions | GrossPay | NetPay | PayrollDate |
+-----------+----------+---------+-------------+---------------+------------+-
---------+---------+-------------+

324
Relational Database Management System
| John | Doe | 5000.00 | 160.00 | 10.00 | 200.00 |
7000.00 | 6800.00 | 2023-09-15 |
| Jane | Smith | 6000.00 | 175.00 | 15.00 | 250.00 |
8000.00 | 7750.00 | 2023-09-15 |
+-----------+----------+---------+-------------+---------------+------------+-
---------+---------+-------------+
2 rows in set (0.015 sec)

PL/SQL Programs explained in Unit 5.

*******************************************************

325
Relational Database Management System

References Books
1. Raghu Ramakrishnan - Database Management Systems-
WCB/McGrawHill

2. C.J Date – An Introduction to Dtabase Systems 7 th Edition –


Addison Wesley-2000

Web References
 https://www.tutorialspoint.com

 https://www.geeksforgeeks.org

 https://artoftesting.com

 https://www.w3schools.com

 www.programiz.com

 https://www.scaler.com

326

You might also like