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

DBMS

The document provides an overview of Database Management Systems (DBMS), defining databases and their functionalities, types, and advantages over file-based systems. It discusses various types of data files, the key features of DBMS, and the different languages used for data manipulation and control. Additionally, it highlights the shift from file systems to DBMS, emphasizing the importance of data integrity, security, and concurrent access.

Uploaded by

Vinaya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DBMS

The document provides an overview of Database Management Systems (DBMS), defining databases and their functionalities, types, and advantages over file-based systems. It discusses various types of data files, the key features of DBMS, and the different languages used for data manipulation and control. Additionally, it highlights the shift from file systems to DBMS, emphasizing the importance of data integrity, security, and concurrent access.

Uploaded by

Vinaya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

DBMS

Unit 1
Introduc on to database:
A collected information which is in an organized form for easier access,
management, and various updating is known as a database.

Data can be defined as a collection of facts and records on which we can


apply reasoning or can-do discussion or some calculation. The data is always
easily available and is in plenty. It can be used for processing some useful
information from it. Also, it can be in redundant, can be irrelevant. Data can
exist in form of graphics, reports, tables, text, etc. that represents every kind
of information, that allows easy retrieval, updating, analysis, and output of
data by systematically organized or structured repository of indexed
information.

Containers having a huge amount of data are known as databases, for


example, a public library stores books. Databases are computer structures that
save, organize, protect, and deliver data.

Any system that manages databases is called a database management


system, or DBM. The typical diagram representation for a database is a
cylinder.

Inside a database, the data is recorded in a table which is a collection of


rows, columns, and it is indexed so that to find relevant information becomes
an easier task. As new information is added, data gets updated, expanded
and deleted. The various processes of databases create and update
themselves, querying the data they contain and running applications against
it.

Types of data, records and files:


Sure! Here are the different types of data, records, and files:
By Structure:
 Flat files: These are the simplest type of file, where each record is a single
line of text with fields separated by delimiters such as commas, tabs, or pipes.
They are easy to create and read, but can be inflexible and difficult to query.
 Delimited files: Similar to flat files, but the fields are separated by a specific
character or sequence of characters, such as commas (CSV), tabs (TSV), or
semicolons.
DBMS
 Fixed-length files: Each record has a fixed length, regardless of the amount of
data in each field. This can be more efficient for storage and processing, but it
can be difficult to accommodate changes in the data structure.
 Hierarchical files: Records are organized in a tree-like structure, with parent
records containing child records. This can be useful for representing complex
relationships between data items.
 Database files: These files are used to store data in a relational database
management system (RDBMS). They are typically organized into tables,
which are collections of records with a fixed schema.
By Content:
 Text files: These files contain human-readable text, such as documents,
emails, or source code.
 Image files: These files store images, such as photographs, drawings, or
diagrams.
 Audio files: These files store audio recordings, such as music, speeches, or
sound effects.
 Video files: These files store video recordings, such as movies, TV shows, or
video games.
 Binary files: These files contain data in a binary format, which is not human-
readable. They are often used to store data that is not text-based, such as
program executables or compressed files.
By Purpose:
 Transaction files: These files are used to record changes to data, such as
sales transactions or financial records.
 Master files: These files contain relatively static data, such as customer
records or product information.
 Log files: These files record events that occur in a system, such as system
errors or user activity.
 Archive files: These files are used to store data that is no longer actively used,
but needs to be kept for historical purposes.
.
File based system:
The systems that are used to organize and maintain data files are known as
file based data systems. These file systems are used to handle a single or
multiple files and are not very efficient.

Functionalities
DBMS
The functionalities of a File-based Data Management System are as follows −

 A file based system helps in basic data management for any user.
 The data stored in the file based system should remain consistent. Any transactions
done in the file based system should not alter the consistency property.
 The file based system should not allow any illegal or potentially hazardous operations
to occur on the data.
 The file based system should allow concurrent access by different processes and this
should be carefully coordinated.
 The file based system should make sure that the data is uniformly structured and
stored so it is easier to access it.

Advantages of File Based System


 The file Based system is not complicated and is simpler to use.
 Because of the above point, this system is quite inexpensive.
 Because the file based system is simple and cheap, it is normally suitable for home
users and owners of small businesses.
 Since the file based system is used by smaller organisations or individual users, it
stores comparatively lesser amount of data. Hence, the data can be accessed faster
and more easily.

Disadvantages of File Based System


 The File based system is limited to a smaller size and cannot store large amounts of
data.
 This system is relatively uncomplicated but this means it cannot support
complicated queries, data recovery etc.
 There may be redundant data in the file based system as it does not have a complex
mechanism to get rid of it.
 The data is not very secure in a file based system and may be corrupted or
destroyed.
 The data files in the file based system may be stored across multiple locations.
Consequently, it is difficult to share the data easily with multiple users.

DBMS:
A Database Management System (DBMS) is a so ware system that is
designed to manage and organize data in a structured manner. It
DBMS
allows users to create, modify, and query a database, as well as
manage the security and access controls for that database.

DBMS provides an environment to store and retrieve the data in


coinvent and efficient manner.

Key Features of DBMS


Data modeling: A DBMS provides tools for crea ng and modifying
data models, which define the structure and rela onships of the data
in a database.
Data storage and retrieval: A DBMS is responsible for storing and
retrieving data from the database, and can provide various methods
for searching and querying the data.
Concurrency control: A DBMS provides mechanisms for controlling
concurrent access to the database, to ensure that mul ple users can
access the data without conflic ng with each other.
Data integrity and security: A DBMS provides tools for enforcing data
integrity and security constraints, such as constraints on the values of
data and access controls that restrict who can access the data.
Backup and recovery: A DBMS provides mechanisms for backing up
and recovering the data in the event of a system failure.
DBMS can be classified into two types: Rela onal Database
Management System (RDBMS) and Non-Rela onal Database
Management System (NoSQL or Non-SQL)
RDBMS: Data is organized in the form of tables and each table has a
set of rows and columns. The data are related to each other through
primary and foreign keys.
DBMS
NoSQL: Data is organized in the form of key-value pairs, documents,
graphs, or column-based. These are designed to handle large-scale,
high-performance scenarios.
A database is a collec on of interrelated data which helps in the
efficient retrieval, inser on, and dele on of data from the database
and organizes the data in the form of tables, views, schemas, reports,
etc. For Example, a university database organizes the data about
students, faculty, admin staff, etc. which helps in the efficient
retrieval, inser on, and dele on of data from it.

1. Database Languages
2. Data Defini on Language
3. Data Manipula on Language
4. Data Control Language
5. Transac onal Control Language
6. Data Defini on Language
DDL is the short name for Data Defini on Language, which deals with
database schemas and descrip ons, of how the data should reside in
the database.

CREATE: to create a database and its objects like (table, index, views,
store procedure, func on, and triggers)
ALTER: alters the structure of the exis ng database
DROP: delete objects from the database
TRUNCATE: remove all records from a table, including all spaces
allocated for the records are removed
COMMENT: add comments to the data dic onary
RENAME: rename an object
DBMS
Data Manipula on Language
DML is the short name for Data Manipula on Language which deals
with data manipula on and includes most common SQL statements
such SELECT, INSERT, UPDATE, DELETE, etc., and it is used to store,
modify, retrieve, delete and update data in a database.

SELECT: retrieve data from a database


