Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 72

Unit- One techniques, which can detect attempt of leaving database in

Introduction inconsistent state. A DBMS can provide greater consistency


1.1 Definition of Database and Database management System as compared to earlier forms of data storing applications
Database is a collection of related data and data is a collection of like file-processing systems.
facts and figures that can be processed to produce information.  Query Language − DBMS is equipped with query
Mostly data represents recordable facts. Data aids in producing language, which makes it more efficient to retrieve and
information, which is based on facts. For example, if we have data manipulate data. A user can apply as many and as different
about marks obtained by all students, we can then conclude about filtering options as required to retrieve a set of data.
toppers and average marks. Traditionally it was not possible where file-processing
A database management system stores data in such a way that it system was used.
becomes easier to retrieve, manipulate, and produce information.  ACID Properties − DBMS follows the concepts
1.2 Characteristics of database approach of Atomicity, Consistency, Isolation, and Durability
Traditionally, data was organized in file formats. DBMS was a new (normally shortened as ACID). These concepts are applied
concept then, and all the research was done to make it overcome the on transactions, which manipulate data in a database. ACID
deficiencies in traditional style of data management. A modern properties help the database stay healthy in multi-
DBMS has the following characteristics − transactional environments and in case of failure.
 Real-world entity − A modern DBMS is more realistic and  Multiuser and Concurrent Access − DBMS supports
uses real-world entities to design its architecture. It uses the multi-user environment and allows them to access and
behavior and attributes too. For example, a school database manipulate data in parallel. Though there are restrictions on
may use students as an entity and their age as an attribute. transactions when users attempt to handle the same data
 Relation-based tables − DBMS allows entities and item, but users are always unaware of them.
relations among them to form tables. A user can understand  Multiple views − DBMS offers multiple views for different
the architecture of a database just by looking at the table users. A user who is in the Sales department will have a
names. different view of database than a person working in the
 Isolation of data and application − A database system is Production department. This feature enables the users to
entirely different than its data. A database is an active have a concentrate view of the database according to their
entity, whereas data is said to be passive, on which the requirements.
database works and organizes. DBMS also stores metadata,  Security − Features like multiple views offer security to
which is data about data, to ease its own process. some extent where users are unable to access data of other
 Less redundancy − DBMS follows the rules of users and departments. DBMS offers methods to impose
normalization, which splits a relation when any of its constraints while entering data into the database and
attributes is having redundancy in values. Normalization is retrieving the same at a later stage. DBMS offers many
a mathematically rich and scientific process that reduces different levels of security features, which enables multiple
data redundancy. users to have different views with different features. For
 Consistency − Consistency is a state where every relation in example, a user in the Sales department cannot see the data
a database remains consistent. There exist methods and that belongs to the Purchase department. Additionally, it

Compiled By : Er. Sanjay Kumar Mishra 1


can also be managed how much data of the Sales many businesses, faster response means better customer
department should be displayed to the user. Since a DBMS service.
is not saved on the disk as traditional file systems, it is very  Multiple access: Database software allows data to be
hard for miscreants to break the code. accessed in a variety of ways (through various key fields), by
1.3 Database System VS Traditional file processing System using several programming languages (both3GL and
nonprocedural4GL programs).
The difference between file processing system and database  Lower user training costs: Users often find it easier to
management system is as follow: learn such systems and training costs may be reduced. Also,
the total time taken to process requests may be less, which
1. A file processing system is a collection of programs that would increase user productivity.
store and manage files in computer hard-disk. On the  Less storage: Theoretically, all occurrences of data items
other hand, A database management system is collection need be stored only once, thereby eliminating the storage of
of programs that enables to create and maintain a redundant data. System developers and database designers
database. often use data normalization to minimize data redundancy.
2. File processing system has more data redundancy, less Disadvantages:
data redundancy in dbms.  DBMS subjects business to risk of critical data loss in its
3. File processing system provides less flexibility in electronic format and can be more readily stolen without
accessing data, whereas dbms has more flexibility in proper security.
accessing data.  The cost of a DBMS can be prohibitive for small enterprises
4. File processing system does not provide data consistency,
as they struggle with cost justification for making investment
whereas dbms provides data consistency through
in the infrastructure.
normalization.
 Improper use of the DBMS can lead to incorrect decision
5. File processing system is less complex, whereas dbms is
making as people take presented data for granted as accurate.
more complex.
 Data can be stolen by weak password security policy.
1.4 Advantages and limitations of using DBMS

The principal advantages of DBMS over file processing system:


 Flexibility: Because programs and data are independent,
programs do not have to be modified when types of
unrelated data are added to or deleted from the database, or
when physical storage changes.
 Fast response to information requests:Because data is
integrated into a single database, complex requests can be
handled much more rapidly than locating data separately. In

Compiled By : Er. Sanjay Kumar Mishra 2


Unit-2 Every attribute is defined by its set of values
Database System Concepts and Architecture called domain. For example, in a school database, a
2.1 Data Models student is considered as an entity. Student has various
Data models define how the logical structure of a database is attributes like name, age, class, etc.
modeled. Data Models are fundamental entities to introduce  Relationship − The logical association among entities
abstraction in a DBMS. Data models define how data is is called relationship. Relationships are mapped with
connected to each other and how they are processed and entities in various ways. Mapping cardinalities define
stored inside the system. the number of association between two entities.
The very first data model could be flat data-models, where all Mapping cardinalities −
the data used are to be kept in the same plane. Earlier data o one to one
models were not so scientific, hence they were prone to o one to many
introduce lots of duplication and update anomalies. o many to one
Entity-Relationship Model o many to many
Entity-Relationship (ER) Model is based on the notion of real- Relational Model
world entities and relationships among them. While The most popular data model in DBMS is the Relational
formulating real-world scenario into the database model, the Model. It is more scientific a model than others. This model is
ER Model creates entity set, relationship set, general attributes based on first-order predicate logic and defines a table as
and constraints. an n-ary relation.
ER Model is best used for the conceptual design of a database.
ER Model is based on −
 Entities and their attributes.
 Relationships among entities.
These concepts are explained below.

 Entity − An entity in an ER Model is a real-world The main highlights of this model are −
entity having properties called attributes.  Data is stored in tables called relations.

Compiled By : Er. Sanjay Kumar Mishra 3


 Relations can be normalized.
 In normalized relations, values saved are atomic values.
 Each row in a relation contains a unique value.
 Each column in a relation contains values from a same
domain.
2.2 DBMS - Architecture
The design of a DBMS depends on its architecture. It can be
centralized or decentralized or hierarchical. The architecture
of a DBMS can be seen as either single tier or multi-tier. An
n-tier architecture divides the whole system into related but
independent n modules, which can be independently
modified, altered, changed, or replaced.  Database (Data) Tier − At this tier, the database
In 1-tier architecture, the DBMS is the only entity where the resides along with its query processing languages. We
user directly sits on the DBMS and uses it. Any changes done also have the relations that define the data and their
here will directly be done on the DBMS itself. It does not constraints at this level.
provide handy tools for end-users. Database designers and  Application (Middle) Tier − At this tier reside the
programmers normally prefer to use single-tier architecture. application server and the programs that access the
If the architecture of DBMS is 2-tier, then it must have an database. For a user, this application tier presents an
application through which the DBMS can be accessed. abstracted view of the database. End-users are unaware
Programmers use 2-tier architecture where they access the of any existence of the database beyond the
DBMS by means of an application. Here the application tier is application. At the other end, the database tier is not
entirely independent of the database in terms of operation, aware of any other user beyond the application tier.
design, and programming. Hence, the application layer sits in the middle and acts
3-tier Architecture as a mediator between the end-user and the database.
A 3-tier architecture separates its tiers from each other based  User (Presentation) Tier − End-users operate on this
on the complexity of the users and how they use the data tier and they know nothing about any existence of the
present in the database. It is the most widely used architecture database beyond this layer. At this layer, multiple
to design a DBMS. views of the database can be provided by the
application. All views are generated by applications
that reside in the application tier.
Multiple-tier database architecture is highly modifiable, as
almost all its components are independent and can be changed
independently.

Compiled By : Er. Sanjay Kumar Mishra 4


DBMS - Data Independence some changes on table format, it should not change the data
If a database system is not multi-layered, then it becomes residing on the disk.
difficult to make any changes in the database system. Physical Data Independence
Database systems are designed in multi-layers as we learnt All the schemas are logical, and the actual data is stored in bit
earlier. format on the disk. Physical data independence is the power to
Data Independence change the physical data without impacting the schema or
A database system normally contains a lot of data in addition logical data.
to users’ data. For example, it stores data about data, known For example, in case we want to change or upgrade the
as metadata, to locate and retrieve data easily. It is rather storage system itself − suppose we want to replace hard-disks
difficult to modify or update a set of metadata once it is stored with SSD − it should not have any impact on the logical data
in the database. But as a DBMS expands, it needs to change or schemas.
over time to satisfy the requirements of the users. If the entire 2.3 Database Languages
data is dependent, it would become a tedious and highly A DBMS must provide appropriate languages and interfaces
complex job. for each category of users to express database queries and
updates. Database Languages are used to create and maintain
database on computer. There are large numbers of database
languages like Oracle, MySQL, MS Access, dBase, FoxPro
etc. SQL statements commonly used in Oracle and MS Access
can be categorized as data definition language (DDL), data
control language (DCL) and data manipulation language
(DML).

Metadata itself follows a layered architecture, so that when we Data Definition Language (DDL)
change data at one layer, it does not affect the data at another It is a language that allows the users to define data and their
level. This data is independent but mapped to each other. relationship to other types of data. It is mainly used to create
Logical Data Independence files, databases, data dictionary and tables within databases.
Logical data is data about database, that is, it stores
information about how data is managed inside. For example, a It is also used to specify the structure of each table, set of
table (relation) stored in the database and all its constraints, associated values with each attribute, integrity constraints,
applied on that relation. security and authorization informationfor each table and
Logical data independence is a kind of mechanism, which physical storage structure of each table on disk.
liberalizes itself from actual data stored on the disk. If we do The following table gives an overview about usage of DDL
statements in SQL

Compiled By : Er. Sanjay Kumar Mishra 5


The following table gives an overview about the usage of DCL
statements in SQL:

Data Manipulation Language (DML)


It is a language that provides a set of operations to support the
basic data manipulation operations on the data held in the In practice, the data definition and data manipulation languages
databases. It allows users to insert, update, delete and retrieve are not two separate languages. Instead they simply form parts
data from the database. The part of DML that involves data of a single database language such as Structured Query
retrieval is called a query language. Language (SQL). SQL represents combination of DDL and
DML, as well as statements for constraints specification and
The following table gives an overview about the usage of DML schema evaluation.
statements in SQL:
2.4 Database Users and Database Administrator
A typical DBMS has users with different rights and
permissions who use it for different purposes. Some users
retrieve data and some back it up. The users of a DBMS can
be broadly categorized as follows −

Data Control Language (DCL)


DCL statements control access to data and the database using
statements such as GRANT and REVOKE. A privilege can
either be granted to a User with the help of GRANT statement.
The privileges assigned can be SELECT, ALTER, DELETE,
EXECUTE, INSERT, INDEX etc. In addition to granting of
privileges, you can also revoke (taken back) it by using  Administrators − Administrators maintain the DBMS
REVOKE command. and are responsible for administrating the database.

Compiled By : Er. Sanjay Kumar Mishra 6


They are responsible to look after its usage and by Attributes
whom it should be used. They create access profiles Entities are represented by means of their properties,
for users and apply limitations to maintain isolation called attributes. All attributes have values. For example, a
and force security. Administrators also look after student entity may have name, class, and age as attributes.
DBMS resources like system license, required tools, There exists a domain or range of values that can be assigned
and other software and hardware related maintenance. to attributes. For example, a student's name cannot be a
 Designers − Designers are the group of people who numeric value. It has to be alphabetic. A student's age cannot
actually work on the designing part of the database. be negative, etc.
They keep a close watch on what data should be kept Types of Attributes
and in what format. They identify and design the  Simple attribute − Simple attributes are atomic
whole set of entities, relations, constraints, and views. values, which cannot be divided further. For example,
 End Users − End users are those who actually reap the a student's phone number is an atomic value of 10
benefits of having a DBMS. End users can range from digits.
simple viewers who pay attention to the logs or market  Composite attribute − Composite attributes are made
rates to sophisticated users such as business analysts. of more than one simple attribute. For example, a
student's complete name may have first_name and
2.5 ER Model - Basic Concepts last_name.
The ER model defines the conceptual view of a database. It  Derived attribute − Derived attributes are the
works around real-world entities and the associations among attributes that do not exist in the physical database, but
them. At view level, the ER model is considered a good their values are derived from other attributes present in
option for designing databases. the database. For example, average_salary in a
Entity department should not be saved directly in the
An entity can be a real-world object, either animate or database, instead it can be derived. For another
inanimate, that can be easily identifiable. For example, in a example, age can be derived from data_of_birth.
school database, students, teachers, classes, and courses  Single-value attribute − Single-value attributes
offered can be considered as entities. All these entities have contain single value. For example −
some attributes or properties that give them their identity. Social_Security_Number.
An entity set is a collection of similar types of entities. An  Multi-value attribute − Multi-value attributes may
entity set may contain entities with attribute sharing similar contain more than one values. For example, a person
values. For example, a Students set may contain all the can have more than one phone number, email_address,
students of a school; likewise a Teachers set may contain all etc.
the teachers of a school from all faculties. Entity sets need not These attribute types can come together in a way like −
be disjoint.  simple single-valued attributes

Compiled By : Er. Sanjay Kumar Mishra 7


 simple multi-valued attributes Cardinality defines the number of entities in one entity set,
 composite single-valued attributes which can be associated with the number of entities of other
 composite multi-valued attributes set via relationship set.
Entity-Set and Keys  One-to-one − One entity from entity set A can be
Key is an attribute or collection of attributes that uniquely associated with at most one entity of entity set B and
identifies an entity among entity set. vice versa.
For example, the roll_number of a student makes him/her
identifiable among students.
 Super Key − A set of attributes (one or more) that
collectively identifies an entity in an entity set.
 Candidate Key − A minimal super key is called a
candidate key. An entity set may have more than one  One-to-many − One entity from entity set A can be
candidate key. associated with more than one entities of entity set B
 Primary Key − A primary key is one of the candidate however an entity from entity set B, can be associated
keys chosen by the database designer to uniquely with at most one entity.
identify the entity set.
Relationship
The association among entities is called a relationship. For
example, an employee works_at a department, a
student enrolls in a course. Here, Works_at and Enrolls are
called relationships.  Many-to-one − More than one entities from entity set
Relationship Set A can be associated with at most one entity of entity
A set of relationships of similar type is called a relationship set B, however an entity from entity set B can be
set. Like entities, a relationship too can have attributes. These associated with more than one entity from entity set A.
attributes are called descriptive attributes.
Degree of Relationship
The number of participating entities in a relationship defines
the degree of the relationship.
 Binary = degree 2
 Ternary = degree 3
 n-ary = degree
Mapping Cardinalities

Compiled By : Er. Sanjay Kumar Mishra 8


 Many-to-many − One entity from A can be associated If the attributes are composite, they are further divided in a
with more than one entity from B and vice versa. tree like structure. Every node is then connected to its
attribute. That is, composite attributes are represented by
ellipses that are connected with an ellipse.

ER Diagram Representation
Let us now learn how the ER Model is represented by means
of an ER diagram. Any object, for example, entities, attributes
of an entity, relationship sets, and attributes of relationship
sets, can be represented with the help of an ER diagram.
Entity Multivalued attributes are depicted by double ellipse.
Entities are represented by means of rectangles. Rectangles
are named with the entity set they represent.

Attributes
Attributes are the properties of entities. Attributes are
represented by means of ellipses. Every ellipse represents one
attribute and is directly connected to its entity (rectangle). Derived attributes are depicted by dashed ellipse.

Compiled By : Er. Sanjay Kumar Mishra 9


Relationship of an entity on the right can be associated with the
Relationships are represented by diamond-shaped box. Name relationship. It depicts many-to-one relationship.
of the relationship is written inside the diamond-box. All the
entities (rectangles) participating in a relationship, are
connected to it by a line.
Binary Relationship and Cardinality  Many-to-many − The following image reflects that
A relationship where two entities are participating is called more than one instance of an entity on the left and
a binary relationship. Cardinality is the number of instance more than one instance of an entity on the right can be
of an entity from a relation that can be associated with the associated with the relationship. It depicts many-to-
relation. many relationship.
 One-to-one − When only one instance of an entity is
associated with the relationship, it is marked as '1:1'.
The following image reflects that only one instance of
each entity should be associated with the relationship. Participation Constraints
It depicts one-to-one relationship.  Total Participation − Each entity is involved in the
relationship. Total participation is represented by
double lines.
 Partial participation − Not all entities are involved in
the relationship. Partial participation is represented by
single lines.
 One-to-many − When more than one instance of an
entity is associated with a relationship, it is marked as
'1:N'. The following image reflects that only one
instance of entity on the left and more than one
instance of an entity on the right can be associated
with the relationship. It depicts one-to-many Generalization Aggregation
relationship. The ER Model has the power of expressing database entities
in a conceptual hierarchical manner. As the hierarchy goes up,
it generalizes the view of entities, and as we go deep in the
hierarchy, it gives us the detail of every entity included.
 Many-to-one − When more than one instance of entity Going up in this structure is called generalization, where
is associated with the relationship, it is marked as entities are clubbed together to represent a more generalized
'N:1'. The following image reflects that more than one view. For example, a particular student named Mira can be
instance of an entity on the left and only one instance generalized along with all the students. The entity shall be a

Compiled By : Er. Sanjay Kumar Mishra 10


