DBMS
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.
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.
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.
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.
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:
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.
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.
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.
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
Schema Instance
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.
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 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.
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.
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 –
Data Theft: When somebody steals the information stored on databases, and servers, this
process is known as Data Theft.
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).
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 –
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.
Data Dictionary: It contains the information about the structure of any database
object. It is the repository of information that governs the metadata.
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".
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.
2. 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.
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.
Entity Set
An entity set is a group of entities of the same entity type.
ADVERTISEMENT
Example of Entity Relationship Diagram representation of the above strong entity set:
DBMS
2. Weak Entity Set:
For example, An entity of smartphones with its attributes, phone's name, phone's
colour, and phone's RAM.
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.
Importance of DM
First, you can identify missing and redundant data. As a result, you can improve
efficiency by making the necessary adjustments to your data.
This article will help you understand the process of data modelling and its
importance and provide different data modeling techniques and examples.
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.
Example
Advantages
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
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
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.
Database design
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).
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:
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.
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.
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.
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.
Normal Descrip on
Form
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.
EMPLOYEE table:
14 John 7272826385, UP
9064738238
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
ADVERTISEMENT
Example: Let's assume, a school can store the data of teachers and the subjects they
teach. In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
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
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
DBMS
EMP_ID EMP_NAME EMP_ZIP
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
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
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
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_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:
Now, this is in BCNF because left side part of both the functional dependencies is a
key.
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.
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
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
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
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.
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.
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
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
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:
Attributes:
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
ERD Issues:
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
Unit 4
SQL : create and manage using create and alter
DBMS
SQL | DDL, DML, TCL and DCL
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 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.
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.
1. SELECT Command
DBMS
2. INSERT Command
3. UPDATE Command
4. DELETE Command
If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:
This SQL statement displays the following values of the student table:
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
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.
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:
BCA1001 Abhay 80
BCA1003 Bheem 80
BCA1005 Sumit 80
Let's take the following student table, which consists of only 2 records of the student.
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:
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.
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:
Example 2: This example describes how to update the value of multiple fields of
the database table.
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:
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.
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
Example 2: This example describes how to delete the multiple records or rows
from the database table.
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. 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).
For more advanced filtering and sorting techniques, refer to your specific SQL
database documentation.
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))
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.
List of Functions
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.
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:
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.
COUNT(*)
----------
24
COUNT(*)
MAX(expression)
MIN(expression)
There are two rules that you must understand and follow when
using aggregates:
Illustrations
The below SELECT query counts the number of employees in the
organization.
COUNT
-----
24
AVERAGE_SAL
-----------
15694
TOTAL_SAL
---------
87472
The below SELECT query returns the oldest and latest hired dates
of employees in the organization.
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:
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.
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.
Geeks1 Table
Geeks2 Table:
Geeks2 Table
Geeks3 Table:
Geeks3 Table
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
STUDENT
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:
Table2: Student2
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:
Let us see each of the SET operators in more detail with the help of examples.
Table 1: t_employees
Table 2: t2_employees
Table 3: t_students
Table 4: t2_students
1. UNION:
ADVERTISEMENT
Example 1:
Write a query to perform union between the table t_employees and the table t2_employees.
Query:
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.
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:
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
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:
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.
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.
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:
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.
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:
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.
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:
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:
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:
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.