DBMS Notes (BCS and Engineering)
DBMS Notes (BCS and Engineering)
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
Database Design can be defined as a set of procedures or
collection of tasks involving various steps taken to implement a
database. Following are some critical points to keep in mind to
achieve a good database design:
1. Data consistency and integrity must be maintained.
2. Low Redundancy
3. Faster searching through indices
4. Security measures should be taken by enforcing various
integrity constraints.
5. Data should be stored in fragmented bits of information in
the most atomic format possible.
However, depending on specific requirements above criteria
might change. But these are the most common things that
ensure a good database design.
What are the Following Steps that can be taken by a
Database Designer to Ensure Good Database Design?
Step 1: Determine the goal of your database, and ensure clear
communication with the stakeholders (if any). Understanding
the purpose of a database will help in thinking of various use
cases & where the problem may arise & how we can prevent it.
Step 2: List down all the entities that will be present in the
database & what relationships exist among them.
DBMS
Step 3: Organize the information into different tables such that
no or very little redundancy is there.
Step 4: Ensure uniqueness in every table. The uniqueness of
records present in any relation is a very crucial part of database
design that helps us avoid redundancy. Identify the key
attributes to uniquely identify every row from columns. You can
use various key constraints to ensure the uniqueness of your
table, also keep in mind the uniquely identifying records must
consume as little space as possible & shall not contain any
NULL values.
Step 5: After all the tables are structured, and information is
organized apply Normalization Forms to identify anomalies that
may arise & redundancy that can cause inconsistency in the
database.
Primary Terminologies Used in Database Design
Following are the terminologies that a person should be
familiar with before designing a database:
Redundancy: Redundancy refers to the duplicity of the
data. There can be specific use cases when we need or
don’t need redundancy in our Database. For ex: If we have
a banking system application then we may need to strictly
prevent redundancy in our Database.
Schema: Schema is a logical container that defines the
structure & manages the organization of the data stored
in it. It consists of rows and columns having data types for
each column.
Records/Tuples: A Record or a tuple is the same thing,
basically its where our data is stored inside a table
DBMS
Indexing: Indexing is a data structure technique to
promote efficient retrieval of the data stored in our
database.
Data Integrity & Consistency: Data integrity refers to the
quality of the information stored in our database and
consistency refers to the correctness of the data stored.
Data Models: Data models provide us with visual
modeling techniques to visualize the data & the
relationship that exists among those data. Ex: model,
Network Model, Object Oriented Model, Hierarchical
model, etc.
Functional Dependency: Functional Dependency is a
relationship between two attributes of the table that
represents that the value of one attribute can be
determined by another. Ex: {A -> B}, A & B are two
attributes and attribute A can uniquely determine the
value of B.
Transaction: Transaction is a single logical unit of work. It
signifies that some changes are made in the database. A
transaction must satisfy the ACID or BASE properties
(depending on the type of Database).
Schedule: Schedule defines the sequence of transactions
in which they’re executed by one or multiple users.
Concurrency: Concurrency refers to allowing multiple
transactions to operate simultaneously without interfering
with one another.
Database Design Lifecycle
DBMS
The database design lifecycle goes something like this:
Data Models
2. Entity: Entities are objects in the real world, which can have
certain properties & these properties are referred to as
attributes of that particular entity. There are 2 types of entities:
Strong and weak entity, weak entity do not have a key attribute
to identify them, their existence solely depends on one 1-
specific strong entity & also have full participation in a
relationship whereas strong entity does have a key attribute to
uniquely identify them.
Weak entity example: Loan -> Loan will be given to a customer
(which is optional) & the load will be identified by the
customer_id to whom the lone is granted.
3. Relationships: How data is logically related to each other
defines the relationship of that data with other entities. In
simple words, the association of one entity with another is
defined here.
A relationship can be further categorized into – unary, binary,
and ternary relationships.
Unary: In this, the associating entity & the associated
entity both are the same. Ex: Employee Manages
themselves, and students are also given the post of
monitor hence here the student themselves is a monitor.
DBMS
Binary: This is a very common relationship that you will
come across while designing a database.
Ex: Student is enrolled in courses, Employee is managed by
different managers, One student can be taught by many
professors.
Ternary: In this, we have 3 entities involved in a single
relationship. Ex: an employee works on a project for a
client. Note that, here we have 3 entities: Employee,
Project & Client.
4. Attributes: Attributes are nothing but properties of a specific
entity that define its behavior. For example, an employee can
have unique_id, name, age, date of birth (DOB), salary,
department, Manager, project id, etc.
5. Normalization: After all the entities are put in place and the
relationship among data is defined, we need to look for
loopholes or possible ambiguities that may arise as a result of
CRUD operations. To prevent various Anomalies such as
INSERTION, UPDATION, and DELETION Anomalies.
Data Normalization is a basic procedure defined for databases
to eliminate such anomalies & prevent redundancy.
An Example of Logical Design
DBMS
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:
It is the process of minimizing redundancy from a relation or set of
relations. Redundancy in relation may cause insertion, deletion, and
update anomalies. So, it helps to minimize the redundancy in
relations. Normal forms are used to eliminate or reduce redundancy in
database tables.
Normalization of DBMS
In database management systems (DBMS), normal forms are a series of
guidelines that help to ensure that the design of a database is efficient,
organized, and free from data anomalies. There are several levels of
normalization, each with its own set of guidelines, known as normal
forms.
DBMS
Important Points Regarding Normal Forms in DBMS
First Normal Form (1NF): This is the most basic level of
normalization. In 1NF, each table cell should contain only a single
value, and each column should have a unique name. The first
normal form helps to eliminate duplicate data and simplify queries.
Second Normal Form (2NF): 2NF eliminates redundant data by
requiring that each non-key attribute be dependent on the primary
key. This means that each column should be directly related to the
primary key, and not to other columns.
Third Normal Form (3NF): 3NF builds on 2NF by requiring that all
non-key attributes are independent of each other. This means that
each column should be directly related to the primary key, and not
to any other columns in the same table.
Boyce-Codd Normal Form (BCNF): BCNF is a stricter form of 3NF
that ensures that each determinant in a table is a candidate key. In
other words, BCNF ensures that each non-key attribute is
dependent only on the candidate key.
Fourth Normal Form (4NF): 4NF is a further refinement of BCNF
that ensures that a table does not contain any multi-valued
dependencies.
Fifth Normal Form (5NF): 5NF is the highest level of
normalization and involves decomposing a table into smaller
tables to remove data redundancy and improve data integrity.
Normal forms help to reduce data redundancy, increase data
consistency, and improve database performance. However, higher levels
of normalization can lead to more complex database designs and
queries. It is important to strike a balance between normalization and
practicality when designing a database.
Advantages of Normal Form
DBMS
Reduced data redundancy: Normalization helps to eliminate
duplicate data in tables, reducing the amount of storage space
needed and improving database efficiency.
Improved data consistency: Normalization ensures that data is
stored in a consistent and organized manner, reducing the risk of
data inconsistencies and errors.
Simplified database design: Normalization provides guidelines for
organizing tables and data relationships, making it easier to design
and maintain a database.
Improved query performance: Normalized tables are typically
easier to search and retrieve data from, resulting in faster query
performance.
Easier database maintenance: Normalization reduces the
complexity of a database by breaking it down into smaller, more
manageable tables, making it easier to add, modify, and delete
data.
Overall, using normal forms in DBMS helps to improve data quality,
increase database efficiency, and simplify database design and
maintenance.
First Normal Form
If a relation contain composite or multi-valued attribute, it violates first
normal form or a relation is in first normal form if it does not contain any
composite or multi-valued attribute. A relation is in first normal form if
every attribute in that relation is singled valued attribute.
Example 1 – Relation STUDENT in table 1 is not in 1NF because of
multi-valued attribute STUD_PHONE. Its decomposition into 1NF
has been shown in table 2.
DBMS
Example
Example 2 –
ID Name Courses
------------------
1 A c1, c2
2 E c3
3 M C2, c3
In the above table Course is a multi-valued attribute so it is not in
1NF. Below Table is in 1NF as there is no multi-valued attribute
ID Name Course
------------------
1 A c1
1 A c2
2 E c3
3 M c2
3 M c3
Second Normal Form
{Note that, there are many courses having the same course fee}
Here, COURSE_FEE cannot alone decide the value of COURSE_NO
or STUD_NO; COURSE_FEE together with STUD_NO cannot decide
the value of COURSE_NO; COURSE_FEE together with COURSE_NO
cannot decide the value of STUD_NO; Hence, COURSE_FEE would
be a non-prime attribute, as it does not belong to the one only
candidate key {STUD_NO, COURSE_NO} ; But, COURSE_NO ->
COURSE_FEE, i.e., COURSE_FEE is dependent on COURSE_NO,
which is a proper subset of the candidate key. Non-prime attribute
COURSE_FEE is dependent on a proper subset of the candidate
key, which is a partial dependency and so this relation is not in
2NF. To convert the above relation to 2NF, we need to split the
table into two tables such as : Table 1: STUD_NO, COURSE_NO
Table 2: COURSE_NO, COURSE_FEE
Table 1 Table 2
STUD_NO COURSE_NO COURSE_NO COURSE_FEE
1 C1 C1 1000
2 C2 C2 1500
1 C4 C3 1000
4 C3 C4 2000
4 C1 C5 2000
DBMS
NOTE: 2NF tries to reduce the redundant data getting stored in
memory. For instance, if there are 100 students taking C1 course,
we don’t need to store its Fee as 1000 for all the 100 records,
instead, once we can store it in the second table as the course fee
for C1 is 1000.
Example 2 – Consider following functional dependencies in
relation R (A, B , C, D )
Step 1: As we can see, (AC)+ ={A,C,B,E,D} but none of its subset can
determine all attribute of relation, So AC will be candidate key. A or C
can’t be derived from any other attribute of the relation, so there will be
only 1 candidate key {AC}.
Step 2: Prime attributes are those attributes that are part of candidate
key {A, C} in this example and others will be non-prime {B, D, E} in this
example.
Step 3: The relation R is in 1st normal form as a relational DBMS does
not allow multi-valued or composite attribute. The relation is in 2nd
normal form because BC->D is in 2nd normal form (BC is not a proper
subset of candidate key AC) and AC->BE is in 2nd normal form (AC is
candidate key) and B->E is in 2nd normal form (B is not a proper subset
of candidate key AC).
For example consider relation R(A, B, C) A -> BC, B -> A and B both are
super keys so above relation is in BCNF.
Third Normal Form
A relation is said to be in third normal form, if we did not have any
transitive dependency for non-prime attributes. The basic condition with
the Third Normal Form is that, the relation must be in Second Normal
Form.
Below mentioned is the basic condition that must be hold in the non-
trivial functional dependency X -> Y:
X is a Super Key.
DBMS
Y is a Prime Attribute ( this means that element of Y is some part of
Candidate Key).
ER Model is used to model the logical view of the system from a data
perspective which consists of these symbols:
Rectangles: Rectangles represent Entities in the ER Model.
Ellipses: Ellipses represent Attributes in the ER Model.
Diamond: Diamonds represent Relationships among Entities.
Components of ER Diagram
ER Model consists of Entities, Attributes, and Relationships among
Entities in a Database System.
Components of ER Diagram
DBMS
Entity
An Entity may be an object with a physical existence – a particular
person, car, house, or employee – or it may be an object with a
conceptual existence – a company, a job, or a university course.
Entity Set: An Entity is an object of Entity Type and a set of all entities is
called an entity set. For Example, E1 is an entity having Entity Type
Student and the set of all students is called Entity Set. In ER diagram,
Entity Type is represented as:
Entity Set
1. Strong Entity
A Strong Entity is a type of entity that has a key Attribute. Strong Entity
does not depend on other Entity in the Schema. It has a primary key, that
helps in identifying it uniquely, and it is represented by a rectangle.
These are called Strong Entity Types.
2. Weak Entity
An Entity type has a key attribute that uniquely identifies each entity in
the entity set. But some entity type exists for which key attributes can’t
be defined. These are called Weak Entity types.
DBMS
For Example, A company may store the information of dependents
(Parents, Children, Spouse) of an Employee. But the dependents don’t
have existed without the employee. So Dependent will be a Weak Entity
Type and Employee will be Identifying Entity type for Dependent, which
means it is Strong Entity Type.
A weak entity type is represented by a Double Rectangle. The
participation of weak entity types is always total. The relationship
between the weak entity type and its identifying strong entity type is
called identifying relationship and it is represented by a double
diamond.
Attribute
1. Key Attribute
The attribute which uniquely identifies each entity in the entity set is
called the key attribute. For example, Roll_No will be unique for each
DBMS
student. In ER diagram, the key attribute is represented by an oval with
underlying lines.
Key Attribute
2. Composite Attribute
An attribute composed of many other attributes is called a composite
attribute. For example, the Address attribute of the student Entity type
consists of Street, City, State, and Country. In ER diagram, the composite
attribute is represented by an oval comprising of ovals.
Composite Attribute
3. Multivalued Attribute
An attribute consisting of more than one value for a given entity. For
example, Phone_No (can be more than one for a given student). In ER
diagram, a multivalued attribute is represented by a double oval.
DBMS
Multivalued Attribute
4. Derived Attribute
An attribute that can be derived from other attributes of the entity type
is known as a derived attribute. e.g.; Age (can be derived from DOB). In
ER diagram, the derived attribute is represented by a dashed oval.
Derived Attribute
The Complete Entity Type Student with its Attributes can be represented
as:
Entity-Relationship Set
Relationship Set
Degree of a Relationship Set
Unary Relationship
2. Binary Relationship: When there are TWO entities set participating in
a relationship, the relationship is called a binary relationship. For
example, a Student is enrolled in a Course.
Binary Relationship
3. n-ary Relationship: When there are n entities set participating in a
relation, the relationship is called an n-ary relationship.
Cardinality
The number of times an entity of an entity set participates in a
relationship set is known as cardinality. Cardinality can be of different
types:
1. One-to-One: When each entity in each entity set can take part only
once in the relationship, the cardinality is one-to-one. Let us assume that
a male can marry one female and a female can marry one male. So the
relationship will be one-to-one.
the total number of tables that can be used in this is 2.
DBMS
Participation Constraint
Participation Constraint is applied to the entity participating in the
relationship set.
1. Total Participation – Each entity in the entity set must participate in
the relationship. If each student must enroll in a course, the participation
of students will be total. Total participation is shown by a double line in
the ER diagram.
2. Partial Participation – The entity in the entity set may or may NOT
participate in the relationship. If some courses are not enrolled by any of
the students, the participation in the course will be partial.
The diagram depicts the ‘Enrolled in’ relationship set with Student Entity
set having total participation and Course Entity set having partial
participation.
Cardinality
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
DBMS
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.
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
DBMS
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.
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
ADVERTISEMENT
ADVERTISEMENT
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.
DBMS
Partial Keys are set of attributes with the help of which the tuples of the weak
entities can be distinguished and identified.
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:
Relationship
Relationships are associations between
or among entities.
DBMS
Arrow notation
Arrow notation is simple and easily recognizable. Its main focus is the number of
relationships entities have within a system.
Invented by Richard Barker, this notation is most commonly used for describing data
in Oracle. Barker’s notations are widely used, and most people familiar with
diagrams have seen these notations before.
Entities are displayed as rectangles with rounded edges, attributes with one of three
symbols inside the entity box, and relationships with various lines.
Relationships are drawn to show both optionality and degree, so there each
relationship line tells the reader two things.
Optionality symbols
Chen notation
Peter Chen, the creator of Chen’s notation, invented this more detailed way of
notation in 1976. Chen’s notation was one of the first to be used in software design,
and is still popular in ERD creation. Unlike Barker’s notation and UML, entities,
relationships, and attributes have many different ways of being represented.
Let’s start with entities. An entity is shown in a rectangle, just like in many other
notations. But, that is where the similarities stop. There are 2 more ways to describe
entities:
Attributes are in ovals. Here are some other symbols used to define attributes:
Type
Optionality
Cardinality (degree)
Participation
Multiplicity symbols
There are two marks that indicate multiplicity. The first mark, closest to the end of
the line, represents the maximum number of times an instance of an entity can be
associated with other instances
The second mark, behind the first, represents the minimum number of times an
instance of an entity can be associated with other instances. The minimum can only
be zero or one, and they are referred to as ‘optional’ or ‘mandatory’, respectively.
Attributes don’t have any specific symbols, but their placement determines what type
they are. Primary attributes are listed in the top half of the entity rectangle, and other
attributes are listed in the lower half.
Like in Chen notation, entities can be described as weak or strong, or, dependant and
independent.
Relationships are shown with either dashed or solid lines, with filled-in circles at one
or both ends, like many other notation types. Letters are at the end of relationships
lines, and tell the reader the cardinalities of the entity relationships.
UML notation
UML, which stands for Unified Modeling Language, is extremely popular and used in
many different diagram types. Check out our recent post on UML diagrams to learn
more about them.
Relationships only have solid lines, and have numbers signifying cardinalities at the
end of the line. There are a few ways to show cardinality.
DBMS
UML notation
ERD Issues:
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.
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
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
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
BCA1004 Ram 79
BCA1005 Sumit 80
Example 2: This example shows all the values of a specific column from the table.
ADVERTISEMENT
DBMS
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.
BCA1001 Abhay 80
BCA1002 Ankit 75
BCA1003 Bheem 80
BCA1004 Ram 79
BCA1005 Sumit 80
If you want to access all the records of those students whose marks is 80 from the
above table, then you have to write the following DML command in SQL:
DBMS
1. SELECT * FROM Student WHERE Stu_Marks = 80;
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.
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:
DBMS
1. INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104,
Anmol, 89, 19);
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
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:
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
DBMS
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:
The WHERE clause is optional and carries out the relational select
operation. It specifies which rows are to be selected.
Arithmetic operators
Operators act upon the columns (known as operands) to result
into a different result. In case of multiple operators in an
expression, the order of evaulation is decided by the operator
precedence. Here are the elementary rules of precedence -
Column Alias
DBMS
An alias is used to rename a column or an expression during
display. The alias to a column or an expression appears as the
heading in the output of a query. It is useful in providing a
meaningful heading to long expressions in the SELECT query. By
default, the alias appears in uppercase in the query output
without spaces. To override this behavior, alias must be enclosed
within double quotes to preserve the case and spaces in the alias
name.
Concatenation operators
Concatenation operator can be used to join two string values or
expressions in a SELECT query. The double vertical bar symbol is
used as string concatenation operator. It is applicable only for
character and string column values resulting into a new character
expression. Example
Literals
Any hard coded value, which is not stored in database, in the
SELECT clause, is known s Literal. It can be number, character,
or date value. Character and date values must be enclosed within
quotes. Consider the below SQL queries.examples of using literals
of different data types in SQL queries.
The query below uses two character literals to join them together.
Quote Operator
The quote operator is used to specify the quotation mark
delimiter of your own. You can chose a convenient delimiter,
depedning on the data.
NULL
If a column doesn't has a definite value, it is considered as NULL.
NULL value denotes unknown or unavailable. It is not zero for
numeric values, not blank space for character values.
NUM
--------
DISTINCT Keyword
If the data is expected to have duplicate results, use DISTINCT
keyword to eliminate duplicates and diplay only the unique results
in the query output. Only the selected columns are validated for
duplication and the rows will be logically eliminated from the
DBMS
query output. To be noted, the DISTINCT keyword must appear
just after the SELECT clause.
DEPARTMENT_ID
---------------
10
20
30
40
DESCRIBE command
The structural metadata of a table may be obtained by querying
the database for the list of columns that comprise it using the
DESCRIBE command. It will list the used column names, their null
property and data type.
Syntax:
For example,
DESC EMPLOYEE
will display the EMPLOYEE table structure i.e. columns, their data
types, precision and nullable property.
Syntax:
SELECT *|{[DISTINCT] column| expression [alias],..}
FROM table
[WHERE condition(s)]
In the syntax,
3 rows selected
Points to be noted -
A SELECT clause can contain only one WHERE clause.
However, multiple filter conditions can be appended to
WHERE clause using AND or OR operator.
DBMS
The columns, literals or expressions in a predicate clause
must be of similar or interconvertible data types.
Column alias cannot be used in the WHERE clause.
Character literals must be enclosed within single quotation
marks and are case sensitive.
Date literals must be enclosed within single quotation marks
and are format sensitive. Default format is DD-MON-RR.
Comparison Operators
Comparison operators are used in predicates to compare one
term or operand with another term. SQL offers comprehensive
set of equality, inequality and miscellaneous operators. They can
be used depending on the data and filter condition logic in the
SELECT query. When you use comparison operators in a WHERE
clause, the arguments (objects or values you are comparing) on
both sides of the operator must be either a column name, or a
specific value. If a specific value is used, then the value must be
either a numeric value or a literal string. If the value is a
character string or date, you must enter the value within single
quotation marks (' ').
Operator Meaning
= equal to
< less than
> greater than
>= greater than or equal to
<= less than or equal to
!= not equal to
<> not equal to
The IN Operator
The IN operator is used to test a column value in a given set of
value. If the column can be equated to any of the values from the
given set, the condition is validated. The condition defined using
the IN operator is also known as the membership condition.
For example, the below SELECT query lists the first names of
those employees whose last name starts with 'SA'.
SELECT first_name
FROM employees
WHERE last_name LIKE 'SA%';
Logical Operators
Multiple filter conditions can be added to the WHERE clause
predicate. More than one condition can be combined together
using logical operators AND, OR and NOT.
The below SQL query uses an ORDER BY clause to sort the result
table by the last_name column in ascending order. Ascending
order is the default sort order.
last_name first_name
--------------- ---------------
Jones Quincey
Klepper Robert
Quattromani Toni
Schultheis Robert
Substitution Variables
When a SQL query has to be executed more than once for the
different set of inputs, substitution variables can be used.
Substitution variables can be used to prompt for user inputs
before the query execution. They are widely used in query based
report generation which takes data range from the users as input
for the conditional filtering and data display. Substitution
variables are prefixed by a single-ampersand (&) symbol to
temporarily store values. For example,
Once the user provides inputs to both the variables, values are
substituted, query is verified and executed.
Points to be noted -
If the variable is meant to substitute a character or date
value, the literal needs to be enclosed in single quotes. A
useful technique is to enclose the ampersand substitution
variable in single quotes when dealing with character and
date values.
DBMS
Both SQL Developer and SQL* Plus support the substitution
variables and the DEFINE/UNDEFINE commands. Though
SQL Developer or SQL* Plus does not support validation
checks (except for data type) on user input.
You can use the substitution variables not only in the
WHERE clause of a SQL statement, but also as substitution
for column names, expressions, or text.
Note that the same value of &DT is substituted twice in the above
query. So, its value once given by the user will be substituted at
two places.
first_name SALARY
------- -------
MARTIN 5000
Single Row functions - Single row functions are the one who work
on single row and return one output per row. For example, length
and case conversion functions are single row functions.
Illustrations
General functions
FIRST_NAME LAST_NAME
SALARY NVL(COMMISSION_PCT,0)
-------------------- ------------------------- --------
-- ---------------------
Steven King
24000 0
Neena Kochhar
17000 0
Lex De Haan
17000 0
Alexander Hunold
9000 0
Case Conversion functions
CONCAT(FIRST_NAME,LAST_NAME)
--------------------------------
EllenAbel
SundarAnde
MozheAtkinson
DavidAustin
SUBST INSTR(FIRST_NAME,'A')
----- ---------------------
Ellen 0
Sunda 5
Mozhe 0
David 2
RPAD(FIRST_NAME,10,'_')||
-------------------------
Steven____________AD_PRES
Neena_______________AD_VP
Lex_________________AD_VP
Alexander_________IT_PROG
Number functions
ROUND(1372.472,1)
-----------------
1372.5
TRUNC(72183,-2)
---------------
72100
Date arithmetic operations
EMPLOYEE_ID EMPLOYMENT_DAYS
----------- ---------------
100 3698.61877
101 2871.61877
102 4583.61877
103 2767.61877
DBMS
Date functions
EMPLOYEE_ID EMPLOYMENT_MONTHS
----------- -----------------
100 121.504216
101 94.3751837
102 150.633248
103 90.9558289
Query-1
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > 15000;
Query-2
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > '15000';
TO_CHAR function
TO_CHAR function is used to typecast a numeric or date input to
character type with a format model (optional).
DBMS
Syntax
TO_CHAR(number1, [format], [nls_parameter])
SELECT first_name,
TO_CHAR (hire_date, 'MONTH DD, YYYY') HIRE_DATE,
TO_CHAR (salary, '$99999.99') Salary
FROM employees
WHERE rownum < 5;
The first TO_CHAR is used to convert the hire date to the date
format MONTH DD, YYYY i.e. month spelled out and padded with
spaces, followed by the two-digit day of the month, and then the
four-digit year. If you prefer displaying the month name in mixed
case (that is, "December"), simply use this case in the format
argument: ('Month DD, YYYY').
Format
Description
Model
TO_NUMBER function
DBMS
The TO_NUMBER function converts a character value to a numeric
datatype. If the string being converted contains nonnumeric
characters, the function returns an error.
Syntax
TO_NUMBER (string1, [format], [nls_parameter])
The below table shows the list of format models which can be
used to typecast character values as number using TO_NUMBER.
Format
Description
Model
CC Century
BC BC/AD Indicator
AM, PM AM or PM
The frac onal seconds. Use with mestamp. The digit controls the
FF1..FF9
number of decimal digits used for frac onal seconds.
IYY OR IY OR I The last 3,2,1 digits of the ISO standard year. Output only
The last 2 digits of the year when used for output. Accepts fout-
RRRR
digit years when used for input.
TO_NUMBER('121.23','9G999D99')
------------------------------
121.23
TO_NUMBER('1210.73','9999.99')
------------------------------
1210.73
TO_DATE function
The function takes character values as input and returns
formatted date equivalent of the same. The TO_DATE function
allows users to enter a date in any format, and then it converts
the entry into the default format used by Oracle 11g.
Syntax:
TO_DATE( string1, [ format_mask ], [ nls_language ] )
TO_DATE('
---------
15-JAN-89
General Functions
General functions are used to handle NULL values in database.
The objective of the general NULL handling functions is to replace
the NULL values with an alternate value. We shall briefly see
through these functions below.
NVL
Syntax:
NVL( Arg1, replace_with )
In the syntax, both the parameters are mandatory. Note that NVL
function works with all types of data types. And also that the data
DBMS
type of original string and the replacement must be in compatible
state i.e. either same or implicitly convertible by Oracle.
Syntax:
NVL2( string1, value_if_NOT_null, value_if_null )
Syntax:
NULLIF (expr1, expr2)
DBMS
Note that first argument can be an expression that evaluates to
NULL, but it can't be the literal NULL. Both the parameters are
mandatory for the function to execute.
The below query returns NULL since both the input values, 12 are
equal.
Similarly, below query return 'SUN' since both the strings are not
equal.
Syntax:
COALESCE (expr1, expr2, ... expr_n )
Consider the below SELECT query. It selects the first not null
value fed into address fields for an employee.
Conditional Functions
Oracle provides conditional functions DECODE and CASE to
impose conditions even in SQL statement.
Syntax:
DECODE (expression, search, result [, search,
result]... [, default])
DECOD
-----
EQUAL
Syntax:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
Oracle search starts from left and moves rightwards until it finds
a true condition, and then returns result expression associated
with it. If no condition is found to be true, and an ELSE clause
exists, then Oracle returns result defined with else. Otherwise,
Oracle returns null.
ENAM CASE
---- -------
JOHN GRADE 2
EDWIN GRADE 3
KING GRADE 1
DBMS
DBMS
Using Group functions
SELECT COUNT(*)
FROM employees;
COUNT(*)
----------
24
COUNT(*)
DBMS
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
TOTAL_SAL
---------
87472
The below SELECT query returns the oldest and latest hired dates
of employees in the organization.
OLDEST LATEST
--------- -----------
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.
FIRST_NAME DNAME
---------- ------
MILLER DEPT 1
JOHN DEPT 1
MARTIN DEPT 2
EDWIN DEPT 2
USING Clause
Using Natural joins, Oracle implicitly identify columns to form the
basis of join. Many situations require explicit declaration of join
conditions. In such cases, we use USING clause to specify the
joining criteria. Since, USING clause joins the tables based on
equality of columns, it is also known as Equijoin. They are also
known as Inner joins or simple joins.
DBMS
Syntax:
SELECT <column list>
FROM TABLE1 JOIN TABLE2
USING (column name)
Self Join
A SELF-JOIN operation produces a result table when the
relationship of interest exists among rows that are stored within a
single table. In other words, when a table is joined to itself, the
join is known as Self Join.
Non Equijoins
A non-equality join is used when the related columns can't be
joined with an equal sign-meaning there are no equivalent rows
in the tables to be joined.A non-equality join enables you to store
a range's minimum value in one column of a record and the
maximum value in another column. So instead of finding a
column-tocolumn match, you can use a non-equality join to
determine whether the item being shipped falls between
minimum and maximum ranges in the columns.If the join does
find a matching range for the item, the corresponding shipping
fee can be returned in the results. As with the traditional method
DBMS
of equality joins, a non-equality join can be performed in a
WHERE clause. In addition, the JOIN keyword can be used with
the ON clause to specify relevant columns for the join.
SELECT E.first_name,
J.job_hisal,
J.job_losal,
E.salary
FROM employees E JOIN job_sal J
ON (E.salary BETWEEN J.job_losal AND J.job_losal);
Outer Joins
An Outer Join is used to identify situations where rows in one
table do not match rows in a second table, even though the two
tables are related.
There are three types of outer joins: the LEFT, RIGHT, and FULL
OUTER JOIN. They all begin with an INNER JOIN, and then they
add back some of the rows that have been dropped. A LEFT
OUTER JOIN adds back all the rows that are dropped from the
first (left) table in the join condition, and output columns from
the second (right) table are set to NULL. A RIGHT OUTER JOIN
adds back all the rows that are dropped from the second (right)
table in the join condition, and output columns from the first
(left) table are set to NULL. The FULL OUTER JOIN adds back all
the rows that are dropped from both the tables.
FIRST_NAME DEPARTMENT_ID
DBMS
---------- --------------------
MAN -
JOHN 10
EDWIN 20
MILLER 10
MARTIN 20
- 30
6 rows selected.
SQL Subqueries
An SQL Subquery, is a SELECT query within another query. It is
also known as Inner query or Nested query and the query
containing it is the outer query.
The outer query can contain the SELECT, INSERT, UPDATE, and
DELETE statements. We can use the subquery as a column
expression, as a condition in SQL clauses, and with operators like
=, >, <, >=, <=, IN, BETWEEN, etc.
Rules to be followed
Following are the rules to be followed while writing subqueries −
UPDATE table
SET column_name = new_value
[WHERE OPERATOR [VALUE](SELECT COLUMN_NAME FROM TABLE_NAME
[WHERE]);
Example
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >= 27 );
This would impact two rows and if you verify the contents of the
CUSTOMERS using the SELECT statement as shown below.
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.