student, and further, the student is a person. The reverse is We use all the above features of ER-Model in order to create
called specialization where a person is a student, and that classes of objects in object-oriented programming. The details
student is Mira. of entities are generally hidden from the user; this process
Generalization known as abstraction.
As mentioned above, the process of generalizing entities, Inheritance is an important feature of Generalization and
where the generalized entities contain the properties of all the Specialization. It allows lower-level entities to inherit the
generalized entities, is called generalization. In generalization, attributes of higher-level entities.
a number of entities are brought together into one generalized
entity based on their similar characteristics. For example,
pigeon, house sparrow, crow and dove can all be generalized
as Birds.

Specialization
Specialization is the opposite of generalization. In For example, the attributes of a Person class such as name,
specialization, a group of entities is divided into sub-groups age, and gender can be inherited by lower-level entities such
based on their characteristics. Take a group ‘Person’ for as Student or Teacher.
example. A person has name, date of birth, gender, etc. These
properties are common in all persons, human beings. But in a 2.6 Data Dictionary
company, persons can be identified as employee, employer,
customer, or vendor, based on what role they play in the A data dictionary is a file or a set of files that contains a database's
company. metadata. The data dictionary contains records about other objects in
the database, such as data ownership, data relationships to other
objects, and other data.

The data dictionary is a crucial component of any relational


database. Ironically, because of its importance, it is invisible to most
database users. Typically, only database administrators interact with
the data dictionary.
Similarly, in a school database, persons can be specialized as
teacher, student, or a staff, based on what role they play in
school as entities.
Inheritance

Compiled By : Er. Sanjay Kumar Mishra 11


Unit-3 database designers who design the schema to help
Relational Data Model programmers understand the database and make it useful.
Relational data model is the primary data model, which is
used widely around the world for data storage and processing.
This model is simple and it has all the properties and
capabilities required to process data with storage efficiency.
3.1 Properties of relation
Tables − In relational data model, relations are saved in the
format of Tables. This format stores the relation among
entities. A table has rows and columns, where rows represents
records and columns represent the attributes.
Tuple − A single row of a table, which contains a single
record for that relation is called a tuple. A database schema can be divided broadly into two categories
Relation instance − A finite set of tuples in the relational −
database system represents relation instance. Relation  Physical Database Schema − This schema pertains to
instances do not have duplicate tuples. the actual storage of data and its form of storage like
Relation schema − A relation schema describes the relation files, indices, etc. It defines how the data will be stored
name (table name), attributes, and their names. in a secondary storage.
Relation key − Each row has one or more attributes, known  Logical Database Schema − This schema defines all
as relation key, which can identify the row in the relation the logical constraints that need to be applied on the
(table) uniquely. data stored. It defines tables, views, and integrity
Attribute domain − Every attribute has some pre-defined constraints.
value scope, known as attribute domain. Database Instance
3.2 Data Schemas It is important that we distinguish these two terms
Database Schema individually. Database schema is the skeleton of database. It is
A database schema is the skeleton structure that represents the designed when the database doesn't exist at all. Once the
logical view of the entire database. It defines how the data is database is operational, it is very difficult to make any
organized and how the relations among them are associated. It changes to it. A database schema does not contain any data or
formulates all the constraints that are to be applied on the information.
data. A database instance is a state of operational database with data
A database schema defines its entities and the relationship at any given time. It contains a snapshot of the database.
among them. It contains a descriptive detail of the database, Database instances tend to change with time. A DBMS
which can be depicted by means of schema diagrams. It’s the ensures that its every instance (state) is in a valid state, by

Compiled By : Er. Sanjay Kumar Mishra 12


diligently following all the validations, constraints, and connectors like and, or, and not. These terms may use
conditions that the database designers have imposed. relational operators like − =, ≠, ≥, < , >, ≤.
3.3 Relational Algebra For example −
σsubject="database"(Books)
Relational database systems are expected to be equipped with
a query language that can assist its users to query the database
instances. There are two kinds of query languages − relational Output − Selects tuples from books where subject is
algebra and relational calculus. 'database'.
Relational Algebra
Relational algebra is a procedural query language, which takes σsubject="database" and price="450"(Books)
instances of relations as input and yields instances of relations
as output. It uses operators to perform queries. An operator
can be either unary or binary. They accept relations as their Output − Selects tuples from books where subject is
input and yield relations as their output. Relational algebra is 'database' and 'price' is 450.
performed recursively on a relation and intermediate results σsubject="database" and price < "450" or year > "2010"(Books)
are also considered relations.
The fundamental operations of relational algebra are as
follows − Output − Selects tuples from books where subject is
 Select 'database' and 'price' is 450 or those books published after
 Project 2010.
 Union Project Operation (∏)
 Set different It projects column(s) that satisfy a given predicate.
 Cartesian product Notation − ∏A1, A2, An (r)
 Rename Where A1, A2 , An are attribute names of relation r.
 Division Duplicate rows are automatically eliminated, as relation is a
 Assignment set.
We will discuss all these operations in the following sections. For example −
Select Operation (σ)
∏subject, author (Books)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for
Selects and projects columns named as subject and author
relation. p is prepositional logic formula which may use
from the relation Books.
Union Operation (∪)

Compiled By : Er. Sanjay Kumar Mishra 13


It performs binary union between two given relations and is Where r and s are relations and their output will be defined as
defined as − −
r ∪ s = { t | t ∈ r or t ∈ s} r Χ s = { q t | q ∈ r and t ∈ s}
∏ author = 'tutorialspoint'(Books Χ Articles)

Notation − r U s
Where r and s are either database relations or relation result Output − Yields a relation, which shows all the books and
set (temporary relation). articles written by tutorialspoint.
For a union operation to be valid, the following conditions Rename Operation (ρ)
must hold − The results of relational algebra are also relations but without
 r, and s must have the same number of attributes. any name. The rename operation allows us to rename the
 Attribute domains must be compatible. output relation. 'rename' operation is denoted with small
 Duplicate tuples are automatically eliminated. Greek letter rho ρ.
∏ author (Books) ∪ ∏ author (Articles) Notation − ρ x (E)
Where the result of expression E is saved with name of x.

Output − Projects the names of the authors who have either Division (÷)
written a book or an article or both. The division is a binary operation that is written as R ÷ S. Division is
Set Difference (−) not implemented directly in SQL. The result consists of the
The result of set difference operation is tuples, which are restrictions of tuples in R to the attribute names unique to R, i.e., in
present in one relation but are not in the second relation. the header of R but not in the header of S, for which it holds that all
Notation − r − s their combinations with tuples in S are present in R. For an example
Finds all the tuples that are present in r but not in s. see the tables Completed, DBProject and their division:
∏ author (Books) − ∏ author (Articles)
Completed DBProject Completed÷ DBProject

Output − Provides the name of authors who have written


books but not articles. Studen Task Student
Task
Cartesian Product (Χ) t
Combines information of two different relations into one.
Notation − r Χ s Database1 Fred
Fred Database1

Compiled By : Er. Sanjay Kumar Mishra 14


No extra relation is added to the database, but the relation variable
Fred Database2 Database2 Sarah created can be used in subsequent expressions. Assignment to a
permanent relation would constitute a modification to the database.

Fred Compiler1
DBMS - Joins
We understand the benefits of taking a Cartesian product of
two relations, which gives us all the possible tuples that are
Eugene Database1
paired together. But it might not be feasible for us in certain
cases to take a Cartesian product where we encounter huge
relations with thousands of tuples having a considerable large
Eugene Compiler1
number of attributes.
Join is a combination of a Cartesian product followed by a
selection process. A Join operation pairs two tuples from
Sarah Database1
different relations, if and only if a given join condition is
satisfied.
We will briefly describe various join types in the following
Sarah Database2
sections.
Theta (θ) Join
If DBProject contains all the tasks of the Database project, then the Theta join combines tuples from different relations provided
result of the division above contains exactly the students who have they satisfy the theta condition. The join condition is denoted
completed both of the tasks in the Database project. by the symbol θ.
The Assignment Operation Notation
Sometimes it is useful to be able to write a relational algebra R1 ⋈θ R2
expression in parts using a temporary relation variable (as we did R1 and R2 are relations having attributes (A1, A2, .., An) and
with and in the division example). (B1, B2,.. ,Bn) such that the attributes don’t have anything in
The assignment operation, denoted , works like assignment in a common, that is R1 ∩ R2 = Φ.
programming language. Theta join can use all kinds of comparison operators.
We could rewrite our division definition as Student

SID Name Std

101 Alex 10

Compiled By : Er. Sanjay Kumar Mishra 15


102 Maria 11
Natural join does not use any comparison operator. It does not
concatenate the way a Cartesian product does. We can
Subjects perform a Natural Join only if there is at least one common
attribute that exists between two relations. In addition, the
Class Subject attributes must have the same name and domain.
Natural join acts on those matching attributes where the
10 Math values of attributes in both the relations are same.
10 English Courses

11 Music CID Course Dept

11 Sports CS01 Database CS

Student_Detail = ME01 Mechanics ME


STUDENT ⋈Student.Std = Subject.Class SUBJECT
EE01 Electronics EE

Student_detail HoD

SID Name St Class Subject Dept Head


d
CS Alex
101 Alex 10 10 Math
ME Maya
101 Alex 10 10 English
EE Mira
102 Maria 11 11 Music
Courses ⋈ HoD
102 Maria 11 11 Sports
Dept CID Course Head
Equijoin
When Theta join uses only equality comparison operator, it is CS CS01 Database Alex
said to be equijoin. The above example corresponds to
equijoin. ME ME01 Mechanics Maya
Natural Join (⋈)

Compiled By : Er. Sanjay Kumar Mishra 16


EE EE01 Electronics Mira Courses HoD
Outer Joins
Theta Join, Equijoin, and Natural Join are called inner joins. A B C D
An inner join includes only those tuples with matching
attributes and the rest are discarded in the resulting relation. 100 Database 10 Alex
Therefore, we need to use outer joins to include all the tuples 0
from the participating relations in the resulting relation. There
are three kinds of outer joins − left outer join, right outer join, 101 Mechanics --- ---
and full outer join.
102 Electronics 10 Maya
Left Outer Join(R S)
2
All the tuples from the Left relation, R, are included in the
resulting relation. If there are tuples in R without any Right Outer Join: ( R S )
matching tuple in the Right relation S, then the S-attributes of All the tuples from the Right relation, S, are included in the
the resulting relation are made NULL. resulting relation. If there are tuples in S without any
matching tuple in R, then the R-attributes of resulting relation
Course
are made NULL.
A B Courses HoD

100 Database A B C D

101 Mechanics 10 Database 10 Alex


0 0
102 Electronics
10 Electronics 10 Maya
HOD 2 2

C D --- --- 10 Mira


4
100 Alex
Full Outer Join: ( R S)
102 Maya All the tuples from both participating relations are included in
the resulting relation. If there are no matching tuples for both
104 Mira relations, their respective unmatched attributes are made
NULL.

Compiled By : Er. Sanjay Kumar Mishra 17


Courses HoD  Find the designation and salary of all employees

A B C D
∏designation, salary (Employee)
 Find the eno and salary of all employees whose salary is
10 Database 10 Alex more than 5000.
0 0
∏eno, salary σsalary>5000(Employee)
10 Mechanics --- --- 2. Consider the following relation given below. (2007)
1

10 Electronics 10 Maya Loan


2 2 Loan_no Branch_name Amount
L-11 Hundhill 900
--- --- 10 Mira L-14 Dowwntown 1500
4 L-15 Perryridge 13000
L-16 Perryridge 1500
Some old questions: L-17 Downtown 1000
1. Consider the following relation given below. (2016) L-23 Redwood 2000
L-93 Mianus 500
Employee
Eno Designation salary Now write relational algebra query to:
10 Manager 10000  Select all tuples from loan relation whose branches is
11 Assistant 5500 downtown.
12 Manager 11000 σbranch_name="downtown"(Loan)
13 Peon 4500
14 Manager 11000  Select all tuples in which the amount lent more than 1200.
15 Clerk 6000 σamount>1200(Loan)
Now write relational algebra query to:  Find the “loan_no and amount” from the loan relation
 Find those tuples whose designation is manager and whose whose branche name is perryridge.
salary is more than 10,000. ∏loanno, amount σbranchname=”perryridge(Loan)
σdesignation="manager"(Employee)

Compiled By : Er. Sanjay Kumar Mishra 18


 Allows users to set permissions on tables, procedures and
Unit-4 views.
SQL - Overview A Brief History of SQL
 1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the
4.1 Introduction to SQL father of relational databases. He described a relational
model for databases.
SQL is a language to operate databases; it includes database  1974 − Structured Query Language appeared.
creation, deletion, fetching rows, modifying rows, etc. SQL is  1978 − IBM worked to develop Codd's ideas and released a
an ANSI (American National Standards Institute) standard product named System/R.
language, but there are many different versions of the SQL  1986 − IBM developed the first prototype of relational
language. database and standardized by ANSI. The first relational
What is SQL? database was released by Relational Software which later
SQL is Structured Query Language, which is a computer language came to be known as Oracle.
for storing, manipulating and retrieving data stored in a relational SQL Process
database. When you are executing an SQL command for any RDBMS, the
SQL is the standard language for Relational Database System. All system determines the best way to carry out your request and SQL
the Relational Database Management Systems (RDMS) like engine figures out how to interpret the task.
MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL There are various components included in this process.
Server use SQL as their standard database language. These components are −
Also, they are using different dialects, such as −  Query Dispatcher
 MS SQL Server using T-SQL,  Optimization Engines
 Oracle using PL/SQL,  Classic Query Engine
 MS Access version of SQL is called JET SQL (native  SQL Query Engine, etc.
format) etc. A classic query engine handles all the non-SQL queries, but a SQL
Why SQL? query engine won't handle logical files.
SQL is widely popular because it offers the following advantages − Following is a simple diagram showing the SQL Architecture −
 Allows users to access data in the relational database
management systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate
that data.
 Allows to embed within other languages using SQL
modules, libraries & pre-compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a
database.

Compiled By : Er. Sanjay Kumar Mishra 19


SQL Commands
INSERT
The standard SQL commands to interact with relational databases 2
Creates a record.
are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP.
These commands can be classified into the following groups based
UPDATE
on their nature − 3
Modifies records.
DDL - Data Definition Language
DELETE
Sr.No. Command & Description 4
Deletes records.
DCL - Data Control Language
CREATE Sr.No. Command & Description
1 Creates a new table, a view of a
table, or other object in the GRANT
1
database. Gives a privilege to user.

REVOKE
ALTER 2
Takes back privileges granted from user.
2 Modifies an existing database What is a NULL value?
object, such as a table. A NULL value in a table is a value in a field that appears to be
blank, which means a field with a NULL value is a field with no
value.
DROP It is very important to understand that a NULL value is different
than a zero value or a field that contains spaces. A field with a
Deletes an entire table, a view of a NULL value is the one that has been left blank during a record
3 creation.
table or other objects in the 4.2 Basic structure of SQL - Syntax
database. SQL is followed by a unique set of rules and guidelines called
Syntax. This tutorial gives you a quick start with SQL by listing all
DML - Data Manipulation Language the basic SQL Syntax.
All the SQL statements start with any of the keywords like
Sr.No. Command & Description SELECT, INSERT, UPDATE, DELETE, ALTER, DROP,
CREATE, USE, SHOW and all the statements end with a
SELECT semicolon (;).
1
Retrieves certain records from one or more tables. The most important point to be noted here is that SQL is case
insensitive, which means SELECT and select have same meaning in

Compiled By : Er. Sanjay Kumar Mishra 20


SQL statements. Whereas, MySQL makes difference in table WHERE column_name LIKE { PATTERN };
names. So, if you are working with MySQL, then you need to give
table names as they exist in the database.  SQL ORDER BY Clause
Various Syntax in SQL SELECT column1, column2....columnN
All the examples given in this tutorial have been tested with a FROM table_name
MySQL server. WHERE CONDITION
 SQL SELECT Statement ORDER BY column_name {ASC|DESC};
SELECT column1, column2....columnN  SQL GROUP BY Clause
FROM table_name;
SELECT SUM(column_name)
 SQL DISTINCT Clause FROM table_name
SELECT DISTINCT column1, column2....columnN WHERE CONDITION
FROM table_name; GROUP BY column_name;
 SQL WHERE Clause  SQL COUNT Clause
SELECT column1, column2....columnN SELECT COUNT(column_name)
FROM table_name FROM table_name
WHERE CONDITION; WHERE CONDITION;
 SQL AND/OR Clause  SQL HAVING Clause
SELECT column1, column2....columnN SELECT SUM(column_name)
FROM table_name FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2; WHERE CONDITION
GROUP BY column_name
 SQL IN Clause HAVING (arithematic function condition);
SELECT column1, column2....columnN  SQL CREATE TABLE Statement
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N); CREATE TABLE table_name(
column1 datatype,
 SQL BETWEEN Clause column2 datatype,
SELECT column1, column2....columnN column3 datatype,
FROM table_name .....
WHERE column_name BETWEEN val-1 AND val-2; columnN datatype,
PRIMARY KEY( one or more columns )
 SQL LIKE Clause
);
SELECT column1, column2....columnN
 SQL DROP TABLE Statement
FROM table_name

Compiled By : Er. Sanjay Kumar Mishra 21


DROP TABLE table_name;  SQL CREATE DATABASE Statement

 SQL CREATE INDEX Statement CREATE DATABASE database_name;

CREATE UNIQUE INDEX index_name  SQL DROP DATABASE Statement


ON table_name ( column1, column2,...columnN); DROP DATABASE database_name;
 SQL DROP INDEX Statement  SQL USE Statement