INSERT: insert data into a table
UPDATE: updates exis ng data within a table
DELETE: Delete all records from a database table
MERGE: UPSERT opera on (insert or update)
CALL: call a PL/SQL or Java subprogram
EXPLAIN PLAN: interpreta on of the data access path
LOCK TABLE: concurrency Control
Data Control Language
DCL is short for Data Control Language which acts as an access
specifier to the database.(basically to grant and revoke permissions
to users in the database

GRANT: grant permissions to the user for running DML(SELECT,


INSERT, DELETE,…) commands on the table
REVOKE: revoke permissions to the user for running DML(SELECT,
INSERT, DELETE,…) command on the specified table
Transac onal Control Language
DBMS
TCL is short for Transac onal Control Language which acts as an
manager for all types of transac onal data and all transac ons. Some
of the command of TCL are

Roll Back: Used to cancel or Undo changes made in the database


Commit: It is used to apply or save changes in the database
Save Point: It is used to save the data on the temporary basis in the
database
Data retrieval language:
DRL is short for Data Retrieval Language which is used for retrieval of
data. It can also be said as DML.

SELECT: Used for extrac ng the required data.


Database Management System
The so ware which is used to manage databases is called Database
Management System (DBMS). For Example, MySQL, Oracle, etc. are
popular commercial DBMS used in different applica ons. DBMS
allows users the following tasks:

Data Defini on: It helps in the crea on, modifica on, and removal of
defini ons that define the organiza on of data in the database.
Data Upda on: It helps in the inser on, modifica on, and dele on of
the actual data in the database.
Data Retrieval: It helps in the retrieval of data from the database
which can be used by applica ons for various purposes.
User Administra on: It helps in registering and monitoring users,
enforcing data security, monitoring performance, maintaining data
DBMS
integrity, dealing with concurrency control, and recovering
informa on corrupted by unexpected failure.
Applica ons of DBMS:
Enterprise Informa on: Sales, accoun ng, human resources,
Manufacturing, online retailers.
Banking and Finance Sector: Banks maintaining the customer details,
accounts, loans, banking transac ons, credit card transac ons.
Finance: Storing the informa on about sales and holdings,
purchasing of financial stocks and bonds.
University: Maintaining the informa on about student course
enrolled informa on, student grades, staff roles.
Airlines: Reserva ons and schedules.
Telecommunica ons: Prepaid, postpaid bills maintance.
Paradigm Shi from File System to DBMS
File System manages data using files on a hard disk. Users are
allowed to create, delete, and update the files according to their
requirements. Let us consider the example of file-based University
Management System. Data of students is available to their respec ve
Departments, Academics Sec on, Result Sec on, Accounts Sec on,
Hostel Office, etc. Some of the data is common for all sec ons like
Roll No, Name, Father Name, Address, and Phone number of
students but some data is available to a par cular sec on only like
Hostel allotment number which is a part of the hostel office. Let us
discuss the issues with this system:

Redundancy of data: Data is said to be redundant if the same data is


copied at many places. If a student wants to change their Phone
number, he or she has to get it updated in various sec ons. Similarly,
DBMS
old records must be deleted from all sec ons represen ng that
student.
Inconsistency of Data: Data is said to be inconsistent if mul ple
copies of the same data do not match each other. If the Phone
number is different in Accounts Sec on and Academics Sec on, it will
be inconsistent. Inconsistency may be because of typing errors or not
upda ng all copies of the same data.
Difficult Data Access: A user should know the exact loca on of the
file to access data, so the process is very cumbersome and tedious. If
the user wants to search the student hostel allotment number of a
student from 10000 unsorted students’ records, how difficult it can
be.
Unauthorized Access: File Systems may lead to unauthorized access
to data. If a student gets access to a file having his marks, he can
change it in an unauthorized way.
No Concurrent Access: The access of the same data by mul ple users
at the same me is known as concurrency. The file system does not
allow concurrency as data can be accessed by only one user at a
me.
No Backup and Recovery: The file system does not incorporate any
backup and recovery of data if a file is lost or corrupted.
Advantages of DBMS
Data organiza on: A DBMS allows for the organiza on and storage of
data in a structured manner, making it easy to retrieve and query the
data as needed.
Data integrity: A DBMS provides mechanisms for enforcing data
integrity constraints, such as constraints on the values of data and
access controls that restrict who can access the data.
DBMS
Concurrent access: A DBMS provides mechanisms for controlling
concurrent access to the database, to ensure that mul ple users can
access the data without conflic ng with each other.
Data security: A DBMS provides tools for managing the security of
the data, such as controlling access to the data and encryp ng
sensi ve data.
Backup and recovery: A DBMS provides mechanisms for backing up
and recovering the data in the event of a system failure.
Data sharing: A DBMS allows mul ple users to access and share the
same data, which can be useful in a collabora ve work environment.
Disadvantages of DBMS
Complexity: DBMS can be complex to set up and maintain, requiring
specialized knowledge and skills.
Performance overhead: The use of a DBMS can add overhead to the
performance of an applica on, especially in cases where high levels
of concurrency are required.
Scalability: The use of a DBMS can limit the scalability of an
applica on, since it requires the use of locking and other
synchroniza on mechanisms to ensure data consistency.
Cost: The cost of purchasing, maintaining and upgrading a DBMS can
be high, especially for large or complex systems.
Limited Use Cases: Not all use cases are suitable for a DBMS, some
solu ons don’t need high reliability, consistency or security and may
be be er served by other types of data storage.
These are the main reasons which made a shi from file system to
DBMS. Also, see
DBMS
A Database Management System (DBMS) is a so ware system that
allows users to create, maintain, and manage databases. It is a
collec on of programs that enables users to access and manipulate
data in a database. A DBMS is used to store, retrieve, and manipulate
data in a way that provides security, privacy, and reliability.

Several Types of DBMS


Rela onal DBMS (RDBMS): An RDBMS stores data in tables with
rows and columns, and uses SQL (Structured Query Language) to
manipulate the data.
Object-Oriented DBMS (OODBMS): An OODBMS stores data as
objects, which can be manipulated using object-oriented
programming languages.
NoSQL DBMS: A NoSQL DBMS stores data in non-rela onal data
structures, such as key-value pairs, document-based models, or graph
models.
Overall, a DBMS is a powerful tool for managing and manipula ng
data, and is used in many industries and applica ons, such as
finance, healthcare, retail, and more.

Levels of Data Abstractions in DBMS


In DBMS, there are three levels of data abstraction, which are as follows:
DBMS
1. Physical or Internal Level:
The physical or internal layer is the lowest level of data abstraction in the database
management system. It is the layer that defines how data is actually stored in the
database. It defines methods to access the data in the database. It defines complex
data structures in detail, so it is very complex to understand, which is why it is kept
hidden from the end user.

Data Administrators (DBA) decide how to arrange data and where to store data. The Data
Administrator (DBA) is the person whose role is to manage the data in the database at the
physical or internal level. There is a data center that securely stores the raw data in detail on
hard drives at this level.

2. Logical or Conceptual Level:


The logical or conceptual level is the intermediate or next level of data abstraction. It
explains what data is going to be stored in the database and what the relationship is
between them.

It describes the structure of the entire data in the form of tables. The logical level or
conceptual level is less complex than the physical level. With the help of the logical
level, Data Administrators (DBA) abstract data from raw data present at the physical
level.

3. View or External Level:


View or External Level is the highest level of data abstraction. There are different views
at this level that define the parts of the overall data of the database. This level is for
the end-user interaction; at this level, end users can access the data based on their
queries.

Advantages of data abstraction in DBMS


o Users can easily access the data based on their queries.
o It provides security to the data stored in the database.
o Database systems work efficiently because of data abstraction.

Instances & Schema

Instances:
An Instance is the state of an operational database with data at any given time. It contains a
snapshot of the database. The instances can be changed by certain CRUD opuations, such as
DBMS
like addition, and deletion of data. It may be noted that any search query will not make any
kind of changes in the instances.

Example:
Let’s say a table teacher in our database whose name is School, suppose the table has 50
records so the instance of the database has 50 records for now and tomorrow we are going
to add another fifty records so tomorrow the instance has a total of 100 records. This is
called an instance.

Schema
Schema is the overall description of the database. The basic structure of how the data will be
stored in the database is called schema.

Schema is of three types: Logical Schema, Physical Schema and view Schema.

Logical Schema – It describes the database designed at a logical level.

Physical Schema – It describes the database designed at the physical level.

View Schema – It defines the design of the database at the view level.
DBMS
Example:
Let’s say a table teacher in our database named school, the teacher table requires the name,
dob, and doj in their table so we design a structure as:

Teacher table
name: String
doj: date
dob: date

Above given is the schema of the table teacher.

Difference Between Schema and Instance

Schema Instance

It is the collection of information stored


It is the overall description of the database.
in a database at a particular moment.

Data in instances can be changed using


The schema is same for the whole database.
addition, deletion, and updation.

Does not change Frequently. Changes Frequently.

Defines the basic structure of the database i.e. It is the set of Information stored at a
how the data will be stored in the database. particular time.

Conclusion

In short, the schema is the blueprint of the database, while the instance is the actual data
that is in the database. The schema is the database’s design, and the instance is the data it
contains.

Database users:
Database users are categorized based up on their interaction with the database. These are
seven types of database users in DBMS.

1. Database Administrator (DBA) : Database Administrator (DBA) is a person/team


who defines the schema and also controls the 3 levels of database. The DBA will then
create a new account id and password for the user if he/she need to access the
database. DBA is also responsible for providing security to the database and he
DBMS
allows only the authorized users to access/modify the data base. DBA is responsible
for the problems such as security breaches and poor system response time.

 DBA also monitors the recovery and backup and provide technical support.

 The DBA has a DBA account in the DBMS which called a system or superuser
account.

 DBA repairs damage caused due to hardware and/or software failures.

 DBA is the one having privileges to perform DCL (Data Control Language)
operations such as GRANT and REVOKE, to allow/restrict a particular user
from accessing the database.

2. Naive / Parametric End Users : Parametric End Users are the unsophisticated who
don’t have any DBMS knowledge but they frequently use the database applications in
their daily life to get the desired results. For examples, Railway’s ticket booking users
are naive users. Clerks in any bank is a naive user because they don’t have any DBMS
knowledge but they still use the database and perform their given task.

3. System Analyst :
System Analyst is a user who analyzes the requirements of parametric end users. They
check whether all the requirements of end users are satisfied.

4. Sophisticated Users : Sophisticated users can be engineers, scientists, business


analyst, who are familiar with the database. They can develop their own database
applications according to their requirement. They don’t write the program code but
they interact the database by writing SQL queries directly through the query
processor.

5. Database Designers : Data Base Designers are the users who design the structure of
database which includes tables, indexes, views, triggers, stored procedures and
constraints which are usually enforced before the database is created or populated
with data. He/she controls what data must be stored and how the data items to be
related. It is responsibility of Database Designers to understand the requirements of
different user groups and then create a design which satisfies the need of all the user
groups.

6. Application Programmers : Application Programmers also referred as System


Analysts or simply Software Engineers, are the back-end programmers who writes the
code for the application programs. They are the computer professionals. These
programs could be written in Programming languages such as Visual Basic,
Developer, C, FORTRAN, COBOL etc. Application programmers design, debug, test,
and maintain set of programs called “canned transactions” for the Naive (parametric)
users in order to interact with database.

7. Casual Users / Temporary Users : Casual Users are the users who occasionally
use/access the database but each time when they access the database they require
the new information, for example, Middle or higher level manager.
DBMS
8. Specialized users : Specialized users are sophisticated users who write
specialized database application that does not fit into the traditional data-
processing framework. Among these applications are computer aided-design
systems, knowledge-base and expert systems etc.

DBMS structure:
Database Management System (DBMS) is software that allows access to data stored in a
database and provides an easy and effective method of –

 Defining the information.

 Storing the information.

 Manipulating the information.

 Protecting the information from system crashes or data theft.

 Differentiating access permissions for different users.

Data Theft: When somebody steals the information stored on databases, and servers, this
process is known as Data Theft.

Note: Structure of Database Management System is also referred to as Overall System


Structure or Database Architecture but it is different from the tier architecture of Database.

The database system is divided into three components: Query Processor, Storage Manager,
and Disk Storage. These are explained as following below.
DBMS

Architecture of DBMS

1. Query Processor: It interprets the requests (queries) received from end user via an
application program into instructions. It also executes the user request which is received
from the DML compiler.
Query Processor contains the following components –

 DML Compiler: It processes the DML statements into low level instruction (machine
language), so that they can be executed.

 DDL Interpreter: It processes the DDL statements into a set of table containing meta
data (data about data).

 Embedded DML Pre-compiler: It processes DML statements embedded in an


application program into procedural calls.

 Query Optimizer: It executes the instruction generated by DML Compiler.

2. Storage Manager: Storage Manager is a program that provides an interface between the
data stored in the database and the queries received. It is also known as Database Control
System. It maintains the consistency and integrity of the database by applying the
constraints and executing the DCL statements. It is responsible for updating, storing,
DBMS
deleting, and retrieving data in the database.
It contains the following components –

 Authorization Manager: It ensures role-based access control, i.e,. checks whether


the particular person is privileged to perform the requested operation or not.

 Integrity Manager: It checks the integrity constraints when the database is


modified.

 Transaction Manager: It controls concurrent access by performing the operations in


a scheduled way that it receives the transaction. Thus, it ensures that the database
remains in the consistent state before and after the execution of a transaction.

 File Manager: It manages the file space and the data structure used to represent
information in the database.

 Buffer Manager: It is responsible for cache memory and the transfer of data
between the secondary storage and main memory.

3. Disk Storage: It contains the following components –

 Data Files: It stores the data.

 Data Dictionary: It contains the information about the structure of any database
object. It is the repository of information that governs the metadata.

 Indices: It provides faster retrieval of data item.

The structure of a Database Management System (DBMS) can be divided into three main
components: the Internal Level, the Conceptual Level, and the External Level.

1. Internal Level: This level represents the physical storage of data in the database. It is
responsible for storing and retrieving data from the storage devices, such as hard
drives or solid-state drives. It deals with low-level implementation details such as data
compression, indexing, and storage allocation.

2. Conceptual Level: This level represents the logical view of the database. It deals with
the overall organization of data in the database and the relationships between them.
It defines the data schema, which includes tables, attributes, and their relationships.
The conceptual level is independent of any specific DBMS and can be implemented
using different DBMSs.

3. External Level: This level represents the user’s view of the database. It deals with how
users access the data in the database. It allows users to view data in a way that makes
DBMS
sense to them, without worrying about the underlying implementation details. The
external level provides a set of views or interfaces to the database, which are tailored
to meet the needs of specific user groups.

The three levels are connected through a schema mapping process that translates data from
one level to another. The schema mapping process ensures that changes made at one level
are reflected in the other levels.

In addition to these three levels, a DBMS also includes a Database Administrator (DBA)
component, which is responsible for managing the database system. The DBA is responsible
for tasks such as database design, security management, backup and recovery, and
performance tuning.

Overall, the structure of a DBMS is designed to provide a high level of abstraction to users,
while still allowing low-level implementation details to be managed effectively. This allows
users to focus on the logical organization of data in the database, without worrying about
the physical storage or implementation details.
DBMS
Unit 2
Entity in DBMS
Database Management System (DBMS) is an essential tool to manage data, but do you
know how important entities are in DBMS?

The role of the entity is the representation and management of data. In this article, we
are going to discuss entities in DBMS.

Entity:
An entity is referred to as an object or thing that exists in the real world. For example,
customer, car, pen, etc.

Entities are stored in the database, and they should be distinguishable, i.e., they should
be easily identifiable from the group. For example, a group of pens that are from the
same company cannot be identified, so they are only objects, but pens with different
colours become unique and will be called an entity like a red pen, green pen, blue pen,
black pen, etc.

In a group of pens, we can easily identify any pen because of its different colours, so a
pen of different colours is an entity.

For extracting data from the database, each data must be unique in its own way so
that it becomes easier to differentiate between them. Distinct and unique data is
known as an entity.
DBMS
An entity has some attributes which depict the entity's characteristics. For example, an
entity "Student" has attributes such as "Student_roll_no", "Student_name",
"Student_subject", and "Student_marks".

Example of Entity in DBMS in tabular form:


Student_rollno Student_name Student_subject Student_marks

1 Robert English 85

2 Parker Mathematics 75

3 Harry Science 80

4 George Geography 70

Some entities are related to other entities in the table. For example, the "Student"
entity is related to the "University" entity. The ERD (Entity Relationship Diagram) model
comes to light to visually show the relationship between several entities.

Kinds of Entity:
There are two kinds of entities, which are as follows:

1. Tangible Entity:

It is an entity in DBMS, which is a physical object that we can touch or see. In simple
words, an entity that has a physical existence in the real world is called a tangible entity.

For example, in a database, a table represents a tangible entity because it contains a


physical object that we can see and touch in the real world. It includes colleges, bank
lockers, mobiles, cars, watches, pens, paintings, etc.

2. Intangible Entity:

It is an entity in DBMS, which is a non-physical object that we cannot see or touch. In


simple words, an entity that does not have any physical existence in the real world is
known as an intangible entity.

For example, a bank account logically exists, but we cannot see or touch it.

Entity Type:
DBMS
A collection of entities with general characteristics is known as an entity type.

For example, a database of a corporate company has entity types such as employees,
departments, etc. In DBMS, every entity type contains a set of attributes that explain
the entity.

The Employee entity type can have attributes such as name, age, address, phone
number, and salary.

The Department entity type can have attributes such as name, number, and location
in the department.

Kinds of Entity Type


There are two kinds of entity type, which are as follows:

1. Strong Entity Type: It is an entity that has its own existence and is independent.

The entity relationship diagram represents a strong entity type with the help of a single
rectangle. Below is the ERD of the strong entity type:
DBMS
In the above example, the "Customer" is the entity type with attributes such as ID,
Name, Gender, and Phone Number. Customer is a strong entity type as it has a unique
ID for each customer.

2. Weak Entity Type: It is an entity that does not have its own existence and relies on
a strong entity for its existence.

The Entity Relationship Diagram represents the weak entity type using double
rectangles. Below is the ERD of the weak entity type:

In the above example, "Address" is a weak entity type with attributes such as House
No., City, Location, and State.

The relationship between a strong and a weak entity type is known as an identifying
relationship.

Using a double diamond, the Entity-Relationship Diagram represents a relationship


between the strong and the weak entity type.
DBMS
Let us see an example of the relationship between the Strong entity type and weak
entity type with the help of ER Diagram:

Entity Set
An entity set is a group of entities of the same entity type.

For example, an entity set of students, an entity set of motorbikes, an entity of


smartphones, an entity of customers, etc.

Entity sets can be classified into two types:

1. Strong Entity Set:

In a DBMS, a strong entity set consists of a primary key.

For example, an entity of motorbikes with the attributes, motorbike's registration


number, motorbike's name, motorbike's model, and motorbike's colour.

ADVERTISEMENT

Below is the representation of a strong entity set in tabular form:


DBMS

Example of Entity Relationship Diagram representation of the above strong entity set:
DBMS
2. Weak Entity Set:

In a DBMS, a weak entity set does not contain a primary key.

For example, An entity of smartphones with its attributes, phone's name, phone's
colour, and phone's RAM.

Below is the representation of a weak entity set in tabular form:

Example of Entity Relationship Diagram representation of the above weak entity set:
DBMS
Conclusion:
In this article, you read all the vital things related to entities in DBMS.

o We have discussed that entity is anything that exists in the real world and is identifiable.
o We have discussed the types of entities, which are tangible entities and intangible
entities.
o We have discussed entity type and types of entity type, which are weak entity type and
strong entity type.
o We have discussed entity sets and types of entity sets, which are weak entity sets and
strong entity sets.

attribute and data association relation between entities

Importance of DM

Data modeling involves creating a visual representation of the different types of


data an organization collects and the relationship between those data objects.

A data modeling process can help you achieve several goals.

First, you can identify missing and redundant data. As a result, you can improve
efficiency by making the necessary adjustments to your data.

Second, data modelling allows you to define how a database should be


structured, including the conceptual, logical, and physical layers.

Third, data modelling eliminates confusion by providing a clear picture of


organizational data needs. Developers or data experts will know how to get
started, which saves significant time and effort.

This article will help you understand the process of data modelling and its
importance and provide different data modeling techniques and examples.

The importance of data modeling


DBMS
Data modeling has numerous benefits, including the following:

 Accurate data representation. Data modeling allows developers, system


designers, database administrators, and other professionals to have a clear
picture of the business needs. Conceptual and logical data models use
flowcharts to show the required data objects and their connection to business
processes. These data models also determine the actual values that the
database will store. For instance, a customer data model can have strings,
integers, and Boolean values. Therefore, you can say that a data model acts
as a roadmap that enables developers to accurately represent all the required
data objects in a database.
 Relationship between database objects. Flowcharts in the conceptual and
logical data models allow you to understand the relationship between different
data objects. These relationships simplify complex business processes and
eliminate confusion during development.
 Control of data redundancy. In this context, redundancy involves having
several copies of the same data in a database. Apart from potentially affecting
data quality, duplicate data also consumes extra storage space. Data
modeling enables you to perform tests and set rules on how to deal with data
redundancies. For example, you can remove a particular table in your
database if there’s another with similar data.
 Security. With data modeling, you can identify security issues likely to affect
the application before you push it to production. Resolving these issues
allows you to end up with a more secure application. Modeling can also assist
you in fulfilling different data integrity requirements.
 Faster software builds. Conceptual, logical, and physical data models guide
developers on what needs to be implemented at different application stages.
As a result, these models eliminate confusion and allow developers to
dedicate more time and focus on the actual code implementation, leading to
faster software builds.
 Reduced cost. Data modeling can help you save significant time and costs.
For example, the planning or conceptual stage allows you to discover
problems that could have otherwise affected your system during production.
Identifying and handling these issues at an early stage is less frustrating and
less cost-intensive than discovering them farther into the process. You can
also use data modeling tools, such as Erwin, to scan for errors and automate
different data processes and structures, which improves data quality.
 Improved performance. Data modeling can make your application run more
efficiently. Data models serve as a high-level plan, enabling developers to
know which type of data the application will need and how to consume or
store it. Such requirements can help meet the needs of data-intensive fields
like machine learning, business intelligence, artificial intelligence, and big
data.
 Better documentation. Data modeling allows developers to document their
data structures, relationships, and other important business requirements.
Developers and other stakeholders can use these documents for future
reference.
 High-quality applications. A huge advantage of data modeling is that it
facilitates the creation of high-quality applications, which are easy to
DBMS
maintain. Such applications follow a detailed plan and structure, making them
less likely to crash. Such apps are also highly scalable.
 Quality decision-making. A data model helps break down complex data
architecture into simple terms that are more understandable to business
stakeholders outside the tech bubble. Business analysts can use this
information to ensure that all data requirements are fulfilled.
 Data analytics and visualization. Data modeling can help you transform
large amounts of data into valuable information for decision-making, business
intelligence, and other uses.

The evolution of data model:

Managing data was the key and was essential. Therefore, data model
originated to solve the file system issues. Here are the Data Models in DBMS

Hierarchical Model
In Hierarchical Model, a hierarchical relation is formed by collection of relations
and forms a tree-like structure.

The relationship can be defined in the form of parent child type.

One of the first and most popular Hierarchical Model is Information


Management System (IMS), developed by IBM.

Example

The hierarchy shows an Employee can be an Intern, on Contract or Full- Time.


Sub-levels show that Full-Time Employee can be hired as a Writer, Senior
Writer or Editor:
DBMS

Advantages

 The design of the hierarchical model is simple.


 Provides Data Integrity since it is based on parent/ child relationship
 Data sharing is feasible since the data is stored in a single database.
 Even for large volumes of data, this model works perfectly.

Disadvantages

 Implementation is complex.
 This model has to deal with anomalies like Insert, Update and Delete.
 Maintenance is difficult since changes done in the database may want you to do
changes in the entire database structure.

Network Model
The Hierarchical Model creates hierarchical tree with parent/ child relationship,
whereas the Network Model has graph and links.

The relationship can be defined in the form of links and it handles many-to-
many relations. This itself states that a record can have more than one parent.

Example
DBMS

Advantages

 Easy to design the Network Model


 The model can handle one-one, one-to-many, many-to-many relationships.
 It isolates the program from other details.
 Based on standards and conventions.

Disadvantages

 Pointers bring complexity since the records are based on pointers and graphs.
 Changes in the database isn’t easy that makes it hard to achieve structural
independence.

Relational Model
A relational model groups data into one or more tables. These tables are
related to each other using common records.

The data is represented in the form of rows and columns i.e. tables:
DBMS

Example

Let us see an example of two


relations <Employee> and <Department> linked to each other,
with DepartmentID, which is Foreign Key of <Employee> table and Primary
key of <Department> table.

Advantages

 The Relational Model does not have any issues that we saw in the previous two
models i.e. update, insert and delete anomalies have nothing to do in this model.
 Changes in the database do not require you to affect the complete database.
 Implementation of a Relational Model is easy.
 To maintain a Relational Model is not a tiresome task.

Disadvantages
DBMS
 Database inefficiencies hide and arise when the model has large volumes of data.
 The overheads of using relational data model come with the cost of using powerful
hardware and devices.

Types of data models , Advantages, Disadvantages:


A Data Model in Database Management System (DBMS) is the
concept of tools that are developed to summarize the
description of the database. Data Models provide us with a
transparent picture of data which helps us in creating an actual
database. It shows us from the design of the data to its proper
implementation of data.
Types of Relational Models
1. Conceptual Data Model
2. Representational Data Model
3. Physical Data Model
It is basically classified into 3 types:-

1. Conceptual Data Model


The conceptual data model describes the database at a very
high level and is useful to understand the needs or
requirements of the database. It is this model, that is used in
DBMS
the requirement-gathering process i.e. before the Database
Designers start making a particular database. One such popular
model is the entity/relationship model (ER model). The E/R
model specializes in entities, relationships, and even attributes
that are used by database designers. In terms of this concept, a
discussion can be made even with non-computer science(non-
technical) users and stakeholders, and their requirements can
be understood.
Entity-Relationship Model( ER Model): It is a high-level data
model which is used to define the data and the relationships
between them. It is basically a conceptual design of any
database which is easy to design the view of data.
Components of ER Model:
1. Entity: An entity is referred to as a real-world object. It can
be a name, place, object, class, etc. These are represented
by a rectangle in an ER Diagram.
2. Attributes: An attribute can be defined as the description
of the entity. These are represented by Eclipse in an ER
Diagram. It can be Age, Roll Number, or Marks for a
Student.
3. Relationship: Relationships are used to define relations
among different entities. Diamonds and Rhombus are
used to show Relationships.
Characteristics of a conceptual data model
 Offers Organization-wide coverage of the business
concepts.
DBMS
 This type of Data Models are designed and developed for
a business audience.
 The conceptual model is developed independently of
hardware specifications like data storage capacity, location
or software specifications like DBMS vendor and
technology. The focus is to represent data as a user will
see it in the “real world.”
Conceptual data models known as Domain models create a
common vocabulary for all stakeholders by establishing basic
concepts and scope
2. Representational Data Model
This type of data model is used to represent only the logical
part of the database and does not represent the physical
structure of the database. The representational data model
allows us to focus primarily, on the design part of the database.
A popular representational model is a Relational model. The
relational Model consists of Relational Algebra and Relational
Calculus. In the Relational Model, we basically use tables to
represent our data and the relationships between them. It is a
theoretical concept whose practical implementation is done in
Physical Data Model.
The advantage of using a Representational data model is to
provide a foundation to form the base for the Physical model
3. Physical Data Model
The physical Data Model is used to practically implement
Relational Data Model. Ultimately, all data in a database is
stored physically on a secondary storage device such as discs
and tapes. This is stored in the form of files, records, and certain
DBMS
other data structures. It has all the information on the format in
which the files are present and the structure of the databases,
the presence of external data structures, and their relation to
each other. Here, we basically save tables in memory so they
can be accessed efficiently. In order to come up with a good
physical model, we have to work on the relational model in a
better way. Structured Query Language (SQL) is used to
practically implement Relational Algebra.
This Data Model describes HOW the system will be
implemented using a specific DBMS system. This model is
typically created by DBA and developers. The purpose is actual
implementation of the database.
Characteristics of a physical data model:
 The physical data model describes data need for a single
project or application though it maybe integrated with
other physical data models based on project scope.
 Data Model contains relationships between tables that
which addresses cardinality and nullability of the
relationships.
 Developed for a specific version of a DBMS, location, data
storage or technology to be used in the project.
 Columns should have exact datatypes, lengths assigned
and default values.
 Primary and Foreign keys, views, indexes, access profiles,
and authorizations, etc. are defined
Some Other Data Models
1. Hierarchical Model
DBMS
The hierarchical Model is one of the oldest models in the data
model which was developed by IBM, in the 1950s. In a
hierarchical model, data are viewed as a collection of tables, or
we can say segments that form a hierarchical relation. In this,
the data is organized into a tree-like structure where each
record consists of one parent record and many children. Even if
the segments are connected as a chain-like structure by logical
associations, then the instant structure can be a fan structure
with multiple branches. We call the illogical associations as
directional associations.
2. Network Model
The Network Model was formalized by the Database Task group
in the 1960s. This model is the generalization of the hierarchical
model. This model can consist of multiple parent segments and
these segments are grouped as levels but there exists a logical
association between the segments belonging to any level.
Mostly, there exists a many-to-many logical association
between any of the two segments.
3. Object-Oriented Data Model
In the Object-Oriented Data Model, data and their relationships
are contained in a single structure which is referred to as an
object in this data model. In this, real-world problems are
represented as objects with different attributes. All objects have
multiple relationships between them. Basically, it is a
combination of Object Oriented programming and a Relational
Database Model.
4. Float Data Model
DBMS
The float data model basically consists of a two-dimensional
array of data models that do not contain any duplicate
elements in the array. This data model has one drawback it
cannot store a large amount of data that is the tables can not
be of large size.
5. Context Data Model
The Context data model is simply a data model which consists
of more than one data model. For example, the Context data
model consists of ER Model, Object-Oriented Data Model, etc.
This model allows users to do more than one thing which each
individual data model can do.
6. Semi-Structured Data Model
Semi-Structured data models deal with the data in a flexible
way. Some entities may have extra attributes and some entities
may have some missing attributes. Basically, you can represent
data here in a flexible way.
Advantages of Data Models
1. Data Models help us in representing data accurately.
2. It helps us in finding the missing data and also in
minimizing Data Redundancy.
3. Data Model provides data security in a better way.
4. The data model should be detailed enough to be used for
building the physical database.
5. The information in the data model can be used for defining
the relationship between tables, primary and foreign keys,
and stored procedures.
DBMS
Disadvantages of Data Models
1. In the case of a vast database, sometimes it becomes
difficult to understand the data model.
2. You must have the proper knowledge of SQL to use
physical models.
3. Even smaller change made in structure require
modification in the entire application.
4. There is no set data manipulation language in DBMS.
5. To develop Data model one should know physical data
stored characteristics.
Conclusion
 Data modeling is the process of developing data model for
the data to be stored in a Database.
 Data Models ensure consistency in naming conventions,
default values, semantics, security while ensuring quality of
the data.
 Data Model structure helps to define the relational tables,
primary and foreign keys and stored procedures.
 There are three types of conceptual, logical, and physical.
 The main aim of conceptual model is to establish the
entities, their attributes, and their relationships.
 Logical data model defines the structure of the data
elements and set the relationships between them.
 A Physical Data Model describes the database specific
implementation of the data model.
DBMS
 The main goal of a designing data model is to make
certain that data objects offered by the functional team
are represented accurately.
 The biggest drawback is that even smaller change made in
structure require modification in the entire application.
 Reading this Data Modeling tutorial, you will learn from the
basic concepts such as What is Data Model? Introduction
to different types of Data Model, advantages,
disadvantages, and data model example.
DBMS
Unit 3

Database design

What is Database Design?


Database design can be generally defined as a collection of tasks or processes that
enhance the designing, development, implementation, and maintenance of enterprise
data management system. Designing a proper database reduces the maintenance cost
thereby improving data consistency and the cost-effective measures are greatly
influenced in terms of disk storage space. Therefore, there has to be a brilliant concept
of designing a database. The designer should follow the constraints and decide how
the elements correlate and what kind of data must be stored.

The main objectives behind database designing are to produce physical and logical
design models of the proposed database system. To elaborate this, the logical model
is primarily concentrated on the requirements of data and the considerations must be
made in terms of monolithic considerations and hence the stored physical data must
be stored independent of the physical conditions. On the other hand, the physical
database design model includes a translation of the logical design model of the
database by keep control of physical media using hardware resources and software
systems such as Database Management System (DBMS).

Why is Database Design important?


The important consideration that can be taken into account while emphasizing the
importance of database design can be explained in terms of the following points given
below.Database designs provide the blueprints of how the data is going to be stored
in a system. A proper design of a database highly affects the overall performance of
any application.

1. The designing principles defined for a database give a clear idea of the behavior
of any application and how the requests are processed.
2. Another instance to emphasize the database design is that a proper database
design meets all the requirements of users.
3. Lastly, the processing time of an application is greatly reduced if the constraints
of designing a highly efficient database are properly implemented.
4. Life Cycle
DBMS

5.

Design phases:

here are three main phases in designing a database management system


(DBMS):
1. Conceptual design: This phase focuses on understanding the
business requirements and identifying the high-level
entities, relationships, and attributes that will be stored in the
database. It is essentially a blueprint of the database, independent
of any specific DBMS product or technology.
DBMS

2. Logical design: This phase refines the conceptual design by


translating it into a specific data model, such as the relational
model. The logical design defines the tables, columns, data
types, and constraints that will be used to store the data.

3. Physical design: This phase takes the logical design and maps it to
the specific storage structures and access methods provided by the
chosen DBMS. The physical design considers factors such as
performance, scalability, and security.

Physical design phase in DBMS


DBMS
Each phase is important for creating a well-designed database that
meets the needs of the organization. The phases are iterative, and it is
common to revisit earlier phases as the design progresses.

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

Why do we need Normalization?

The main reason for normalizing the relations is removing these anomalies. Failure to
eliminate anomalies leads to data redundancy and can cause data integrity and other
problems as the database grows. Normalization consists of a series of guidelines that
helps to guide you in creating a good database structure.

Data modification anomalies can be categorized into three types:

o Insertion Anomaly: Insertion Anomaly refers to when one cannot insert a new tuple
into a relationship due to lack of data.
o Deletion Anomaly: The delete anomaly refers to the situation where the deletion of
data results in the unintended loss of some other important data.
o Updatation Anomaly: The update anomaly is when an update of a single data value
requires multiple rows of data to be updated.

Types of Normal Forms:


Normalization works through a series of stages called Normal forms. The normal forms
apply to individual relations. The relation is said to be in particular normal form if it
satisfies constraints.

Following are the various types of Normal forms:


DBMS

Normal Descrip on
Form

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

2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are fully functional
dependent on the primary key.

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

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

First Normal Form (1NF)


o A relation will be 1NF if it contains an atomic value.
o It states that an attribute of a table cannot hold multiple values. It must hold only
single-valued attribute.
o First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.

Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute


EMP_PHONE.

EMPLOYEE table:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385, UP
9064738238

20 Harry 8574783832 Bihar

12 Sam 7390372389, Punjab


8589830302

The decomposition of the EMPLOYEE table into 1NF has been shown below:
DBMS
EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385 UP

14 John 9064738238 UP

20 Harry 8574783832 Bihar

12 Sam 7390372389 Punjab

12 Sam 8589830302 Punjab

Second Normal Form (2NF)


ADVERTISEMENT

ADVERTISEMENT

o In the 2NF, relational must be in 1NF.


o In the second normal form, all non-key attributes are fully functional dependent on the
primary key

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

TEACHER_ID SUBJECT TEACHER_AGE

25 Chemistry 30

25 Biology 30

47 English 35

83 Math 38

83 Computer 38
DBMS
In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID
which is a proper subset of a candidate key. That's why it violates the rule for 2NF.

To convert the given table into 2NF, we decompose it into two tables:

TEACHER_DETAIL table:

TEACHER_ID TEACHER_AGE

25 30

47 35

83 38

TEACHER_SUBJECT table:

TEACHER_ID SUBJECT

25 Chemistry

25 Biology

47 English

83 Math

83 Computer

Third Normal Form (3NF)


o A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
o 3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
o If there is no transitive dependency for non-prime attributes, then the relation must be
in third normal form.
DBMS
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:

EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY

222 Harry 201010 UP Noida

333 Stephan 02228 US Boston

444 Lan 60007 US Chicago

555 Katharine 06389 UK Norwich

666 John 462007 MP Bhopal

Super key in the table above:

1. {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....so o


n

Candidate key: {EMP_ID}

Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.

Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent


on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY) transitively
dependent on super key(EMP_ID). It violates the rule of third normal form.

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:
DBMS
EMP_ID EMP_NAME EMP_ZIP

222 Harry 201010

333 Stephan 02228

444 Lan 60007

555 Katharine 06389

666 John 462007

EMPLOYEE_ZIP table:

EMP_ZIP EMP_STATE EMP_CITY

201010 UP Noida

02228 US Boston

60007 US Chicago

06389 UK Norwich

462007 MP Bhopal

Boyce Codd normal form (BCNF)


o BCNF is the advance version of 3NF. It is stricter than 3NF.
o A table is in BCNF if every functional dependency X → Y, X is the super key of the table.
o For BCNF, the table should be in 3NF, and for every FD, LHS is super key.

Example: Let's assume there is a company where employees work in more than one
department.

EMPLOYEE table:
DBMS
EMP_ID EMP_COUNTRY EMP_DEPT DEPT_TYPE EMP_DEPT_NO

264 India Designing D394 283

264 India Testing D394 300

364 UK Stores D283 232

364 UK Developing D283 549

In the above table Functional dependencies are as follows:

1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate key: {EMP-ID, EMP-DEPT}

The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.

To convert the given table into BCNF, we decompose it into three tables:

EMP_COUNTRY table:

EMP_ID EMP_COUNTRY

264 India

264 India

EMP_DEPT table:

EMP_DEPT DEPT_TYPE EMP_DEPT_NO

Designing D394 283

Testing D394 300


DBMS
Stores D283 232

Developing D283 549

EMP_DEPT_MAPPING table:

EMP_ID EMP_DEPT

D394 283

D394 300

D283 232

D283 549

Functional dependencies:

1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate keys:

For the first table: EMP_ID


For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}

