Types and Importance of Databases
Types and Importance of Databases
Chapter-1
A database is a collection of data, usually stored in electronic form. A database is typically designed
so that it is easy to store and access information.
A good database is crucial to any company or organisation. This is because the database stores all
the pertinent details about the company such as employee records, transactional records, salary
details etc.
A database stores and manages a large amount of data on a daily basis. This would not be possible
using any other tool such as a spreadsheet as they would simply not work.
Accurate
A database is pretty accurate as it has all sorts of build in constraints, checks etc. This means that
the information available in a database is guaranteed to be correct in most cases.
In a database, it is easy to update data using various Data Manipulation languages (DML) available.
One of these languages is SQL.
Security of data
Databases have various methods to ensure security of data. There are user logins required before
accessing a database and various access specifiers. These allow only authorised users to access
the database.
Data integrity
2
This is ensured in databases by using various constraints for data. Data integrity in databases makes
sure that the data is accurate and consistent in a database.
It is very easy to access and research data in a database. This is done using Data Query Languages
(DQL) which allow searching of any data in the database and performing computations on it.
Types of Databases
There are various types of databases used for storing different varieties of data:
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.
o It has decreased the risk of data management, i.e., manipulation of data will not
affect the core data.
o Data consistency is maintained as it manages data in a central repository.
o It provides better data quality, which enables organizations to establish data
standards.
o It is less costly because fewer vendors are required to handle the data sets.
o The size of the centralized database is large, which increases the response time for
fetching the data.
o It is not easy to update such an extensive database system.
3
o If any server failure occurs, entire data will be lost, which could be a huge loss.
2) Distributed Database
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
4
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.
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 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.
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:
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:
o Microsoft Azure
o Kamatera
o PhonixNAP
o ScienceSoft
o Google Cloud SQL, etc.
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.
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.
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.
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.
10) Operational DatabaseThe type of database which creates and updates the database
in real-time. It is basically designed for executing and handling the daily data operations
in several businesses. For example, An organization uses operational databases for
managing per day transactions.
DBMS applications
Applications where we use Database Management Systems are:
managing the vast catalogue of items, there is a need to secure the user
private information such as bank & card details. All this is taken care of by
database management systems.
Because there are many users who use the database so chances of data duplicity are
very high. As in database management system, data files are shared that in turns
minimizes data duplication and redundancy. All the information in database
management system occurs only once so chances of duplicity are very less.
Saves Storage Space and Cost
All the Database management systems have a lot of data to save. But proper integration
of data saves much more space in DBMS. Companies are paying so much amount of
money to store data. If they have managed data to storing then it will save their cost of
storing data and data entry.
Anyone Can Work on It
Users who are not having any technical skills can work on database management
system. The query language provided by DBMS is so easy to understand. If you want to
update, insert, delete and search any record then it is very easy with the help of queries
provided by DBMS. Any non programming user can do this without any help of skilled
programmer.
Large Database Maintenance
Security is a very big concern for all the organizations who are handling a large amount
of data. DBMS doesn’t give the full access of database except DBA or head of the
department. They are able to alter the database and all the users are created by them
so security level of DBMS becomes so high. No other person or user can access the full
database; all of them have restrictions according to their work.
DBMS stores all the data files permanently and there is no chance of any loss of data. If
somehow the data get lost then there is a backup and recovery method too that can
save organization’s data files. So no need to worry about data loss in DBMS.
Multi-user Access
In DBMS, multiple users can access all kind data and information stored in one data
store. There are certain limits that users can access or view particular data according to
the rights given to them. This increases the security and privacy of data for users
because they will have their own interface to access data.
So all these are the features of database management system. If you have problem and
want to ask anything about DBMS then please comment below, we will surely help you.
DBMS Providers
Oracle remains the most popular provider of database management systems,
followed by Microsoft SQL and MySQL.
Components of DBMS
The database management system can be divided into five major components, they are:
1. Hardware
2. Software
3. Data
4. Procedures
Let's have a simple diagram to see how they all fit together to form a database management system.
10
When we say Hardware, we mean computer, hard disks, I/O channels for data, and any other physical
component involved before any data is successfully stored into the memory.
When we run Oracle or MySQL on our personal computer, then our computer's Hard Disk, our Keyboard
using which we type in all the commands, our computer's RAM, ROM all become a part of the DBMS
hardware.
This is the main component, as this is the program which controls everything. The DBMS software is more
like a wrapper around the physical database, which provides us with an easy-to-use interface to store,
access and update data.
The DBMS software is capable of understanding the Database Access Language and intrepret it into actual
database commands to execute them on the DB.
Data is that resource, for which DBMS was designed. The motive behind the creation of DBMS was to store
and utilise data.
In a typical Database, the user saved Data is present and meta data is stored.
11
Metadata is data about the data. This is information stored by the DBMS to better understand the data
stored in it.
For example: When I store my Name in a database, the DBMS will store when the name was stored in
the database, what is the size of the name, is it stored as related data to some other data, or is it
independent, all this information is metadata.
Procedures refer to general instructions to use a database management system. This includes procedures
to setup and install a DBMS, To login and logout of DBMS software, to manage databases, to take backups,
generating reports etc.
Database Access Language is a simple language designed to write commands to access, insert, update
and delete data stored in any database.
A user can write commands in the Database Access Language and submit it to the DBMS for execution,
which is then translated and executed by the DBMS.
User can create new databases, tables, insert data, fetch stored data, update data and delete the data
using the access language.
Users
Database Administrators: Database Administrator or DBA is the one who manages the complete
database management system. DBA takes care of the security of the DBMS, it's availability,
managing the license keys, managing user accounts and access etc.
Application Programmer or Software Developer: This user group is involved in developing and
desiging the parts of DBMS.
End User: These days all the modern applications, web or mobile, store user data. How do you
think they do it? Yes, applications are programmed in such a way that they collect user data and
store the data on DBMS systems running on their server. End users are the one who store, retrieve,
update and delete data.
DBMS Architecture
A Database Management system is not always directly available for users and applications to
access and store data in it. A Database Management system can be centralised(all the data stored at one
location), decentralised(multiple copies of database at different locations) or hierarchical, depending
upon its architecture.
1-tier DBMS architecture also exist, this is when the database is directly available to the user for using it
to store data. Generally such a setup is used for local application development, where programmers
communicate directly with the database for quick response.
2-tier DBMS architecture includes an Application layer between the user and the DBMS, which is
responsible to communicate the user's request to the database management system and then send the
response from the DBMS to the user.
An application interface known as ODBC(Open Database Connectivity) provides an API that allow client
side program to call the DBMS. Most DBMS vendors provide ODBC drivers for their DBMS.
Such an architecture provides the DBMS extra security as it is not exposed to the End User directly.
Also, security can be improved by adding security and authentication checks in the Application layer too.
3-tier DBMS Architecture3-tier DBMS architecture is the most commonly used architecture for web
applications.
It is an extension of the 2-tier architecture. In the 2-tier architecture, we have an application layer which
can be accessed programatically to perform various operations on the DBMS. The application generally
understands the Database Access Language and processes end users requests to the DBMS.
13
In 3-tier architecture, an additional Presentation or GUI Layer is added, which provides a graphical user
interface for the End user to interact with the DBMS.
For the end user, the GUI layer is the Database System, and the end user has no idea about the
application layer and the DBMS system.
To fully understand the view of data, you must have a basic knowledge of data abstraction and
instance & schema. Refer these two tutorials to learn them in detail.
1. Data abstraction:Database systems are made-up of complex data structures. To ease the
user interaction with database, the developers hide internal irrelevant details from users.
This process of hiding irrelevant details from user is called data abstraction.
2. Instance and schema: Design of a database is called the schema. Schema is of three types:
Physical schema, logical schema and view schema. The data stored in database at a
particular moment of time is called instance of database. Database schema defines the
variable declarations in tables that belong to a particular database; the value of
these variables at a moment of time is called the instance of that database.
For example: When you are booking a train ticket, you are not concerned how data is processing at the
back end when you click “book ticket”, what processes are happening when you are doing online
payments. You are just concerned about the message that pops up when your ticket is successfully
booked. This doesn’t mean that the process happening at the back end is not relevant, it just means that
you as a user are not concerned what is happening in the database.
14
4.
Physical level: This is the lowest level of data abstraction. It describes how data is actually stored in
database. You can get the complex data structure details at this level.
5. Logical level: This is the middle level of 3-level data abstraction architecture. It describes what data is
stored in database.
6. View level: Highest level of data abstraction. This level describes the user interaction with database
system.
7. Example: Let’s say we are storing customer information in a customer table. At physical level these
records can be described as blocks of storage (bytes, gigabytes, terabytes etc.) in memory. These details
are often hidden from the programmers.
8. At the logical level these records can be described as fields and attributes along with their data types,
their relationship among each other can be logically implemented. The programmers generally work at
this level because they are aware of such things about database systems.
9. At view level, user just interact with system with the help of GUI and enter the details at the screen, they
are not aware of how the data is stored and what data is stored; such details are hidden from them.
Database users
Database users are the one who really use and take the benefits of database. There will be
different types of users depending on their need and way of accessing the database.
Application Programmers - They are the developers who interact with the database by
means of DML queries. These DML queries are written in the application programs like C, C++,
JAVA, Pascal [Link] queries are converted into object code to communicate with the
database.
15
For example, writing a C program to generate the report of employees who are working in
particular department will involve a query to fetch the data from database. It will include a
embedded SQL query in the C Program.
Sophisticated Users - They are database developers, who write SQL queries to
select/insert/delete/update data. They do not use any application or programs to request the
database. They directly interact with the database by means of query language like SQL. These
users will be scientists, engineers, analysts who thoroughly study SQL and DBMS to apply the
concepts in their requirement. In short, we can say this category includes designers and
developers of DBMS and SQL.
Specialized Users - These are also sophisticated users, but they write special database
application programs. They are the developers who develop the complex programs to the
requirement.
Stand-alone Users - These users will have stand –alone database for their personal use.
These kinds of database will have readymade database packages which will have menus and
graphical interfaces.
Native Users - these are the users who use the existing application to interact with the
database. For example, online library system, ticket booking systems, ATMs etc. which has
existing application and users use them to interact with the database to fulfil their requests.
database administrator:
A DBA is frequently involved in the initial installation and configuration of a new Oracle,
SQL Server, or other databases. The system administrator configures the database
server’s hardware and implements the operating system, after which the DBA installs
and configures the database software. The DBA is in charge of ongoing maintenance,
such as updates and patches.
DBAs primarily handle the overall integrity of a company’s database. They make sure
that the Data integrity is carefully managed because it protects data from unauthorized
use. DBAs manage data relationships to ensure data consistency.
DBAs are responsible for Data extraction, transformation, and loading, also known as
(ETL), which refers to the efficient import of large amount of data extracted from
multiple systems into a data warehouse environment. The external data is cleaned and
transformed to fit the required format before being imported into a central repository.
4. Monitoring Performance
16
Only implementing a database is not the task of the database administrator. Once the
database is implemented, they are required to monitor databases for performance
issues. If a system component slows down processing, the DBA may need to change the
software configuration or add more hardware capacity. There are numerous monitoring
tools available, and understanding what they need to track to improve the system is part
of the DBA’s job.
5. Data Handling
DBAs create backup and recovery plans and procedures as per the industry standards.
Not only that, but DBAs make certain that all necessary steps are taken. DBAs are
responsible for ensuring that everything is completed on time, in addition to taking the
required precautions to keep data safe.
7. Database Recovery
The DBA’s responsibility in the event of a server failure or other type of data loss is to
restore lost data to the system using existing backups.
Different types of failures may necessitate different recovery strategies, and a DBA
performs his duties while keeping the necessary requirements in mind. Furthermore, as
technology advances, it becomes crucial for a DBA to backup databases to the cloud.
8. Database Security
One of the most critical responsibilities of a DBA is identifying and correcting any flaws in
the database software. No system is entirely secure; however, DBAs mitigate risks by
implementing best practices. A DBA must be able to identify potential flaws in the
database software and the overall system of the company and take appropriate steps to
mitigate risks.
9. Database Integrity
DBAs are primarily responsible for the overall integrity of a company’s database. This
includes putting the database in place, keeping it safe from loss and corruption, making
it easily accessible, ensuring it works properly, and constantly tweaking it for ease of use
and maximum productivity. In addition, the database administrator is also in charge of
training eligible employees on how to access and use the database so that they can
perform their duties.
They also determine which users will have access to the database and which users will
use data. Without the permission of the DBA, no user has the authority to access the
database.
If a user requires assistance at any time, it is the DBA’s responsibility to assist him. The
DBA provides complete support to users who are new to the database.
12. Troubleshooting
In this guide, you will learn about instance and schema in DBMS.
DBMS Schema
Definition of schema: Design of a database is called the schema. For example:
An employee table in database exists with the following attributes:
below.
The design of a database at physical level is called physical schema, how the
data stored in blocks of storage is described at this level.
Design of database at view level is called view schema. This generally describes
end user interaction with database systems.
To learn more about these schemas, refer 3 level data abstraction architecture.
DBMS Instance
Definition of instance: The data stored in database at a particular moment of
time is called instance of database. Database schema defines the attributes in
tables that belong to a particular database. The value of these attributes at a
moment of time is called the instance of that database.
20
For example, we have seen the schema of table “employee” above. Let’s see the
table with the data now. At this moment the table contains two rows (records).
This is the the current instance of the table “employee” because this is the data
that is stored in this table at this particular moment of time.
A database administrator's (DBA) primary job is to ensure that data is available, protected from loss and
corruption, and easily accessible as needed. Below are some of the chief responsibilities that make up the
day-to-day work of a DBA
A DBA often collaborates on the initial installation and configuration of a new Oracle, SQL Server etc
database. The system administrator sets up hardware and deploys the operating system for the database
server, then the DBA installs the database software and configures it for use. As updates and patches are
required, the DBA handles this on-going maintenance.
And if a new server is needed, the DBA handles the transfer of data from the existing system to the new
platform.
This external data is cleaned up and transformed to fit the desired format so that it can be imported into a
central repository.
DBAs create backup and recovery plans and procedures based on industry best practices, then make sure
that the necessary steps are followed. Backups cost time and money, so the DBA may have to persuade
management to take necessary precautions to preserve data.
System admins or other personnel may actually create the backups, but it is the DBA’s responsibility to
make sure that everything is done on schedule.
In the case of a server failure or other form of data loss, the DBA will use existing backups to restore lost
information to the system
5. Security
A DBA needs to know potential weaknesses of the database software and the company’s overall system
and work to minimise risks. No system is one hundred per cent immune to attacks, but implementing best
practices can minimise risks.
In the case of a security breach or irregularity, the DBA can consult audit logs to see who has done what to
the data. Audit trails are also important when working with regulated data.
6. Authentication
Setting up employee access is an important aspect of database security. DBAs control who has access and
what type of access they are allowed. For instance, a user may have permission to see only certain pieces
of information, or they may be denied the ability to make changes to the system.
7. Capacity Planning
The DBA needs to know how large the database currently is and how fast it is growing in order to make
predictions about future needs. Storage refers to how much room the database takes up in server and
backup space. Capacity refers to usage level.
If the company is growing quickly and adding many new users, the DBA will have to create the capacity to
handle the extra workload.
8. Performance Monitoring
Monitoring databases for performance issues is part of the on-going system maintenance a DBA performs.
If some part of the system is slowing down processing, the DBA may need to make configuration changes
to the software or add additional hardware capacity
Chapter-2
Data Model
Data Model gives us an idea that how the final system will look like after its complete implementation.
It defines the data elements and the relationships between the data elements. Data Models are used to
show how data is stored, connected, accessed and updated in the database management system. Here,
we use a set of symbols and text to represent the information so that members of the organisation can
communicate and understand it. Though there are many data models being used nowadays but the
22
Relational model is the most widely used model. Apart from the Relational model, there are many other
types of data models about which we will study in details in this blog. Some of the Data Models in
DBMS are:
1. Hierarchical Model
2. Network Model
3. Entity-Relationship Model
4. Relational Model
5. Object-Oriented Data Model
6. Object-Relational Data Model
7. Flat Data Model
8. Semi-Structured Data Model
9. Associative Data Model
10. Context Data Model
Hierarchical Model
Hierarchical Model was the first DBMS model. This model organises the data in the hierarchical tree
structure. The hierarchy starts from the root which has root data and then it expands in the form of a
tree adding child node to the parent node. This model easily represents some of the real-world
relationships like food recipes, sitemap of a website etc. Example: We can represent the relationship
between the shoes present on a shopping website in the following way:
1. One-to-many relationship: The data here is organised in a tree-like structure where the one-
to-many relationship is between the datatypes. Also, there can be only one path from parent to
any node. Example: In the above example, if we want to go to the node sneakers we only have
one path to reach there i.e through men's shoes node.
23
2. Parent-Child Relationship: Each child node has a parent node but a parent node can have
more than one child node. Multiple parents are not allowed.
3. Deletion Problem: If a parent node is deleted then the child node is automatically deleted.
4. Pointers: Pointers are used to link the parent node with the child node and are used to navigate
between the stored data. Example: In the above example the 'shoes' node points to the two other
nodes 'women shoes' node and 'men's shoes' node.
Advantages of Hierarchical Model
Network Model
This model is an extension of the hierarchical model. It was the most popular model before the
relational model. This model is the same as the hierarchical model, the only difference is that a record
can have more than one parent. It replaces the hierarchical tree with a graph. Example: In the
example below we can see that node student has two parents i.e. CSE Department and Library. This
was earlier not possible in the hierarchical model.
1. Ability to Merge more Relationships: In this model, as there are more relationships so data
is more related. This model has the ability to manage one-to-one relationships as well as many-
to-many relationships.
2. Many paths: As there are more relationships so there can be more than one path to the same
record. This makes data access fast and simple.
3. Circular Linked List: The operations on the network model are done with the help of the
circular linked list. The current position is maintained with the help of a program and this
position navigates through the records according to the relationship.
Advantages of Network Model
The data can be accessed faster as compared to the hierarchical model. This is because the data
is more related in the network model and there can be more than one path to reach a particular
node. So the data can be accessed in many ways.
As there is a parent-child relationship so data integrity is present. Any change in parent record
is reflected in the child record.
Disadvantages of Network Model
As more and more relationships need to be handled the system might get complex. So, a user
must be having detailed knowledge of the model to work with the model.
Any change like updation, deletion, insertion is very complex.
Entity-Relationship Model
Entity-Relationship Model or simply ER Model is a high-level data model diagram. In this model, we
represent the real-world problem in the pictorial form to make it easy for the stakeholders to
understand. It is also very easy for the developers to understand the system by just looking at the ER
diagram. We use the ER diagram as a visual tool to represent an ER Model. ER diagram has the
following three components:
Example:
25
In the above diagram, the entities are Teacher and Department. The attributes of Teacher entity are
Teacher_Name, Teacher_id, Age, Salary, Mobile_Number. The attributes of entity Department entity
are Dept_id, Dept_name. The two entities are connected using the relationship. Here, each teacher
works for a department.
Features of ER Model
Graphical Representation for Better Understanding: It is very easy and simple to
understand so it can be used by the developers to communicate with the stakeholders.
ER Diagram: ER diagram is used as a visual tool for representing the model.
Database Design: This model helps the database designers to build the database and is widely
used in database design.
Advantages of ER Model
Simple: Conceptually ER Model is very easy to build. If we know the relationship between the
attributes and the entities we can easily build the ER Diagram for the model.
Effective Communication Tool: This model is used widely by the database designers for
communicating their ideas.
Easy Conversion to any Model: This model maps well to the relational model and can be easily
converted relational model by converting the ER model to the table. This model can also be
converted to any other model like network model, hierarchical model etc.
Disadvatages of ER Model
Data constitute the most basic information units employed by a system. Applications are created to
manage data and to help transform data into information.
But data are viewed in different ways by different people. So that there is a huge importance of data
modeling in DBMS.
For example, contrast the (data) view of a company manager with that of a company clerk. Although the
manager and the clerk both work for the same company, the manager is more likely to have an enterprise-
wide view of company data than the clerk.
Applications programmers have yet another view of data, being more concerned with data location,
formatting, and specific reporting requirements.
Basically, applications programmers translate company policies and procedures from a variety of sources
into appropriate interfaces, reports, and query screens.
When a good database blueprint is available, it does not matter that an applications programmer’s view of
the data is different from that of the manager and/or the end user. Conversely, when a good database
blueprint is not available, problems are likely to occur.
For instance, an inventory management program and an order entry system may use conflicting product-
numbering schemes, thereby costing the company thousands (or even millions) of dollars. The data model
is an abstraction; you cannot draw the required data out of the data model.
1. Conceptual design
When every data requirement is stored and analyzed, the next thing that we need to do is creating a conceptual
database plan. Here, a highly leveled conceptual data model is used. This phase is called conceptual
design.
When the conceptual design phase is in progress, the basic data modeling operations can be deployed to define
the high-level user operations that are noted during analysis of the functions.
2. Logical Design
The logical phase of database design is also called the data modeling mapping phase. This phase
gives us a result of relation schemas. The basis for these schemas is the ER or the Class Diagram.
To create the relation schemas is mainly mechanical operation. There are rules for transferring the ER model or
class diagram to relation schemas.
3. Normalization
Normalization is, in fact, the last piece of the logical design puzzle. The main purpose of normalization is to
remove superfluity and every other potential anomaly during the update.
Normalization in database design is a way to change the relation schema to reduce any superfluity. With every
normalization phase, a new table is added to the database.
4. Physical Design
The last phase of database design is the physical design phase. In this phase, we implement the database
design. Here, a DBMS (Database Management System) must be chosen to use.
28
For instance, different DBM systems have different names for every datatype and they have different data
types.
SQL clauses are written to help in creating the database. Also, the indexes and the integrity constraints (rules)
are defined in this phase. And finally the data is added and the database can finally be tested.
This phase consists of three parts: the conceptual design, the logical design and the physical design. Some
methodologies merge the logical design phase into the other two phases. This section is not aimed at being a
definitive discussion of database design methodologies (there are whole books written on that!); rather it aims to
introduce you to the topic.
Conceptual design
The purpose of the conceptual design phase is to build a conceptual model based upon the previously identified
requirements, but closer to the final physical model. A commonly-used conceptual model is called an entity-
relationship model.
The following are examples of entities involving a university with possible attributes in parentheses.
An instance of an entity is one particular occurrence of that entity. For example, the student Rudolf Sono is one
instance of the student entity. There will probably be many instances. If there is only one instance, consider whether
the entity is warranted. The top level usually does not warrant an entity. For example, if the system is being
developed for a particular university, university will not be an entity because the whole system is for that one
university. However, if the system was developed to track legislation at all universities in the country,
then university would be a valid entity.
Relationships
29
Entities are related in certain ways. For example, a borrower may belong to a library and can take out books. A book
can be found in a particular library. Understanding what you are storing data about, and how the data relate, leads
you a large part of the way to a physical implementation in the database.
Mandatory
For each instance of entity A, there must exist one or more instances of entity B. This does not necessarily mean
that for each instance of entity B, there must exist one or more instances of entity A. Relationships are optional or
mandatory in one direction only, so the A-to-B relationship can be optional, while the B-to-A relationship is
mandatory.
Optional
For each instance of entity A, there may or may not exist instances of entity B.
One-to-one (1:1)
This is where for each instance of entity A, there exists one instance of entity B, and vice-versa. If the relationship is
optional, there can exist zero or one instances, and if the relationship is mandatory, there exists one and only one
instance of the associated entity.
One-to-many (1:M)
For each instance of entity A, many instances of entity B can exist, which for each instance of entity B, only one
instance of entity A exists. Again, these can be optional or mandatory relationships.
Many-to-many (M:N)
For each instance of entity A, many instances of entity B can exist, and vice versa. These can be optional or
mandatory relationships.
There are numerous ways of showing these relationships. The image below shows student and course entities. In
this case, each student must have registered for at least one course, but a course does not necessarily have to
have students registered. The student-to-course relationship is mandatory, and the course-to-student relationship is
optional.
The image below shows invoice_line and product entities. Each invoice line must have at least one product (but no
more than one); however each product can appear on many invoice lines, or none at all. The invoice_line-to-
product relationship is mandatory, while the product-to-invoice_line relationship is optional.
30
The figure below shows husband and wife entities. In this system (others are of course possible), each husband
must have one and only one wife, and each wife must have one, and only one, husband. Both relationships are
mandatory.
An entity can also have a relationship with itself. Such an entity is called a recursive entity. Take a person entity. If
you're interested in storing data about which people are brothers, you wlll have an "is brother to" relationship. In this
case, the relationship is an M:N relationship.
Conversely, a weak entity is an entity that cannot exist without another entity. For example, in a school,
the scholar entity is related to the weak entity parent/guardian. Without the scholar, the parent or guardian cannot
exist in the system. Weak entities usually derive their primary key, in part or in totality, from the associated
entity. parent/guardian could take the primary key from the scholar table as part of its primary key (or the entire key
if the system only stored one parent/guardian per scholar).
The term cardinality refers to the specific number of instances possible for a relationship. Cardinality limits list the
minimum and maximum possible occurrences of the associated entity. In the husband and wife example, the
cardinality limit is (1,1), and in the case of a student who can take between one and eight courses, the cardinality
limits would be represented as (1,8).
The first step in developing the diagram is to identify all the entities in the system. In the initial stage, it is not
necessary to identify the attributes, but this may help to clarify matters if the designer is unsure about some of the
31
entities. Once the entities are listed, relationships between these entities are identified and modeled according to
their type: one-to-many, optional and so on. There are many software packages that can assist in drawing an entity-
relationship diagram, but any graphical package should suffice.
Once the initial entity-relationship diagram has been drawn, it is often shown to the stakeholders. Entity-relationship
diagrams are easy for non-technical people to understand, especially when guided through the process. This can
help identify any errors that have crept in. Part of the reason for modeling is that models are much easier to
understand than pages of text, and they are much more likely to be viewed by stakeholders, which reduces the
chances of errors slipping through to the next stage, when they may be more difficult to fix.
Once the diagram has been approved, the next stage is to replace many-to-many relationships with two one-to-
many relationships. A DBMS cannot directly implement many-to-many relationships, so they are decomposed into
two smaller relationships. To achieve this, you have to create an intersection, or composite entity type. Because
intersection entities are less "real-world" than ordinary entities, they are sometimes difficult to name. In this case,
you can name them according to the two entities being intersected. For example, you can intersect the many-to-
many relationship between student and course by a student-course entity.
The same applies even if the entity is recursive. The person entity that has an M:N relationship "is brother to" also
needs an intersection entity. You can come up with a good name for the intersection entity in this case: brother. This
entity would contain two fields, one for each person of the brother relationship — in other words, the primary key of
the first brother and the primary key of the other brother.
Notation of ER diagram
32
Database can be represented using the notations. In ER diagram, many notations are
used to express the cardinality. These notations are as follows:
Chapter-3
Relational Model
Relational Model is the most widely used model. In this model, the data is maintained in the form of a
two-dimensional table. All the information is stored in the form of row and columns. The basic structure
of a relational model is [Link] tables are also called relations in the relational model. Example: In
this example, we have an Employee table.
In the above example, we have two objects Employee and Department. All the data and
relationships of each object are contained as a single unit. The attributes like Name, Job_title of the
employee and the methods which will be performed by that object are stored as a single object. The
two objects are connected through a common attribute i.e the Department_id and the communication
between these two will be done with the help of this common id.
Database design is a framework that the database uses for planning, storing and managing data in companies and organizations. Data
and database design are the lifeblood of every company.
We can say that the consistency of a data is achieved when the database is designed in such a way so it can store only useful and often
most required data.
In this article, we will explain you the main phases that create database design and their roles in the design.
Relational data model is the primary data model, which is used widely around the world for data storage and
processing. This model is simple and it has all the properties and capabilities required to process data with storage
efficiency.
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores the relation among
entities. A table has rows and columns, where rows represents records and columns represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation instance. Relation
instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and their names.
Relation key − Each row has one or more attributes, known as relation key, which can identify the row in the relation
(table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute domain.
35
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These conditions are called Relational
Integrity Constraints. There are three main integrity constraints −
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.
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.
1. Each relation in a database must have a distinct or unique name which would separate it from the other relations in
a database.
2. A relation must not have two attributes with the same name. Each attribute must have a distinct name.
4. Each tuple must have exactly one data value for an attribute. For example, below in the first table, you can see that
for Roll_No. 265 we have enrolled two students Jhoson and Charles, this would not work. We must have only one student
for one Roll_No.
5. Tuples in a relation do not have to follow a significant order as the relation is not order-sensitive.
6. Similarly, the attributes of a relation also do not have to follow certain ordering, it’s up to the developer to decide the
ordering of attributes.
Inherent Model-Based Constraints: The constraints that are implicit in a data model are inherent model-based
constraints. For example, a relation in a database must not have duplicate tuples, there is no constraint in the ordering of
the tuples and attributes.
37
Schema-Based Constraints: The constraints that are specified while defining the schema of a database using DDL are
schema-based constraints. They are further categorized as domain constraints, key constraints, entity integrity
constraints, referential integrity constraints and constraints on Null Value.
Application-based Constraints: The constraints that cannot be applied while defining the database schema are
expressed in application programs. For example, the salary of an employee cannot be more than his supervisor.
Domain Constraints:
Each attribute in a tuple is declared to be of a particular domain (for example, integer, character, Boolean, String, etc.)
which specifies a constraint on the values that an attribute can take.
Key Constraint and Constraint on Null Values:
In relation, a key can either be a single attribute or a subgroup of attributes that can recognize a particular tuple in a
relation. Now, the key constraint specifies that a key (attribute/subset of attribute) must not have the same set of values
for the tuples in a relation.
The constraint on NULL values defines whether an attribute is allowed to carry Null value or not. For example, in a
student tuple, its name attribute must be NOT NULL.
Entity Integrity Constraint:
Entity integrity constraint specifies that a primary key of a tuple can never be NULL. As primary key used to identify
individual tuple in a relation.
Referential Integrity Constraint:
The referential integrity constraint holds if the foreign key of relation R1 that refers to the relation R2 satisfies following
two conditions:
1. The set of attributes that form foreign key of relation R1 should have the same domain as the primary key of the
referenced relation R2.
2. In the current state, the set of values of the foreign key in tuple t1 of relation R1 must match a primary key value in
referenced relation R2 or it could be NULL.
Disadvantages:
Taking an account of the advantages, the disadvantages are negligible.
Key Takeaways:
Relational data model implements the database schema of the relational database.
The relational model is also termed as a record-based model as it stores the data in fixed-format records (tuples) of
various types.
A relation is a table whose columns indicates the attributes and rows indicates the tuples/entities/records.
Many relations together form a relational database.
The relational model has some constraints on the database schema and data values in the database which we have
discussed in the content.
So, this is all about the relational data model. Today it is widely used to design the database systems. Majority of
database system today are constructed using the relational data model.
Advantages
The primary benefit of the relational database approach is the ability to create meaningful information by joining
thetables. Joining tables allows you to understand the relationships between the data, or how the tables connect. SQL
includes the ability to count, add, group, and also combine queries
38
1. Speed
Even though a relational database is poor in terms of performance, still its speed is considerably higher
because of its ease and simplicity. And also various optimizations that is included in a relational database
further increases its speed. So all the applications will run with appropriate speed when used in a relational
database.
2. Security
Since there are several tables in a relational database, certain tables can be made to be confidential.
These tables are protected with username and password such that only authorized users will be able to
access them. The users are only allowed to work on that specific table.
3. Simplicity
Compared to other types of network models, a relational database model is much simpler. It is free from
query processing and complex structuring. As a result, it does not require any complex queries. A simple
SQL query is sufficient enough for handling.
4. Accessibility
Unlike other types of databases, a relational database does not require any specific path for accessing the
data. Even modifying data in the relevant column is made easy. So whatever the outcome shown is
appropriate to the user.
5. Accuracy
As mentioned earlier, relational database uses primary keys and foreign keys to make the tables
interrelated to each other. Thus, all the data which is stored is non-repetitive. Which means that the data
does not duplicate. Therefore, the data stored can be guaranteed to be accurate.
6. Multi User
Multiple users will be able to access a relational database at the same time. Even if the data is updated,
the users can access them conveniently. Hence, the crashes happening from multi access is possibly
prevented.
1. Cost
The underlaying cost involved in a relational database is quite expensive. For setting up a relational
database, there must be separate software which needs to be purchased. And a professional technician
should be hired to maintain the system. All these can be costly, especially for businesses with small
budget.
2. Performance
Always the performance of the relational database depends on the number of tables. If there are more
number of tables, the response given to the queries will be slower. Additionally, more data presence not
39
only slows down the machine, it eventually makes it complex to find information. Thus, a relational
database is known to be a slower database.
3. Physical Storage
A relational database also requires tremendous amount of physical memory since it is with rows and
columns. Each of the operations depend on separate physical storage. Only through proper optimization,
the targeted applications can be made to have maximum physical memory.
4. Complexity
Although a relational database is free from complex structuring, occasionally it may become complex too.
When the amount of data in a relational database increases, it eventually makes the system more
complicated. Each and every data is been complex since the data is arranged using common
characteristics.
5. Information Loss
Large organizations tends to use more number of number of database systems with more tables. These
information can be used to be transferred from one system to another. This could pose a risk of data loss.
6. Structure Limitations
The fields that is present on a relational database is with limitations. Limitations in essence means that it
cannot accommodate more information. Despite if more information are provided, it may lead to data
loss. Therefore, it is necessary to describe the exact amount of data volume which the field will be given.
Chapter-4
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.
1. X → Y
The left side of FD is known as a determinant, the right side of the production is known
as a dependent.
For example:
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.
1. Emp_Id → Emp_Name
Example:
Example:
1. ID → Name,
2. Name → DOB
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 the undesirable characteristics like Insertion, Update and Deletion
Anomalies.
o Normalization divides the larger table into the smaller table and links them using
relationship.
o The normal form is used to reduce redundancy from the database table.
42
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
43
14 John 9064738238 UP
12 Sam 8589830302
Example: Let's assume, a school can store the data of teachers and the subjects they
teach. In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
22.7M
429
Prime Ministers of India | List of Prime Minister of India (1947-2020)
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
44
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
A relation is in third normal form if it holds atleast one of the following conditions for
every non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Chapter-5
46
MySQL
MySQL, the most popular Open Source SQL database management system, is developed, distributed, and
supported by Oracle Corporation.
The MySQL website ([Link] provides the latest information about MySQL software.
MySQL is a database management system.
A database is a structured collection of data. It may be anything from a simple shopping list to a picture
gallery or the vast amounts of information in a corporate network. To add, access, and process data
stored in a computer database, you need a database management system such as MySQL Server.
Since computers are very good at handling large amounts of data, database management systems
play a central role in computing, as standalone utilities, or as parts of other applications.
MySQL Server was originally developed to handle large databases much faster than existing solutions
and has been successfully used in highly demanding production environments for several years.
Although under constant development, MySQL Server today offers a rich and useful set of functions. Its
connectivity, speed, and security make MySQL Server highly suited for accessing databases on the
Internet.
We also provide MySQL Server as an embedded multithreaded library that you can link into your
application to get a smaller, faster, easier-to-manage standalone product.
The official way to pronounce “MySQL” is “My Ess Que Ell” (not “my sequel”), but we do not mind if you
pronounce it as “my sequel” or in some other localized way.
Designed to be fully multithreaded using kernel threads, to easily use multiple CPUs if they are
available.
Uses very fast B-tree disk tables (MyISAM) with index compression.
Designed to make it relatively easy to add other storage engines. This is useful if you want to provide
an SQL interface for an in-house database.
Implements SQL functions using a highly optimized class library that should be as fast as possible.
Usually there is no memory allocation at all after query initialization.
Provides the server as a separate program for use in a client/server networked environment.
Data Types
Many data types: signed/unsigned integers 1, 2, 3, 4, and 8 bytes
long, FLOAT, DOUBLE, CHAR, VARCHAR, BINARY, VARBINARY, TEXT, BLOB, DATE, TIME, DATETIME, TIME
STAMP, YEAR, SET, ENUM, and OpenGIS spatial types
Fixed-length and variable-length string types.
This section describes how MySQL relates to the ANSI/ISO SQL standards. MySQL Server has
many extensions to the SQL standard, and here you can find out what they are and how to use
them. You can also find information about functionality missing from MySQL Server, and how to
work around some of the differences.
The SQL standard has been evolving since 1986 and several versions exist. In this manual, “SQL-
92” refers to the standard released in 1992, “SQL:1999” refers to the standard released in 1999,
and “SQL:2003” refers to the current version of the standard. We use the phrase “the SQL
standard” or “standard SQL” to mean the current version of the SQL Standard at any time.
One of our main goals with the product is to continue to work toward compliance with the SQL
standard, but without sacrificing speed or reliability. We are not afraid to add extensions to SQL
or support for non-SQL features if this greatly increases the usability of MySQL Server for a large
segment of our user base. The HANDLER interface is an example of this strategy
We continue to support transactional and non-transactional databases to satisfy both mission-
critical 24/7 usage and heavy Web or logging usage.
MySQL Server was originally designed to work with medium-sized databases (10-100 million
rows, or about 100MB per table) on small computer systems. Today MySQL Server handles
terabyte-sized databases, but the code can also be compiled in a reduced version suitable for
hand-held and embedded devices. The compact design of the MySQL server makes development
in both directions possible without any conflicts in the source tree.
Currently, we are not targeting real-time support, although MySQL replication capabilities offer
significant functionality.
Chapter-6&7
49
MySQL Queries
A list of commonly used MySQL queries to create database, use database, create table,
insert record, update record, delete record, select record, truncate table and drop table
are given below.
use db1;
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
51
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
Chapter-8
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND Example
The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city is "Berlin":
OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"Stuttgart":
Example
SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'Stuttgart';
NOT Example
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":
Example
53
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Demo Database
The table below shows the complete "Customers" table from the Northwind sample
database:
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
The following SQL statement selects all customers that are NOT located in "Germany",
"France" or "UK":
Example
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
1 Chais 1 1 1
2 Chang 1 1 2
3 Aniseed Syrup 1 2 1
BETWEEN Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
There are two wildcards often used in conjunction with the LIKE operator:
LIKE Syntax
56
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:
INNER JOIN
LEFT JOIN
Consider the two tables below:
Student
StudentCourse
57
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
2. LEFT JOIN: This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no
matching row on right side, the result-set will contain null. LEFT JOIN is also known as
LEFT OUTER [Link]:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Chapter-9
Count():
Sum():
sum(salary): Sum all Non Null values of Column salary i.e., 310
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
61
Avg():
Min():
Min(salary): Minimum value in the salary column except NULL i.e., 40.
Max(salary): Maximum value in the salary i.e., 80.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
The following SQL statement lists the number of customers in each country, sorted high to low:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
Demo Database
Below is a selection from the "Orders" table in the Northwind sample database:
10248 90 5 1996-07-04
10249 81 6 1996-07-05
63
10250 34 4 1996-07-08
ShipperID ShipperName
1 Speedy Express
2 United Package
3 Federal Shipping
SELECT (CustomerID),
Country
FROM Customers
;
64
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
The following SQL statement lists the number of customers in each country, sorted high to
low (Only include countries with more than 5 customers):
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Note: The column names in the result-set are usually equal to the column names in the
first SELECT statement.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New O
Description
The SQL MINUS operator is used to return all rows in the first SELECT statement that are not
returned by the second SELECT statement. Each SELECT statement will define a dataset. The
MINUS operator will retrieve all records from the first dataset and then remove from the results all
records from the second dataset.
Minus Query
Explanation: The MINUS query will return the records in the blue shaded area. These are the
records that exist in Dataset1 and not in Dataset2.
Each SELECT statement within the MINUS query must have the same number of fields in the result
sets with similar data types.
TIP: The MINUS operator is not supported in all SQL databases. It can used in databases such as
Oracle.
For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT operator to perform
this type of query.
68
Syntax
The syntax for the MINUS operator in SQL is:
Parameters or Arguments
expression1, expression2, expression_n
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the
FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.
Note
There must be same number of expressions in both SELECT statements.
The corresponding expressions must have the same data type in the SELECT statements. For
example: expression1 must be the same data type in both the first and second SELECT
statement.
SELECT supplier_id
FROM suppliers
MINUS
SELECT supplier_id
FROM orders;
This SQL MINUS example returns all supplier_id values that are in the suppliers table and not in the
orders table. What this means is that if a supplier_id value existed in the suppliers table and also
existed in the orders table, the supplier_id value would not appear in this result set.
69
Description
The SQL INTERSECT operator is used to return the results of 2 or more SELECT statements.
However, it only returns the rows selected by all queries or data sets. If a record exists in one query
and not in the other, it will be omitted from the INTERSECT results.
Intersect Query
Explanation: The INTERSECT query will return the records in the blue shaded area. These are the
records that exist in both Dataset1 and Dataset2.
Each SQL statement within the SQL INTERSECT must have the same number of fields in the result
sets with similar data types.
Syntax
The syntax for the INTERSECT operator in SQL is:
Parameters or Arguments
expression1, expression2, expression_n
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the
FROM clause.
WHERE conditions
70
Optional. These are conditions that must be met for the records to be selected.
Note
There must be same number of expressions in both SELECT statements.
The corresponding expressions must have the same data type in the SELECT statements. For
example: expression1 must be the same data type in both the first and second SELECT
statement.
SELECT supplier_id
FROM suppliers
INTERSECT
SELECT supplier_id
FROM orders;
In this SQL INTERSECT example, if a supplier_id appeared in both the suppliers and orders table, it
would appear in your result set.
Now, let's complicate our example further by adding WHERE conditions to the INTERSECT query.
SELECT supplier_id
FROM suppliers
WHERE supplier_id > 78
INTERSECT
SELECT supplier_id
FROM orders
WHERE quantity <> 0;
In this example, the WHERE clauses have been added to each of the datasets. The first dataset has
been filtered so that only records from the suppliers table where the supplier_id is greater than 78 are
returned. The second dataset has been filtered so that only records from the orders table are
returned where the quantity is not equal to 0.
In this INTERSECT example, the query will return the records from the contacts table where
the contact_id, last_name, and first_name values match the customer_id, last_name,
and first_name value from the customers table.
There are WHERE conditions on each data set to further filter the results so that only records from
the contacts are returned where the last_name is not Anderson. The records from
the customers table are returned where the customer_id is less than 50.
72
Chapter-10
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
1 Chais 1 1
2 Chang 1 1
3 Aniseed Syrup 1 2
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New
The following SQL statement returns TRUE and lists the suppliers with a product price equal
to 22:
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE [Link] =
[Link] AND Price = 22);
Output:
74
SupplierName
The NOT EXISTS in SQL Server will check the Subquery for rows existence, and if there are no rows then it
will return TRUE, otherwise FALSE. Or we can simply say, SQL Server Not Exists operator will return the
results exactly opposite to the result returned by the Subquery.
Before going into this example, I suggest you to refer the SQL Subquery article to understand the subquery
designing and query parsing.
FROM [Source]
Columns: It allows us to choose the number of columns from the tables. It may be One or more.
Source: One or more tables present in the Database. SQL JOINS are used to join multiple tables.
Subquery: Here we have to provide the Subquery. If the subquery returns true then it will return
the records otherwise, it doesn’t return any records.
In this article, we will show you, How to use the SQL Server NOT EXISTS Operator
with examples. For this, We are going to use the below-shown data
75
GO
SELECT Employ1.[EmpID]
,Employ1.[Education]
,Employ1.[Occupation]
,Employ1.[YearlyIncome]
,Employ1.[Sales]
,Employ1.[HireDate]
76
OUTPUT
77
Let me change the Not Exists condition as Sales < 10000, it means subquery will
return all the available rows. And the NOT EXISTS will return zero records because
it will return the exact opposite result of the subquery.
-- SQL Server NOT EXISTS Example
GO
SELECT Employ1.[EmpID]
,Employ1.[Education]
,Employ1.[Occupation]
,Employ1.[YearlyIncome]
,Employ1.[Sales]
,Employ1.[HireDate]
OUTPUT
78
This query shows the details of a teacher from the Teacher_Info table. Here, the teacher
is also the head of the department from the Department_Info table.
The output of the above SELECT query with Equal operatoris shown in the
below table:
The following query uses ANY operator with less than operator and GROUP BY
clause:
79
1. SELECT * FROM Teacher_Info WHERE Teacher_Salary < ANY (SELECT AVG ( Tea
cher_Salary ) from Teacher_Info GROUP BY Teacher_Dept_Id );
This query shows the details of all teachers whose salaries are less than the average
salary of every department.
The output of the above SELECT query with less than operator is shown in the
below table:
Any in SQL
The ALL is an operator in SQL. This operator compares the single record to every record of the
the inner query.
In the ALL syntax, the ALL operator is followed by the SQL comparison operator, which helps com
We can use the following comparison operators with the ALL operator in the statements of SQL:
This comparison operator with ALL operator evaluates to TRUE when the value of specified colum
Syntax:
This comparison operator with the ALL operator evaluates to TRUE when the value of the specifi
Syntax:
This comparison operator with the ALL operator evaluates to TRUE when the value of the specifi
Syntax:
This comparison operator with the ALL operator evaluates to TRUE when the value of the specifi
Syntax:
This comparison operator with the ALL operator evaluates to TRUE when the value of the specifi
Syntax:
This comparison operator with the ALL operator evaluates to TRUE when the value of the specifi
Syntax:
If you want to perform the 'ALL' operator in the tables of SQL, then you have to follow the below
Now, we are going to explain the steps in detail with a SQL example:
81
The following query creates the Faculty_Info table in the University Database:
The following query creates the Department_Info table in the University Database:
2.
The following query uses the ALL operator with an Equal comparison operator:
1. SELECT * FROM Faculty_Info WHERE Faculty_Id = ALL (SELECT Head_Id from Departme
This query shows the details of that Faculty from the Faculty_Info table who is also the head of t
The output of the above SELECT query with Equal operator is shown in the below table:
The following query uses ALL operator with less than operator and GROUP BY clause:
1. SELECT * FROM Faculty_Info WHERE Faculty_Salary < ALL (SELECT AVG ( Faculty_Salar
Firstly, you have to make a new database in Structured Query Language. So, Let's start.
The following query creates the new University Database in SQL server:
This query shows the details of all Faculties whose salaries are less than the average salary of e
The output of the above SELECT query with less than operator is shown in the below
1. SELECT * FROM Faculty_Info WHERE Faculty_Salary < ALL (SELECT AVG ( Faculty_Salary ) from Faculty_Info G
The following query uses ALL operator with greater than operator and GROUP BY clau
SELECT * FROM Faculty_Info WHERE Faculty_Salary > ALL (SELECT AVG ( Faculty_Salary ) from F
This query shows the details of all Faculties whose salaries are greater than the average salary o
The output of the above SELECT query with the Greater Than operator is shown in the
table:
SOME operator evaluates the condition between the outer and inner tables and
evaluates to true if the final result returns any one row. If not, then it evaluates
to false.
The SOME and ANY comparison conditions are similar to each other and
are completely interchangeable.
SOME must match at least one row in the subquery and must be
preceded by comparison operators.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE expression comparison_operator SOME (subquery)
nstructor Table:
Name Department Salary
Samantha
Debarka
Explanation
The instructors with salary > (salary of some instructor in the ‘Computer
Science’ department) get returned. The salaries in the ‘Computer Science’
department are 1.3, 2 and 2. This implies any instructor with a salary greater
than 1.3 can be included in the final result.
SOME operator evaluates the condition between the outer and inner tables and
evaluates to true if the final result returns any one row. If not, then it evaluates
to false.
86
The SOME and ANY comparison conditions are similar to each other and
are completely interchangeable.
SOME must match at least one row in the subquery and must be
preceded by comparison operators.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE expression comparison_operator SOME (subquery)
nstructor Table:
Name Department Salary
Samantha
Debarka
87
Explanation
The instructors with salary > (salary of some instructor in the ‘Computer
Science’ department) get returned. The salaries in the ‘Computer Science’
department are 1.3, 2 and 2. This implies any instructor with a salary greater
than 1.3 can be included in the final result.
Week-11
Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in the
database.
88
o A view can either have specific rows based on certain condition or all the rows of a
table.
Advantages of View:
1. Complexity: Views help to reduce the complexity. Different views can be created
on the same base table for different users.
2. Security: It increases the security by excluding the sensitive information from the
view.
3. Query Simplicity: It helps to simplify commands from the user. A view can draw
data from several different tables and present it as a single table.
4. Consistency: A view can present a consistent, unchanged image of the structure
of the database. Views can be used to rename the columns without affecting the
base table.
5. Data Integrity: If data is accessed and entered through a view, the DBMS can
automatically check the data to ensure that it meets the specified integrity
constraints.
6. Storage Capacity: Views take very little space to store the data.
7. Logical Data Independence: View can make the application and database
tables to a certain extent independent.
Disadvantages of View:
The DML statements which can be performed on a view created using single base table
have certain restrictions are:
1. You cannot INSERT if the base table has any not null column that do not appear in
view.
2. You cannot INSERT or UPDATE if any of the column referenced in the INSERT or
UPDATE contains group functions or columns defined by expression.
3. You can't execute INSERT, UPDATE, DELETE statements on a view if with read only
option is enabled.
4. You can't be created view on temporary tables.
5. You cannot INSERT, UPDATE, DELETE if the view contains group functions GROUP
BY, DISTINCT or a reference to a psuedocolumn rownum.
6. You can't pass parameters to the SQL server views.
7. You can't associate rules and defaults with views.
Sample table:
89
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from a
single table or multiple tables.
Syntax:
Query:
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
91
Example:
Significance of Views:
Views are highly significant, as they can provide advantages over tasks. Views can
represent a subset of data contained in a table. Consequently they can limit the degree
of exposure of the underlying base table to the outer world. They are used for security
purpose in database and act as an intermediate between real table schemas and
programmability. They act as aggregate tables.
DCL Commands
The Data Definition Language (DCL) commands are as follows −
GRANT Command
Syntax
Here,
REVOKE Command
It is employed to remove a privilege from a user. REVOKE helps the owner to cancel
previously granted permissions.
Syntax
Here,
92
Example
GRANT SELECT, UPDATE ON employees TO Bhanu
Explanation − Firstly, to give the permissions to user, we have to use GRANT command. The
privileges are SELECT because to view the records and UPDATE to modify the records.
The objectname is table name which is Employee. The user name is "bhanu".
Explanation − Firstly, to revoke the permissions to user, we have to use REVOKE command.
The privileges Need to revoke are SELECT because to view the records and UPDATE to
modify the records. The objectname is table name which is Employee. The user name is
"Bhanu".
Chapter-12
93
PL/SQL
PL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed
by Oracle Corporation in the early 90's to enhance the capabilities of SQL. PL/SQL is one of three key programming
languages embedded in the Oracle Database, along with SQL itself and Java. This tutorial will give you great
understanding on PL/SQL to proceed with Oracle database and other advanced RDBMS concepts.
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.
Features of PL/SQL
Step 1
Let us now launch the Oracle Database Installer using the setup file. Following is the first screen. You can provide your
email ID and check the checkbox as shown in the following screenshot. Click the Next button.
Step 2
You will be directed to the following screen; uncheck the checkbox and click the Continue button to proceed.
Step 3
Just select the first option Create and Configure Database using the radio button and click the Next button to proceed.
95
Step 4
We assume you are installing Oracle for the basic purpose of learning and that you are installing it on your PC or Laptop.
Thus, select the Desktop Class option and click the Next button to proceed.
Step 5
96
Provide a location, where you will install the Oracle Server. Just modify the Oracle Base and the other locations will set
automatically. You will also have to provide a password; this will be used by the system DBA. Once you provide the
required information, click the Next button to proceed.
Step 6
Again, click the Next button to proceed.
97
Step 7
Click the Finish button to proceed; this will start the actual server installation.
Step 8
This will take a few moments, until Oracle starts performing the required configuration.
98
Step 9
Here, Oracle installation will copy the required configuration files. This should take a moment −
Step 10Once the database files are copied, you will have the following dialogue box. Just click the OK button and come
out.
Step 11
99
Final Step
It is now time to verify your installation. At the command prompt, use the following command if you are using Windows −
sqlplus "/ as sysdba"
You should have the SQL prompt where you will write your PL/SQL commands and scripts −
Text Editor
Running large programs from the command prompt may land you in inadvertently losing some of the work. It is always
recommended to use the command files. To use the command files −
Type your code in a text editor, like Notepad, Notepad+, or EditPlus, etc.
Save the file with the .sql extension in the home directory.
100
Launch the SQL*Plus command prompt from the directory where you created your PL/SQL file.
Type @file_name at the SQL*Plus command prompt to execute your program.
If you are not using a file to execute the PL/SQL scripts, then simply copy your PL/SQL code and right-click on the black
window that displays the SQL prompt; use the paste option to paste the complete code at the command prompt. Finally,
just press Enter to execute the code, if it is not already executed.
In this chapter, we will discuss the Basic Syntax of PL/SQL which is a block-structured language; this means that the
PL/SQL programs are divided and written in logical blocks of code. Each block consists of three sub-parts −
Declarations
1
This section starts with the keyword DECLARE. It is an optional section and defines all variables, cursors,
subprograms, and other elements to be used in the program.
Executable Commands
2 This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It consists of
the executable PL/SQL statements of the program. It should have at least one executable line of code, which
may be just a NULL command to indicate that nothing should be executed.
Exception Handling
3 This section starts with the keyword EXCEPTION. This optional section contains exception(s) that handle
errors in the program.
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks
using BEGIN and END. Following is the basic structure of a PL/SQL block −
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
The end; line signals the end of the PL/SQL block. To run the code from the SQL command line, you may need to type /
at the beginning of the first blank line after the last line of the code. When the above code is executed at the SQL prompt,
it produces the following result −
Hello World
PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved words. The identifiers consist
of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not
exceed 30 characters.
By default, identifiers are not case-sensitive. So you can use integer or INTEGER to represent a numeric value. You
cannot use a reserved keyword as an identifier.
Delimiter Description
% Attribute indicator
. Component selector
, Item separator
= Relational operator
; Statement terminator
:= Assignment operator
|| Concatenation operator
** Exponentiation operator
102
.. Range operator
DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Hello World
PL/SQL block
Function
Package
Package body
Procedure
Trigger
Type
103
Type body
we will discuss the Data Types in PL/SQL. The PL/SQL variables, constants and parameters must have a valid data
type, which specifies a storage format, constraints, and a valid range of values. We will focus on the SCALAR and
the LOB data types in this chapter. The other two data types will be covered in other chapters.
1 Scalar
Single values with no internal components, such as a NUMBER, DATE, or BOOLEAN.
Composite
3 Data items that have internal components that can be accessed individually. For example, collections and
records.
Reference
4
Pointers to other data items.
1 Numeric
Numeric values on which arithmetic operations are performed.
Character
2
Alphanumeric values that represent single characters or strings of characters.
Boolean
3
Logical values on which logical operations are performed.
Datetime
4
Dates and times.
PL/SQL provides subtypes of data types. For example, the data type NUMBER has a subtype called INTEGER. You can
use the subtypes in your PL/SQL program to make the data types compatible with data types in other programs while
embedding the PL/SQL code in another program, such as a Java program.
104
Following table lists out the PL/SQL pre-defined numeric data types and their sub-types −
1 PLS_INTEGER
Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits
BINARY_INTEGER
2
Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits
BINARY_FLOAT
3
Single-precision IEEE 754-format floating-point number
BINARY_DOUBLE
4
Double-precision IEEE 754-format floating-point number
NUMBER(prec, scale)
5 Fixed-point or floating-point number with absolute value in range 1E-130 to (but not including) 1.0E126. A
NUMBER variable can also represent 0
DEC(prec, scale)
6
ANSI specific fixed-point type with maximum precision of 38 decimal digits
DECIMAL(prec, scale)
7
IBM specific fixed-point type with maximum precision of 38 decimal digits
NUMERIC(pre, secale)
8
Floating type with maximum precision of 38 decimal digits
DOUBLE PRECISION
9 ANSI specific floating-point type with maximum precision of 126 binary digits (approximately 38 decimal
digits)
FLOAT
10 ANSI and IBM specific floating-point type with maximum precision of 126 binary digits (approximately 38
decimal digits)
INT
11
ANSI specific integer type with maximum precision of 38 decimal digits
105
INTEGER
12
ANSI and IBM specific integer type with maximum precision of 38 decimal digits
SMALLINT
13
ANSI and IBM specific integer type with maximum precision of 38 decimal digits
REAL
14
Floating-point type with maximum precision of 63 binary digits (approximately 18 decimal digits)
DECLARE
num1 INTEGER;
num2 REAL;
num3 DOUBLE PRECISION;
BEGIN
null;
END;
/
When the above code is compiled and executed, it produces the following result −
Following is the detail of PL/SQL pre-defined character data types and their sub-types −
1 CHAR
Fixed-length character string with maximum size of 32,767 bytes
VARCHAR2
2
Variable-length character string with maximum size of 32,767 bytes
RAW
3
Variable-length binary or byte string with maximum size of 32,767 bytes, not interpreted by PL/SQL
NCHAR
4
Fixed-length national character string with maximum size of 32,767 bytes
NVARCHAR2
5
Variable-length national character string with maximum size of 32,767 bytes
6
LONG
106
LONG RAW
7
Variable-length binary or byte string with maximum size of 32,760 bytes, not interpreted by PL/SQL
ROWID
8
Physical row identifier, the address of a row in an ordinary table
UROWID
9
Universal row identifier (physical, logical, or foreign row identifier)
SQL statements
Built-in SQL functions (such as TO_CHAR)
PL/SQL functions invoked from SQL statements
When you provide a size, scale or precision limit with the data type, it is called a constrained
declaration. Constrained declarations require less memory than unconstrained declarations. For
example −
sales number(10, 2);
107
name varchar2(25);
address varchar2(100);
DECLARE
a integer := 10;
b integer := 20;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/
DECLARE
-- Global variables
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);
108
Table Created
The following program assigns values from the above table to PL/SQL variables using the SELECT INTO clause of SQL
−
DECLARE
c_id [Link]%type := 1;
c_name [Link]%type;
c_addr [Link]%type;
c_sal [Link]%type;
BEGIN
SELECT name, address, salary INTO c_name, c_addr, c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line
109
('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal);
END;
/
Declaring a Constant
A constant is declared using the CONSTANT keyword. It requires an initial value and does not allow that value to be
changed. For example −
When the above code is executed at the SQL prompt, it produces the following result −
Radius: 9.5
Diameter: 19
Circumference: 59.69
Area: 283.53
Numeric Literals
Character Literals
String Literals
BOOLEAN Literals
Date and Time Literals
The following table provides examples from all these categories of literal values.
Numeric Literals
Character Literals
2
'A' '%' '9' ' ' 'z' '('
String Literals
'Hello, world!'
3
'Tutorials Point'
'19-NOV-12'
BOOLEAN Literals
4
TRUE, FALSE, and NULL.
5 DATE '1978-12-25';
TIMESTAMP '2012-10-29 [Link]';
To embed single quotes within a string literal, place two single quotes next to each other as shown in the following
program −
DECLARE
message varchar2(30):= 'That''s [Link]!';
BEGIN
dbms_output.put_line(message);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
That's [Link]!
In this chapter, we will discuss operators in PL/SQL. An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulation. PL/SQL language is rich in built-in operators and provides the following types of
operators −
Arithmetic operators
Relational operators
Comparison operators
Logical operators
String operators
111
Here, we will understand the arithmetic, relational, comparison and logical operators one by one. The String operators
will be discussed in a later chapter − PL/SQL - Strings.
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, then −
Show Examples
** Exponentiation operator, raises one operand to the power of other A ** B will give 100000
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. Let us assume variable A holds 10 and variable B holds 20, then −
Show Examples
Checks if the values of two operands are equal or not, if yes then condition becomes (A = B) is not
=
true. true.
!=
Checks if the values of two operands are equal or not, if values are not equal then
<> (A != B) is true.
condition becomes true.
~=
Checks if the value of left operand is greater than the value of right operand, if yes then (A > B) is not
>
condition becomes true. true.
Checks if the value of left operand is less than the value of right operand, if yes then (A < B) is true.
<
condition becomes true.
Checks if the value of left operand is greater than or equal to the value of right operand, (A >= B) is not
>=
if yes then condition becomes true. true.
112
Checks if the value of left operand is less than or equal to the value of right operand, if (A <= B) is true
<=
yes then condition becomes true.
Comparison Operators
Comparison operators are used for comparing one expression to another. The result is always either TRUE,
FALSE or NULL.
Show Examples
The LIKE operator compares a character, string, or CLOB If 'Zara Ali' like 'Z% A_i' returns a Boolean
LIKE value to a pattern and returns TRUE if the value matches true, whereas, 'Nuha Ali' like 'Z% A_i'
the pattern and FALSE if it does not. returns a Boolean false.
The BETWEEN operator tests whether a value lies in a If x = 10 then, x between 5 and 20 returns
BETWEE
specified range. x BETWEEN a AND b means that x >= a true, x between 5 and 10 returns true, but
N
and x <= b. x between 11 and 20 returns false.
Logical Operators
Following table shows the Logical operators supported by PL/SQL. All these operators work on Boolean operands and
produce Boolean results. Let us assume variable A holds true and variable B holds false, then −
Show Examples
Called the logical AND operator. If both the operands are true then condition becomes (A and B) is
and
true. false.
Called the logical OR Operator. If any of the two operands is true then condition (A or B) is true.
or
becomes true.
Called the logical NOT Operator. Used to reverse the logical state of its operand. If a not (A and B) is
not
condition is true then Logical NOT operator will make it false. true.
113
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.
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.
Show Examples
Operator Operation
** exponentiation
+, - identity, negation
*, / multiplication, division
comparison
AND conjunction
OR inclusion
Here, we will discuss conditions in PL/SQL. Decision-making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition
is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical conditional (i.e., decision making) structure found in most of the programming
languages −
114
PL/SQL programming language provides following types of decision-making statements. Click the following links to
check their detail.
IF - THEN statement
1
The IF statement associates a condition with a sequence of statements enclosed by the
keywords THEN and END IF. If the condition is true, the statements get executed and if the condition is false
or NULL then the IF statement does nothing.
IF-THEN-ELSE statement
IF statement adds the keyword ELSE followed by an alternative sequence of statement. If the condition is
2
false or NULL, then only the alternative sequence of statements get executed. It ensures that either of the
sequence of statements is executed.
IF-THEN-ELSIF statement
3
It allows you to choose between several alternatives.
Case statement
Like the IF statement, the CASE statement selects one sequence of statements to execute.
4
However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean
expressions. A selector is an expression whose value is used to select one of several alternatives.
nested IF-THEN-ELSE
6 You can use one IF-THEN or IF-THEN-ELSIF statement inside another IF-THEN or IF-THEN-
ELSIF statement(s).
115
here, we will discuss Loops in PL/SQL. There may be a situation when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general
form of a loop statement in most of the programming languages −
PL/SQL provides the following types of loop to handle the looping requirements. Click the following links to check their
detail.
1
In this loop structure, sequence of statements is enclosed between the LOOP and the END LOOP
statements. At each iteration, the sequence of statements is executed and then control resumes at the top of
the loop.
DECLARE
i number(1);
j number(1);
BEGIN
<< outer_loop >>
FOR i IN 1..3 LOOP
<< inner_loop >>
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END loop inner_loop;
END loop outer_loop;
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
i is: 1 and j is: 1
i is: 1 and j is: 2
i is: 1 and j is: 3
i is: 2 and j is: 1
i is: 2 and j is: 2
i is: 2 and j is: 3
i is: 3 and j is: 1
i is: 3 and j is: 2
i is: 3 and j is: 3
EXIT statement
1
The Exit statement completes the loop and control passes to the statement immediately after the END LOOP.
CONTINUE statement
2
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
GOTO statement
3 Transfers control to the labeled statement. Though it is not advised to use the GOTO statement in your
program.
In this chapter, we will discuss Procedures in PL/SQL. A subprogram is a program unit/module that performs a
particular task. These subprograms are combined to form larger programs. This is basically called the 'Modular design'.
A subprogram can be invoked by another subprogram or program which is called the calling program.
A subprogram can be created −
This chapter is going to cover important aspects of a PL/SQL procedure. We will discuss PL/SQL function in
the next chapter.
Declarative Part
1 It is an optional part. However, the declarative part for a subprogram does not start with the DECLARE
keyword. It contains declarations of types, cursors, constants, variables, exceptions, and nested
subprograms. These items are local to the subprogram and cease to exist when the subprogram completes
execution.
Executable Part
2
This is a mandatory part and contains statements that perform the designated action.
Exception-handling
3
This is again an optional part. It contains the code that handles run-time errors.
Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified syntax for the
CREATE OR REPLACE PROCEDURE statement is as follows −
Where,
procedure-name specifies the name of the procedure.
[OR REPLACE] option allows the modification of an existing procedure.
118
The optional parameter list contains name, mode and types of the parameters. IN represents the value that will
be passed from outside and OUT represents the parameter that will be used to return a value outside of the
procedure.
procedure-body contains the executable part.
The AS keyword is used instead of the IS keyword for creating a standalone procedure.
Example
The following example creates a simple procedure that displays the string 'Hello World!' on the screen when executed.
When the above code is executed using the SQL prompt, it will produce the following result −
Procedure created.
BEGIN
greetings;
END;
/
IN
An IN parameter lets you pass a value to the subprogram. It is a read-only parameter. Inside the
1 subprogram, an IN parameter acts like a constant. It cannot be assigned a value. You can pass a constant,
literal, initialized variable, or expression as an IN parameter. You can also initialize it to a default value;
however, in that case, it is omitted from the subprogram call. It is the default mode of parameter passing.
Parameters are passed by reference.
OUT
2 An OUT parameter returns a value to the calling program. Inside the subprogram, an OUT parameter acts like
a variable. You can change its value and reference the value after assigning it. The actual parameter must
be variable and it is passed by value.
IN OUT
An IN OUT parameter passes an initial value to a subprogram and returns an updated value to the caller. It
3 can be assigned a value and the value can be read.
The actual parameter corresponding to an IN OUT formal parameter must be a variable, not a constant or an
expression. Formal parameter must be assigned a value. Actual parameter is passed by value.
This program finds the minimum of two values. Here, the procedure takes two numbers using the IN mode and returns
their minimum using the OUT parameters.
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Minimum of (23, 45) : 23
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
PL/SQL procedure successfully completed.
Methods for Passing Parameters When the above code is executed at the SQL prompt, it
produces the following result −
Square of (23): 529
Positional notation
Named notation
Mixed notation
Positional Notation
In positional notation, you can call the procedure as −
findMin(a, b, c, d);
In positional notation, the first actual parameter is substituted for the first formal parameter; the second actual parameter
is substituted for the second formal parameter, and so on. So, a is substituted for x, b is substituted for y, c is substituted
for z and d is substituted for m.
Named Notation
In named notation, the actual parameter is associated with the formal parameter using the arrow symbol ( => ). The
procedure call will be like the following −
findMin(x => a, y => b, z => c, m => d);
Mixed Notation
In mixed notation, you can mix both notations in procedure call; however, the positional notation should precede the
named notation.
The following call is legal −
findMin(a, b, c, m => d);
However, this is not legal:
findMin(x => a, b, c, d);
In this chapter, we will discuss the functions in PL/SQL. A function is same as a procedure except that it returns a value.
Therefore, all the discussions of the previous chapter are true for functions too.
121
Creating a Function
A standalone function is created using the CREATE FUNCTION statement. The simplified syntax for the CREATE OR
REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Where,
function-name specifies the name of the function.
[OR REPLACE] option allows the modification of an existing function.
The optional parameter list contains name, mode and types of the parameters. IN represents the value that will
be passed from outside and OUT represents the parameter that will be used to return a value outside of the
procedure.
The function must contain a return statement.
The RETURN clause specifies the data type you are going to return from the function.
function-body contains the executable part.
The AS keyword is used instead of the IS keyword for creating a standalone function.
Example
The following example illustrates how to create and call a standalone function. This function returns the total number of
CUSTOMERS in the customers table.
We will use the CUSTOMERS table, which we had created in the PL/SQL Variables chapter −
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | 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 |
+----+----------+-----+-----------+----------+
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;
RETURN total;
END;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Function created.
Calling a Function
122
While creating a function, you 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. When a program calls a function, the program control is transferred to the called function.
A called function performs the defined task and when its return statement is executed or when the last end statement is reached, it
returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name and if the function returns a value,
then you can store the 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;
/
When the above code is executed at the SQL prompt, it produces the following result −
Total no. of Customers: 6
Example
The following example demonstrates Declaring, Defining, and Invoking a Simple PL/SQL Function that computes and
returns the maximum of two values.
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Maximum of (23,45): 45
We have seen that a program or subprogram may call another subprogram. When a subprogram calls itself, it is referred
to as a recursive call and the process is known as recursion.
To illustrate the concept, let us calculate the factorial of a number. Factorial of a number n is defined as −
n! = n*(n-1)!
= n*(n-1)*(n-2)!
...
= n*(n-1)*(n-2)*(n-3)... 1
123
The following program calculates the factorial of a given number by calling itself recursively −
DECLARE
num number;
factorial number;
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
When the above code is executed at the SQL prompt, it produces the following result −
Factorial 6 is 720
Chapter-13
o There are certain commands present in SQL known as TCL commands that help
the user manage the transactions that take place in a database.
o COMMIT. ROLLBACK and SAVEPOINT are the most commonly used TCL
commands in SQL.
Now let us take a deeper dive into the TCL commands of SQL with the help of examples.
All the queries in the examples will be written using the MySQL database.
1. COMMIT
COMMIT command in SQL is used to save all the transaction-related changes
permanently to the disk. Whenever DDL commands such as INSERT, UPDATE and
DELETE are used, the changes made by these commands are permanent only after
closing the current session. So before closing the session, one can easily roll back the
changes made by the DDL commands. Hence, if we want the changes to be saved
permanently to the disk without closing the session, we will use the commit command.
Syntax:
1. COMMIT;
Example:
Now, we will execute the following query to insert multiple records at the same time in
the t_school table.
We will now execute the SELECT query to verify the execution of the INSERT INTO query
executed above.
After executing the SELECT query on the t_school table, you will get the following output:
4 Ashoka 1110 40 40
Universal
School
5 Calibers 9000 31 50
English
Medium
School
The output of the SELECT query shows that all the records are inserted successfully.
We will execute the COMMIT command to save the results of the operations carried on
the t_school table.
1. mysql> COMMIT;
Autocommit is by default enabled in MySQL. To turn it off, we will set the value of
autocommit as 0.
MySQL, by default, commits every query the user executes. But if the user wishes to
commit only the specific queries instead of committing every query, then turning off the
autocommit is useful.
2. SAVEPOINT
We can divide the database operations into parts. For example, we can consider all the
insert related queries that we will execute consecutively as one part of the transaction
and the delete command as the other part of the transaction. Using the SAVEPOINT
command in SQL, we can save these different parts of the same transaction using
different names. For example, we can save all the insert related queries with the
savepoint named INS. To save all the insert related queries in one savepoint, we have to
execute the SAVEPOINT query followed by the savepoint name after finishing the insert
command execution.
127
Syntax:
1. SAVEPOINT savepoint_name;
3. ROLLBACK
While carrying a transaction, we must create savepoints to save different parts of the
transaction. According to the user's changing requirements, he/she can roll back the
transaction to different savepoints. Consider a scenario: We have initiated a transaction
followed by the table creation and record insertion into the table. After inserting records,
we have created a savepoint INS. Then we executed a delete query, but later we thought
that mistakenly we had removed the useful record. Therefore in such situations, we have
an option of rolling back our transaction. In this case, we have to roll back our
transaction using the ROLLBACK command to the savepoint INS, which we have created
before executing the DELETE query.
Syntax:
1. ROLLBACK TO savepoint_name;
Example 1:
Now, we will execute the following query to insert multiple records at the same time in
the t_school table.
We will now execute the SELECT query to verify the execution of the INSERT INTO query
executed above.
After executing the SELECT query on the t_school table, you will get the following output:
4 Ashoka 1110 40 40
Universal
School
5 Calibers 9000 31 50
English
Medium
School
The output of the SELECT query shows that all the records are inserted successfully.
As we know, the SAVEPOINT command in SQL is used to save the different parts of the
same transaction using different names. Consider till this point as one part of our
transaction. We will save this part using a savepoint named Insertion.
Now, we will execute the update command on the t_school table to set the
Number_Of_Students as 9050 for the record with ID 5.
To verify that the record with ID 5 now has the Number_Of_Students as 9050, we will
execute the SELECT query.
After executing the SELECT query on the t_school table, you will get the following output:
4 Ashoka 1110 40 40
Universal
School
5 Calibers 9050 31 50
English
Medium
School
The output of the SELECT query shows that the record with ID 5 is updated successfully.
Consider the update operation as one part of our transaction. We will save this part using
a savepoint named Updation.
Suddenly, our requirement changed, and we realized that we had updated a record that
was not supposed to be. In such a scenario, we need to roll back our transaction to the
savepoint, which was created prior to the execution of the UPDATE command.
We didn't need the updation carried on the record. Hence, we have rolled back to the
savepoint named Insertion.
For confirming that we have got the same t_school table that we had before carrying out
the updation operation, we will again execute the SELECT query.
D e s s s
4 Ashoka 1110 40 40
Universal
School
5 Calibers 9000 31 50
English
Medium
School
The SELECT query output confirms that the transaction is now successfully rolled back to
the savepoint 'Insertion'.
Example 2:
Now, we will execute the following query to insert multiple records at the same time in
the customer table.
We will now execute the SELECT query to verify the execution of the INSERT INTO query
executed above.
After executing the SELECT query on the t_school table, you will get the following output:
The output of the SELECT query shows that all the records are inserted successfully.
The output of the SELECT query shows that all the records are inserted successfully.
As we know, the SAVEPOINT command in SQL is used to save the different parts of the
same transaction using different names. Consider till this point as one part of our
transaction. We will save this part using a savepoint named Insertion.
We will execute the delete command on the customer table to remove the record with ID
5.
We will execute the SELECT query to verify that the record with ID 5 has been removed.
The output of the SELECT query shows that the record with ID 5 is removed successfully.
134
Consider the delete operation as one part of our transaction. We will save this part using
a savepoint named Deletion.
Suddenly, our requirement changed, and we realized that we had deleted a record that
was not supposed to be. In such a scenario, we need to roll back our transaction to the
savepoint, which was created prior to the execution of the DELETE command.
We didn't need the deletion carried on the record. Hence, we have rolled back to the
savepoint named Insertion.
For confirming that we have got the same customer table that we had before carrying
out the deletion operation, we will again execute the SELECT query.
The SELECT query output confirms that the transaction is now successfully rolled back to
the savepoint 'Insertion'.