ALTER TABLE table_name USE database_name;
DROP INDEX index_name;
 SQL COMMIT Statement
 SQL DESC Statement
COMMIT;
DESC table_name;
 SQL ROLLBACK Statement
 SQL TRUNCATE TABLE Statement
ROLLBACK;
TRUNCATE TABLE table_name;
SQL - Data Types
 SQL ALTER TABLE Statement SQL Data Type is an attribute that specifies the type of data of any
ALTER TABLE table_name {ADD|DROP|MODIFY} object. Each column, variable and expression has a related data type
column_name {data_ype}; in SQL. You can use these data types while creating your tables.
You can choose a data type for a table column based on your
 SQL ALTER TABLE Statement (Rename) requirement.
ALTER TABLE table_name RENAME TO SQL Server offers six categories of data types for your use which
new_table_name; are listed below −
Exact Numeric Data Types
 SQL INSERT INTO Statement
DATA TYPE FROM TO
INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
Bigint - 9,223,372,036,
 SQL UPDATE Statement 9,223,372,036,854,775,808 854,775,807
UPDATE table_name
SET column1 = value1, column2 = Int -2,147,483,648 2,147,483,647
value2....columnN=valueN
[ WHERE CONDITION ]; Smallint -32,768 32,767

 SQL DELETE Statement Tinyint 0 255


DELETE FROM table_name
WHERE {CONDITION}; Bit 0 1

Compiled By : Er. Sanjay Kumar Mishra 22


Decimal -10^38 +1 10^38 -1 Unicode characters)

Numeric -10^38 +1 10^38 -1 Varchar


2 Maximum of 8,000 characters.(Variable-length non-
Money - +922,337,203, Unicode data).
922,337,203,685,477.5808 685,477.5807
varchar(max)
Smallmoney -214,748.3648 +214,748.3647 3 Maximum length of 2E + 31 characters, Variable-length
non-Unicode data (SQL Server 2005 only).
Approximate Numeric Data Types
DATA TYPE FROM TO Text
4 Variable-length non-Unicode data with a maximum
Float -1.79E + 308 1.79E + 308 length of 2,147,483,647 characters.
Unicode Character Strings Data Types
Real -3.40E + 38 3.40E + 38
Sr.No. DATA TYPE & Description
Date and Time Data Types
DATA FROM TO Nchar
TYPE 1 Maximum length of 4,000 characters.( Fixed length
Unicode)
Datetime Jan 1, 1753 Dec 31, 9999
Nvarchar
smalldatetim Jan 1, 1900 Jun 6, 2079 2 Maximum length of 4,000 characters.(Variable length
e Unicode)

Date Stores a date like June 30, 1991 nvarchar(max)


3 Maximum length of 2E + 31 characters (SQL Server
Time Stores a time of day like 12:30 P.M. 2005 only).( Variable length Unicode)
Note − Here, datetime has 3.33 milliseconds accuracy where as
Ntext
smalldatetime has 1 minute accuracy.
4 Maximum length of 1,073,741,823 characters. ( Variable
Character Strings Data Types
length Unicode )
Sr.No. DATA TYPE & Description
Binary Data Types
1 Char S.N. DATA TYPE & Description
Maximum length of 8,000 characters.( Fixed length non-

Compiled By : Er. Sanjay Kumar Mishra 23


Binary * Multiplies values on either
1
Maximum length of 8,000 bytes(Fixed-length binary data ) (Multiplication side of the operator. a * b will give 200
)
Varbinary
2 Maximum length of 8,000 bytes.(Variable length binary Divides left hand operand by
/ (Division) b / a will give 2
data) right hand operand.

varbinary(max) Divides left hand operand by


3 Maximum length of 2E + 31 bytes (SQL Server 2005 only). % (Modulus) right hand operand and b % a will give 0
( Variable length Binary data) returns remainder.
SQL Comparison Operators
Image
Assume 'variable a' holds 10 and 'variable b' holds 20, then −
4 Maximum length of 2,147,483,647 bytes. ( Variable length
Show Examples
Binary Data)
Operator Description Example
SQL - Operators
What is an Operator in SQL?
Checks if the values of two operands are
An operator is a reserved word or a character used primarily in an (a = b) is
= equal or not, if yes then condition
SQL statement's WHERE clause to perform operation(s), such as not true.
becomes true.
comparisons and arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to serve as conjunctions
Checks if the values of two operands are
for multiple conditions in a statement. (a != b)
!= equal or not, if values are not equal then
 Arithmetic operators is true.
condition becomes true.
 Comparison operators
 Logical operators
Checks if the values of two operands are
 Operators used to negate conditions (a <> b)
<> equal or not, if values are not equal then
SQL Arithmetic Operators is true.
condition becomes true.
Assume 'variable a' holds 10 and 'variable b' holds 20, then −
Show Examples
Checks if the value of left operand is
(a > b) is
Operator Description Example > greater than the value of right operand, if
not true.
yes then condition becomes true.
Adds values on either side of
+ (Addition) a + b will give 30
the operator. Checks if the value of left operand is less
(a < b) is
< than the value of right operand, if yes
true.
Subtracts right hand operand then condition becomes true.
- (Subtraction) a - b will give -10
from left hand operand.

Compiled By : Er. Sanjay Kumar Mishra 24


Checks if the value of left operand is BETWEEN
(a >= b)
greater than or equal to the value of right The BETWEEN operator is used to search for values
>= is not 4
operand, if yes then condition becomes that are within a set of values, given the minimum
true.
true. value and the maximum value.

Checks if the value of left operand is less EXISTS


than or equal to the value of right (a <= b) The EXISTS operator is used to search for the
<= 5
operand, if yes then condition becomes is true. presence of a row in a specified table that meets a
true. certain criterion.

Checks if the value of left operand is not IN


(a !< b)
!< less than the value of right operand, if 6 The IN operator is used to compare a value to a list of
is false.
yes then condition becomes true. literal values that have been specified.

Checks if the value of left operand is not LIKE


(a !> b)
!> greater than the value of right operand, if 7 The LIKE operator is used to compare a value to
is true.
yes then condition becomes true. similar values using wildcard operators.
SQL Logical Operators
NOT
Here is a list of all the logical operators available in SQL.
The NOT operator reverses the meaning of the logical
Show Examples
8 operator with which it is used. Eg: NOT EXISTS,
Sr.No Operator & Description NOT BETWEEN, NOT IN, etc. This is a negate
. operator.

ALL OR
1 The ALL operator is used to compare a value to all 9 The OR operator is used to combine multiple
values in another value set. conditions in an SQL statement's WHERE clause.

AND IS NULL
2 The AND operator allows the existence of multiple 10 The NULL operator is used to compare a value with a
conditions in an SQL statement's WHERE clause. NULL value.

ANY UNIQUE
3 The ANY operator is used to compare a value to any 11 The UNIQUE operator searches every row of a
applicable value in the list as per the condition. specified table for uniqueness (no duplicates).
SQL - Expressions

Compiled By : Er. Sanjay Kumar Mishra 25


An expression is a combination of one or more values, operators +----+----------+-----+-----------+----------+
and SQL functions that evaluate to a value. These SQL 7 rows in set (0.00 sec)
EXPRESSIONs are like formulae and they are written in query
language. You can also use them to query the database for a specific The following table is a simple example showing the usage of
set of data. various SQL Boolean Expressions −
Syntax SQL> SELECT * FROM CUSTOMERS WHERE SALARY =
Consider the basic syntax of the SELECT statement as follows − 10000;
SELECT column1, column2, columnN +----+-------+-----+---------+----------+
FROM table_name | ID | NAME | AGE | ADDRESS | SALARY |
WHERE [CONDITION|EXPRESSION]; +----+-------+-----+---------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 |
There are different types of SQL expressions, which are mentioned +----+-------+-----+---------+----------+
below − 1 row in set (0.00 sec)
 Boolean
 Numeric Numeric Expression
 Date These expressions are used to perform any mathematical operation
Let us now discuss each of these in detail. in any query. Following is the syntax −
Boolean Expressions SELECT numerical_expression as OPERATION_NAME
SQL Boolean Expressions fetch the data based on matching a single [FROM table_name
value. Following is the syntax − WHERE CONDITION] ;
SELECT column1, column2, columnN Here, the numerical_expression is used for a mathematical
FROM table_name expression or any formula. Following is a simple example showing
WHERE SINGLE VALUE MATCHING EXPRESSION; the usage of SQL Numeric Expressions −
Consider the CUSTOMERS table having the following records − SQL> SELECT (15 + 6) AS ADDITION
SQL> SELECT * FROM CUSTOMERS; +----------+
+----+----------+-----+-----------+----------+ | ADDITION |
| ID | NAME | AGE | ADDRESS | SALARY | +----------+
+----+----------+-----+-----------+----------+ | 21 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | +----------+
| 2 | Khilan | 25 | Delhi | 1500.00 | 1 row in set (0.00 sec)
| 3 | kaushik | 23 | Kota | 2000.00 | There are several built-in functions like avg(), sum(), count(), etc.,
| 4 | Chaitali | 25 | Mumbai | 6500.00 | to perform what is known as the aggregate data calculations against
| 5 | Hardik | 27 | Bhopal | 8500.00 | a table or a specific table column.
| 6 | Komal | 22 | MP | 4500.00 |
SQL> SELECT COUNT(*) AS "RECORDS" FROM
| 7 | Muffy | 24 | Indore | 10000.00 |
CUSTOMERS;

Compiled By : Er. Sanjay Kumar Mishra 26


+---------+ If you want to create a new database <testDB>, then the CREATE
| RECORDS | DATABASE statement would be as shown below −
+---------+ SQL> CREATE DATABASE testDB;
| 7| Make sure you have the admin privilege before creating any
+---------+ database. Once a database is created, you can check it in the list of
1 row in set (0.00 sec) databases as follows −
Date Expressions SQL> SHOW DATABASES;
Date Expressions return current system date and time values − +--------------------+
SQL> SELECT CURRENT_TIMESTAMP; | Database |
+---------------------+ +--------------------+
| Current_Timestamp | | information_schema |
+---------------------+ | AMROOD |
| 2009-11-12 06:40:23 | | TUTORIALSPOINT |
+---------------------+ | mysql |
1 row in set (0.00 sec) | orig |
Another date expression is as shown below − | test |
| testDB |
SQL> SELECT GETDATE();; +--------------------+
+-------------------------+ 7 rows in set (0.00 sec)
| GETDATE |
+-------------------------+ SQL - DROP or DELETE Database
| 2009-10-22 12:07:18.140 | The SQL DROP DATABASE statement is used to drop an existing
+-------------------------+ database in SQL schema.
1 row in set (0.00 sec) Syntax
The basic syntax of DROP DATABASE statement is as follows −
SQL - CREATE Database
The SQL CREATE DATABASE statement is used to create a new DROP DATABASE DatabaseName;
SQL database. Always the database name should be unique within the RDBMS.
Syntax Example
The basic syntax of this CREATE DATABASE statement is as If you want to delete an existing database <testDB>, then the DROP
follows − DATABASE statement would be as shown below −
CREATE DATABASE DatabaseName; SQL> DROP DATABASE testDB;
Always the database name should be unique within the RDBMS. NOTE − Be careful before using this operation because by deleting
Example an existing database would result in loss of complete information
stored in the database.

Compiled By : Er. Sanjay Kumar Mishra 27


Make sure you have the admin privilege before dropping any | mysql |
database. Once a database is dropped, you can check it in the list of | orig |
the databases as shown below − | test |
SQL> SHOW DATABASES; +--------------------+
+--------------------+ 6 rows in set (0.00 sec)
| Database | Now, if you want to work with the AMROOD database, then you
+--------------------+ can execute the following SQL command and start working with
| information_schema | the AMROOD database.
| AMROOD |
| TUTORIALSPOINT | SQL> USE AMROOD;
| mysql | SQL - CREATE Table
| orig | Creating a basic table involves naming the table and defining its
| test | columns and each column's data type.
+--------------------+ The SQL CREATE TABLE statement is used to create a new
6 rows in set (0.00 sec) table.
SQL - SELECT Database, USE Statement Syntax
When you have multiple databases in your SQL Schema, then The basic syntax of the CREATE TABLE statement is as follows −
before starting your operation, you would need to select a database CREATE TABLE table_name(
where all the operations would be performed. column1 datatype,
The SQL USE statement is used to select any existing database in column2 datatype,
the SQL schema. column3 datatype,
Syntax .....
The basic syntax of the USE statement is as shown below − columnN datatype,
USE DatabaseName; PRIMARY KEY( one or more columns )
);
Always the database name should be unique within the RDBMS.
Example CREATE TABLE is the keyword telling the database system what
You can check the available databases as shown below − you want to do. In this case, you want to create a new table. The
unique name or identifier for the table follows the CREATE
SQL> SHOW DATABASES; TABLE statement.
+--------------------+ Then in brackets comes the list defining each column in the table
| Database | and what sort of data type it is. The syntax becomes clearer with the
+--------------------+ following example.
| information_schema | A copy of an existing table can be created using a combination of
| AMROOD | the CREATE TABLE statement and the SELECT statement. You
| TUTORIALSPOINT | can check the complete details at Create Table Using another Table.

Compiled By : Er. Sanjay Kumar Mishra 28


Example NOTE − You should be very careful while using this command
The following code block is an example, which creates a because once a table is deleted then all the information available in
CUSTOMERS table with an ID as a primary key and NOT NULL that table will also be lost forever.
are the constraints showing that these fields cannot be NULL while Syntax
creating records in this table − The basic syntax of this DROP TABLE statement is as follows −
SQL> CREATE TABLE CUSTOMERS( DROP TABLE table_name;
ID INT NOT NULL, Example
NAME VARCHAR (20) NOT NULL, Let us first verify the CUSTOMERS table and then we will delete it
AGE INT NOT NULL, from the database as shown below −
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2), SQL> DESC CUSTOMERS;
PRIMARY KEY (ID) +---------+---------------+------+-----+---------+-------+
); | Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
You can verify if your table has been created successfully by | ID | int(11) | NO | PRI | | |
looking at the message displayed by the SQL server, otherwise you | NAME | varchar(20) | NO | | | |
can use the DESCcommand as follows − | AGE | int(11) | NO | | | |
SQL> DESC CUSTOMERS; | ADDRESS | char(25) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+ | SALARY | decimal(18,2) | YES | | NULL | |
| Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+---------+-------+
+---------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
| ID | int(11) | NO | PRI | | | This means that the CUSTOMERS table is available in the
| NAME | varchar(20) | NO | | | | database, so let us now drop it as shown below.
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | | SQL> DROP TABLE CUSTOMERS;
| SALARY | decimal(18,2) | YES | | NULL | | Query OK, 0 rows affected (0.01 sec)
+---------+---------------+------+-----+---------+-------+ Now, if you would try the DESC command, then you will get the
5 rows in set (0.00 sec) following error −
Now, you have CUSTOMERS table available in your database SQL> DESC CUSTOMERS;
which you can use to store the required information related to ERROR 1146 (42S02): Table 'TEST.CUSTOMERS' doesn't exist
customers.
Here, TEST is the database name which we are using for our
SQL - DROP or DELETE Table
examples.
The SQL DROP TABLE statement is used to remove a table
SQL - INSERT Query
definition and all the data, indexes, triggers, constraints and
The SQL INSERT INTO Statement is used to add new rows of
permission specifications for that table.
data to a table in the database.

Compiled By : Er. Sanjay Kumar Mishra 29


Syntax INSERT INTO CUSTOMERS
There are two basic syntaxes of the INSERT INTO statement which (ID,NAME,AGE,ADDRESS,SALARY)
are shown below. VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );
INSERT INTO TABLE_NAME (column1, column2,
column3,...columnN) INSERT INTO CUSTOMERS
VALUES (value1, value2, value3,...valueN); (ID,NAME,AGE,ADDRESS,SALARY)
Here, column1, column2, column3,...columnN are the names of the VALUES (6, 'Komal', 22, 'MP', 4500.00 );
columns in the table into which you want to insert the data. You can create a record in the CUSTOMERS table by using the
You may not need to specify the column(s) name in the SQL query second syntax as shown below.
if you are adding values for all the columns of the table. But make INSERT INTO CUSTOMERS
sure the order of the values is in the same order as the columns in VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
the table.
The SQL INSERT INTO syntax will be as follows − All the above statements would produce the following records in
the CUSTOMERS table as shown below.
INSERT INTO TABLE_NAME VALUES
(value1,value2,value3,...valueN); +----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
Example +----+----------+-----+-----------+----------+
The following statements would create six records in the | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
CUSTOMERS table. | 2 | Khilan | 25 | Delhi | 1500.00 |
INSERT INTO CUSTOMERS | 3 | kaushik | 23 | Kota | 2000.00 |
(ID,NAME,AGE,ADDRESS,SALARY) | 4 | Chaitali | 25 | Mumbai | 6500.00 |
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
INSERT INTO CUSTOMERS | 7 | Muffy | 24 | Indore | 10000.00 |
(ID,NAME,AGE,ADDRESS,SALARY) +----+----------+-----+-----------+----------+
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); Populate one table using another table
You can populate the data into a table through the select statement
INSERT INTO CUSTOMERS over another table; provided the other table has a set of fields,
(ID,NAME,AGE,ADDRESS,SALARY) which are required to populate the first table.
VALUES (3, 'kaushik', 23, 'Kota', 2000.00 ); Here is the syntax −
INSERT INTO CUSTOMERS INSERT INTO first_table_name [(column1, column2, ... columnN)]
(ID,NAME,AGE,ADDRESS,SALARY) SELECT column1, column2, ...columnN
VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 ); FROM second_table_name
[WHERE condition];

Compiled By : Er. Sanjay Kumar Mishra 30