Now, this is in BCNF because left side part of both the functional dependencies is a
key.

ER model entity set/ ER model: (ER diagram basic structure)

ER (Entity Relationship) Diagram in DBMS


o ER model stands for an Entity-Relationship model. It is a high-level data model. This
model is used to define the data elements and relationship for a specified system.
DBMS
o It develops a conceptual design for the database. It also develops a very simple and
easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.

For example, Suppose we design a school database. In this database, the student will
be an entity with attributes like address, name, id, age, etc. The address can be another
entity with attributes like city, street name, pin code, etc and there will be a relationship
between them.

Component of ER Diagram
DBMS

1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.

Consider an organization as an example- manager, product, employee, department


etc. can be taken as an entity.

a. Weak Entity
DBMS
An entity that depends on another entity called a weak entity. The weak entity doesn't
contain any key attribute of its own. The weak entity is represented by a double
rectangle.

2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to represent
an attribute.

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

a. Key Attribute

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

b. Composite Attribute

An attribute that composed of many other attributes is known as a composite attribute.


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

c. Multivalued Attribute

An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued attribute.

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

d. Derived Attribute

An attribute that can be derived from other attribute is known as a derived attribute.
It can be represented by a dashed ellipse.

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

3. Relationship
A relationship is used to describe the relation between entities. Diamond or rhombus
is used to represent the relationship.
DBMS

Types of relationship are as follows:

a. One-to-One Relationship

When only one instance of an entity is associated with the relationship, then it is known
as one to one relationship.

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

ADVERTISEMENT

ADVERTISEMENT

b. One-to-many relationship

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

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

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

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

ADVERTISEMENT

d. Many-to-many relationship

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

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

Cardinality in DBMS (Mapping Constraints)


DBMS
DBMS stands for Database Management System, which is a tool, or a software used to
do various operations on a Database like the Creation of the Database, Deletion of the
Database, or Updating the current Database. To simplify processing and data querying,
the most popular types of Databases currently in use typically model their data as rows
and columns in a set of tables. The data may then be handled, updated, regulated, and
structured with ease. For writing and querying data, most Databases employ
Structured Query Language (SQL).

Cardinality
DBMS
Cardinality means how the entities are arranged to each other or what is the
relationship structure between entities in a relationship set. In a Database
Management System, Cardinality represents a number that denotes how many times
an entity is participating with another entity in a relationship set. The Cardinality of
DBMS is a very important attribute in representing the structure of a Database. In a
table, the number of rows or tuples represents the Cardinality.

Cardinality Ratio
Cardinality ratio is also called Cardinality Mapping, which represents the mapping of
one entity set to another entity set in a relationship set. We generally take the example
of a binary relationship set where two entities are mapped to each other.

Cardinality is very important in the Database of various businesses. For example, if we


want to track the purchase history of each customer then we can use the one-to-many
cardinality to find the data of a specific customer. The Cardinality model can be used
in Databases by Database Managers for a variety of purposes, but corporations often
use it to evaluate customer or inventory data.

There are four types of Cardinality Mapping in Database Management Systems:

1. One to one
2. Many to one
3. One to many
4. Many to many

One to One
One to one cardinality is represented by a 1:1 symbol. In this, there is at most one
relationship from one entity to another entity. There are a lot of examples of one-to-
one cardinality in real life databases.

For example, one student can have only one student id, and one student id can
belong to only one student. So, the relationship mapping between student and
student id will be one to one cardinality mapping.

Another example is the relationship between the director of the school and the school
because one school can have a maximum of one director, and one director can belong
to only one school.
DBMS
Note: it is not necessary that there would be a mapping for all entities in an entity
set in one-to-one cardinality. Some entities cannot participate in the mapping.

Many to One Cardinality:


In many to one cardinality mapping, from set 1, there can be multiple sets that can
make relationships with a single entity of set 2. Or we can also describe it as from set
2, and one entity can make a relationship with more than one entity of set 1.

One to one Cardinality is the subset of Many to one Cardinality. It can be represented
by M:1.

ADVERTISEMENT

For example, there are multiple patients in a hospital who are served by a single
doctor, so the relationship between patients and doctors can be represented by Many
to one Cardinality.
DBMS

One to Many Cardinalities:


In One-to-many cardinality mapping, from set 1, there can be a maximum single set
that can make relationships with a single or more than one entity of set 2. Or we can
also describe it as from set 2, more than one entity can make a relationship with only
one entity of set 1.

One to one cardinality is the subset of One-to-many Cardinality. It can be represented


by 1: M.

For Example, in a hospital, there can be various compounders, so the relationship


between the hospital and compounders can be mapped through One-to-many
Cardinality.
DBMS

Many to Many Cardinalities:


In many, many cardinalities mapping, there can be one or more than one entity that
can associate with one or more than one entity of set 2. In the same way from the end
of set 2, one or more than one entity can make a relation with one or more than one
entity of set 1.

It is represented by M: N or N: M.

One to one cardinality, One to many cardinalities, and Many to one cardinality is the
subset of the many to many cardinalities.

ADVERTISEMENT

For Example, in a college, multiple students can work on a single project, and a single
student can also work on multiple projects. So, the relationship between the project
and the student can be represented by many to many cardinalities.
DBMS

Appropriate Mapping Cardinality


Evidently, the real-world context in which the relation set is modeled determines the
Appropriate Mapping Cardinality for a specific relation set.

o We can combine relational tables with many involved tables if the Cardinality is one-
to-many or many-to-one.
o One entity can be combined with a relation table if it has a one-to-one relationship
and total participation, and two entities can be combined with their relation to form a
single table if both of them have total participation.
o We cannot mix any two tables if the Cardinality is many-to-many.

An entity type should have a key attribute which uniquely identifies each entity
in the entity set, but there exists some entity type for which key attribute can’t be
defined. These are called Weak Entity type.
The entity sets which do not have sufficient attributes to form a primary key are
known as weak entity sets and the entity sets which have a primary key are
known as strong entity sets.
As the weak entities do not have any primary key, they cannot be identified on
their own, so they depend on some other entity (known as owner entity). The
weak entities have total participation constraint (existence dependency) in its
identifying relationship with owner identity. Weak entity types have partial keys.
Partial Keys are set of attributes with the help of which the tuples of the weak
entities can be distinguished and identified.
DBMS
Note – Weak entity always has total participation but Strong entity may not have
total participation.
Weak entity is depend on strong entity to ensure the existence of weak entity.
Like strong entity, weak entity does not have any primary key, It has partial
discriminator key. Weak entity is represented by double rectangle. The relation
between one strong and one weak entity is represented by double diamond.

Weak entities are represented with double rectangular box in the ER Diagram
and the identifying relationships are represented with double diamond. Partial
Key attributes are represented with dotted lines.

Example-1:
In the below ER Diagram, ‘Payment’ is the weak entity. ‘Loan Payment’ is the
identifying relationship and ‘Payment Number’ is the partial key. Primary Key of
the Loan along with the partial key would be used to identify the records.
DBMS
Example-2:
The existence of rooms is entirely dependent on the existence of a hotel. So room
can be seen as the weak entity of the hotel.
Example-3:
The bank account of a particular bank has no existence if the bank doesn’t exist
anymore.
Example-4:
A company may store the information of dependents (Parents, Children, Spouse)
of an Employee. But the dependents don’t have existence without the employee.
So Dependent will be weak entity type and Employee will be Identifying Entity
type for Dependent.
Other examples:

Strong entity | Weak entity