SQL - SELECT Query | 2 | Khilan | 1500.00 |
The SQL SELECT statement is used to fetch the data from a | 3 | kaushik | 2000.00 |
database table which returns this data in the form of a result table. | 4 | Chaitali | 6500.00 |
These result tables are called result-sets. | 5 | Hardik | 8500.00 |
Syntax | 6 | Komal | 4500.00 |
The basic syntax of the SELECT statement is as follows − | 7 | Muffy | 10000.00 |
SELECT column1, column2, columnN FROM table_name; +----+----------+----------+
Here, column1, column2... are the fields of a table whose values If you want to fetch all the fields of the CUSTOMERS table, then
you want to fetch. If you want to fetch all the fields available in the you should use the following query.
field, then you can use the following syntax. SQL> SELECT * FROM CUSTOMERS;
SELECT * FROM table_name; This would produce the result as shown below.
Example +----+----------+-----+-----------+----------+
Consider the CUSTOMERS table having the following records − | ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 3 | kaushik | 23 | Kota | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
| 6 | Komal | 22 | MP | 4500.00 | +----+----------+-----+-----------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 | SQL - WHERE Clause
+----+----------+-----+-----------+----------+ The SQL WHERE clause is used to specify a condition while
The following code is an example, which would fetch the ID, Name fetching the data from a single table or by joining with multiple
and Salary fields of the customers available in CUSTOMERS table. tables. If the given condition is satisfied, then only it returns a
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS; specific value from the table. You should use the WHERE clause to
filter the records and fetching only the necessary records.
This would produce the following result − The WHERE clause is not only used in the SELECT statement, but
+----+----------+----------+ it is also used in the UPDATE, DELETE statement, etc., which we
| ID | NAME | SALARY | would examine in the subsequent chapters.
+----+----------+----------+ Syntax
| 1 | Ramesh | 2000.00 | The basic syntax of the SELECT statement with the WHERE clause
is as shown below.

Compiled By : Er. Sanjay Kumar Mishra 31


SELECT column1, column2, columnN +----+----------+----------+
FROM table_name The following query is an example, which would fetch the ID,
WHERE [condition] Name and Salary fields from the CUSTOMERS table for a
You can specify a condition using the comparison or logical customer with the name Hardik.
operators like >, <, =, LIKE, NOT, etc. The following examples Here, it is important to note that all the strings should be given inside
would make this concept clear. single quotes (''). Whereas, numeric values should be given without
Example any quote as in the above example.
Consider the CUSTOMERS table having the following records − SQL> SELECT ID, NAME, SALARY
+----+----------+-----+-----------+----------+ FROM CUSTOMERS
| ID | NAME | AGE | ADDRESS | SALARY | WHERE NAME = 'Hardik';
+----+----------+-----+-----------+----------+ This would produce the following result −
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | +----+----------+----------+
| 3 | kaushik | 23 | Kota | 2000.00 | | ID | NAME | SALARY |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | +----+----------+----------+
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 5 | Hardik | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 | +----+----------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 | SQL - AND and OR Conjunctive Operators
+----+----------+-----+-----------+----------+ The SQL AND & OR operators are used to combine multiple
The following code is an example which would fetch the ID, Name conditions to narrow data in an SQL statement. These two operators
and Salary fields from the CUSTOMERS table, where the salary is are called as the conjunctive operators.
greater than 2000 − These operators provide a means to make multiple comparisons
with different operators in the same SQL statement.
SQL> SELECT ID, NAME, SALARY The AND Operator
FROM CUSTOMERS The AND operator allows the existence of multiple conditions in an
WHERE SALARY > 2000; SQL statement's WHERE clause.
This would produce the following result − Syntax
+----+----------+----------+ The basic syntax of the AND operator with a WHERE clause is as
| ID | NAME | SALARY | follows −
+----+----------+----------+ SELECT column1, column2, columnN
| 4 | Chaitali | 6500.00 | FROM table_name
| 5 | Hardik | 8500.00 | WHERE [condition1] AND [condition2]...AND [conditionN];
| 6 | Komal | 4500.00 | You can combine N number of conditions using the AND operator.
| 7 | Muffy | 10000.00 | For an action to be taken by the SQL statement, whether it be a

Compiled By : Er. Sanjay Kumar Mishra 32


transaction or a query, all conditions separated by the AND must be SELECT column1, column2, columnN
TRUE. FROM table_name
Example WHERE [condition1] OR [condition2]...OR [conditionN]
Consider the CUSTOMERS table having the following records −
You can combine N number of conditions using the OR operator.
+----+----------+-----+-----------+----------+ For an action to be taken by the SQL statement, whether it be a
| ID | NAME | AGE | ADDRESS | SALARY | transaction or query, the only any ONE of the conditions separated
+----+----------+-----+-----------+----------+ by the OR must be TRUE.
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | Example
| 2 | Khilan | 25 | Delhi | 1500.00 | Consider the CUSTOMERS table having the following records −
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | +----+----------+-----+-----------+----------+
| 5 | Hardik | 27 | Bhopal | 8500.00 | | ID | NAME | AGE | ADDRESS | SALARY |
| 6 | Komal | 22 | MP | 4500.00 | +----+----------+-----+-----------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
Following is an example, which would fetch the ID, Name and | 4 | Chaitali | 25 | Mumbai | 6500.00 |
Salary fields from the CUSTOMERS table, where the salary is | 5 | Hardik | 27 | Bhopal | 8500.00 |
greater than 2000 and the age is less than 25 years − | 6 | Komal | 22 | MP | 4500.00 |
SQL> SELECT ID, NAME, SALARY | 7 | Muffy | 24 | Indore | 10000.00 |
FROM CUSTOMERS +----+----------+-----+-----------+----------+
WHERE SALARY > 2000 AND age < 25; The following code block hasa query, which would fetch the ID,
This would produce the following result − Name and Salary fields from the CUSTOMERS table, where the
+----+-------+----------+ salary is greater than 2000 OR the age is less than 25 years.
| ID | NAME | SALARY | SQL> SELECT ID, NAME, SALARY
+----+-------+----------+ FROM CUSTOMERS
| 6 | Komal | 4500.00 | WHERE SALARY > 2000 OR age < 25;
| 7 | Muffy | 10000.00 | This would produce the following result −
+----+-------+----------+
+----+----------+----------+
The OR Operator | ID | NAME | SALARY |
The OR operator is used to combine multiple conditions in an SQL +----+----------+----------+
statement's WHERE clause. | 3 | kaushik | 2000.00 |
Syntax | 4 | Chaitali | 6500.00 |
The basic syntax of the OR operator with a WHERE clause is as | 5 | Hardik | 8500.00 |
follows −

Compiled By : Er. Sanjay Kumar Mishra 33


| 6 | Komal | 4500.00 | Now, the CUSTOMERS table would have the following records −
| 7 | Muffy | 10000.00 | +----+----------+-----+-----------+----------+
+----+----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY |
SQL - UPDATE Query +----+----------+-----+-----------+----------+
The SQL UPDATE Query is used to modify the existing records in | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
a table. You can use the WHERE clause with the UPDATE query to | 2 | Khilan | 25 | Delhi | 1500.00 |
update the selected rows, otherwise all the rows would be affected. | 3 | kaushik | 23 | Kota | 2000.00 |
Syntax | 4 | Chaitali | 25 | Mumbai | 6500.00 |
The basic syntax of the UPDATE query with a WHERE clause is as | 5 | Hardik | 27 | Bhopal | 8500.00 |
follows − | 6 | Komal | 22 | Pune | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
UPDATE table_name +----+----------+-----+-----------+----------+
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition]; If you want to modify all the ADDRESS and the SALARY column
values in the CUSTOMERS table, you do not need to use the
You can combine N number of conditions using the AND or the OR WHERE clause as the UPDATE query would be enough as shown
operators. in the following code block.
Example
Consider the CUSTOMERS table having the following records − SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune', SALARY = 1000.00;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY | Now, CUSTOMERS table would have the following records −
+----+----------+-----+-----------+----------+ +----+----------+-----+---------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | ID | NAME | AGE | ADDRESS | SALARY |
| 2 | Khilan | 25 | Delhi | 1500.00 | +----+----------+-----+---------+---------+
| 3 | kaushik | 23 | Kota | 2000.00 | | 1 | Ramesh | 32 | Pune | 1000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 2 | Khilan | 25 | Pune | 1000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 3 | kaushik | 23 | Pune | 1000.00 |
| 6 | Komal | 22 | MP | 4500.00 | | 4 | Chaitali | 25 | Pune | 1000.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | | 5 | Hardik | 27 | Pune | 1000.00 |
+----+----------+-----+-----------+----------+ | 6 | Komal | 22 | Pune | 1000.00 |
The following query will update the ADDRESS for a customer | 7 | Muffy | 24 | Pune | 1000.00 |
whose ID number is 6 in the table. +----+----------+-----+---------+---------+

SQL> UPDATE CUSTOMERS SQL - DELETE Query


SET ADDRESS = 'Pune' The SQL DELETE Query is used to delete the existing records
WHERE ID = 6; from a table.

Compiled By : Er. Sanjay Kumar Mishra 34


You can use the WHERE clause with a DELETE query to delete the | 4 | Chaitali | 25 | Mumbai | 6500.00 |
selected rows, otherwise all the records would be deleted. | 5 | Hardik | 27 | Bhopal | 8500.00 |
Syntax | 7 | Muffy | 24 | Indore | 10000.00 |
The basic syntax of the DELETE query with the WHERE clause is +----+----------+-----+-----------+----------+
as follows −
If you want to DELETE all the records from the CUSTOMERS
DELETE FROM table_name table, you do not need to use the WHERE clause and the DELETE
WHERE [condition]; query would be as follows −
You can combine N number of conditions using AND or OR SQL> DELETE FROM CUSTOMERS;
operators.
Example Now, the CUSTOMERS table would not have any record.
Consider the CUSTOMERS table having the following records − SQL - LIKE Clause
The SQL LIKE clause is used to compare a value to similar values
+----+----------+-----+-----------+----------+ using wildcard operators. There are two wildcards used in
| ID | NAME | AGE | ADDRESS | SALARY | conjunction with the LIKE operator.
+----+----------+-----+-----------+----------+  The percent sign (%)
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |  The underscore (_)
| 2 | Khilan | 25 | Delhi | 1500.00 | The percent sign represents zero, one or multiple characters. The
| 3 | kaushik | 23 | Kota | 2000.00 | underscore represents a single number or character. These symbols
| 4 | Chaitali | 25 | Mumbai | 6500.00 | can be used in combinations.
| 5 | Hardik | 27 | Bhopal | 8500.00 | Syntax
| 6 | Komal | 22 | MP | 4500.00 | The basic syntax of % and _ is as follows −
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+ SELECT FROM table_name
WHERE column LIKE 'XXXX%'
The following code has a query, which will DELETE a customer,
whose ID is 6. or
SQL> DELETE FROM CUSTOMERS
WHERE ID = 6; SELECT FROM table_name
Now, the CUSTOMERS table would have the following records. WHERE column LIKE '%XXXX%'

+----+----------+-----+-----------+----------+ or
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ SELECT FROM table_name
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | WHERE column LIKE 'XXXX_'
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | or

Compiled By : Er. Sanjay Kumar Mishra 35


WHERE SALARY LIKE '2___3'
SELECT FROM table_name 7 Finds any values in a five-digit number that start with 2 and end
WHERE column LIKE '_XXXX' with 3.

or Let us take a real example, consider the CUSTOMERS table having


the records as shown below.
SELECT FROM table_name +----+----------+-----+-----------+----------+
WHERE column LIKE '_XXXX_' | ID | NAME | AGE | ADDRESS | SALARY |
You can combine N number of conditions using AND or OR +----+----------+-----+-----------+----------+
operators. Here, XXXX could be any numeric or string value. | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
Example | 2 | Khilan | 25 | Delhi | 1500.00 |
The following table has a few examples showing the WHERE part | 3 | kaushik | 23 | Kota | 2000.00 |
having different LIKE clause with '%' and '_' operators − | 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
Sr.No. Statement & Description | 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
WHERE SALARY LIKE '200%' +----+----------+-----+-----------+----------+
1
Finds any values that start with 200.
Following is an example, which would display all the records from
the CUSTOMERS table, where the SALARY starts with 200.
WHERE SALARY LIKE '%200%'
2 SQL> SELECT * FROM CUSTOMERS
Finds any values that have 200 in any position.
WHERE SALARY LIKE '200%';
WHERE SALARY LIKE '_00%' This would produce the following result −
3
Finds any values that have 00 in the second and third positions.
+----+----------+-----+-----------+----------+
WHERE SALARY LIKE '2_%_%' | ID | NAME | AGE | ADDRESS | SALARY |
4 Finds any values that start with 2 and are at least 3 characters in+----+----------+-----+-----------+----------+
length. | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
WHERE SALARY LIKE '%2' +----+----------+-----+-----------+----------+
5
Finds any values that end with 2. SQL - TOP, LIMIT or ROWNUM Clause
The SQL TOP clause is used to fetch a TOP N number or X
WHERE SALARY LIKE '_2%3' percent records from a table.
6 Finds any values that have a 2 in the second position and endNote − All the databases do not support the TOP clause. For
with a 3. example MySQL supports the LIMIT clause to fetch limited

Compiled By : Er. Sanjay Kumar Mishra 36


number of records while Oracle uses the ROWNUM command to If you are using MySQL server, then here is an equivalent example
fetch a limited number of records. −
Syntax SQL> SELECT * FROM CUSTOMERS
The basic syntax of the TOP clause with a SELECT statement LIMIT 3;
would be as follows.
This would produce the following result −
SELECT TOP number|percent column_name(s)
FROM table_name +----+---------+-----+-----------+---------+
WHERE [condition] | ID | NAME | AGE | ADDRESS | SALARY |
+----+---------+-----+-----------+---------+
Example | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
Consider the CUSTOMERS table having the following records − | 2 | Khilan | 25 | Delhi | 1500.00 |
+----+----------+-----+-----------+----------+ | 3 | kaushik | 23 | Kota | 2000.00 |
| ID | NAME | AGE | ADDRESS | SALARY | +----+---------+-----+-----------+---------+
+----+----------+-----+-----------+----------+ If you are using an Oracle server, then the following code block has
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | an equivalent example.
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | SQL> SELECT * FROM CUSTOMERS
| 4 | Chaitali | 25 | Mumbai | 6500.00 | WHERE ROWNUM <= 3;
| 5 | Hardik | 27 | Bhopal | 8500.00 | This would produce the following result −
| 6 | Komal | 22 | MP | 4500.00 |
+----+---------+-----+-----------+---------+
| 7 | Muffy | 24 | Indore | 10000.00 |
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
+----+---------+-----+-----------+---------+
The following query is an example on the SQL server, which would | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
fetch the top 3 records from the CUSTOMERS table. | 2 | Khilan | 25 | Delhi | 1500.00 |
SQL> SELECT TOP 3 * FROM CUSTOMERS; | 3 | kaushik | 23 | Kota | 2000.00 |
+----+---------+-----+-----------+---------+
This would produce the following result −
SQL - ORDER BY Clause
+----+---------+-----+-----------+---------+ The SQL ORDER BY clause is used to sort the data in ascending
| ID | NAME | AGE | ADDRESS | SALARY | or descending order, based on one or more columns. Some
+----+---------+-----+-----------+---------+ databases sort the query results in an ascending order by default.
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | Syntax
| 2 | Khilan | 25 | Delhi | 1500.00 | The basic syntax of the ORDER BY clause is as follows −
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+---------+-----+-----------+---------+ SELECT column-list
FROM table_name

Compiled By : Er. Sanjay Kumar Mishra 37


[WHERE condition] +----+----------+-----+-----------+----------+
[ORDER BY column1, column2, .. columnN] [ASC | DESC]; The following code block has an example, which would sort the
You can use more than one column in the ORDER BY clause. result in the descending order by NAME.
Make sure whatever column you are using to sort that column SQL> SELECT * FROM CUSTOMERS
should be in the column-list. ORDER BY NAME DESC;
Example
Consider the CUSTOMERS table having the following records − This would produce the following result −
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY | | ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 2 | Khilan | 25 | Delhi | 1500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 3 | kaushik | 23 | Kota | 2000.00 |
| 6 | Komal | 22 | MP | 4500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
The following code block has an example, which would sort the SQL - Group By
result in an ascending order by the NAME and the SALARY − The SQL GROUP BY clause is used in collaboration with the
SELECT statement to arrange identical data into groups. This
SQL> SELECT * FROM CUSTOMERS GROUP BY clause follows the WHERE clause in a SELECT
ORDER BY NAME, SALARY; statement and precedes the ORDER BY clause.
This would produce the following result − Syntax
+----+----------+-----+-----------+----------+ The basic syntax of a GROUP BY clause is shown in the following
| ID | NAME | AGE | ADDRESS | SALARY | code block. The GROUP BY clause must follow the conditions in
+----+----------+-----+-----------+----------+ the WHERE clause and must precede the ORDER BY clause if one
| 4 | Chaitali | 25 | Mumbai | 6500.00 | is used.
| 5 | Hardik | 27 | Bhopal | 8500.00 | SELECT column1, column2
| 3 | kaushik | 23 | Kota | 2000.00 | FROM table_name
| 2 | Khilan | 25 | Delhi | 1500.00 | WHERE [ conditions ]
| 6 | Komal | 22 | MP | 4500.00 | GROUP BY column1, column2
| 7 | Muffy | 24 | Indore | 10000.00 | ORDER BY column1, column2
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | Example

Compiled By : Er. Sanjay Kumar Mishra 38


Consider the CUSTOMERS table is having the following records − | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+ | 2 | Ramesh | 25 | Delhi | 1500.00 |
| ID | NAME | AGE | ADDRESS | SALARY | | 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+ | 4 | kaushik | 25 | Mumbai | 6500.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | +----+----------+-----+-----------+----------+
| 5 | Hardik | 27 | Bhopal | 8500.00 | Now again, if you want to know the total amount of salary on each
| 6 | Komal | 22 | MP | 4500.00 | customer, then the GROUP BY query would be as follows −
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+ SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS
GROUP BY NAME;
If you want to know the total amount of the salary on each
customer, then the GROUP BY query would be as follows. This would produce the following result −
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS +---------+-------------+
GROUP BY NAME; | NAME | SUM(SALARY) |
+---------+-------------+
This would produce the following result − | Hardik | 8500.00 |
+----------+-------------+ | kaushik | 8500.00 |
| NAME | SUM(SALARY) | | Komal | 4500.00 |
+----------+-------------+ | Muffy | 10000.00 |
| Chaitali | 6500.00 | | Ramesh | 3500.00 |
| Hardik | 8500.00 | +---------+-------------+
| kaushik | 2000.00 | SQL - Distinct Keyword
| Khilan | 1500.00 | The SQL DISTINCT keyword is used in conjunction with the
| Komal | 4500.00 | SELECT statement to eliminate all the duplicate records and
| Muffy | 10000.00 | fetching only unique records.
| Ramesh | 2000.00 | There may be a situation when you have multiple duplicate records
+----------+-------------+ in a table. While fetching such records, it makes more sense to fetch
Now, let us look at a table where the CUSTOMERS table has the only those unique records instead of fetching duplicate records.
following records with duplicate names − Syntax
+----+----------+-----+-----------+----------+ The basic syntax of DISTINCT keyword to eliminate the duplicate
| ID | NAME | AGE | ADDRESS | SALARY | records is as follows −
+----+----------+-----+-----------+----------+ SELECT DISTINCT column1, column2,.....columnN

Compiled By : Er. Sanjay Kumar Mishra 39


FROM table_name Now, let us use the DISTINCT keyword with the above SELECT
WHERE [condition] query and then see the result.

Example SQL> SELECT DISTINCT SALARY FROM CUSTOMERS


Consider the CUSTOMERS table having the following records − ORDER BY SALARY;

+----+----------+-----+-----------+----------+ This would produce the following result where we do not have any
| ID | NAME | AGE | ADDRESS | SALARY | duplicate entry.
+----+----------+-----+-----------+----------+ +----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | SALARY |
| 2 | Khilan | 25 | Delhi | 1500.00 | +----------+
| 3 | kaushik | 23 | Kota | 2000.00 | | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 4500.00 |
| 6 | Komal | 22 | MP | 4500.00 | | 6500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | | 8500.00 |
+----+----------+-----+-----------+----------+ | 10000.00 |
First, let us see how the following SELECT query returns the +----------+
duplicate salary records. SQL - SORTING Results
SQL> SELECT SALARY FROM CUSTOMERS The SQL ORDER BY clause is used to sort the data in ascending
ORDER BY SALARY; or descending order, based on one or more columns. Some
databases sort the query results in an ascending order by default.
This would produce the following result, where the salary (2000) is Syntax
coming twice which is a duplicate record from the original table. The basic syntax of the ORDER BY clause which would be used to
+----------+ sort the result in an ascending or descending order is as follows −
| SALARY | SELECT column-list
+----------+ FROM table_name
| 1500.00 | [WHERE condition]
| 2000.00 | [ORDER BY column1, column2, .. columnN] [ASC | DESC];
| 2000.00 |
| 4500.00 | You can use more than one column in the ORDER BY clause.
| 6500.00 | Make sure that whatever column you are using to sort, that column
| 8500.00 | should be in the column-list.
| 10000.00 | Example
+----------+ Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+

Compiled By : Er. Sanjay Kumar Mishra 40


| ID | NAME | AGE | ADDRESS | SALARY | | ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 2 | Khilan | 25 | Delhi | 1500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 3 | kaushik | 23 | Kota | 2000.00 |
| 6 | Komal | 22 | MP | 4500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
Following is an example, which would sort the result in an To fetch the rows with their own preferred order, the SELECT
ascending order by NAME and SALARY. query used would be as follows −
SQL> SELECT * FROM CUSTOMERS SQL> SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY; ORDER BY (CASE ADDRESS
This would produce the following result − WHEN 'DELHI' THEN 1
WHEN 'BHOPAL' THEN 2
+----+----------+-----+-----------+----------+ WHEN 'KOTA' THEN 3
| ID | NAME | AGE | ADDRESS | SALARY | WHEN 'AHMADABAD' THEN 4
+----+----------+-----+-----------+----------+ WHEN 'MP' THEN 5
| 4 | Chaitali | 25 | Mumbai | 6500.00 | ELSE 100 END) ASC, ADDRESS DESC;
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | This would produce the following result −
| 2 | Khilan | 25 | Delhi | 1500.00 | +----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 | | ID | NAME | AGE | ADDRESS | SALARY |
| 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 |
+----+----------+-----+-----------+----------+ | 5 | Hardik | 27 | Bhopal | 8500.00 |
The following code block has an example, which would sort the | 3 | kaushik | 23 | Kota | 2000.00 |
result in a descending order by NAME. | 6 | Komal | 22 | MP | 4500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
SQL> SELECT * FROM CUSTOMERS | 7 | Muffy | 24 | Indore | 10000.00 |
ORDER BY NAME DESC; | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
This would produce the following result − +----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+ This will sort the customers by ADDRESS in your ownoOrder of
preference first and in a natural order for the remaining addresses.

Compiled By : Er. Sanjay Kumar Mishra 41


Also, the remaining Addresses will be sorted in the reverse +----+----------+-----+--------+
alphabetical order. | ID | NAME | AGE | AMOUNT |
4.4 SQL - Using Joins +----+----------+-----+--------+
The SQL Joins clause is used to combine records from two or more | 3 | kaushik | 23 | 3000 |
tables in a database. A JOIN is a means for combining fields from | 3 | kaushik | 23 | 1500 |
two tables by using values common to each. | 2 | Khilan | 25 | 1560 |
Consider the following two tables − | 4 | Chaitali | 25 | 2060 |
Table 1 − CUSTOMERS Table +----+----------+-----+--------+
+----+----------+-----+-----------+----------+ Here, it is noticeable that the join is performed in the WHERE
| ID | NAME | AGE | ADDRESS | SALARY | clause. Several operators can be used to join tables, such as =, <, >,
+----+----------+-----+-----------+----------+ <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | to join tables. However, the most common operator is the equal to
| 2 | Khilan | 25 | Delhi | 1500.00 | symbol.
| 3 | kaushik | 23 | Kota | 2000.00 | There are different types of joins available in SQL −
| 4 | Chaitali | 25 | Mumbai | 6500.00 |  INNER JOIN − returns rows when there is a match in both
| 5 | Hardik | 27 | Bhopal | 8500.00 | tables.
| 6 | Komal | 22 | MP | 4500.00 |  LEFT JOIN − returns all rows from the left table, even if
| 7 | Muffy | 24 | Indore | 10000.00 | there are no matches in the right table.
+----+----------+-----+-----------+----------+  RIGHT JOIN − returns all rows from the right table, even if
Table 2 − ORDERS Table there are no matches in the left table.
+-----+---------------------+-------------+--------+  FULL JOIN − returns rows when there is a match in one of
|OID | DATE | CUSTOMER_ID | AMOUNT | the tables.
+-----+---------------------+-------------+--------+  SELF JOIN − is used to join a table to itself as if the table
| 102 | 2009-10-08 00:00:00 | 3 | 3000 | were two tables, temporarily renaming at least one table in
| 100 | 2009-10-08 00:00:00 | 3 | 1500 | the SQL statement.
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |  CARTESIAN JOIN − returns the Cartesian product of the
| 103 | 2008-05-20 00:00:00 | 4 | 2060 | sets of records from the two or more joined tables.
+-----+---------------------+-------------+--------+ Let us now discuss each of these joins in detail.
SQL - UNIONS CLAUSE
Now, let us join these two tables in our SELECT statement as The SQL UNION clause/operator is used to combine the results of
shown below. two or more SELECT statements without returning any duplicate
SQL> SELECT ID, NAME, AGE, AMOUNT rows.
FROM CUSTOMERS, ORDERS To use this UNION clause, each SELECT statement must have
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;  The same number of columns selected
 The same number of column expressions
This would produce the following result.
 The same data type and

Compiled By : Er. Sanjay Kumar Mishra 42


 Have them in the same order | 100 | 2009-10-08 00:00:00 | 3 | 1500 |
But they need not have to be in the same length. | 101 | 2009-11-20 00:00:00 | 2 | 1560 |
Syntax | 103 | 2008-05-20 00:00:00 | 4 | 2060 |
The basic syntax of a UNION clause is as follows − +-----+---------------------+-------------+--------+
SELECT column1 [, column2 ] Now, let us join these two tables in our SELECT statement as
FROM table1 [, table2 ] follows −
[WHERE condition]
SQL> SELECT ID, NAME, AMOUNT, DATE
UNION FROM CUSTOMERS
LEFT JOIN ORDERS
SELECT column1 [, column2 ] ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
FROM table1 [, table2 ] UNION
[WHERE condition] SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
Here, the given condition could be any given expression based on RIGHT JOIN ORDERS
your requirement. ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Example
Consider the following two tables. This would produce the following result −
Table 1 − CUSTOMERS Table is as follows. +------+----------+--------+---------------------+
+----+----------+-----+-----------+----------+ | ID | NAME | AMOUNT | DATE |
| ID | NAME | AGE | ADDRESS | SALARY | +------+----------+--------+---------------------+
+----+----------+-----+-----------+----------+ | 1 | Ramesh | NULL | NULL |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 5 | Hardik | NULL | NULL |
| 6 | Komal | 22 | MP | 4500.00 | | 6 | Komal | NULL | NULL |
| 7 | Muffy | 24 | Indore | 10000.00 | | 7 | Muffy | NULL | NULL |
+----+----------+-----+-----------+----------+ +------+----------+--------+---------------------+
Table 2 − ORDERS Table is as follows. The UNION ALL Clause
The UNION ALL operator is used to combine the results of two
+-----+---------------------+-------------+--------+ SELECT statements including duplicate rows.
|OID | DATE | CUSTOMER_ID | AMOUNT | The same rules that apply to the UNION clause will apply to the
+-----+---------------------+-------------+--------+ UNION ALL operator.
| 102 | 2009-10-08 00:00:00 | 3 | 3000 | Syntax

Compiled By : Er. Sanjay Kumar Mishra 43


The basic syntax of the UNION ALL is as follows. +-----+---------------------+-------------+--------+
SELECT column1 [, column2 ] Now, let us join these two tables in our SELECT statement as
FROM table1 [, table2 ] follows −
[WHERE condition]
SQL> SELECT ID, NAME, AMOUNT, DATE
UNION ALL FROM CUSTOMERS
LEFT JOIN ORDERS
SELECT column1 [, column2 ] ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
FROM table1 [, table2 ] UNION ALL
[WHERE condition] SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
Here, the given condition could be any given expression based on RIGHT JOIN ORDERS
your requirement. ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Example
Consider the following two tables, This would produce the following result −
Table 1 − CUSTOMERS Table is as follows. +------+----------+--------+---------------------+
+----+----------+-----+-----------+----------+ | ID | NAME | AMOUNT | DATE |
| ID | NAME | AGE | ADDRESS | SALARY | +------+----------+--------+---------------------+
+----+----------+-----+-----------+----------+ | 1 | Ramesh | NULL | NULL |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 5 | Hardik | NULL | NULL |
| 6 | Komal | 22 | MP | 4500.00 | | 6 | Komal | NULL | NULL |
| 7 | Muffy | 24 | Indore | 10000.00 | | 7 | Muffy | NULL | NULL |
+----+----------+-----+-----------+----------+ | 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
Table 2 − ORDERS table is as follows. | 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
+-----+---------------------+-------------+--------+ | 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
|OID | DATE | CUSTOMER_ID | AMOUNT | +------+----------+--------+---------------------+
+-----+---------------------+-------------+--------+ There are two other clauses (i.e., operators), which are like the
| 102 | 2009-10-08 00:00:00 | 3 | 3000 | UNION clause.
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |  SQL INTERSECT Clause − This is used to combine two
| 101 | 2009-11-20 00:00:00 | 2 | 1560 | SELECT statements, but returns rows only from the first
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |

Compiled By : Er. Sanjay Kumar Mishra 44


SELECT statement that are identical to a row in the second +----+----------+-----+-----------+----------+
SELECT statement. | ID | NAME | AGE | ADDRESS | SALARY |
 SQL EXCEPT Clause − This combines two SELECT +----+----------+-----+-----------+----------+
statements and returns rows from the first SELECT | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
statement that are not returned by the second SELECT | 2 | Khilan | 25 | Delhi | 1500.00 |
statement. | 3 | kaushik | 23 | Kota | 2000.00 |
SQL - NULL Values | 4 | Chaitali | 25 | Mumbai | 6500.00 |
The SQL NULL is the term used to represent a missing value. A | 5 | Hardik | 27 | Bhopal | 8500.00 |
NULL value in a table is a value in a field that appears to be blank. | 6 | Komal | 22 | MP | |
A field with a NULL value is a field with no value. It is very | 7 | Muffy | 24 | Indore | |
important to understand that a NULL value is different than a zero +----+----------+-----+-----------+----------+
value or a field that contains spaces.
Syntax Now, following is the usage of the IS NOT NULLoperator.
The basic syntax of NULL while creating a table. SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
SQL> CREATE TABLE CUSTOMERS( FROM CUSTOMERS
ID INT NOT NULL, WHERE SALARY IS NOT NULL;
NAME VARCHAR (20) NOT NULL, This would produce the following result −
AGE INT NOT NULL,
+----+----------+-----+-----------+----------+
ADDRESS CHAR (25) ,
| ID | NAME | AGE | ADDRESS | SALARY |
SALARY DECIMAL (18, 2),
+----+----------+-----+-----------+----------+
PRIMARY KEY (ID)
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
);
| 2 | Khilan | 25 | Delhi | 1500.00 |
Here, NOT NULL signifies that column should always accept an | 3 | kaushik | 23 | Kota | 2000.00 |
explicit value of the given data type. There are two columns where | 4 | Chaitali | 25 | Mumbai | 6500.00 |
we did not use NOT NULL, which means these columns could be | 5 | Hardik | 27 | Bhopal | 8500.00 |
NULL. +----+----------+-----+-----------+----------+
A field with a NULL value is the one that has been left blank during
Now, following is the usage of the IS NULL operator.
the record creation.
Example SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
The NULL value can cause problems when selecting data. FROM CUSTOMERS
However, because when comparing an unknown value to any other WHERE SALARY IS NULL;
value, the result is always unknown and not included in the results. This would produce the following result −
You must use the IS NULL or IS NOT NULL operators to check
for a NULL value. +----+----------+-----+-----------+----------+
Consider the following CUSTOMERS table having the records as | ID | NAME | AGE | ADDRESS | SALARY |
shown below.

Compiled By : Er. Sanjay Kumar Mishra 45


+----+----------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | | Table 2 − ORDERS Table is as follows.
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+ +-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
SQL - Alias Syntax +-----+---------------------+-------------+--------+
You can rename a table or a column temporarily by giving another | 102 | 2009-10-08 00:00:00 | 3 | 3000 |
name known as Alias. The use of table aliases is to rename a table | 100 | 2009-10-08 00:00:00 | 3 | 1500 |
in a specific SQL statement. The renaming is a temporary change | 101 | 2009-11-20 00:00:00 | 2 | 1560 |
and the actual table name does not change in the database. The | 103 | 2008-05-20 00:00:00 | 4 | 2060 |
column aliases are used to rename a table's columns for the purpose +-----+---------------------+-------------+--------+
of a particular SQL query.
Syntax Now, the following code block shows the usage of a table alias.
The basic syntax of a table alias is as follows. SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT
SELECT column1, column2.... FROM CUSTOMERS AS C, ORDERS AS O
FROM table_name AS alias_name WHERE C.ID = O.CUSTOMER_ID;
WHERE [condition]; This would produce the following result.
The basic syntax of a column alias is as follows. +----+----------+-----+--------+
SELECT column_name AS alias_name | ID | NAME | AGE | AMOUNT |
FROM table_name +----+----------+-----+--------+
WHERE [condition]; | 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
Example | 2 | Khilan | 25 | 1560 |
Consider the following two tables. | 4 | Chaitali | 25 | 2060 |
Table 1 − CUSTOMERS Table is as follows. +----+----------+-----+--------+
+----+----------+-----+-----------+----------+ Following is the usage of a column alias.
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ SQL> SELECT ID AS CUSTOMER_ID, NAME AS
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | CUSTOMER_NAME
| 2 | Khilan | 25 | Delhi | 1500.00 | FROM CUSTOMERS
| 3 | kaushik | 23 | Kota | 2000.00 | WHERE SALARY IS NOT NULL;
| 4 | Chaitali | 25 | Mumbai | 6500.00 | This would produce the following result.
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+-------------+---------------+
| 6 | Komal | 22 | MP | 4500.00 |
| CUSTOMER_ID | CUSTOMER_NAME |
| 7 | Muffy | m 24 | Indore | 10000.00 |

Compiled By : Er. Sanjay Kumar Mishra 46