Order | Order Item
Employee | Dependent
Class | Section
Host | Logins
Note – Strong-Weak entity set always has parent-child relationship.

Attributes:

In a Database Management System (DBMS), attributes are the individual


characteristics or properties that describe an entity within a database. An entity
is a real-world object or concept that is represented in the database. For example,
in a student database, the entity might be "Student" and the attributes could be
"Student Name", "Student ID", "Student Age", and "Student Major".
Attributes are essentially the building blocks of a database table. Each table in a
database is composed of columns and rows. The columns represent the
attributes of the entity, and the rows represent individual records of the entity.
For instance, in the student database example, the table would have columns for
"Student Name", "Student ID", "Student Age", and "Student Major", and each row
would represent a specific student with their corresponding information.
There are different types of attributes in a DBMS, including:
 Simple Attributes: These are atomic values that cannot be further
decomposed. An example of a simple attribute is a student's name or ID
number.
 Composite Attributes: These are attributes that are formed by grouping
together two or more simple attributes. An example of a composite
attribute is a student's address, which could be composed of separate
attributes for street address, city, state, and zip code.
DBMS
 Single-Valued Attributes: These attributes can only have one value for
each entity instance. For example, a student can only have one student ID.
 Multi-Valued Attributes: These attributes can have multiple values for
each entity instance. An example of a multi-valued attribute is a student's
course enrollment, where a student can be enrolled in multiple courses at
a time.
 Derived Attributes: These attributes are not stored directly in the
database but are calculated from other attributes. An example of a derived
attribute is a student's grade point average (GPA), which can be calculated
from the student's course grades.
Constrains:
Constraints in a DBMS (Database Management System) are essentially rules
or limitations applied to data within a database. They act as safeguards to
ensure data integrity, consistency, and adherence to business rules.
Constraints prevent the entry of invalid or inconsistent data, thereby
maintaining the accuracy and reliability of your database.
Why are Constraints Important?
Imagine a library database without constraints. You could end up with
duplicate entries for the same book, invalid genre classifications, or even
nonsensical publication years. Constraints help prevent such issues by
enforcing specific rules on the data.
Types of Constraints in DBMS:
There are several key types of constraints commonly used in relational
databases:
1. Domain Constraints: These define the valid set of values an
attribute can hold. For instance, an attribute storing an age might
have a domain constraint restricting values to be between 0 and 120.
Data types (e.g., integer, string) also act as a form of domain
constraint.
2. Key Constraints: These ensure the uniqueness and identity of rows
within a table. They come in different flavors:
o Primary Key: A table can have only one primary key, which is
a minimal set of columns that uniquely identifies each row.
Think of it as the main identifier for a record. In the library
example, the primary key could be an ISBN number (assuming
it's unique for each book).
o Candidate Key: Any set of columns that can uniquely identify
rows in a table. There can be multiple candidate keys, but only
one is chosen as the primary key.
o Alternate Key: Any additional key that uniquely identifies
rows, but is not chosen as the primary key.
DBMS
3. Entity Integrity Constraints: These enforce that a primary key
cannot be null (have no value). A table cannot exist without a way to
identify its rows!
4. Referential Integrity Constraints: These ensure relationships
between tables are maintained. They prevent orphaned data, where a
foreign key (a column referencing a primary key in another table)
points to a non-existent record. Imagine a library database with a
table for borrowing books. A foreign key in the "Borrowed Books"
table might reference the primary key in the "Books" table.
Referential integrity constraints would prevent someone from
borrowing a non-existent book.
Benefits of Constraints:
 Improved Data Quality: Constraints prevent invalid or inconsistent
data from entering the database.
 Data Consistency: They enforce rules across the database,
maintaining a unified structure.
 Reduced Errors: Constraints help prevent errors during data
manipulation, such as insertions, updates, or deletions.
 Enhanced Data Relationships: Referential integrity constraints
ensure relationships between tables are valid.

ERD symbols and notations:

Notation of ER diagram
Database can be represented using the notations. In ER diagram, many notations are
used to express the cardinality. These notations are as follows:
DBMS

Fig: Notations of ER diagram

ERD Issues:

1) Use of Entity Set vs Attributes


The use of an entity set or attribute depends on the structure of the real-world
enterprise that is being modelled and the semantics associated with its attributes. It
leads to a mistake when the user use the primary key of an entity set as an attribute of
another entity set. Instead, he should use the relationship to do so. Also, the primary
key attributes are implicit in the relationship set, but we designate it in the relationship
sets.

2) Use of Entity Set vs. Relationship Sets


It is difficult to examine if an object can be best expressed by an entity set or relationship set.
To understand and determine the right use, the user need to designate a relationship set for
describing an action that occurs in-between the entities. If there is a requirement of
representing the object as a relationship set, then its better not to mix it with the entity set.
DBMS
3) Use of Binary vs n-ary Relationship Sets
Generally, the relationships described in the databases are binary relationships. However,
non-binary relationships can be represented by several binary relationships. For example, we
can create and represent a ternary relationship 'parent' that may relate to a child, his father,
as well as his mother. Such relationship can also be represented by two binary relationships
i.e, mother and father, that may relate to their child. Thus, it is possible to represent a non-
binary relationship by a set of distinct binary relationships.

4) Placing Relationship Attributes


The cardinality ratios can become an affective measure in the placement of the
relationship attributes. So, it is better to associate the attributes of one-to-one or one-
to-many relationship sets with any participating entity sets, instead of any relationship
set. The decision of placing the specified attribute as a relationship or entity attribute
should possess the charactestics of the real world enterprise that is being modelled.

For example, if there is an entity which can be determined by the combination of


participating entity sets, instead of determing it as a separate entity. Such type of
attribute must be associated with the many-to-many relationship sets.

Thus, it requires the overall knowledge of each part that is involved inb desgining and
modelling an ER diagram. The basic requirement is to analyse the real-world enterprise
and the connectivity of one entity or attribute with other.

12 Codd's Rules
Every database has tables, and constraints cannot be referred to as a rational database
system. And if any database has only relational data model, it cannot be a Relational
Database System (RDBMS). So, some rules define a database to be the correct
RDBMS. These rules were developed by Dr. Edgar F. Codd (E.F. Codd) in 1985, who
has vast research knowledge on the Relational Model of database Systems. Codd
presents his 13 rules for a database to test the concept of DBMS against his relational
model, and if a database follows the rule, it is called a true relational database
(RDBMS). These 13 rules are popular in RDBMS, known as Codd's 12 rules.
DBMS

Rule 0: The Foundation Rule


The database must be in relational form. So that the system can handle the database
through its relational capabilities.

Rule 1: Information Rule


A database contains various information, and this information must be stored in each
cell of a table in the form of rows and columns.

Rule 2: Guaranteed Access Rule


Every single or precise data (atomic value) may be accessed logically from a relational
database using the combination of primary key value, table name, and column name.
DBMS
Rule 3: Systematic Treatment of Null Values
This rule defines the systematic treatment of Null values in database records. The null
value has various meanings in the database, like missing the data, no value in a cell,
inappropriate information, unknown data and the primary key should not be null.

Rule 4: Active/Dynamic Online Catalog based on the


relational model
It represents the entire logical structure of the descriptive database that must be stored
online and is known as a database dictionary. It authorizes users to access the database
and implement a similar query language to access the database.

Rule 5: Comprehensive Data SubLanguage Rule


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

Rule 6: View Updating Rule


All views table can be theoretically updated and must be practically updated by the
database systems.

Rule 7: Relational Level Operation (High-Level Insert,


Update and delete) Rule
A database system should follow high-level relational operations such as insert,
update, and delete in each level or a single row. It also supports union, intersection
and minus operation in the database system.

Rule 8: Physical Data Independence Rule


All stored data in a database or an application must be physically independent to
access the database. Each data should not depend on other data or an application. If
data is updated or the physical structure of the database is changed, it will not show
any effect on external applications that are accessing the data from the database.
DBMS
Rule 9: Logical Data Independence Rule
It is similar to physical data independence. It means, if any changes occurred to the
logical level (table structures), it should not affect the user's view (application). For
example, suppose a table either split into two tables, or two table joins to create a
single table, these changes should not be impacted on the user view application.

Rule 10: Integrity Independence Rule


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

Rule 11: Distribution Independence Rule


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

Rule 12: Non Subversion Rule


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

Unit 4
SQL : create and manage using create and alter
DBMS
SQL | DDL, DML, TCL and DCL

DDL (Data Definition Language) :


Data Definition Language is used to define the database structure or schema. DDL is
also used to specify additional properties of the data. The storage structure and
access methods used by the database system by a set of statements in a special
type of DDL called a data storage and definition language. These statements define
the implementation details of the database schema, which are usually hidden from
the users. The data values stored in the database must satisfy certain consistency
constraints.
For example, suppose the university requires that the account balance of a
department must never be negative. The DDL provides facilities to specify such
constraints. The database system checks these constraints every time the database
is updated. In general, a constraint can be an arbitrary predicate pertaining to the
database. However, arbitrary predicates may be costly to the test. Thus, the
database system implements integrity constraints that can be tested with minimal
overhead.

1. Domain Constraints : A domain of possible values must be associated with


every attribute (for example, integer types, character types, date/time types).
Declaring an attribute to be of a particular domain acts as the constraints on
the values that it can take.
2. Referential Integrity : There are cases where we wish to ensure that a value
appears in one relation for a given set of attributes also appear in a certain set
of attributes in another relation i.e. Referential Integrity. For example, the
department listed for each course must be one that actually exists.
3. Assertions : An assertion is any condition that the database must always
satisfy. Domain constraints and Integrity constraints are special form of
assertions.
4. Authorization : We may want to differentiate among the users as far as the
type of access they are permitted on various data values in database. These
differentiation are expressed in terms of Authorization. The most common
being :
read authorization – which allows reading but not modification of data ;
insert authorization – which allow insertion of new data but not modification of
existing data
update authorization – which allows modification, but not deletion.
DBMS
Some Commands:

CREATE : to create objects in database


ALTER : alters the structure of database
DROP : delete objects from database
RENAME : rename an objects
Following SQL DDL-statement defines the department table :

create table department


(dept_name char(20),
building char(15),
budget numeric(12,2));
Execution of the above DDL statement creates the department table with three
columns – dept_name, building, and budget; each of which has a specific datatype
associated with it.

DML (Data Manipulation Language) :


DML statements are used for managing data with in schema objects.
DML are of two types –

1. Procedural DMLs : require a user to specify what data are needed and how
to get those data.
2. Declarative DMLs (also referred as Non-procedural DMLs) : require a user
to specify what data are needed without specifying how to get those data.
Declarative DMLs are usually easier to learn and use than procedural DMLs.
However, since a user does not have to specify how to get the data, the database
system has to figure out an efficient means of accessing data.
Some Commands :

SELECT: retrieve data from the database


INSERT: insert data into a table
UPDATE: update existing data within a table
DELETE: deletes all records from a table, space for the records remain
Example of SQL query that finds the names of all instructors in the History
department :

select instructor.name
from instructor
DBMS
where instructor.dept_name = 'History';
The query specifies that those rows from the table instructor where the dept_name is
History must be retrieved and the name attributes of these rows must be displayed.

TCL (Transaction Control Language) :


Transaction Control Language commands are used to manage transactions in the
database. These are used to manage the changes made by DML-statements. It also
allows statements to be grouped together into logical transactions.
Examples of TCL commands –

COMMIT: Commit command is used to permanently save any transaction


into the database.
ROLLBACK: This command restores the database to last committed state.
It is also used with savepoint command to jump to a savepoint
in a transaction.
SAVEPOINT: Savepoint command is used to temporarily save a transaction so
that you can rollback to that point whenever necessary.

DCL (Data Control Language) :


A Data Control Language is a syntax similar to a computer programming language
used to control access to data stored in a database (Authorization). In particular, it is
a component of Structured Query Language (SQL).
Examples of DCL commands :

GRANT: allow specified users to perform specified tasks.


REVOKE: cancel previously granted or denied permissions.
The operations for which privileges may be granted to or revoked from a user or role
apply to both the Data definition language (DDL) and the Data manipulation
language (DML), and may include CONNECT, SELECT, INSERT, UPDATE,
DELETE, EXECUTE and USAGE.
In the Oracle database, executing a DCL command issues an implicit commit.
Hence, you cannot roll back the command.
References : kakeboksen.td.org.uit.no

DDL Statements for Creating and Managing Tables:


CREATE TABLE:
 Creates a new table in the database.
 Syntax:
DBMS
SQL
CREATE TABLE table_name (
column1 data_type constraints,
column2 data_type constraints,
...
);
 Example:
SQL
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE
);
ALTER TABLE:
 Modifies the structure of an existing table.
 Common uses:
o Adding a new column:
SQL
ALTER TABLE table_name ADD column_name data_type constraints;
- Dropping a column:
SQL
ALTER TABLE table_name DROP COLUMN column_name;
- Renaming a column:
SQL
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
- Modifying a column's data type or constraints:
SQL
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE
new_data_type constraints;
Other Common DDL Statements:
 DROP TABLE: Deletes a table from the database.
 TRUNCATE TABLE: Deletes all data from a table, but keeps the table
structure.
 CREATE INDEX: Creates an index on one or more columns to improve query
performance.
 DROP INDEX: Deletes an index.
DBMS
DDL Statements for Creating and Managing Tables:
CREATE TABLE:
 Creates a new table in the database.
 Syntax:
SQL
CREATE TABLE table_name (
column1 data_type constraints,
column2 data_type constraints,
...
);
 Example:
SQL
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE
);
ALTER TABLE:
 Modifies the structure of an existing table.
 Common uses:
o Adding a new column:
SQL
ALTER TABLE table_name ADD column_name data_type constraints;
- Dropping a column:
SQL
ALTER TABLE table_name DROP COLUMN column_name;
- Renaming a column:
SQL
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
- Modifying a column's data type or constraints:
SQL
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE
new_data_type constraints;

DML Commands in SQL


DML is an abbreviation of Data Manipulation Language.

The DML commands in Structured Query Language change the data present in the
SQL database. We can easily access, store, modify, update and delete the existing
records from the database using DML commands.

Following are the four main DML commands in SQL:

1. SELECT Command
DBMS
2. INSERT Command
3. UPDATE Command
4. DELETE Command

SELECT DML Command


SELECT is the most important data manipulation command in Structured Query
Language. The SELECT command shows the records of the specified table. It also shows
the particular record of a particular column by using the WHERE clause.

Backward Skip 10sPlay VideoForward Skip 10s

Syntax of SELECT DML command

1. SELECT column_Name_1, column_Name_2, ….., column_Name_N FROM Name


_of_table;

Here, column_Name_1, column_Name_2, ….., column_Name_N are the names of


those columns whose data we want to retrieve from the table.

If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:

1. SELECT * FROM table_name;


Examples of SELECT Command
Example 1: This example shows all the values of every column from the table.

1. SELECT * FROM Student;

This SQL statement displays the following values of the student table:

Student_ID Student_Name Student_Marks

BCA1001 Abhay 85

BCA1002 Anuj 75

BCA1003 Bheem 60
DBMS
BCA1004 Ram 79

BCA1005 Sumit 80

Example 2: This example shows all the values of a specific column from the table.

ADVERTISEMENT

1. SELECT Emp_Id, Emp_Salary FROM Employee;

This SELECT statement displays all the values of Emp_Salary and Emp_Id column
of Employee table:

Emp_Id Emp_Salary

201 25000

202 45000

203 30000

204 29000

205 40000

Example 3: This example describes how to use the WHERE clause with the SELECT
DML command.

Let's take the following Student table:

Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1002 Ankit 75

BCA1003 Bheem 80
DBMS
BCA1004 Ram 79

BCA1005 Sumit 80

If you want to access all the records of those students whose marks is 80 from the
above table, then you have to write the following DML command in SQL:

1. SELECT * FROM Student WHERE Stu_Marks = 80;

The above SQL query shows the following table in result:

Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1003 Bheem 80

BCA1005 Sumit 80

INSERT DML Command


INSERT is another most important data manipulation command in Structured Query
Language, which allows users to insert data in database tables.

Syntax of INSERT Command

1. INSERT INTO TABLE_NAME ( column_Name1 , column_Name2 , column_Nam


e3 , .... column_NameN ) VALUES (value_1, value_2, value_3, .... value_N ) ;

Examples of INSERT Command


Example 1: This example describes how to insert the record in the database table.

Let's take the following student table, which consists of only 2 records of the student.

Stu_Id Stu_Name Stu_Marks Stu_Age


DBMS
101 Ramesh 92 20

201 Jatin 83 19

Suppose, you want to insert a new record into the student table. For this, you have to
write the following DML INSERT command:

1. INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104,


Anmol, 89, 19);

UPDATE DML Command


UPDATE is another most important data manipulation command in Structured Query
Language, which allows users to update or modify the existing data in database tables.

Syntax of UPDATE Command

1. UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = val


ue_N] WHERE CONDITION;
ADVERTISEMENT

ADVERTISEMENT

Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is the
name of the table whose values you want to update.

Examples of the UPDATE command


Example 1: This example describes how to update the value of a single field.

Let's take a Product table consisting of the following records:

Product_Id Product_Name Product_Price Product_Quan ty

P101 Chips 20 20

P102 Chocolates 60 40

P103 Maggi 75 5
DBMS
P201 Biscuits 80 20

P203 Namkeen 40 50

Suppose, you want to update the Product_Price of the product whose Product_Id is
P102. To do this, you have to write the following DML UPDATE command:

1. UPDATE Product SET Product_Price = 80 WHERE Product_Id = 'P102' ;

Example 2: This example describes how to update the value of multiple fields of
the database table.

Let's take a Student table consisting of the following records:

Stu_Id Stu_Name Stu_Marks Stu_Age

101 Ramesh 92 20

201 Jatin 83 19

202 Anuj 85 19

203 Monty 95 21

102 Saket 65 21

103 Sumit 78 19

104 Ashish 98 20

Suppose, you want to update Stu_Marks and Stu_Age of that student whose Stu_Id is
103 and 202. To do this, you have to write the following DML Update command:

1. UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WHERE Stu_Id = 103 AN


D Stu_Id = 202;

DELETE DML Command


DBMS
DELETE is a DML command which allows SQL users to remove single or multiple
existing records from the database tables.

This command of Data Manipulation Language does not delete the stored data
permanently from the database. We use the WHERE clause with the DELETE command
to select specific rows from the table.

Syntax of DELETE Command

1. DELETE FROM Table_Name WHERE condition;


Examples of DELETE Command
Example 1: This example describes how to delete a single record from the table.

Let's take a Product table consisting of the following records:

Product_Id Product_Name Product_Price Product_Quan ty

P101 Chips 20 20

P102 Chocolates 60 40

P103 Maggi 75 5

P201 Biscuits 80 20

P203 Namkeen 40 50

Suppose, you want to delete that product from the Product table whose Product_Id is
P203. To do this, you have to write the following DML DELETE command:

ADVERTISEMENT

1. DELETE FROM Product WHERE Product_Id = 'P202' ;

Example 2: This example describes how to delete the multiple records or rows
from the database table.

Let's take a Student table consisting of the following records:


DBMS
Stu_Id Stu_Name Stu_Marks Stu_Age

101 Ramesh 92 20

201 Jatin 83 19

202 Anuj 85 19

203 Monty 95 21

102 Saket 65 21

103 Sumit 78 19

104 Ashish 98 20

Suppose, you want to delete the record of those students whose Marks is greater than
70. To do this, you have to write the following DML Update command:

1. DELETE FROM Student WHERE Stu_Marks > 70 ;

Retrieving data using the SQL Select Statement


The SQL SELECT statement is your workhorse for retrieving data from relational
databases. It allows you to pick specific columns and rows from a table or multiple
tables based on certain conditions. Here's a breakdown of its core components:
Basic Structure:
SQL
SELECT column1, column2, ...
FROM table_name
WHERE condition;
 SELECT clause: Specifies the columns you want to retrieve. You can use *
to select all columns.
 FROM clause: Indicates the table(s) containing the data you want.
DBMS
 WHERE clause (Optional): Filters the results based on a specific condition.
You can use comparison operators (=, >, <, etc.) and logical operators (AND,
OR, NOT) to refine your selection.

Additional Clauses (Optional):


 GROUP BY clause: Groups data based on one or more columns for
aggregate operations (e.g., COUNT, SUM, AVG).
 HAVING clause (Used with GROUP BY): Filters groups based on a
condition applied to aggregate values.
 ORDER BY clause: Sorts the results based on one or more columns in
ascending or descending order.
Examples:
1. Select all columns from the Customers table:
SQL
SELECT * FROM Customers;
2. Select CustomerName and City from Customers where Country is 'USA':
SQL
SELECT CustomerName, City
FROM Customers
WHERE Country = 'USA';
3. Group customers by Country and count the number of customers in each
country:
SQL
SELECT Country, COUNT(*) AS NumberOfCustomers
FROM Customers
GROUP BY Country;

4. Select customers with orders exceeding $1000, ordered by order amount in


descending order:
SQL
SELECT c.CustomerName, o.OrderAmount
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderAmount > 1000
ORDER BY o.OrderAmount DESC;
By understanding these core concepts and practicing with various SELECT statement
combinations, you can effectively retrieve and analyze data from your relational
databases.
DBMS
Restricting and Sorting Data
In SQL, you can restrict and sort data using two main clauses within a SELECT
statement:

1. WHERE Clause:
 Used to filter and restrict the rows retrieved based on specific conditions.
 Placed after the FROM clause in a SELECT statement.
 Conditions are written using comparison operators (=, >, <, !=, etc.) and
logical operators (AND, OR, NOT).
 You can also use functions and wildcards (% and _) for more complex filtering.
Example:
SQL
SELECT name, city
FROM customers
WHERE country = 'USA' AND (city = 'New York' OR city = 'Los Angeles');
This query selects name and city columns from the customers table, restricted to
customers in the USA and located either in New York or Los Angeles.
2. ORDER BY Clause:
 Used to sort the retrieved data in a specific order.
 Placed at the end of the SELECT statement (after WHERE clause if used).
 Sorts by one or more columns specified after ORDER BY.
 Use ASC for ascending order (default) and DESC for descending order.
Example:
SQL
SELECT product_name, price
FROM products
ORDER BY price DESC;
This query selects product_name and price from the products table, sorted by price
in descending order (most expensive first).
Combining WHERE and ORDER BY:

You can combine these clauses for more specific results. The WHERE clause filters
the data first, then the ORDER BY clause sorts the filtered results.

Additional Points:
DBMS
 You can use functions in the WHERE clause for complex conditions (e.g.,
WHERE SUBSTRING(name, 1, 3) = 'XYZ').
 You can sort by multiple columns in the ORDER BY clause (e.g., ORDER BY
name ASC, city DESC).

 Some databases offer additional options for sorting, like case-sensitivity


control.

For more advanced filtering and sorting techniques, refer to your specific SQL
database documentation.

Using Single-Row Functions


Single-row functions in SQL operate on a single row of data at a time and return a
single output value for each row. They are useful for various tasks like text
manipulation, mathematical calculations, date operations, and data type
conversions.

Here are some common categories of single-row functions:

1. String Functions:
 CONCAT: Merges multiple strings into a single string. (e.g.,
CONCAT(first_name, ' ', last_name))
 LENGTH: Returns the number of characters in a string. (e.g., LENGTH(name))
 SUBSTR: Extracts a portion of a string based on starting position and length.
(e.g., SUBSTR(address, 1, 10))
 INSTR: Locates the first occurrence of a substring within a string and returns
its position. (e.g., INSTR(email, '@'))
 UPPER/LOWER: Converts a string to uppercase or lowercase. (e.g.,
UPPER(city))
 TRIM: Removes leading/trailing spaces from a string. (e.g., TRIM(BOTH ' '
FROM description))

2. Numeric Functions:
DBMS
 ROUND: Rounds a number to a specified number of decimal places. (e.g.,
ROUND(price, 2))
 ABS: Returns the absolute value of a number. (e.g., ABS(balance))
 MOD: Calculates the modulus (remainder) of a division operation. (e.g.,
MOD(quantity, 10))
 POWER: Raises a number to a specified power. (e.g., POWER(2, 3))
 CEIL/FLOOR: Rounds a number up (ceiling) or down (floor) to the nearest
integer. (e.g., CEIL(average_rating))
3. Date Functions:
 CURDATE/CURTIME/NOW: Returns the current date, time, or both.
 YEAR/MONTH/DAY: Extracts specific components (year, month, day) from a
date value. (e.g., YEAR(order_date))
 DATE_ADD/SUB: Adds or subtracts a specified interval to/from a date value.
(e.g., DATE_ADD(hire_date, INTERVAL 1 YEAR))
 DATEDIFF: Calculates the difference between two date values in days.
4. Conversion Functions:
 CAST: Converts a value from one data type to another. (e.g., CAST(price AS
INT))

 CONVERT: Similar to CAST, but might offer more database-specific


conversions.
Using Single-Row Functions in SELECT:

You can directly use single-row functions within the SELECT clause of your query to
manipulate data before retrieval.

Example:
SQL
SELECT customer_id, UPPER(last_name) AS full_name,
ROUND(average_order_value, 2) AS avg_order_value
FROM orders
GROUP BY customer_id;
This query retrieves customer ID, last name converted to uppercase (UPPER), and
average order value rounded to two decimal places (ROUND) from the orders table,
grouped by customer.
Using Single-Row Functions in WHERE:
DBMS
Single-row functions can also be used within the WHERE clause to filter data based
on specific conditions involving function results.

Example:
SQL
SELECT product_id, name
FROM products
WHERE LENGTH(description) > 200;
This query selects product ID and name from the products table where the
description length (LENGTH) exceeds 200 characters.

By understanding and using single-row functions effectively, you can enhance your
SQL queries to perform various data transformations and filtering operations on a
row-by-row basis.

Conversion Functions and conditional expressions


In SQL, the conversion functions are used to convert a value from
one datatype to another. Generally, the form of the function
names follows the convention datatype TO datatype. The first
datatype is the input datatype. The second datatype is the output
datatype.

List of Functions

Following is a list of functions available in the SQL server for


Conversion of one datatype to another −

Sr.No. Function & Description

CAST
1
Is used to transform the numerical data into character or string data.

CONVERT
2
Is used to transform an expression from one data type to another.

PARSE
3
Is used to convert a string data to the desired data format and returns
the outcome as an expression.
DBMS
TRY_CAST
4
Is used to return the expression in the chosen data type.

TRY_CONVERT
5
Is used to change the datatype of an expression.

TRY_PARSE
6
Is used to return a result of an expression that has been converted to
the specified data type, or NULL if the conversion is unsuccessful.