+-------------+---------------+ ALTER TABLE table_name
| 1 | Ramesh | ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1,
| 2 | Khilan | column2...);
| 3 | kaushik | The basic syntax of an ALTER TABLE command to ADD
| 4 | Chaitali | CHECK CONSTRAINTto a table is as follows.
| 5 | Hardik |
| 6 | Komal | ALTER TABLE table_name
| 7 | Muffy | ADD CONSTRAINT MyUniqueConstraint CHECK
+-------------+---------------+ (CONDITION);
The basic syntax of an ALTER TABLE command to ADD
SQL - ALTER TABLE Command PRIMARY KEYconstraint to a table is as follows.
The SQL ALTER TABLE command is used to add, delete or ALTER TABLE table_name
modify columns in an existing table. You should also use the ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1,
ALTER TABLE command to add and drop various constraints on column2...);
an existing table.
The basic syntax of an ALTER TABLE command to DROP
Syntax
CONSTRAINT from a table is as follows.
The basic syntax of an ALTER TABLE command to add a New
Column in an existing table is as follows. ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;
ALTER TABLE table_name ADD column_name datatype;
If you're using MySQL, the code is as follows −
The basic syntax of an ALTER TABLE command to DROP
COLUMN in an existing table is as follows. ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;
ALTER TABLE table_name DROP COLUMN column_name;
The basic syntax of an ALTER TABLE command to DROP
The basic syntax of an ALTER TABLE command to change
PRIMARY KEYconstraint from a table is as follows.
the DATA TYPE of a column in a table is as follows.
ALTER TABLE table_name
ALTER TABLE table_name MODIFY COLUMN column_name
DROP CONSTRAINT MyPrimaryKey;
datatype;
If you're using MySQL, the code is as follows −
The basic syntax of an ALTER TABLE command to add a NOT
NULL constraint to a column in a table is as follows. ALTER TABLE table_name
DROP PRIMARY KEY;
ALTER TABLE table_name MODIFY column_name datatype NOT
NULL; Example
Consider the CUSTOMERS table having the following records −
The basic syntax of ALTER TABLE to ADD UNIQUE
CONSTRAINT to a table is as follows. +----+----------+-----+-----------+----------+

Compiled By : Er. Sanjay Kumar Mishra 47


| ID | NAME | AGE | ADDRESS | SALARY | | ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+ +----+---------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 2 | Ramesh | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | | 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 | | 4 | kaushik | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+ +----+---------+-----+-----------+----------+
Following is the example to ADD a New Column to an existing SQL - TRUNCATE TABLE Command
table − The SQL TRUNCATE TABLE command is used to delete
ALTER TABLE CUSTOMERS ADD SEX char(1); complete data from an existing table.
You can also use DROP TABLE command to delete complete table
Now, the CUSTOMERS table is changed and following would be but it would remove complete table structure form the database and
output from the SELECT statement. you would need to re-create this table once again if you wish you
+----+---------+-----+-----------+----------+------+ store some data.
| ID | NAME | AGE | ADDRESS | SALARY | SEX | Syntax
+----+---------+-----+-----------+----------+------+ The basic syntax of a TRUNCATE TABLE command is as
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | NULL | follows.
| 2 | Ramesh | 25 | Delhi | 1500.00 | NULL | TRUNCATE TABLE table_name;
| 3 | kaushik | 23 | Kota | 2000.00 | NULL |
| 4 | kaushik | 25 | Mumbai | 6500.00 | NULL | Example
| 5 | Hardik | 27 | Bhopal | 8500.00 | NULL | Consider a CUSTOMERS table having the following records −
| 6 | Komal | 22 | MP | 4500.00 | NULL | +----+----------+-----+-----------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 | NULL | | ID | NAME | AGE | ADDRESS | SALARY |
+----+---------+-----+-----------+----------+------+ +----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
Following is the example to DROP sex column from the existing | 2 | Khilan | 25 | Delhi | 1500.00 |
table. | 3 | kaushik | 23 | Kota | 2000.00 |
ALTER TABLE CUSTOMERS DROP SEX; | 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
Now, the CUSTOMERS table is changed and following would be
| 6 | Komal | 22 | MP | 4500.00 |
the output from the SELECT statement.
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+---------+-----+-----------+----------+ +----+----------+-----+-----------+----------+
Following is the example of a Truncate command.

Compiled By : Er. Sanjay Kumar Mishra 48


SQL > TRUNCATE TABLE CUSTOMERS; +----+----------+-----+-----------+----------+
Now, the CUSTOMERS table is truncated and the output from | ID | NAME | AGE | ADDRESS | SALARY |
SELECT statement will be as shown in the code block below − +----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
SQL> SELECT * FROM CUSTOMERS; | 2 | Khilan | 25 | Delhi | 1500.00 |
Empty set (0.00 sec) | 3 | kaushik | 23 | Kota | 2000.00 |
SQL - Having Clause | 4 | Chaitali | 25 | Mumbai | 6500.00 |
The HAVING Clause enables you to specify conditions that filter | 5 | Hardik | 27 | Bhopal | 8500.00 |
which group results appear in the results. | 6 | Komal | 22 | MP | 4500.00 |
The WHERE clause places conditions on the selected columns, | 7 | Muffy | 24 | Indore | 10000.00 |
whereas the HAVING clause places conditions on groups created +----+----------+-----+-----------+----------+
by the GROUP BY clause. Following is an example, which would display a record for a similar
Syntax age count that would be more than or equal to 2.
The following code block shows the position of the HAVING
Clause in a query. SQL > SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
SELECT GROUP BY age
FROM HAVING COUNT(age) >= 2;
WHERE
GROUP BY This would produce the following result −
HAVING +----+--------+-----+---------+---------+
ORDER BY | ID | NAME | AGE | ADDRESS | SALARY |
The HAVING clause must follow the GROUP BY clause in a query +----+--------+-----+---------+---------+
and must also precede the ORDER BY clause if used. The | 2 | Khilan | 25 | Delhi | 1500.00 |
following code block has the syntax of the SELECT statement +----+--------+-----+---------+---------+
including the HAVING clause − SQL GROUP Functions
SELECT column1, column2
FROM table1, table2 Group functions are built-in SQL functions that operate on
WHERE [ conditions ] groups of rows and return one value for the entire group. These
GROUP BY column1, column2 functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT
HAVING [ conditions ]
ORDER BY column1, column2 SQL COUNT (): This function returns the number of rows in
the table that satisfies the condition specified in the WHERE
Example condition. If the WHERE condition is not specified, then the
Consider the CUSTOMERS table having the following records. query returns the total number of rows in the table.

Compiled By : Er. Sanjay Kumar Mishra 49


For Example: If you want the number of employees in a
particular department, the query would be:
SELECT COUNT (*) FROM employee SQL SUM(): This function is used to get the sum of a numeric
WHERE dept = 'Electronics'; column
The output would be '2' rows. To get the total salary given out to the employees,
If you want the total number of employees in all the SELECT SUM (salary) FROM employee;
department, the query would take the form:
SELECT COUNT (*) FROM employee; SQL - Sub Queries
The output would be '5' rows. A Subquery or Inner query or a Nested query is a query within
SQL DISTINCT(): This function is used to select the distinct rows. another SQL query and embedded within the WHERE clause.
For Example: If you want to select all distinct department A subquery is used to return data that will be used in the main query
names from employee table, the query would be: as a condition to further restrict the data to be retrieved.
SELECT DISTINCT dept FROM employee; Subqueries can be used with the SELECT, INSERT, UPDATE, and
To get the count of employees with unique name, the query DELETE statements along with the operators like =, <, >, >=, <=,
would be: IN, BETWEEN, etc.
SELECT COUNT (DISTINCT name) FROM employee; There are a few rules that subqueries must follow −
 Subqueries must be enclosed within parentheses.
 A subquery can have only one column in the SELECT
SQL MAX(): This function is used to get the maximum value clause, unless multiple columns are in the main query for
from a column. the subquery to compare its selected columns.
To get the maximum salary drawn by an employee, the query  An ORDER BY command cannot be used in a subquery,
would be: although the main query can use an ORDER BY. The
SELECT MAX (salary) FROM employee; GROUP BY command can be used to perform the same
function as the ORDER BY in a subquery.
 Subqueries that return more than one row can only be used
SQL MIN(): This function is used to get the minimum value with multiple value operators such as the IN operator.
from a column.  The SELECT list cannot include any references to values
To get the minimum salary drawn by an employee, he query that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
would be:  A subquery cannot be immediately enclosed in a set
SELECT MIN (salary) FROM employee; function.
 The BETWEEN operator cannot be used with a subquery.
However, the BETWEEN operator can be used within the
SQL AVG(): This function is used to get the average value of a subquery.
numeric column. Subqueries with the SELECT Statement
To get the average salary, the query would be Subqueries are most frequently used with the SELECT statement.
SELECT AVG (salary) FROM employee; The basic syntax is as follows −

Compiled By : Er. Sanjay Kumar Mishra 50


SELECT column_name [, column_name ] +----+----------+-----+---------+----------+
FROM table1 [, table2 ] Subqueries with the INSERT Statement
WHERE column_name OPERATOR Subqueries also can be used with INSERT statements. The INSERT
(SELECT column_name [, column_name ] statement uses the data returned from the subquery to insert into
FROM table1 [, table2 ] another table. The selected data in the subquery can be modified
[WHERE]) with any of the character, date or number functions.
Example The basic syntax is as follows.
Consider the CUSTOMERS table having the following records − INSERT INTO table_name [ (column1 [, column2 ]) ]
+----+----------+-----+-----------+----------+ SELECT [ *|column1 [, column2 ]
| ID | NAME | AGE | ADDRESS | SALARY | FROM table1 [, table2 ]
+----+----------+-----+-----------+----------+ [ WHERE VALUE OPERATOR ]
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 | Example
| 2 | Khilan | 25 | Delhi | 1500.00 | Consider a table CUSTOMERS_BKP with similar structure as
| 3 | kaushik | 23 | Kota | 2000.00 | CUSTOMERS table. Now to copy the complete CUSTOMERS
| 4 | Chaitali | 25 | Mumbai | 6500.00 | table into the CUSTOMERS_BKP table, you can use the following
| 5 | Hardik | 27 | Bhopal | 8500.00 | syntax.
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 | SQL> INSERT INTO CUSTOMERS_BKP
+----+----------+-----+-----------+----------+ SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
Now, let us check the following subquery with a SELECT FROM CUSTOMERS) ;
statement.
Subqueries with the UPDATE Statement
SQL> SELECT * The subquery can be used in conjunction with the UPDATE
FROM CUSTOMERS statement. Either single or multiple columns in a table can be
WHERE ID IN (SELECT ID updated when using a subquery with the UPDATE statement.
FROM CUSTOMERS The basic syntax is as follows.
WHERE SALARY > 4500) ;
UPDATE table
This would produce the following result. SET column_name = new_value
+----+----------+-----+---------+----------+ [ WHERE OPERATOR [ VALUE ]
| ID | NAME | AGE | ADDRESS | SALARY | (SELECT COLUMN_NAME
+----+----------+-----+---------+----------+ FROM TABLE_NAME)
| 4 | Chaitali | 25 | Mumbai | 6500.00 | [ WHERE) ]
| 5 | Hardik | 27 | Bhopal | 8500.00 | Example
| 7 | Muffy | 24 | Indore | 10000.00 |

Compiled By : Er. Sanjay Kumar Mishra 51


Assuming, we have CUSTOMERS_BKP table available which is deletes the records from the CUSTOMERS table for all the
backup of CUSTOMERS table. The following example updates customers whose AGE is greater than or equal to 27.
SALARY by 0.25 times in the CUSTOMERS table for all the SQL> DELETE FROM CUSTOMERS
customers whose AGE is greater than or equal to 27. WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
SQL> UPDATE CUSTOMERS WHERE AGE >= 27 );
SET SALARY = SALARY * 0.25 This would impact two rows and finally the CUSTOMERS table
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP would have the following records.
WHERE AGE >= 27 );
+----+----------+-----+---------+----------+
This would impact two rows and finally CUSTOMERS table would | ID | NAME | AGE | ADDRESS | SALARY |
have the following records. +----+----------+-----+---------+----------+
+----+----------+-----+-----------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 |
| ID | NAME | AGE | ADDRESS | SALARY | | 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+ | 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 1 | Ramesh | 35 | Ahmedabad | 125.00 | | 6 | Komal | 22 | MP | 4500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 | | 7 | Muffy | 24 | Indore | 10000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 | +----+----------+-----+---------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 2125.00 |
| 6 | Komal | 22 | MP | 4500.00 |
Some old questions:
| 7 | Muffy | 24 | Indore | 10000.00 | 1. Consider the following two tables: (2016)
+----+----------+-----+-----------+----------+ Course Student-course
coursei Course Fee Rollno Name Course
Subqueries with the DELETE Statement
The subquery can be used in conjunction with the DELETE d name d
statement like with any other statements mentioned above. C001 BBA 50000 1 Jim C002
The basic syntax is as follows. C002 BBS 20000 2 James C003
DELETE FROM TABLE_NAME C003 MBA 3 Amand C005
[ WHERE OPERATOR [ VALUE ] a
(SELECT COLUMN_NAME C004 MBS 30000 4 Simon C002
FROM TABLE_NAME) C005 EMB 60000 5 Ricky C003
[ WHERE) ] A
Example 6 Adison C004
Assuming, we have a CUSTOMERS_BKP table available which is 7 Smith C005
a backup of the CUSTOMERS table. The following example
Now answer the following questions:

Compiled By : Er. Sanjay Kumar Mishra 52


a) Write the SQL syntax to create the two given tables. specify SELECT COURSENAME FROM COURSE WHERE
the primary key, foreign key and appropriate data type and FEE IS NULL;
constraints.
f) Find the students name and coursename taken by each
CREATE TABLE COURSE( student.
CourseID CHAR(4) NOT NULL,
CourseName VARCHAR (5) NOT NULL, SELECT STUDENT-COURSE.NAME,
Fee INT, COURSE.COURSENAME FROM COURSE, STUDENT-
PRIMARY KEY (CourseID) COURSE WHERE
); COURSE.COURSEID=STUDENT-COURSE.COURSEID;
CREATE TABLE STUDENT-COURSE( 2. Consider the “Employee” table (2009)
Rollno INT NOT NULL, Empno Ename Address Salary Post Age Comm
Name VARCHAR (20) NOT NULL, 101 Sagar Bhaktapur 3500 Helper 28 5
CouseID CHAR(4), 102 Namrata Pokhara 5000 secretary 23 10
PRIMARY KEY (Rollno), 103 Pooja Syanja 4500 Officeasst 24 5
FOREIGN KEY(courseid) REFERENCES 104 Ram Kathmand 15000 Doctor 30 5
COURSE(courseid) u
105 Rasmi Kathmand 9000 administrator 25 5
);
u
b) Write SQL syntax to insert records in both the tables. 106 Sachin Lalitpur 12000 Manager 30 5
INSERT INTO COURSE VALUES 107 Sam Pokhara 8500 Professor 45 10
(“COO1”,”BBA”,50000); 108 Narendra Biratnagar 8000 Lecture 40 5
INSERT INTO STUDENT-COURSE VALUES 109 Sarad Syanja 12000 Doctor 35 -
110 Bidur Chitwan 4500 secretary 28 -
(1,”Jim”,"c002");
Now answer the following questions:
c) Find the students name and course id whose name starts a) Create the above table.
with letter A or whose course id is C005.
CREATE TABLE EMPLOYEE(
SELECT COURSEID, NAME FROM STUDENT- EMPNO INT NOT NULL,
COURSE ENAME VARCHAR (10) NOT NULL,
WHERE NAME LIKE ‘A%’ OR COURSEID=”C005”; ADDRESS VARCHAR(20),
SALARY DECIMAL(10,2),
d) Find the name of the course whose course fee is maximum. POST VARCHAR(10),
SELECT COURSENAME FROM Course where fee in AGE INT,
(select max(fee) from course); COMMISSION INT,
e) Find the name of course which do not have any fee PRIMARY KEY (EMPNO)
);

Compiled By : Er. Sanjay Kumar Mishra 53


b) Display all information of employee that belongs to C001 Computer basic 02feb02 2000
department no. 10 or 40. C002 Hardware/ 01jan01 3500
SELECT * FROM EMPLOYEE software
WHERE DEPTNO=10 OR DEPTNO=40; C003 C programming 02jan04 6000
c) Display all employees whose age is greater than 30 and less
than 50. a) Find coursid and coursename for courses C001 and C002.
SELECT COURSED,COURSENAME FROM
SELECT * FROM EMPLOYEE WHERE age>30 COMPUTERMASTER
AND age<50; WHERE COURSID=”C001” OR COURSID
d) Display empno, empname, salary,age according to =”C002”;
ascending order of salary. b) Find tehe information of all the courses whose cost is greater
SELECT EMPNO,ENAME,SALARY,AGE than the minimum cost of all courses.
FROM EMPLOYEE SELECT * FROM COMPUTERMASTER
ORDER BY SALARY ASC; WHERE COST > IN(SELECT MIN(COST)
FROM COMPUTERMASTER);
e) Update the salary with 10000 whose empno is 107.
UPDATE EMPLOYEE SET SALARY = 10000 c) Find the cost and coursenme of all the courses whose
WHERE EMPNO =107; coursesname starts with letter c.
SELECT COURSENAME,COST FROM
f) Delete all records whose empno is 110. COMPUTERMASTER
DELETE FROM EMPLOYEE WHERE COURSENAME LIKE ‘C%’;
WHERE EMPNO = 110; d) Find the information of all the courses whose entries date is
g) Display information of all employees who do not have any between 01_march_01 and 01_jan_05.
commission. SELECT * FROM COMPUTERMASTER
SELECT * FROM EMPLOYEE WHERE WHERE ENTRYDATE BETWEEN ’01-03-
COMMISSION IS NULL; 2001’ AND ’01-01-2005’;
h) Display the salary of all employees whose name start with 4. a) Create the following table CUSTOMER(2010)
letter ‘R’. Fieldname Datatype Constraints
SELECT SALARY FROM EMPLOYEE Cust_id Number Primary key
WHERE ENAME LIKE ‘R%’; Cust_name Varchar Not null
Address Varchar Only ktm is allowed
3.Consider the following table COMPUTERMASTER(2008) Salary Number Should be positive
Courseid Cousename Entry date Cost Phoneno Number Should be unique