SQL | Condi onal Expressions


Following are Condi onal Expressions in SQL
1. The CASE Expression: Let you use IF-THEN-ELSE statements without
having to invoke procedures.
In a simple CASE expression, the SQL searches for the first
WHEN……THEN pair for which expr is equal to comparison_expr and
returns return_expr. If above condi on is not sa sfied, an ELSE clause
exists, the SQL returns else_expr. Otherwise, returns NULL.
We cannot specify literal null for the return_expr and the else_expr. All
of the expressions(expr, comparison_expr, return_expr) must be of the
same data type.
Syntax:
2. CASE expr WHEN comparison_expr1 THEN return_expr1
3. [WHEN comparison_expr2 THEN return_expr2
4. .
5. .
6. .
7. WHEN comparison_exprn THEN return_exprn
8. ELSE else_expr]
9. END
Example:
Input :
DBMS
SELECT first_name, department_id, salary,
CASE department_id WHEN 50 THEN 1.5*salary
WHEN 12 THEN 2.0*salary
ELSE salary
END "REVISED SALARY"
FROM Employee;
Output :

Explana on: In above SQL statements, the value of department_id is decoded.


If it is 50 then salary is made 1.5 mes, if it is 12 then salary is made 2 mes,
else there is no change in salary.
10.The DECODE Func on : Facilitates condi onal inquiries by doing the
work of a CASE or IF-THEN-ELSE statement.
The DECODE func on decodes an expression in a way similar to the IF-
THEN-ELSE logic used in various languages. The DECODE func on
decodes expression a er comparing it to each search value. If the
expression is the same as search, result is returned.
If the default value is omi ed, a null value is returned where a search
value does not match any of the result values.
Syntax:
11.DECODE(col/expression, search1, result1
12. [, search2, result2,........,]
DBMS
13. [, default])
14.Input :
15.SELECT first_name, department_id, salary,
16. DECODE(department_id, 50, 1.5*salary,
17. 12, 2.0*salary,
18. salary)
19. "REVISED SALARY"
20.FROM Employee;
Output :

Explana on: In above SQL statements, the value of department_id is tested. If


it is 50 then salary is made 1.5 mes, if it is 12 then salary is made 2 mes, else
there is no change in salary.
21.COALESCE : Returns the first non-null argument. Null is returned only if
all arguments are null. It is o en used to subs tute a default value for
null values when data is retrieved for display.
NOTE: Same as CASE expressions, COALESCE also will not evaluate the
arguments to the right of the first non-null argument found.
Syntax:
22.COALESCE(value [, ......] )
23.Input:
DBMS
24.SELECT COALESCE(last_name, '- NA -')
25.from Employee;
Output:

Explana on: “- NA -” will be displayed in place where last name is null else
respec ve last names will be shown.
26.GREATEST: Returns the largest value from a list of any number of
expressions. Comparison is case sensi ve. If datatypes of all the
expressions in the list are not same, rest all expressions are converted to
the datatype of the first expression for comparison and if this conversion
is not possible, SQL will throw an error.
NOTE: Returns null if any expression in the list is null.
Syntax:
27.GREATEST(expr1, expr2 [, .....] )
 Input:
 SELECT GREATEST('XYZ', 'xyz')
 from dual;
 Output:
 GREATEST('XYZ', 'xyz')
 xyz
 Explana on: ASCII value of small alphabets is greater.
DBMS
 Input:
 SELECT GREATEST('XYZ', null, 'xyz')
 from dual;

 Output:
 GREATEST('XYZ', null, 'xyz')
 -
Explana on: Since null is present hence, null will be shown as output (as
men oned to note in descrip on above).
28.IFNULL: If expr1 is not NULL, returns expr1; otherwise it returns expr2.
Returns a numeric or string value, depending on the context in which it is
used.
Syntax:
29.IFNULL(expr1, expr2)
 Input:
 SELECT IFNULL(1,0)
 FROM dual;
 Output:
 -
 1
 Explana on : Since, no expression is null.
 Input:
 SELECT IFNULL(NULL,10)
 FROM dual;

 Output:
 --
DBMS
 10
 Explana on:Since, expr1 is null hence, expr2 is shown.
30.IN: Checks whether a value is present within a set of values and can be
used with WHERE, CHECK and crea on of views.
NOTE: Same as CASE and COALESCE expressions, IN also will not evaluate
the arguments to the right of the first non-null argument found.
Syntax:
31.WHERE column IN (x1, x2, x3 [,......] )
32.Input:
33.SELECT * from Employee
34.WHERE department_id IN(50, 12);
Output:

Explana on:All data of Employees is shown with department ID 50 or 12.


35.LEAST: Returns the smallest value from a list of any number of
expressions. Comparison is case sensi ve. If datatypes of all the
expressions in the list are not same, rest all expressions are converted to
the datatype of the first expression for comparison and if this conversion
is not possible, SQL will throw an error.
NOTE: Returns null if any expression in the list is null.
Syntax:
LEAST(expr1, expr2 [, ......])


DBMS
 strong>Input:
 SELECT LEAST('XYZ', 'xyz')
 from dual;

 Output:
 LEAST('XYZ', 'xyz')
 XYZ
Explana on: ASCII value of capital alphabets is smaller.

 Input:
 SELECT LEAST('XYZ', null, 'xyz')
 from dual;

 Output:
 LEAST('XYZ', null, 'xyz')
 -
Explana on: Since null is present hence, null will be shown as output (as
men oned to note in descrip on above).
36.NULLIF: Returns a null value if value1=value2, otherwise it returns
value1.
Syntax:
37.NULLIF(value1, value2)
Example:

Input:
SELECT NULLIF(9995463931, contact_num)
DBMS
from Employee;
Output:

Explana on: NULL is displayed for the Employee whose number is matched
with the given number. For rest of the Employees value1 is returned.

Reporting Aggregate data using the Group functions


SQL has numerous predefined aggregate functions that can be
used to write queries to produce exactly this kind of
information.The GROUP BY clause specifies how to group rows
from a data table when aggregating information, while the
HAVING clause filters out rows that do not belong in specified
groups.

Aggregate functions perform a variety of actions such as counting


all the rows in a table, averaging a column's data, and summing
numeric data. Aggregates can also search a table to find the
highest "MAX" or lowest "MIN" values in a column. As with other
types of queries, you can restrict, or filter out the rows these
functions act on with the WHERE clause. For example, if a
manager needs to know how many employees work in an
organization, the aggregate function named COUNT(*) can be
used to produce this information.The COUNT(*) function shown in
the below SELECT statement counts all rows in a table.
DBMS
SELECT COUNT(*)
FROM employees;

COUNT(*)
----------
24

The result table for the COUNT(*) function is a single column


from a single row known as a scalar result or value. Notice that
the result table has a column heading that corresponds to the
name of the aggregate function specified in the SELECT clause.

Some of the commonly used aggregate functions are as below -

SUM( [ALL | DISTINCT] expression )

AVG( [ALL | DISTINCT] expression )

COUNT( [ALL | DISTINCT] expression )

COUNT(*)

MAX(expression)

MIN(expression)

The ALL and DISTINCT keywords are optional, and perform as


they do with the SELECT clauses that you have learned to
write.The ALL keyword is the default where the option is
allowed.The expression listed in the syntax can be a constant,a
function, or any combination of column names, constants, and
functions connected by arithmetic operators.However, aggregate
functions are most often used with a column name. Except
COUNT function,all the aggregate functions do not consider NULL
values.

There are two rules that you must understand and follow when
using aggregates:

 Aggregate functions can be used in both the SELECT and


HAVING clauses (the HAVING clause is covered later in this
chapter).
DBMS
 Aggregate functions cannot be used in a WHERE clause. Its
violation will produce the Oracle ORA-00934 group function
is not allowed here error message.

Illustrations
The below SELECT query counts the number of employees in the
organization.

SELECT COUNT(*) Count


FROM employees;

COUNT
-----
24

The below SELECT query returns the average of the salaries of


employees in the organization.

SELECT AVG(Salary) average_sal


FROM employees;

AVERAGE_SAL
-----------
15694

The below SELECT query returns the sum of the salaries of


employees in the organization.

SELECT SUM(Salary) total_sal


FROM employees;

TOTAL_SAL
---------
87472

The below SELECT query returns the oldest and latest hired dates
of employees in the organization.

SELECT MIN (hire_date) oldest, MAX (hire_date) latest


FROM employees;

OLDEST LATEST
DBMS
--------- -----------
16-JAN-83 01-JUL-2012

GROUP BY
Aggregate functions are normally used in conjunction with a
GROUP BY clause. The GROUP BY clause enables you to use
aggregate functions to answer more complex managerial
questions such as:

What is the average salary of employees in each department?

How many employees work in each department?

How many employees are working on a particular project?

Group by function establishes data groups based on columns and


aggregates the information within a group only. The grouping
criterion is defined by the columns specified in GROUP BY clause.
Following this hierarchy, data is first organized in the groups and
then WHERE clause restricts the rows in each group.

Guidelines of using GROUP BY clause

(1) All the dependent columns or columns used in GROUP BY


function must form the basis of grouping, hence must be included
in GROUP BY clause also.

SELECT DEPARTMENT_ID, SUM(SALARY)


FROM employees;

DEPARTMENT_ID,
*
ERROR at line 2:
ORA-00937: not a single-group group function

(2) GROUP BY clause does not support the use of column alias,
but the actual names.

(3) GROUP BY clause can only be used with aggregate functions


like SUM, AVG, COUNT, MAX, and MIN.If it is used with single row
functions,Oracle throws an exception as "ORA-00979: not a
GROUP BY expression".
DBMS
(4) Aggregate functions cannot be used in a GROUP BY clause.
Oracle will return the "ORA-00934: group function not allowed"
here error message.

Below query lists the count of employees working in each


department.

SELECT DEPARTMENT_ID, COUNT (*)


FROM employees
GROUP BY DEPARTMENT_ID;

Similarly, below query to find sum of salaries for respective job


ids in each department. Note the group is established based on
Department and Job id. So they appear in GROUP BY clause.

SELECT DEPARTMENT_ID, JOB_ID, SUM (SAL)


FROM employees
GROUP BY DEPARTMENT_ID, JOB_ID;

The below query also produces the same result. Please note that
grouping is based on the department id and job id columns but
not used for display purpose.

SELECT SUM (SALARY)


FROM employees
GROUP BY DEPARTMENT_ID, JOB_ID;

Use of DISTINCT, ALL keywords with Aggregate functions


By specifying DISTINCT keyword with the input parameter, group
by function considers only the unique value of the column for
aggregation. By specifying ALL keyword with the input parameter,
group by function considers all the values of the column for
aggregation, including nulls and duplicates. ALL is the default
specification.

The HAVING clause


The HAVING clause is used for aggregate functions in the same
way that a WHERE clause is used for column names and
expressions.Essentially,the HAVING and WHERE clauses do the
same thing, that is filter rows from inclusion in a result table
based on a condition. While it may appear that a HAVING clause
DBMS
filters out groups, it does not.Rather,a HAVING clause filters
rows.

When all rows for a group are eliminated so is the group.To


summarize, the important differences between the WHERE and
HAVING clauses are:

A WHERE clause is used to filter rows BEFORE the GROUPING


action (i.e., before the calculation of the aggregate functions).

A HAVING clause filters rows AFTER the GROUPING action (i.e.,


after the calculation of the aggregate functions).

SELECT JOB_ID, SUM (SALARY)


FROM employees
GROUP BY JOB_ID
HAVING SUM (SALARY) > 10000;

The HAVING clause is a conditional option that is directly related


to the GROUP BY clause option because a HAVING clause
eliminates rows from a result table based on the result of a
GROUP BY clause.

SELECT department_id, AVG(Salary)


FROM employees
HAVING AVG(Salary) > 33000;
ERROR at line 1: ORA-00937: not a single-group group
fun

Get Data from Multiple Tables

Displaying Data from Multiple Tables


The statement is used to retrieve the fields from multiple tables and with the help of
JOIN operations we easily fetch the records from multiple tables, Generally JOINS are
used when there are common records between two tables. In this article, we will look
into various types of JOIN that are used in SQL.
SQL Joins
SQL joins combine two or more tables based on a common field between them. Here
we are describing different types of Joins that are used in SQL below.
 LEFT JOIN
 RIGHT JOIN
DBMS
 INNER JOIN
 FULL JOIN
Syntax
SELECT tablename1.colunmnname, tablename2.columnname FROM tablename1
JOIN tablename2
ON tablename1.colunmnname = tablename2.columnname
ORDER BY columnname;
LEFT JOIN
In SQL, the Left join is used to fetch all the data from the Left table and common
from both tables. If there are no matching records present in the left table then it
returns the NULL value.
Syntax
SELECT column_name(s)
FROM table_nameA
LEFT JOIN table_nameB ON table_nameA.column_name =
table_nameB.column_name;
RIGHT JOIN
In SQL, Right join is used to fetch all the data from the Right table and common from
both tables. If there are no matching records present in the right table then it returns
the NULL value.
Syntax
SELECT column_name(s)
FROM table_nameA
RIGHT JOIN table_nameB ON table_nameA.column_name =
table_nameB.column_name;
INNER JOIN
It is used to fetch the common data from the tables. It returns only that value when
there is a matched column between them.
Syntax
SELECT column_name(s)
FROM table_nameA
INNER JOIN table_nameB ON table_nameA.column_name =
table_nameB.column_name;
FULL JOIN
In SQL, FULL JOIN is used to fetch all the records from both tables. If there is no
matches in one of the table then its return the NULL value.
Syntax
SELECT column_name(s)
FROM table_nameA
FULL JOIN table_nameB ON table_nameA.column_name =
table_nameB.column_name;
Example:
Let us take three tables named Geeks1, Geeks2, and Geeks3.
DBMS
Geeks1 Table:

Geeks1 Table

Geeks2 Table:

Geeks2 Table

Geeks3 Table:

Geeks3 Table

Example to select the records from multiple tables:


Query
SELECT Geeks3.GID, Geeks3.PID,
Geeks3.Asset, Geeks1.FirstName,
Geeks2.LastName
FROM Geeks3
LEFT JOIN Geeks1
ON Geeks3.GID = Geeks1.ID
LEFT JOIN Geeks2
ON Geeks3.GID = Geeks2.ID
DBMS
Output

SQL Subqueries
In SQL a Subquery can be simply defined as a query within
another query. In other words we can say that a Subquery is a
query that is embedded in WHERE clause of another SQL query.
Important rules for Subqueries:
 You can place the Subquery in a number of SQL
clauses: WHERE clause, HAVING clause, FROM clause.
Subqueries can be used with SELECT, UPDATE, INSERT,
DELETE statements along with expression operator. It could
be equality operator or comparison operator such as =, >,
=, <= and Like operator.
 A subquery is a query within another query. The outer query
is called as main query and inner query is called
as subquery.
 The subquery generally executes first when the subquery
doesn’t have any co-relation with the main query, when
there is a co-relation the parser takes the decision on the
fly on which query to execute on precedence and uses the
output of the subquery accordingly.
 Subquery must be enclosed in parentheses.
 Subqueries are on the right side of the comparison operator.
 ORDER BY command cannot be used in a
Subquery. GROUPBY command can be used to perform
same function as ORDER BY command.
 Use single-row operators with singlerow Subqueries. Use
multiple-row operators with multiple-row Subqueries.
DBMS
Syntax: There is not any general syntax for Subqueries.
However, Subqueries are seen to be used most frequently with
SELECT statement as shown below:
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
Sample Table:
DATABASE

NAME ROLL_NO LOCATION PHONE_NUMBER

Ram 101 Chennai 9988775566

Raj 102 Coimbatore 8877665544

Sasi 103 Madurai 7766553344

Ravi 104 Salem 8989898989

Sumathi 105 Kanchipuram 8989856868

STUDENT

NAME ROLL_NO SECTION

Ravi 104 A

Sumathi 105 B

Raj 102 A

Sample Queries
:
 To display NAME, LOCATION, PHONE_NUMBER of the
students from DATABASE table whose section is A
Select NAME, LOCATION, PHONE_NUMBER from DATABASE
WHERE ROLL_NO IN
DBMS
(SELECT ROLL_NO from STUDENT where SECTION=’A’);
 Explanation : First subquery executes “ SELECT ROLL_NO
from STUDENT where SECTION=’A’ ” returns ROLL_NO from
STUDENT table whose SECTION is ‘A’.Then outer-query
executes it and return the NAME, LOCATION,
PHONE_NUMBER from the DATABASE table of the student
whose ROLL_NO is returned from inner subquery. Output:

NAME ROLL_NO LOCATION PHONE_NUMBER

Ravi 104 Salem 8989898989

Raj 102 Coimbatore 8877665544

 Insert Query Example:


Table1: Student1

NAME ROLL_NO LOCATION PHONE_NUMBER

Ram 101 chennai 9988773344

Raju 102 coimbatore 9090909090

Ravi 103 salem 8989898989

Table2: Student2

NAME ROLL_NO LOCATION PHONE_NUMBER

Raj 111 chennai 8787878787

Sai 112 mumbai 6565656565

Sri 113 coimbatore 7878787878

 To insert Student2 into Student1 table:


INSERT INTO Student1 SELECT * FROM Student2;
 Output:
DBMS
NAME ROLL_NO LOCATION PHONE_NUMBER

Ram 101 chennai 9988773344

Raju 102 coimbatore 9090909090

Ravi 103 salem 8989898989

Raj 111 chennai 8787878787

Sai 112 mumbai 6565656565

Sri 113 coimbatore 7878787878

 To delete students from Student2 table whose rollno is same


as that in Student1 table and having location as chennai
DELETE FROM Student2
WHERE ROLL_NO IN ( SELECT ROLL_NO
FROM Student1
WHERE LOCATION = ’chennai’);
 Output:
1 row delete successfully.
 Display Student2 table:

NAME ROLL_NO LOCATION PHONE_NUMBER

Sai 112 mumbai 6565656565

Sri 113 coimbatore 7878787878

 To update name of the students to geeks in Student2 table


whose location is same as Raju,Ravi in Student1 table
UPDATE Student2
SET NAME=’geeks’
WHERE LOCATION IN ( SELECT LOCATION
FROM Student1
DBMS
WHERE NAME IN (‘Raju’,’Ravi’));
 Output:
1 row updated successfully.
 Display Student2 table:

NAME ROLL_NO LOCATION PHONE_NUMBER

Sai 112 mumbai 6565656565

geeks 113 coimbatore 7878787878


DBMS
SET Operators in SQL
SET operators are special type of operators which are used to combine the result of two queries.

Operators covered under SET operators are:

1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS

There are certain rules which must be followed to perform operations using SET operators in SQL.
Rules are as follows:

1. The number and order of columns must be the same.


2. Data types must be compatible.

Let us see each of the SET operators in more detail with the help of examples.

All the examples will be written using the MySQL database.


DBMS
Consider we have the following tables with the given data.

Table 1: t_employees

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2

7 Paras Jaiswal Marketing 32000 1

Table 2: t2_employees

ID Name Department Salary Year_of_Experience

1 Prashant Wagh R&D 49000 1

2 Abhishek Pawar Production 45000 1

3 Gautam Jain Development 56000 4

4 Shubham Mahale Accounts 57000 2

5 Rahul Thakur Production 76000 4

6 Bhushan Wagh R&D 75000 2


DBMS
7 Anand Singh Marketing 28000 1

Table 3: t_students

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

2 Harshada Sharma Kanpur 92 Chemistry

3 Anuja Rajput Jaipur 78 History

4 Pranali Singh Nashik 88 Geography

5 Renuka Deshmukh Panipat 90 Biology

6 Swati Kumari Faridabad 93 English

7 Prachi Jaiswal Gurugram 96 Hindi

Table 4: t2_students

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

2 Ishwari Dixit Delhi 86 Hindi

3 Anuja Rajput Jaipur 78 History

4 Pakhi Arora Surat 70 Sanskrit

5 Renuka Deshmukh Panipat 90 Biology

6 Jayshree Patel Pune 91 Maths


DBMS
7 Prachi Jaiswal Gurugram 96 Hindi

1. UNION:
ADVERTISEMENT

o UNION will be used to combine the result of two select statements.


o Duplicate rows will be eliminated from the results obtained after performing the UNION operation.

Example 1:

Write a query to perform union between the table t_employees and the table t2_employees.

Query:

1. mysql> SELECT *FROM t_employees UNION SELECT *FROM t2_employees;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_employees table and perform a UNION operation with the records fetched by
the second SELECT query from the t2_employees table.

You will get the following output:

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2

7 Paras Jaiswal Marketing 32000 1


DBMS
1 Prashant Wagh R&D 49000 1

3 Gautam Jain Development 56000 4

5 Rahul Thakur Production 76000 4

7 Anand Singh Marketing 28000 1

Since we have performed union operation between both the tables, so only the records from the
first and second table are displayed except for the duplicate records.

Example 2:

Write a query to perform union between the table t_students and the table t2_students.

Query:

1. mysql> SELECT *FROM t_students UNION SELECT *FROM t2_students;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_students table and perform a UNION operation with the records fetched by the
second SELECT query from the t2_students table.

ADVERTISEMENT

You will get the following output:

ID Name Department Salary Year_of_Experience

1 Soniya Jain Udaipur 89 Physics

2 Harshada Sharma Kanpur 92 Chemistry

3 Anuja Rajput Jaipur 78 History

4 Pranali Singh Nashik 88 Geography

5 Renuka Deshmukh Panipat 90 Biology


DBMS
6 Swati Kumari Faridabad 93 English

7 Prachi Jaiswal Gurugram 96 Hindi

2 Ishwari Dixit Delhi 86 Hindi

4 Pakhi Arora Surat 70 Sanskrit

6 Jayshree Patel Pune 91 Maths

Since we have performed union operation between both the tables, so only the records from the
first and second table are displayed except for the duplicate records.

2. UNION ALL
o This operator combines all the records from both the queries.
o Duplicate rows will be not be eliminated from the results obtained after performing the UNION ALL
operation.

ADVERTISEMENT
ADVERTISEMENT

Example 1:

Write a query to perform union all operation between the table t_employees and the table
t2_employees.

Query:

1. mysql> SELECT *FROM t_employees UNION ALL SELECT *FROM t2_employees;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_employees table and perform UNION ALL operation with the records fetched
by the second SELECT query from the t2_employees table.

You will get the following output:

ID Name Department Salary Year_of_Experience


DBMS
1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2

7 Paras Jaiswal Marketing 32000 1

1 Prashant Wagh R&D 49000 1

2 Abhishek Pawar Production 45000 1

3 Gautam Jain Development 56000 4

4 Shubham Mahale Accounts 57000 2

5 Rahul Thakur Production 76000 4

6 Bhushan Wagh R&D 75000 2

7 Anand Singh Marketing 28000 1

Since we have performed union all operation between both the tables, so all the records from the
first and second table are displayed, including the duplicate records.

Example 2:

Write a query to perform union all operation between the table t_students and the table
t2_students.

Query:
DBMS
1. mysql> SELECT *FROM t_students UNION ALL SELECT *FROM t2_students;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_students table and perform UNION ALL operation with the records fetched by
the second SELECT query from the t2_students table.

You will get the following output:

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

2 Harshada Sharma Kanpur 92 Chemistry

3 Anuja Rajput Jaipur 78 History

4 Pranali Singh Nashik 88 Geography

5 Renuka Deshmukh Panipat 90 Biology

6 Swati Kumari Faridabad 93 English

7 Prachi Jaiswal Gurugram 96 Hindi

1 Soniya Jain Udaipur 89 Physics

2 Ishwari Dixit Delhi 86 Hindi

3 Anuja Rajput Jaipur 78 History

4 Pakhi Arora Surat 70 Sanskrit

5 Renuka Deshmukh Panipat 90 Biology

6 Jayshree Patel Pune 91 Maths

7 Prachi Jaiswal Gurugram 96 Hindi


DBMS
Since we have performed union all operation between both the tables, so all the records from the
first and second table are displayed, including the duplicate records.

3. INTERSECT:
o It is used to combine two SELECT statements, but it only returns the records which are common from
both SELECT statements.

Example 1:

Write a query to perform intersect operation between the table t_employees and the table
t2_employees.

Query:

1. mysql> SELECT *FROM t_employees INTERSECT SELECT *FROM t2_employees;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_employees table and perform INTERSECT operation with the records fetched by
the second SELECT query from the t2_employees table.

You will get the following output:

ID Name Hometown Percentage Favourite_Subject

2 Abhishek Pawar Production 45000 1

4 Shubham Mahale Accounts 57000 2

6 Bhushan Wagh R&D 75000 2

Since we have performed intersect operation between both the tables, so only the common records
from both the tables are displayed.

Example 2:

Write a query to perform intersect operation between the table t_students and the table
t2_students.
DBMS
Query:

1. mysql> SELECT *FROM t_students INTERSECT SELECT *FROM t2_students;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_students table and perform a UNION operation with the records fetched by the
second SELECT query from the t2_students table.

You will get the following output:

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

3 Anuja Rajput Jaipur 78 History

5 Renuka Deshmukh Panipat 90 Biology

7 Prachi Jaiswal Gurugram 96 Hindi

Since we have performed intersect operation between both the tables, so only the common records
from both the tables are displayed.

4. MINUS

o It displays the rows which are present in the first query but absent in the second query with no
duplicates.

Example 1:

Write a query to perform a minus operation between the table t_employees and the table
t2_employees.

Query:

1. mysql> SELECT *FROM t_employees MINUS SELECT *FROM t2_employees;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_employees table and perform MINUS operation with the records fetched by the
second SELECT query from the t2_employees table.
DBMS
You will get the following output:

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

3 Pranav Deshmukh HR 59900 3

5 Sunil Kulkarni Development 87000 3

7 Paras Jaiswal Marketing 32000 1

Since we have performed Minus operation between both the tables, so only the unmatched records
from both the tables are displayed.

Example 2:

Write a query to perform a minus operation between the table t_students and the table t2_students.

Query:

1. mysql> SELECT *FROM t_students MINUS SELECT *FROM t2_students;

Here, in a single query, we have written two SELECT queries. The first SELECT query will fetch the
records from the t_employees table and perform a UNION operation with the records fetched by
the second SELECT query from the t2_employees table.

You will get the following output:

ID Name Hometown Percentage Favourite_Subject

2 Harshada Sharma Kanpur 92 Chemistry

4 Pranali Singh Nashik 88 Geography

6 Swati Kumari Faridabad 93 English


DBMS
Since we have performed a minus operation between both the tables, so only the Unmatched
records from both the tables are displayed.
DBMS

You might also like