Compiled By : Er. Sanjay Kumar Mishra 54


CREATE TABLE CUSTOMER( GROUP BY DEPTNO;
CUST_ID NUMBER NOT NULL, IV. Find the employee whose name start with letter s or salary is
CUSTNAME VARCHAR (10) NOT NULL,
greater than 2500.
ADDRESS VARCHAR(20) CHECK
(ADDRESS=”KTM”), SELECT NAME,SALARY FROM EMPLOYEE
SALARY NUMBER CHECK (SALARY>0), WHERE NAME LIKE ‘S%’ OR SALARY>2500;
PHONENO NUMBER UNIQUE, V. Find information of all employees in descending order for
PRIMARY KEY (CUST_ID) their salary.
);
SELECT * FROM EMPLOYEE
b) Insert a new record in customer table. ORDER BY SALARY DESC;
INSERT INTO CUSTOMER VALUES 5. Consider the following employee table(2011)
(101,”RAM”,”KTM”,10000,8954090900); empid ename designatio salary comm Deptno
c) Add a new fieldname ‘e-mail’ with datatype varchar to the n
customer table. A01 Ram manager 10000 10 101
ALTER TABLE CUSTOMER ADD EMAIL A02 gopal Clerk 4500 10 110
VARCHAR(255); A03 Hari Salesman 12000 5 102
A04 Smith Peon 3200 2 101
d) Consider the following table employee
A05 Jack Clerk 4800 110
empno name Salary comm deptno
A06 Prakash salesman 6500 4 101
101 ram 2000 10
Write the query for the following
102 Shyam 3000 100 20
a) Display empid, empname and designation of all employee in
103 Hari 2500 50 10 ascending order on the basis of their name.
104 Rishi 4000 200 20
SELECT EMPID,ENAME,DESIGNATION FROM
Now answer the following question EMPLOYEE
I. Find all employees and salary who work in department 10. ORDER BY ENAME ASC;
SELECT NAME,SALARY FROM EMPLOYEE b) Find the name and salary of those employees who works in
WHERE DEPTNO=10;
department no 101 or 110.
II. Find name of employee who have no commission. SELECT ENAME,SALARY FROM EMPLOYEE
SELECT * FROM EMPLOYEE WHERE DEPTNO IN (101,110);
WHERE COMM. IS NULL;
c) Delete the record of those employees who works in
III. Find department wise total salary of all departments. department no 101.
SELECT SUM(SALARY) FROM EMPLOYEE

Compiled By : Er. Sanjay Kumar Mishra 55


DELETE FROM EMPLOYEE WHERE DEPTNO=110;
Every relation has some conditions that must hold for it to be
d) Select the record of only those employees who work in same
a valid relation. These conditions are called Relational
department where employee hari works.
Integrity Constraints. There are three main integrity
SELECT * FROM EMPLOYEE constraints −
WHERE DEPTNO IN (SELECT DEPTNO FROM
 Key constraints
EMPLOYEE WHERE ENAME=”HARI”);
 Domain constraints
e) Display the records of only those employees whose name  Referential integrity constraints
ends with letter k. 5.1 Domain Constraints
SELECT * FROM EMPLOYEE Attributes have specific values in real-world scenario. For
WHERE ENAME LIKE ‘%K’; example, age can only be a positive integer. The same
f) Retrieve all information of only those employees who does constraints have been tried to employ on the attributes of a
have any commission. relation. Every attribute is bound to have a specific range of
SELECT * FROM EMPLOYEE values. For example, age cannot be less than zero and
WHERE COMM. IS NOT NULL; telephone numbers cannot contain a digit outside 0-9.
g) Find out total number of employee in the given table.
5.2 Key Constraints(Entity Integrity)
SELECT COUNT(EMPID) FROM EMPLOYEE; There must be at least one minimal subset of attributes in the
h) Find the information of only those employeewho have the relation, which can identify a tuple uniquely. This minimal
salary greater than 5600 andworks in department no 101. subset of attributes is called keyfor that relation. If there are
SELECT * FROM EMPLOYEE more than one such minimal subsets, these are
WHERE DEPTNO=101 AND SALARY>5600; called candidate keys.
Key constraints force that −
 in a relation with a key attribute, no two tuples can
have identical values for key attributes.
 a key attribute can not have NULL values.
Key constraints are also referred to as Entity Constraints.
5.3 Referential integrity Constraints
Referential integrity constraints work on the concept of
Foreign Keys. A foreign key is a key attribute of a relation
that can be referred in other relation.
Unit-5
Integrity Constraints

Compiled By : Er. Sanjay Kumar Mishra 56


Referential integrity constraint states that if a relation refers to ALTER TABLE EMPLOYEES DROP CONSTRAINT
a key attribute of a different or same relation, then that key EMPLOYEES_PK;
element must exist.
Some implementations may provide shortcuts for dropping certain
SQL - Constraints
constraints. For example, to drop the primary key constraint for a
Constraints are the rules enforced on the data columns of a table.
table in Oracle, you can use the following command.
These are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the database. ALTER TABLE EMPLOYEES DROP PRIMARY KEY;
Constraints could be either on a column level or a table level. The Some implementations allow you to disable constraints. Instead of
column level constraints are applied only to one column, whereas permanently dropping a constraint from the database, you may want
the table level constraints are applied to the whole table. to temporarily disable the constraint and then enable it later.
Following are some of the most commonly used constraints Integrity Constraints
available in SQL. These constraints have already been discussed Integrity constraints are used to ensure accuracy and consistency of
in SQL - RDBMS Conceptschapter, but it’s worth to revise them at the data in a relational database. Data integrity is handled in a
this point. relational database through the concept of referential integrity.
 NOT NULL Constraint − Ensures that a column cannot There are many types of integrity constraints that play a role
have NULL value. in Referential Integrity (RI). These constraints include Primary
 DEFAULT Constraint − Provides a default value for a Key, Foreign Key, Unique Constraints and other constraints which
column when none is specified. are mentioned above.
 UNIQUE Constraint − Ensures that all values in a column
are different.
 PRIMARY Key − Uniquely identifies each row/record in a
database table.
 FOREIGN Key − Uniquely identifies a row/record in any of
the given database table.
 CHECK Constraint − The CHECK constraint ensures that
all the values in a column satisfies certain conditions.
 INDEX − Used to create and retrieve data from the database
very quickly.
Constraints can be specified when a table is created with the
CREATE TABLE statement or you can use the ALTER TABLE
statement to create constraints even after the table is created.
Dropping Constraints
Any constraint that you have defined can be dropped using the
ALTER TABLE command with the DROP CONSTRAINT option.
For example, to drop the primary key constraint in the
EMPLOYEES table, you can use the following command.

Compiled By : Er. Sanjay Kumar Mishra 57


6.2 Functional Dependency

Functional dependency (FD) is a set of constraints between


two attributes in a relation. Functional dependency says that if
two tuples have same values for attributes A1, A2,..., An, then
those two tuples must have to have same values for attributes
B1, B2, ..., Bn.
Unit -6 Functional dependency is represented by an arrow sign (→)
Relational Database Design that is, X→Y, where X functionally determines Y. The left-
6.1 Pitfalls of Relational Model hand side attributes determine the values of attributes on the
Redundancy right-hand side.
Data redundancy is a condition created within a Armstrong's Axioms
database or data storage technology in which the same piece If F is a set of functional dependencies then the closure of F,
of data is held in two separate places. This can mean two denoted as F+, is the set of all functional dependencies
different fields within a singledatabase, or two different spots logically implied by F. Armstrong's Axioms are a set of rules,
in multiple software environments or platforms. that when applied repeatedly, generates a closure of functional
Anomalies dependencies.
If a database design is not perfect, it may contain anomalies,  Reflexive rule − If alpha is a set of attributes and beta
which are like a bad dream for any database administrator. is_subset_of alpha, then alpha holds beta.
Managing a database with anomalies is next to impossible.  Augmentation rule − If a → b holds and y is attribute
 Update anomalies − If data items are scattered and are set, then ay → by also holds. That is adding attributes
not linked to each other properly, then it could lead to in dependencies, does not change the basic
strange situations. For example, when we try to update dependencies.
one data item having its copies scattered over several  Transitivity rule − Same as transitive rule in algebra,
places, a few instances get updated properly while a if a → b holds and b → c holds, then a → c also holds.
few others are left with old values. Such instances a → b is called as a functionally that determines b.
leave the database in an inconsistent state. Trivial Functional Dependency
 Deletion anomalies − We tried to delete a record, but  Trivial − If a functional dependency (FD) X → Y
parts of it was left undeleted because of unawareness, holds, where Y is a subset of X, then it is called a
the data is also saved somewhere else. trivial FD. Trivial FDs always hold.
 Insert anomalies − We tried to insert data in a record  Non-trivial − If an FD X → Y holds, where Y is not a
that does not exist at all. subset of X, then it is called a non-trivial FD.

Compiled By : Er. Sanjay Kumar Mishra 58


 Completely non-trivial − If an FD X → Y holds, First Normal Form is defined in the definition of relations
where x intersect Y = Φ, it is said to be a completely (tables) itself. This rule defines that all the attributes in a
non-trivial FD. relation must have atomic domains. The values in an atomic
6.3 Normalization domain are indivisible units.
In simple words Normalization is a systematic way of
ensuring that a database structure is suitable for general-
purpose querying and free of certain undesirable
characteristics—insertion, update, and deletion anomalies— We re-arrange the relation (table) as below, to convert it to
that could lead to a loss of data integrity. First Normal Form.

Normal forms in a database or the concept


of Normalization makes a Relation or Table free from
insert/update/delete anomalies and saves space by removing
duplicate data.

According to E. F. Codd the objectives of normalization Each attribute must contain only a single value from its pre-
were stated as follows: defined domain.
1. To free the collection of relations from undesirable Second Normal Form
insertion, update and deletion dependencies. Before we learn about the second normal form, we need to
2. To reduce the need for restructuring the collection of understand the following −
relations as new types of data are introduced, and thus  Prime attribute − An attribute, which is a part of the
increase the life span of application programs. candidate-key, is known as a prime attribute.
3. To make the relational model more informative to users.  Non-prime attribute − An attribute, which is not a
4. To make the collection of relations neutral to the query part of the prime-key, is said to be a non-prime
statistics, where these statistics are liable to change as time attribute.
goes by. If we follow second normal form, then every non-prime
attribute should be fully functionally dependent on prime key
As of now there are total 8 normal forms, but to keep our data attribute. That is, if X → A holds, then there should not be
consistent & non-redundant the first 3 Normal Forms are any proper subset Y of X, for which Y → A also holds true.
sufficient.

First Normal Form

Compiled By : Er. Sanjay Kumar Mishra 59


We see here in Student_Project relation that the prime key superkey nor is City a prime attribute. Additionally, Stu_ID
attributes are Stu_ID and Proj_ID. According to the rule, non- → Zip → City, so there exists transitive dependency.
key attributes, i.e. Stu_Name and Proj_Name must be To bring this relation into third normal form, we break the
dependent upon both and not on any of the prime key attribute relation into two relations as follows −
individually. But we find that Stu_Name can be identified by
Stu_ID and Proj_Name can be identified by Proj_ID
independently. This is called partial dependency, which is
not allowed in Second Normal Form.

We broke the relation in two as depicted in the above picture.


So there exists no partial dependency. Boyce-Codd Normal Form
Third Normal Form Boyce-Codd Normal Form (BCNF) is an extension of Third
For a relation to be in Third Normal Form, it must be in Normal Form on strict terms. BCNF states that −
Second Normal form and the following must satisfy −  For any non-trivial functional dependency, X → A, X
 No non-prime attribute is transitively dependent on must be a super-key.
prime key attribute. In the above image, Stu_ID is the super-key in the relation
 For any non-trivial functional dependency, X → A, Student_Detail and Zip is the super-key in the relation
then either − ZipCodes. So,
o X is a superkey or, Stu_ID → Stu_Name, Zip
o A is prime attribute. and
Zip → City
Which confirms that both the relations are in BCNF.
4NF

 A table is in the 4NF if it is in BCNF and has no


We find that in the above Student_detail relation, Stu_ID is multivalued dependencies.
the key and only prime key attribute. We find that City can be Example
identified by Stu_ID as well as Zip itself. Neither Zip is a

Compiled By : Er. Sanjay Kumar Mishra 60


 The multi-valued dependency X ->Y holds in a
relation R if whenever we have two tuples of R that
agree (same) in all the attributes of X, then we can
swap their Y components and get two new tuples that
are also in R.
 Suppose a student can have more than one subject
and more than one activity.

Note that all three attributes make up the Primary Key. Unit-7

 Note that Student_Id can be associated with many Database security


subject as well as many activities (multi-valued Database security refers to the collective measures used to protect
dependency). and secure a database or database management software from
 Suppose student 100 signs up for skiing. Then we illegitimate use and malicious threats and attacks.
would insert (100, Music, Skiing). This row implies It is a broad term that includes a multitude of processes, tools and
that student 100 skies as Music subject but not as an methodologies that ensure security within a database environment.
accounting subject, so in order to keep the data Database security covers and enforces security on all aspects and
consistent we must add one more row (100, components of databases. This includes:
Accounting, Skiing). This is an insertion anomaly  Data stored in database
 Database server
 Suppose we have a relation R(A) with a multi valued
 Database management system (DBMS)
dependency X-> Y. The MVD can be removed by
 Other database workflow applications
decomposing R into R1(R - Y) and R2(X U Y). Database security is generally planned, implemented and maintained
 Here are the tables Normalized by a database administrator and or other information security
professional.
Some of the ways database security is analyzed and implemented
include:
 Restricting unauthorized access and use by implementing
strong and multifactor access and data management controls
 Load/stress testing and capacity testing of a database to
ensure it does not crash in a distributed denial of service
(DDoS) attack or user overload
 Physical security of the database server and backup
equipment from theft and natural disasters

Compiled By : Er. Sanjay Kumar Mishra 61


 Reviewing existing system for any known or unknown 7.2 Authentication and Authorization
vulnerabilities and defining and implementing a road
map/plan to mitigate them Authentication means verifying the identity of someone (a user,
7.1 Access control device, or other entity) who wants to use data, resources, or
Access control is a security technique that regulates who or what can applications. Validating that identity establishes a trust relationship
view or use resources in a computing environment. It is a for further interactions. Authentication also enables accountability by
fundamental concept in security that minimizes risk to the business making it possible to link access and actions to specific identities.
or organization. After authentication, authorization processes can allow or limit the
Types of access control levels of access and action permitted to that entity.
The main types of access control are: For simplicity, the same authentication method is generally used for
 Mandatory access control (MAC): A security model in which all database users, but Oracle allows a single database instance to use
access rights are regulated by a central authority based on any or all methods. Oracle requires special authentication procedures
multiple levels of security. Often used in government and for database administrators, because they perform special database
military environments, classifications are assigned to system operations. Oracle also encrypts passwords during transmission to
resources and the operating system or security kernel, grants or ensure the security of network authentication.
denies access to those resource objects based on the information Authorization primarily includes two processes:
security clearance of the user or device. For example, Security  Permitting only certain users to access, process, or alter data
Enhanced Linux is an implementation of MAC on the Linux  Applying varying limitations on users' access or actions. The
operating system. limitations placed on (or removed from) users can apply to
 Discretionary access control (DAC): An access control method objects, such as schemas, tables, or rows; or to resources,
in which owners or administrators of the protected system, data such as time (CPU, connect, or idle times).
or resource set the policies defining who or what is authorized to
access the resource. Many of these systems enable 7.3 Data encryption and Decryption
administrators to limit the propagation of access rights. A
common criticism of DAC systems is a lack of centralized Encryption is a security method in which information is encoded in
control. such a way that only authorized user can read it. It uses encryption
 Role-based access control (RBAC): A widely used access algorithm to generate ciphertext that can only be read if decrypted.
control mechanism that restricts access to computer resources Types of Encryption
based on individuals or groups with defined business functions -- There are two types of encryptions schemes as listed below:
executive level, engineer level 1 -- rather than the identities of  Symmetric Key encryption
individual users. The role-based security model relies on a  Public Key encryption
complex structure of role assignments, role authorizations and SYMMETRIC KEY ENCRYPTION
role permissions developed using role engineering to regulate Symmetric key encryption algorithm uses same cryptographic
employee access to systems. RBAC systems can be used to keys for both encryption and decryption of cipher text.
enforce MAC and DAC frameworks.

Compiled By : Er. Sanjay Kumar Mishra 62


PUBLIC KEY ENCRYPTION
Public key encryption algorithm uses pair of keys, one of which is
a secret key and one of which is public. These two keys are
mathematically linked with each other.

Database decryption is converting the meaningless cipher text into


the original information using keys generated by the encryption
algorithms. Digital signature should not only be tied to the signing user,
but also to the message.
Digital Signature Applications
Digital signatures allow us to verify the author, date and time of There are several reasons to implement digital signatures to
signatures, authenticate the message contents. It also includes communications:
authentication function for additional capabilities.  Authentication
Digital signatures help to authenticate the sources of
messages. For example, if a bank’s branch office sends a
message to central office, requesting for change in balance
of an account. If the central office could not authenticate
that message is sent from an authorized source, acting of
such request could be a grave mistake.
 Integrity
Once the message is signed, any change in the message
would invalidate the signature.
 Non-repudiation
By this property, any entity that has signed some
information cannot at a later time deny having signed it.

Firewall

Compiled By : Er. Sanjay Kumar Mishra 63


Firewall is a barrier between Local Area Network (LAN) and the
Internet. It allows keeping private resources confidential and
minimizes the security risks. It controls network traffic, in both
directions.
The following diagram depicts a sample firewall between LAN and
the internet. The connection between the two is the point of
vulnerability. Both hardware and the software can be used at this
point to filter network traffic.

There are two types of Firewall system: One works by using filters at Unit-8
the network layer and the other works by using proxy servers at the
Transaction management, Recovery and Query processing
user, application, or network layer.
Key Points
 Firewall management must be addressed by both system 8.1 Introduction to transaction
managers and the network managers.
 The amount of filtering a firewall varies. For the same A transaction can be defined as a group of tasks. A single task
firewall, the amount of filtering may be different in is the minimum processing unit which cannot be divided
different directions. further.
Let’s take an example of a simple transaction. Suppose a bank
employee transfers Rs 500 from A's account to B's account.
This very simple and small transaction involves several low-
level tasks.
A’s Account
Open_Account(A)

Compiled By : Er. Sanjay Kumar Mishra 64


Old_Balance = A.balance restarts. If a transaction updates a chunk of data in a
New_Balance = Old_Balance - 500 database and commits, then the database will hold the
A.balance = New_Balance modified data. If a transaction commits but the system
Close_Account(A) fails before the data could be written on to the disk,
then that data will be updated once the system springs
B’s Account back into action.
Open_Account(B)  Isolation − In a database system where more than one
Old_Balance = B.balance transaction are being executed simultaneously and in
New_Balance = Old_Balance + 500 parallel, the property of isolation states that all the
B.balance = New_Balance transactions will be carried out and executed as if it is
Close_Account(B) the only transaction in the system. No transaction will
ACID Properties affect the existence of any other transaction.
A transaction is a very small unit of a program and it may Serializability
contain several lowlevel tasks. A transaction in a database When multiple transactions are being executed by the
system must maintain Atomicity, Consistency, Isolation, operating system in a multiprogramming environment, there
and Durability − commonly known as ACID properties − in are possibilities that instructions of one transactions are
order to ensure accuracy, completeness, and data integrity. interleaved with some other transaction.
 Schedule − A chronological execution sequence of a
 Atomicity − This property states that a transaction
must be treated as an atomic unit, that is, either all of transaction is called a schedule. A schedule can have
its operations are executed or none. There must be no many transactions in it, each comprising of a number
state in a database where a transaction is left partially of instructions/tasks.
 Serial Schedule − It is a schedule in which
completed. States should be defined either before the
execution of the transaction or after the transactions are aligned in such a way that one
execution/abortion/failure of the transaction. transaction is executed first. When the first transaction
 Consistency − The database must remain in a
completes its cycle, then the next transaction is
consistent state after any transaction. No transaction executed. Transactions are ordered one after the other.
should have any adverse effect on the data residing in This type of schedule is called a serial schedule, as
the database. If the database was in a consistent state transactions are executed in a serial manner.
before the execution of a transaction, it must remain In a multi-transaction environment, serial schedules are
consistent after the execution of the transaction as considered as a benchmark. The execution sequence of an
well. instruction in a transaction cannot be changed, but two
 Durability − The database should be durable enough
transactions can have their instructions executed in a random
to hold all its latest updates even if the system fails or fashion. This execution does no harm if two transactions are

Compiled By : Er. Sanjay Kumar Mishra 65


mutually independent and working on different segments of Two schedules having multiple transactions with conflicting
data; but in case these two transactions are working on the operations are said to be conflict equivalent if and only if −
same data, then the results may vary. This ever-varying result  Both the schedules contain the same set of
may bring the database to an inconsistent state. Transactions.
To resolve this problem, we allow parallel execution of a  The order of conflicting pairs of operation is maintained
transaction schedule, if its transactions are either serializable in both the schedules.
or have some equivalence relation among them. Note − View equivalent schedules are view serializable and
Equivalence Schedules conflict equivalent schedules are conflict serializable. All
An equivalence schedule can be of the following types − conflict serializable schedules are view serializable too.
Result Equivalence
If two schedules produce the same result after execution, they States of Transactions
are said to be result equivalent. They may yield the same A transaction in a database can be in one of the following
result for some value and different results for another set of states −
values. That's why this equivalence is not generally
considered significant.
View Equivalence
Two schedules would be view equivalence if the transactions
in both the schedules perform similar actions in a similar
manner.
For example −
 If T reads the initial data in S1, then it also reads the  Active − In this state, the transaction is being executed.
initial data in S2. This is the initial state of every transaction.
 If T reads the value written by J in S1, then it also  Partially Committed − When a transaction executes
reads the value written by J in S2. its final operation, it is said to be in a partially
 If T performs the final write on the data value in S1, committed state.
then it also performs the final write on the data value  Failed − A transaction is said to be in a failed state if
in S2. any of the checks made by the database recovery
Conflict Equivalence system fails. A failed transaction can no longer
Two schedules would be conflicting if they have the following proceed further.
properties −  Aborted − If any of the checks fails and the transaction
 Both belong to separate transactions. has reached a failed state, then the recovery manager
 Both accesses the same data item. rolls back all its write operations on the database to
 At least one of them is "write" operation. bring the database back to its original state where it
was prior to the execution of the transaction.

Compiled By : Er. Sanjay Kumar Mishra 66


Transactions in this state are called aborted. The There are four types of lock protocols available −
database recovery module can select one of the two Simplistic Lock Protocol
operations after a transaction aborts − Simplistic lock-based protocols allow transactions to obtain a
o Re-start the transaction lock on every object before a 'write' operation is performed.
o Kill the transaction Transactions may unlock the data item after completing the
 Committed − If a transaction executes all its ‘write’ operation.
operations successfully, it is said to be committed. All Pre-claiming Lock Protocol
its effects are now permanently established on the Pre-claiming protocols evaluate their operations and create a
database system. list of data items on which they need locks. Before initiating
8.2 Introduction to Concurrency Control an execution, the transaction requests the system for all the
locks it needs beforehand. If all the locks are granted, the
In a multiprogramming environment where multiple transaction executes and releases all the locks when all its
transactions can be executed simultaneously, it is highly operations are over. If all the locks are not granted, the
important to control the concurrency of transactions. We have transaction rolls back and waits until all the locks are granted.
concurrency control protocols to ensure atomicity, isolation,
and serializability of concurrent transactions. Concurrency
control protocols can be broadly divided into two categories −
 Lock based protocols
 Time stamp based protocols
Lock-based Protocols Two-Phase Locking 2PL
Database systems equipped with lock-based protocols use a This locking protocol divides the execution phase of a
mechanism by which any transaction cannot read or write data transaction into three parts. In the first part, when the
until it acquires an appropriate lock on it. Locks are of two transaction starts executing, it seeks permission for the locks it
kinds − requires. The second part is where the transaction acquires all
 Binary Locks − A lock on a data item can be in two the locks. As soon as the transaction releases its first lock, the
states; it is either locked or unlocked. third phase starts. In this phase, the transaction cannot demand
 Shared/exclusive − This type of locking mechanism any new locks; it only releases the acquired locks.
differentiates the locks based on their uses. If a lock is
acquired on a data item to perform a write operation, it
is an exclusive lock. Allowing more than one
transaction to write on the same data item would lead
the database into an inconsistent state. Read locks are Two-phase locking has two phases, one is growing, where all
shared because no data value is being changed. the locks are being acquired by the transaction; and the second

Compiled By : Er. Sanjay Kumar Mishra 67


phase is shrinking, where the locks held by the transaction are The timestamp-ordering protocol ensures serializability
being released. among transactions in their conflicting read and write
To claim an exclusive (write) lock, a transaction must first operations. This is the responsibility of the protocol system
acquire a shared (read) lock and then upgrade it to an that the conflicting pair of tasks should be executed according
exclusive lock. to the timestamp values of the transactions.
Strict Two-Phase Locking  The timestamp of transaction Ti is denoted as TS(Ti).
The first phase of Strict-2PL is same as 2PL. After acquiring  Read time-stamp of data-item X is denoted by R-
all the locks in the first phase, the transaction continues to timestamp(X).
execute normally. But in contrast to 2PL, Strict-2PL does not  Write time-stamp of data-item X is denoted by W-
release a lock after using it. Strict-2PL holds all the locks until timestamp(X).
the commit point and releases all the locks at a time. Timestamp ordering protocol works as follows −
 If a transaction Ti issues a read(X) operation −
o If TS(Ti) < W-timestamp(X)
 Operation rejected.
Strict-2PL does not have cascading abort as 2PL does. o If TS(Ti) >= W-timestamp(X)
Timestamp-based Protocols  Operation executed.
The most commonly used concurrency protocol is the o All data-item timestamps updated.
timestamp based protocol. This protocol uses either system  If a transaction Ti issues a write(X) operation −
time or logical counter as a timestamp. o If TS(Ti) < R-timestamp(X)
Lock-based protocols manage the order between the  Operation rejected.
conflicting pairs among transactions at the time of execution, o If TS(Ti) < W-timestamp(X)
whereas timestamp-based protocols start working as soon as a  Operation rejected and Ti rolled back.
transaction is created. o Otherwise, operation executed.
Every transaction has a timestamp associated with it, and the Thomas' Write Rule
ordering is determined by the age of the transaction. A This rule states if TS(Ti) < W-timestamp(X), then the
transaction created at 0002 clock time would be older than all operation is rejected and Ti is rolled back.
other transactions that come after it. For example, any Time-stamp ordering rules can be modified to make the
transaction 'y' entering the system at 0004 is two seconds schedule view serializable.
younger and the priority would be given to the older one. Instead of making Ti rolled back, the 'write' operation itself is
In addition, every data item is given the latest read and write- ignored.
timestamp. This lets the system know when the last ‘read and
write’ operation was performed on the data item. Data Backup
Timestamp Ordering Protocol

Compiled By : Er. Sanjay Kumar Mishra 68


Loss of Volatile Storage  Remote backup ; Here a backup copy of the database is
A volatile storage like RAM stores all the active logs, disk stored at a remote location from where it can be
buffers, and related data. In addition, it stores all the restored in case of a catastrophe.
transactions that are being currently executed. What happens  Alternatively, database backups can be taken on
if such a volatile storage crashes abruptly? It would obviously magnetic tapes and stored at a safer place. This backup
take away all the logs and active copies of the database. It can later be transferred onto a freshly installed
makes recovery almost impossible, as everything that is database to bring it to the point of backup.
required to recover the data is lost. Grown-up databases are too bulky to be frequently backed up.
Following techniques may be adopted in case of loss of In such cases, we have techniques where we can restore a
volatile storage − database just by looking at its logs. So, all that we need to do
 We can have checkpoints at multiple stages so as to here is to take a backup of all the logs at frequent intervals of
save the contents of the database periodically. time. The database can be backed up once a week, and the
 A state of active database in the volatile memory can logs being very small can be backed up every day or as
be periodically dumped onto a stable storage, which frequently as possible.
may also contain logs and active transactions and Remote Backup
buffer blocks. Remote backup provides a sense of security in case the
 <dump> can be marked on a log file, whenever the primary location where the database is located gets destroyed.
database contents are dumped from a non-volatile Remote backup can be offline or real-time or online. In case it
memory to a stable one. is offline, it is maintained manually.
Recovery
 When the system recovers from a failure, it can restore
the latest dump.
 It can maintain a redo-list and an undo-list as
checkpoints.
 It can recover the system by consulting undo-redo lists
to restore the state of all transactions up to the last Online backup systems are more real-time and lifesavers for
checkpoint. database administrators and investors. An online backup
Database Backup & Recovery from Catastrophic Failure system is a mechanism where every bit of the real-time data is
A catastrophic failure is one where a stable, secondary storage backed up simultaneously at two distant places. One of them
device gets corrupt. With the storage device, all the valuable is directly connected to the system and the other one is kept at
data that is stored inside is lost. We have two different a remote place as backup.
strategies to recover data from such a catastrophic failure − As soon as the primary database storage fails, the backup
system senses the failure and switches the user system to the

Compiled By : Er. Sanjay Kumar Mishra 69


remote storage. Sometimes this is so instant that the users There are problems − external to the system − that may cause
can’t even realize a failure. the system to stop abruptly and cause the system to crash. For
example, interruptions in power supply may cause the failure
Data Recovery of underlying hardware or software failure.
Examples may include operating system errors.
Crash Recovery Disk Failure
DBMS is a highly complex system with hundreds of In early days of technology evolution, it was a common
transactions being executed every second. The durability and problem where hard-disk drives or storage drives used to fail
robustness of a DBMS depends on its complex architecture frequently.
and its underlying hardware and system software. If it fails or Disk failures include formation of bad sectors, unreachability
crashes amid transactions, it is expected that the system would to the disk, disk head crash or any other failure, which
follow some sort of algorithm or techniques to recover lost destroys all or a part of disk storage.
data. Storage Structure
Failure Classification We have already described the storage system. In brief, the
To see where the problem has occurred, we generalize a storage structure can be divided into two categories −
failure into various categories, as follows −  Volatile storage − As the name suggests, a volatile
Transaction failure storage cannot survive system crashes. Volatile storage
A transaction has to abort when it fails to execute or when it devices are placed very close to the CPU; normally
reaches a point from where it can’t go any further. This is they are embedded onto the chipset itself. For
called transaction failure where only a few transactions or example, main memory and cache memory are
processes are hurt. examples of volatile storage. They are fast but can
Reasons for a transaction failure could be − store only a small amount of information.
 Logical errors − Where a transaction cannot complete  Non-volatile storage − These memories are made to
because it has some code error or any internal error survive system crashes. They are huge in data storage
condition. capacity, but slower in accessibility. Examples may
 System errors − Where the database system itself include hard-disks, magnetic tapes, flash memory, and
terminates an active transaction because the DBMS is non-volatile (battery backed up) RAM.
not able to execute it, or it has to stop because of some Recovery and Atomicity
system condition. For example, in case of deadlock or When a system crashes, it may have several transactions being
resource unavailability, the system aborts an active executed and various files opened for them to modify the data
transaction. items. Transactions are made of various operations, which are
System Crash atomic in nature. But according to ACID properties of DBMS,

Compiled By : Er. Sanjay Kumar Mishra 70


atomicity of transactions as a whole must be maintained, that  When the transaction modifies an item X, it write logs
is, either all the operations are executed or none. as follows −
When a DBMS recovers from a crash, it should maintain the <Tn, X, V1, V2>
following −
 It should check the states of all the transactions, which It reads Tn has changed the value of X, from V1 to V2.
were being executed.  When the transaction finishes, it logs −
 A transaction may be in the middle of some operation; <Tn, commit>
the DBMS must ensure the atomicity of the transaction The database can be modified using two approaches −
in this case.  Deferred database modification − All logs are
 It should check whether the transaction can be
written on to the stable storage and the database is
completed now or it needs to be rolled back. updated when a transaction commits.
 No transactions would be allowed to leave the DBMS
 Immediate database modification − Each log follows
in an inconsistent state. an actual database modification. That is, the database
There are two types of techniques, which can help a DBMS in is modified immediately after every operation.
recovering as well as maintaining the atomicity of a Recovery with Concurrent Transactions
transaction − When more than one transaction are being executed in
 Maintaining the logs of each transaction, and writing
parallel, the logs are interleaved. At the time of recovery, it
them onto some stable storage before actually would become hard for the recovery system to backtrack all
modifying the database. logs, and then start recovering. To ease this situation, most
 Maintaining shadow paging, where the changes are
modern DBMS use the concept of 'checkpoints'.
done on a volatile memory, and later, the actual Checkpoint
database is updated. Keeping and maintaining logs in real time and in real
Log-based Recovery environment may fill out all the memory space available in
Log is a sequence of records, which maintains the records of the system. As time passes, the log file may grow too big to be
actions performed by a transaction. It is important that the handled at all. Checkpoint is a mechanism where all the
logs are written prior to the actual modification and stored on previous logs are removed from the system and stored
a stable storage media, which is failsafe. permanently in a storage disk. Checkpoint declares a point
Log-based recovery works as follows − before which the DBMS was in consistent state, and all the
 The log file is kept on a stable storage media.
transactions were committed.
 When a transaction enters the system and starts
Recovery
execution, it writes a log about it. When a system with concurrent transactions crashes and
<Tn, Start> recovers, it behaves in the following manner −

Compiled By : Er. Sanjay Kumar Mishra 71


 Query Optimization : At this step, Query optimizer
takes the best possible query plan to process with
checking in the system for the conditions and
indexes.
 Query Execution : It is the final step of query
processing. At this step , Command processor
 The recovery system reads the logs backwards from the
executes the above query plan retrieve the data from
end to the last checkpoint.
the database and returns the result.
 It maintains two lists, an undo-list and a redo-list.
 If the recovery system sees a log with <Tn, Start> and
<Tn, Commit> or just <Tn, Commit>, it puts the
transaction in the redo-list.
 If the recovery system sees a log with <Tn, Start> but
no commit or abort log found, it puts the transaction in
undo-list.
All the transactions in the undo-list are then undone and their
logs are removed. All the transactions in the redo-list and their
previous logs are removed and then redone before saving their
logs.

8.4 Query Processing

Query processing is a process for the purpose of transforming a


high level query language into a correct and efficient execution
plan expressed in low level language.

There are several steps of any query processing. These steps


are given below:

 Query Parsing and Translation : At this step, Parser


check the syntax and query. Then translator translate
the query into different possible query plan . Each
query plan represented by a relational algebra.

Compiled By : Er. Sanjay Kumar Mishra 72

You might also like