Unit 1 DBMS
Unit 1 DBMS
TEXT BOOKS:
1. Abraham Silberschatz, Henry F. Korth, S. Sudharshan, “Database System Concepts”,
Seventh Edition, McGraw Hill, 2020
INTRODUCTION
INTRODUCTION TO DBMS
Data: Known facts that can be recorded that have implicit meaning.
Information: When data is processed, organized, structured or presented in a given context it
becomes information.
Database: collection of data.
DBMS: DBMS is a collection of interrelated data and a set of program to access those data.
The primary goal of a DBMS is to provide a way to store and retrieve database information
that is both convenient and efficient.
Database System: Database and DBMS collectively known as database system.
1
allowed to access only a part of database i.e., the data that is related to them or related
to their department.
vii. Data Atomicity:
A transaction in commercial databases is referred to as atomic unit of work. For
example, when you purchase something from a point of sale terminal, a number of
tasks are performed such as;
Company stock is updated.
Amount is added in company's account.
Sales person's commission increases etc.
All these tasks collectively are called an atomic unit of work or transaction. These
tasks must be completed in all; otherwise partially completed tasks are rolled back.
Thus through DBMS, it is ensured that only consistent data exists within the database.
viii. Development of Application:
The cost and time for developing new applications is reduced. The DBMS provides
tools that can be used to develop application programs.
ix. Creating Forms:
Form is very important object of DBMS. You can create Forms very easily and
quickly in DBMS, Once a Form is created, it can be used many times and it can be
modified very easily. The created Forms are also saved along with database and
behave like a software component.
A Form provides very easy way (user-friendly interface) to enter data into database,
edit data, and display data from database.
The non-technical users can also perform various operations on databases through
Forms without going into the technical details of a database.
x. Report Writers:
Most of the DBMSs provide the report writer tools used to create reports. The users
can create reports very easily and quickly.
Once a report is created, it can be used many times and it can be modified very easily.
The created reports are also saved along with database and behave like a software
component.
xi. Control Over Concurrency:
In a computer file-based system, if two users are allowed to access data
simultaneously, it is possible that they will interfere with each other.
For example, if both users attempt to perform update operation on the same record,
then one may overwrite the values recorded by the other. Most DBMSs have sub-
systems to control the concurrency so that transactions are always recorded with
accuracy.
xii. Backup and Recovery Procedures:
In a computer file-based system, the user creates the backup of data regularly to
protect the valuable data from damaging due to failures to the computer system or
application program. It is a time consuming method, if volume of data is large.
Most of the DBMSs provide the 'backup and recovery' sub-systems that automatically
create the backup of data and restore data if required.
xiii. Data Independence:
The separation of data structure of database from the application program that is used
to access data from database is called data independence.
2
In DBMS, database and application programs are separated from each other. The
DBMS sits in between them. You can easily change the structure of database without
modifying the application program.
For example you can modify the size or data type of a data items. On the other hand,
in computer file-based system, the structure of data items is built into the individual
application programs. Thus the data is dependent on the data file and vice versa.
xiv. Advanced Capabilities:
DBMS also provides advance capabilities for online access and reporting of data
through Internet.
Today, most of the database systems are online.
The database technology is used in conjunction with Internet technology to access
data on the web servers.
Views of Data
Database is a collection of interrelated data and set of programs that allow users to
access or modify the data.
Abstract view of the system is a view in which the system hides certain details of how
the data are stored and maintained.
The main purpose of database systems is to provide users with abstract view of the
data.
The view of the system helps the user to retrieve data efficiently. For simplifying the
user interaction with the system there are several levels of abstraction - these levels
are - Physical level, logical level and view level.
Data abstraction :
Data abstraction means retrieving only required amount of information of the system
and hiding background details.
There are several levels of abstraction that simplify the user interactions with the
system. These are
i) Physical level:
This is the lowest level.
This level describes how actually the data are stored.
This level describes complex low level data structures.
2) Logical level:
This is the next higher level, which describes what data are stored in
database.
This level also describes the relationship among the data.
The logical level thus describes then entire database in terms of small
number of relatively simple structures. The database administrators use
logical level of abstraction for deciding what information to keep in
database.
3) View level:
This is highest level of abstraction that describes only part of the entire
database.
The view level can provide the access to only part of the database.
This level helps in simplifying the interaction with the system.
The system can provide multiple views of the same system.
3
Clerk at the reservation system, can see only part of the database and
can access the required information of the passenger.
View level
Logical
Level
The three levels of data abstraction
Physical
Level
Data Independence
The ability to modify a scheme definition in one level without affecting a scheme
definition in the next higher level is called data independence.
There are two levels of data independence:
1. Physical data independence is the ability to modify the physical scheme without causing
application programs to be rewritten. Modifications at the physical level are occasionally
necessary in order to improve performance.
2. Logical data independence is the ability to modify the conceptual scheme without
causing application programs to be rewritten. Modifications at the conceptual level are
necessary whenever the logical structure of the database is altered.
Logical data independence is more difficult to achieve than physical data
independence since application programs are heavily dependent on the logical structure of the
data they access.
Instances and schemas
Database change over times as information is inserted and deleted. The collection of
information stored in the database at a particular moment is called an instance of the
database. The overall design of the database is called the database schema.
DATA MODELS
Underlying structure of the database is called as data models.
It is a collection of conceptual tools for describing data, data relationships, data
semantics, and consistency constraints.
Different types of data models are:
Hierarchical model
4
Network model
Object oriented model
Object relational model
Relational model
Entity relationship model
HIERARCHICAL MODEL
The hierarchical data model organizes data in a tree structure.
In this model each entity has only one parent but can have several children.
Only one entity at the top of the hierarchy is called as Root.
The structure is based on the rule that one parent can have many children but children
are allowed only one parent.
Linkages are only possible vertically but not horizontally or diagonally, i.e. there is no
relation between different trees at the same level unless they share the same parent.
Advantages
NETWORK MODEL
The Network Data Model is based on directed graph theory.
The network model replaces the hierarchical tree with a graph thus allowing more
general connections among the nodes.
The main difference of the network model from the hierarchical model is its ability to
handle many-to-many (n: n) relationship
5
Advantages:
Conceptual simplicity
Capability to handle more relationship types
Data independence
Disadvantages:
Detailed structural knowledge is required.
Lack of structural independence.
OBJECT-ORIENTED MODEL
Object DBMSs add database functionality to object programming languages.
The object- oriented model is based on a collection of objects.
An object contains values stored in instance variables within the object.
An object also contains bodies of code that operate in the object these bodies of code
are called methods.
Objects that contain the same types of values and the same methods are grouped
together into classes.
Advantages:
Applications require less code.
Applications use more natural data model.
Code is easier to maintain.
It provides higher performance management of objects and complex
interrelationships between objects.
Object-oriented features improve productivity.
Data access is easy.
OBJECT RELATIONAL MODEL
Object/relational database management systems (ORDBMSs) add new object storage
capabilities to the relational systems at the core of modern information systems.
These new facilities integrate management of traditional fielded data, complex objects
such as time-series and geospatial data and diverse binary media such as audio, video,
images, and applets.
RELATIONAL MODEL
Relation model consists of collection of tables which stores data and also represents
the relationship among the data
Table is also known as relation.
The table contains one or more columns and each column has unique name.
6
Each table contains record of particular type, and each record type defines a fixed
number of fields or attributes.
Advantages:
(i) Structural Independence
(ii) Conceptual Simplicity
(iii) Query Capability
(iv) Easy design, maintenance and
Disadvantages:
(i) Relational model requires powerful hardware and large data storage devices.
(ii) May lead to slower processing time.
(iii) Poorly designed systems lead to poor implementation of database systems.
ENTITY RELATIONSHIP MODEL
As the name suggests the entity relationship model uses collection of basic objects
called entities and relationships.
The entity is a thing or object in the real world.
The entity relationship model is widely used in database design.
Advantages:
i) Simple: It is simple to draw ER diagram when we know entities and relationships.
ii) Easy to understand : The design of ER diagram is very logical and hence they are easy to
design and understand.
iii) Effective: It is effective communication tool.
iv) Integrated: The ER model can be easily integrated with Relational model.
v) Easy conversion: ER model can be converted easily into other type of models.
Disadvantages:
i) Loss of information: While drawing ER model some information can be hidden or lost.
ii) Limited relationships: The ER model can represent limited relationships as compared to
other models.
iii) No Representation for data manipulation: It is not possible to represent data manipulation
in ER model.
iv) No industry standard: There is no industry standard for notations of ER diagram.
8
Naive users are a forms interface, where the user can fill in appropriate fields of
the form. Naive users may also simply read reports generated from the database.
2. Application Programmers
Application programmers are computer professionals who write application
programs.
Rapid application development (RAD) tools are tools that enable an application
programmer to construct forms and reports without writing a program.
Special types of programming languages that combine control structures with
data manipulation language. These languages are sometimes called as fourth-
generation languages.
3. Sophisticated users
Sophisticated users interact with the system without writing programs. Instead,
they form their requests in a database query language. They submit each such
query to a query processor that the storage manager understands.
Online analytical processing (OLAP) tools simplify analyst’s tasks.
Another tool for analysts is data mining tools, which help them to find certain
kinds of patterns in data.
4. Specialized users
Specialized users are sophisticated users who write specialized database
applications that do not fit into the traditional data-processing framework.
The applications are computer-aided design systems, knowledge base and expert
systems, systems that store data with complex data types
Database administrators
A person who has such central control over the system is called a database
administrator (DBA).
The functions of a DBA
• Schema definition. The DBA creates the original database schema by executing a set of
data definition statements in the DDL.
• Storage structure and access-method definition.
• Schema and physical-organization modification. The DBA carries out changes to the
schema and physical organization to reflect the changing needs of the organization.
• Granting of authorization for data access. By granting different types of authorization,
the database administrator can regulate which parts of the database various users can access.
The authorization information is kept in a special system structure that the database system
consults whenever someone attempts to access the data in the system.
• Routine maintenance. Examples of the database administrator’s routine maintenance
activities are:
1. periodically backing up the database
2. Ensuring that enough free disk space
Monitoring jobs running on the database and ensuring that performance is not degraded by
very expensive tasks submitted by some users.
9
INTRODUCTION TO RELATIONAL DATABASES
RELATIONAL MODEL
The relational model represents data in the form of two dimensional tables.
The organization of data into relational tables is known as the logical view of the database.
Software such as Oracle, Microsoft SQL Server, Sybase, are based on the relational
model
Characteristics of Relational Model
The relational model eliminated all parent child relationships and instead represented all
data in the database as simple row/column tables of data values.
A relation is similar to a table with rows/columns of data values.
Each table is an independent entity and there is no physical relationship between tables.
Relational model of data management is based on set theory.
Principle Components of Relational Model
The relational model is consists of following three basic components :
1. Data structure
2. Data integrity
3. Data manipulation
1. The Relational Data Structure
The relational data structure is show through an employee relation.
10
The relatinal Data Structure
Structural terminology summary
Formal relational term Informal Equivalence
Relation Table
Tuple Row or record
Cardinality Number of rows
Attribute Column or field
Degree Number of columns
Primary key Unique identifier
Domain Pool of legal values
2. Relational Integrity
Integrity constraints means when changes made to the database by authorized users
that should not result in a loss of data consistency.
There are two main types of integrity constraints.
Domain constraints
Referential integrity
Domain Constraints
Domain constraints specify the set of values that can be associated with an attribute.
Domain constraints are tested easily by the system whenever a new data item is
entered into the database.
Domain constraints also prohibit use of null values for particular fields.
Referential Integrity
A value that appears in one relation for a given set of attributes also appears for a
certain set of attributes in another relation. This is called referential integrity.
Referential integrity in SQL:
Using SQL primary key, candidate key, and foreign key are defined as part of the
create table statement as given below
Other integrity constraints are:
Entity integrity
Enterprise constraints.
Entity Integrity
“In a base relation, no attribute of a primary key can be null”.
11
A primary key is used to identify tuples uniquely. Therefore, primary key should not
be null.
Enterprise Constraints
These are additional rules specified by the uses or database administrators of a
database.
3. Data Manipulation
The manipulate part of the relational algebra model consists of a set of operators
known collectively as the relational algebra.
Advantages of relational Model
Structural independence:
Conceptual simplicity:
Design, implementation , maintenance and usage ease:
Good for ad hoc requests
It is simpler to navigate
Greater flexibility.
Disadvantages of relational model
Significant hardware and software overheads.
Not as good for transaction process modeling as hierarchical and network
models.
May have slower processing tomes than hierarchical and network models.
KEYS
A key allows us to identify a set of attributes and thus distinguishes entities from each
other.
Keys also help uniquely identify relationships, and thus distinguish relationships from
each other.
12
The candidate key selected to uniquely identify all rows Cannot contain
Primary Key null values
Example: Roll_No is a primary set of ‘student’ entity set.
RELATIONAL ALGEBRA
The relational algebra is a procedural query language.
It consists of a set of operations that take one or two relations as input and produce a
new relation as their result.
Formal Definition
A basic expression in the relational algebra consists of either one of the following:
A relation in the database
A constant relation
Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra
expressions:
E1 E2
E1 – E 2
E1 x E 2
p (E1), P is a predicate on attributes in E1
s(E1), S is a list consisting of some of the attributes in E1
x (E1), x is the new name for the result of E1
Operations can be divided into
Basic operations or Fundamental Operations -Select, Project, Union, rename, set
difference & Cartesian product.
Additional operations- operations that can be expressed in terms of basic operations
are called as additional operation-Set intersection, Natural join Division and
Assignment.
Extended operations-Generalized projection, Aggregate operations and Outer join.
Basic operations
The select, project, and rename operations are called unary operations, because they
operate on one relation.
The other three operations operate on pairs of relations and are called as binary
operations.
1. Select
2. Project
3. Union
13
4. Rename
5. Set difference
6. Cartesian product.
1. Select Operation (σ)
The select operation selects tuples that satisfy a given predicate.
Syntax
σ<select condition>(R)
Example 3: Select the tuples for all books whose publishing year is 2000 or price is greater
than 300.
Query 3: σ(year=2000) OR (price>300)(Book)
The output of query 3 is shown below.
Book_Id Title Author Publisher Year Price
B001 DBMS Korth McGraw_Hill 2000 250
B002 Compiler Ulman 2004 350
B003 OOMD Rambaugh 2003 450
B004 PPL Sabista 2000 500
14
Example 4: Select the tuples for all books whose publishing year is 2000 and price is greater
than 300.
Query 3: σ(year=2000) AND (price>300)(Book)
The output of query 4 is shown below.
Book_Id Title Author Publisher Year Price
B004 PPL Sabista 2000 500
15
3. Rename operation (ρ)
In relational algebra, you can rename either the relation or the attributes or both. The
general rename operation can take any of the following forms:
Syntax
I. ρs(new attribute names)( R )
II. ρs( R )
III. ρ(new attribute names)( R )
16
The depositor Relation
Customer_name Account_number
Hayes A-102
Johnson A-101
Johnson A-201
Jones A-217
Lindsay A-222
Smith A-215
Turner A-305
Example: find all customers of the bank who have an account but not a loan .
Πcustomer-name (depositor) − Πcustomer-name (borrower )
The result relation for this query
Customer_name
Johnson
Lindsay
Turner
6. Cartesian-Product Operation(X)
Cartesian product is also known as CROSS PRODUCT or CROSS JOINS.
Cartesian product allows us to combine information from any 2 relation.
Syntax: Relation1 x Relation 2
18
P0003 Pearson
Book_Info
Book_ID Title
B0001 DBMS
B0002 Compiler
The Cartesian product of Publisher_Info and Book_Info is given in fig .
Publisher_Info X Book_Info
Publisher_code Name Book_ID Title
P0001 McGraw_Hill B0001 DBMS
P0002 PHI B0001 DBMS
P0003 Pearson B0001 DBMS
P0001 McGraw_Hill B0002 Compiler
P0002 PHI B0002 Compiler
P0003 Pearson B0002 Compiler
SQL- Fundamentals
INTRODUCTION
“Structured Query Language” is abbreviated as SQL. SQL was designed to be “human-
friendly”.
SQL is easy to read, understand, and learn.
The SQL language may be considered one of the major reasons for the success of relational
databases in the commercial world.
The Structured Query Language is a relational database language.
SQL is referred to as nonprocedural database language.
ADVANTAGES OF SQL
SQL is a high level language that provides a greater degree of abstraction than procedural
languages.
SQL enables the end-users and systems personnel to deal with a number of database
management systems.
SQL specifies what is required and not how it should be done.
SQL is simple and easy to learn.
SQL handles complex situations easily.
All SQL operations are performed at a set level.
Reduced training cost
Enhanced productivity
Application portability - Application portability means applications can be moved from
machine to machine when each machine uses SQL.
Application durability - A standard language tends to remain for a long time, hence there
will be little pressure to rewrite old applications.
Reduced dependence on a single vendor
19
PARTS OF SQL
The SQL language has several parts:
Data-definition language (DDL). The SQL DDL provides commands for defining
relation schemas, deleting relations, and modifying relation schemas.
Interactive data-manipulation language (DML). The SQL DML includes a query
language based on both the relational algebra and the tuple relational calculus. It also
includes commands to insert tuples into, delete tuples from, and modify tuples in the
database.
View definition. The SQL DDL includes commands for defining views.
Transaction control. SQL includes commands for specifying the beginning and ending
of transactions.
Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL
statements can be embedded within general-purpose programming languages, such as C,
C++, Java, PL/I, COBOL, Pascal, and FORTRAN.
Integrity. The SQL DDL includes commands for specifying integrity constraints that the
data stored in the database must satisfy. Updates that violate integrity constraints are
disallowed.
Authorization. The SQL DDL includes commands for specifying access rights to
relations and views.
DOMAIN TYPES IN SQL
1. Char (n):
The CHAR data type specifies a fixed-length character string. The syntax of CHAR data
type is:
CHAR (n) – Fixed length character data, “n” characters long.
“n” specifies the character length. If a value shorter than the column length is inserted
then the input is padded with space to the column length. If a value greater than column
length is inserted then an error is returned.
2. varchar(n):
Variable length character strings, with user-specified maximum length n. The syntax of
varchar data type is:
VARCHAR2 (n) – Variable length character of “n” length.
“n” specifies the character length.
3. int: Integer (a finite subset of the integers that is machine-dependent).
4. Smallint: Small integer (a machine-dependent subset of the integer domain type).
5. numeric (p,d): fixed point number, where “p” is the total number of digits and “q” is the
number of digits to the right of decimal point.
6. Real, double precision: Floating point and double-precision floating point numbers, with
machine-dependent precision.
7. float (n): Floating point number, with user-specified precision of at least n digits.
8. Date: Dates, containing a (4 digit) year, month and date
Example: date ‘2005-7-27’
9. Time: Time of day, in hours, minutes and seconds.
20
Example: time ‘09:00:30’ time ‘09:00:30.75’
10. Timestamp: date plus time of day
Example: timestamp ‘2005-7-27 09:00:30.75’
11. Interval: period of time
Example: interval ‘1’ day
TERMINOLOGY
A table is the basic storage unit of DBMS. The table stores the necessary data. The common
terminologies used in relational databases are as follows
1. A row or tuple represents all the data required for a particular entity. Each row in a table
is identified using primary key.
2. A column or attribute represents each column in the table. Each column represents a
property or attribute of an entity. The columns can a key attribute or non key attribute.
3. A field can be intersection of column and a row. There can be only one value in it
DATA DEFINITION LANGUAGE (DDL)
A DDL is a language used to define data structures within a database. It is considered to be the
subset of Structured Query Language. A Data Definition Language has a pre-defined syntax for
describing data.
The various DDL commands are as follows
o CREATE - to create objects in the database
o DESC - describes the structure of the database
o ALTER - alters the structure of the database
o DROP - removes a table from the database
o TRUNCATE - remove all records from a table, including all spaces allocated for the
records are removed
o RENAME - rename an object
The different structures that are created by DDL are Tables, Views, Sequences, Triggers, Indexes,
etc.
DDL Commands
1. CREATE – Command is used for creating tables.
Syntax: create table <table name> (columnname1 data type (size), Columnname 2 data
type(size),.., columnname n data type(size));
Example: create table customer (cust_name varchar2 (15), social_security_no
number(11),cust_street varchar2(7),cust_city varchar2(10));
21
2. DESC – Command is used to view the table structure.
Syntax:
desc <table name>;
Example:
desc customer;
ii) Syntax:
alter table <table name> modify (columnname new data type (size));
Example:
alter table customer modify (social_security_no varchar2 (11));
22
iii) Syntax:
alter table <table name> add (new columnname data type (size));
Example:
alter table customer add (acc_no varchar2(5));
iv) Syntax:
alter table<table name> drop (column name);
Example:
alter table customer drop (acc_no);
23
5. DROP – The DROP command removes a table from the database. All the tables' rows,
indexes and privileges will also be removed. No DML triggers will be fired. The operation
cannot be rolled back. Syntax:
drop table <table name>;
Example:
drop table customer1;
6. TRUNCATE – removes all rows from a table. The operation cannot be rolled back and no
triggers will be fired. TRUCATE is faster.
Syntax:
truncate table <table name>;
Example:
truncate table customer1;
24
1. INSERT – To insert one or more number of Rows
Syntax 1:
insert into <table name> values (List of Data Values);
Example:
insert into departments values(‘10’,’administration’,’300’,’1300’);
Syntax 2:
insert into <table name> (column names) values (list of data values);
Example:
1. insert into departments (department_id,department_name,manager_id,location_id)
values('60','IT','101','1300');
2. insert into departments (department_id,department_name,location_id)
values('30','sales','1111');
Using this insert statement value of available columns can alone be inserted. In our first
example we have inserted values for all the columns in the departments table but in the second
example value for manager_id are not inserted.
25
Insert command using User interaction
Syntax 3:
insert into <Table name> values (&columnname1, &columnname2…);
Example:
insert into departments values
(&department_id,&department_name,&manager_id,&location_id);
While inserting values into the table values inserted should be enclosed in single quotes. For
numerical values it’s optional.
2. SELECT – To display one or more rows
Syntax 1:
select * from <table name>;
Example:
select * from departments;
This query selects all the columns present in the specified table.
26
Syntax 2:
select columnname1, columnname 2 from <table name>;
Example:
select department_id, department_name from departments;
This select query helps us to select and display the required columns from the table. If the
original table contains 10 columns and if the user wants to view only two or three then this type
of select statement can be used. In our example columns department_id and department_name is
selected from the departments table.
Syntax 3:
select * from <table name> where <condition>;
Example:
select * from departments where department_name=’IT’;
27
This type of select statements helps the user to select values from the table based on some
condition given in where clause. In our example we have selected the details of
department_name=’IT’;
3. UPDATE – Used to alter the column values in a table. In the Update statement, WHERE
clause identifies the rows that get affected. If you do not include the WHERE clause, column
values for all the rows get affected.
Syntax:
update <table name> set column_name=new_value where <condition>;
Example:
update departments set manager_id=120 where department_id=60;
In our example the manager_id is affected for the department with department_id=60.
28
4. DELETE – Used to delete one or more rows.
Syntax:
delete from <table name> where <condition>;
Example:
delete from departments where department_name=’IT’;
In out example the entire details of department with department_name=’IT’ will be deleted from
the table departments.
29
Select: The select clause corresponds to projection operation of the relational algebra.
The SELECT statement is used to query the database and retrieve the fields that you
specify. You can select as many fields (column names) as you want, or use the asterisk
symbol "*" to select all fields.
From: The from clause corresponds to the Cartesian product operation of the relational
algebra. It specifies the table names that will be queried to retrieve the desired data.
Where: The where clause corresponds to the selection predicate of the relations that
appear in the form clause. The WHERE clause (optional) specifies which data values or
rows will be returned or displayed, based on the criteria you specify.
4.8.1. General form of SQL query
Example:
Selecting all columns from a relation
Syntax:
Select * from <table name>;
Example:
Select * from departments;
30
All the columns in a table can be displayed by following the SELECT keyword with an
asterisk (*). Instead of using * it can also be displayed by listing all the columns after the
SELECT Keyword.
For example:
Select department_id, department_name, manager_id, location_id form departments;
31
Specific columns of the table can be selected by specifying the column name separated
by commas in the SELECT statement. The order in which the column names are listed in the
query will be the order in which the names will appear in the output. In the example given above
the department_id and department_name are listed from the departments table.
32
Usage of ALL keyword in SELECT statement
The SELECT ALL statement is used to return all the values. In a table, a column may
contain many duplicate values and if you want to list all the values without caring about the
duplicate values in such cases ALL keyword can be used. If SELECT statement will use ALL
keyword by default even if ALL keyword is not specified explicitly. If duplicate is to be
removed then DISTINCT key should be explicitly used in the SELECT statement.
Syntax:
SELECT ALL column_name1, column_name2,… FROM table_name;
Example:
select all location_id from departments;
33
Column Alias Name
Column Alias is nothing but renaming the columns. The SQL provides a mechanism for
renaming both relations and attributes. It uses the as clause using the form:
Old-name as new-name
The keyword ‘as’ is optional. The result of the query is same whether the AS keyword is
used or not. Even if the column heading is given in lowercase it to be in uppercase by default
when the query displays the result. The alias name should immediately follow the column name.
The alias name should be specified in double quotation if it contains spaces or special character
or if it is case sensitive.
Syntax:
SELECT old column_name1 AS new column_name1, old column_name2 AS new
column_name2,……… FROM table_name WHERE [condition];
Example:
select department_id as d_id, department_name as d_name from departments;
The same query can be rewritten as
select department_id “d_id”, department_name “d_name” from departments;
34
String Operation
SQL includes a string-matching operator for comparisons on character strings. The operator
“like” use patterns that are described using two special characters:
The percent sign (%) matches any substring
The underscore (_) matches any character
The percent sign represents zero, one, or multiple characters. The underscore represents a
single number or character. The symbols can be used in combinations.
Syntax:
SELECT FROM table_name WHERE column LIKE 'XXXX%'
or
SELECT FROM table_name WHERE column LIKE '%XXXX%'
or
SELECT FROM table_name WHERE column LIKE 'XXXX_'
or
SELECT FROM table_name WHERE column LIKE '_XXXX'
or
SELECT FROM table_name WHERE column LIKE '_XXXX_'
35
You can combine N number of conditions using AND or OR operators. Here, XXXX
could be any numeric or string value.
Example:
Here are number of examples showing WHERE part having different LIKE clause with
'%' and '_' operators:
Statement Description
WHERE LOCATION_ID Finds any values that start with 200
LIKE '200%'
WHERE LOCATION_ID Finds any values that have 200 in any position
LIKE '%200%'
WHERE LOCATION_ID Finds any values that have 00 in the second and third positions
LIKE '_00%'
Finds any values that start with 2 and are at least 3 characters in
WHERE LOCATION_ID
length
LIKE '2_%_%'
Finds any values in a five-digit number that start with 2 and end
WHERE LOCATION_ID
with 3
LIKE '2_ _ _3'
36
Concatenation Operation
Concatenation operation links columns or character strings to other columns. It is
represented by two vertical bars (||).
Syntax: SELECT column_name1||column_name2 as new column_name from
TABLE_NAME;
Example: select department_id||department_name as id_name from departments;
37
Syntax:
SELECT column_name1||’literal’||column_name2 as new column_name from
TABLE_NAME;
Example:
select department_id||’corresponds to’||department_name as id_name from
departments;
38
Ordering the Display of Tuples
The ORDER BY clause is used in a SELECT statement to sort results either in ascending
or descending order. Oracle sorts query results in ascending order by default.
Syntax:
SELECT column-list FROM table_name [WHERE condition] [ORDER BY
column1, column2, .. columnN] [ASC | DESC];
39
Set Operations
Set operators combine the results of two queries into a single one. Let us consider the
following tables to learn about set operations.
EMPLOYEES TABLE
JOB_HISTORY TABLE
40
Union
The SQL UNION clause/operator is used to combine the results of two or more SELECT
statements without returning any duplicate rows. To use UNION, each SELECT must have the
same number of columns selected, the same number of column expressions, the same data type,
and have them in the same order, but they do not have to be the same length.
Syntax:
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
UNION
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
Example:
select employee_id, job_id from employees union select employee_id, job_id from
job_history;
To know the current and previous job details of all employees the above query is
executed.
41
Union all
The UNION ALL operator is used to combine the results of two SELECT statements
including duplicate rows. The same rules that apply to UNION apply to the UNION ALL
operator. The output is not sorted by default and distinct keyword cannot be used with this
operator.
Syntax:
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
UNION ALL
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
Example:
select employee_id, job_id from employees union all select employee_id, job_id from
job_history;
Intersect
The SQL INTERSECT clause/operator is used to combine two SELECT statements, but
returns rows only from the first SELECT statement that are identical to a row in the second
SELECT statement. This means INTERSECT returns only common rows returned by the two
SELECT statements. Just as with the UNION operator, the same rules apply when using the
INTERSECT operator.
Syntax:
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
INTERSECT
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
Example:
select employee_id, job_id from employees intersect select employee_id, job_id from
job_history;
42
Minus
The SQL EXCEPT clause/operator is used to combine two SELECT statements and
returns rows from the first SELECT statement that are not returned by the second SELECT
statement. This means EXCEPT returns only rows, which are not available in second SELECT
statement. EXCEPT operator follows the same rules as the UNION operator.
Syntax:
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
EXCEPT
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
Example:
select employee_id from employees minus select employee_id from job_history;
43
Where Clause
The WHERE Clause is used to retrieve specific information from a table excluding other
irrelevant data.
For example, when you want to see the information of all employees who work in a
particular department we can remove the irrelevant information of other employees using where
clause. This helps to reduce the processing time. So SQL offers a feature called WHERE clause,
which we can use to restrict the data that is retrieved. The condition you provide in the WHERE
clause filters the rows retrieved from the table and gives you only those rows which you
expected to see. WHERE clause can be used along with SELECT, DELETE and UPDATE
statements.
Syntax:
SELECT column_list FROM table-name WHERE condition;
Example:
select employee_id from employees where department_id=50;
This query filters and displays the employee_id of those employees who belongs to
department with department_id=50.
Character strings and date values can also be used in where clause but should be enclosed in
single quotation marks. Character values are case sensitive and date values are format sensitive.
Example:
select last_name, department_id from employees where last_name=’Whalen’;
44
Comparison Operators and Logical Operators are used in WHERE Clause. Aliases defined
for the columns in the SELECT statement cannot be used in the WHERE clause to set conditions.
Only aliases created for tables can be used to reference the columns in the table. Expressions can
also be used in the WHERE clause of the SELECT statement.
45
Example 2: (Between operator)
select last_name, salary from employees where salary between 2500 and
3000;
Example 3: (Between operator)
select last_name from employees where last_name between ‘King’ and ‘Smith’;
46
Example 4: (IN)
Select employee_id, last_name, salary from employees where manager_id
in(100,101,201);
47
Operators
An operator is a reserved word or a character used primarily in an SQL statement's
WHERE clause to perform operation(s), such as comparisons and arithmetic operations.
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
Arithmetic Operators:
Operator Description
+ Addition - Adds values on either side of the operator
Subtraction - Subtracts right hand operand from left
-
hand operand
Multiplication - Multiplies values on either side of the
*
operator
Division - Divides left hand operand by right hand
/
operand
Modulus - Divides left hand operand by right hand
%
operand and returns remainder
48
SQL Comparison Operators:
Operator Description
Checks if the values of two operands are equal or not, if
=
yes then condition becomes true.
Checks if the values of two operands are equal or not, if
!=
values are not equal then condition becomes true.
Checks if the values of two operands are equal or not, if
<>
values are not equal then condition becomes true.
Checks if the value of left operand is greater than the
>
value of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value
<
of right operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal
>= to the value of right operand, if yes then condition
becomes true.
Checks if the value of left operand is less than or equal to
<= the value of right operand, if yes then condition becomes
true.
Checks if the value of left operand is not less than the
!<
value of right operand, if yes then condition becomes true.
Checks if the value of left operand is not greater than the
!>
value of right operand, if yes then condition becomes true.
SQL Logical Operators:
Operator Description
The ALL operator is used to compare a value to all values
ALL
in another value set.
The AND operator allows the existence of multiple
AND
conditions in an SQL statement's WHERE clause.
The ANY operator is used to compare a value to any
ANY
applicable value in the list according to the condition.
The BETWEEN operator is used to search for values that
BETWEEN are within a set of values, given the minimum value and
the maximum value.
The EXISTS operator is used to search for the presence of
EXISTS
a row in a specified table that meets certain criteria.
The IN operator is used to compare a value to a list of
IN
literal values that have been specified.
49
The LIKE operator is used to compare a value to similar
LIKE
values using wildcard operators.
The NOT operator reverses the meaning of the logical
NOT operator with which it is used. Eg: NOT EXISTS, NOT
BETWEEN, NOT IN, etc. This is a negate operator.
The OR operator is used to combine multiple conditions
OR
in an SQL statement's WHERE clause.
The NULL operator is used to compare a value with a
IS NULL
NULL value.
The UNIQUE operator searches every row of a specified
UNIQUE
table for uniqueness (no duplicates).
Aggregate Function
Aggregate functions perform a calculation on a set of values and return a single value.
Except for COUNT, aggregate functions ignore null values. Aggregate functions are frequently
used with the GROUP BY clause of the SELECT statement. All aggregate functions are
deterministic. This means aggregate functions return the same value any time that they are called
by using a specific set of input values.
These functions operate on the multiset of values of a column of a relation, and return a value
Consider the following employees table
AVG
The AVG() function returns the average value of a numeric column.
Syntax : SELECT AVG(column_name) FROM table_name
Example : Find the average of salary from the employees table
Query : select avg (salary) from employees;
50
SUM
The SUM() function returns the total sum of a numeric column.
Syntax : SELECT SUM(column_name) FROM table_name;
Example : Find the sum of salary from the employees table
Query : select sum (salary) from employees;
MAX
The MAX() function returns the largest value of the selected column.
Syntax : SELECT MAX(column_name) FROM table_name;
Example : Find the Maximum value of account balance from the account table.
Query : select max (salary) from employees;
MIN
The MIN() function returns the smallest value of the selected column.
Syntax : SELECT MIN(column_name) FROM table_name;
51
Example : Find the Minimum value of account balance from the account table.
Query : select min (salary) from employees;
COUNT
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax : SELECT COUNT(column_name) FROM table_name;
Example : Find the number rows in the employees table.
Query : select count (*) from employees;
COUNT*
The COUNT(*) function returns the number of records in a table.
Syntax : SELECT COUNT(*) FROM table_name;
Example : Find the number of rows in the salary column of employees table.
Query : select count (salary) from employees;
Group by clause
52
The SQL GROUP BY clause is used in collaboration with the SELECT statement to
arrange identical data into groups. The GROUP BY clause follows the WHERE clause in a
SELECT statement and precedes the ORDER BY clause.
In group function WHERE clause can be used to exclude rows before dividing them into
groups. GROUP BY clause should include column names and column alias name cannot be
included in the GROUP BY clause. The group by clause should contain all the columns in the
select list expect those used along with the group functions.
Syntax:
SELECT column1, column2 FROM table_name WHERE [ conditions ]
[GROUP BY column1, column2]
[ORDER BY column1, column2]
Example:
Select department_id, AVG(salary) from employees group by department_id;
Having Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions. Having clause is used to filter data based on the group functions. This
is similar to WHERE condition but is used with group functions. Group functions cannot be used
in WHERE Clause but can be used in HAVING clause.
If you want to select the department that has total salary paid for its employees more than
25000, the sql query would be like;
Query:
select department_id, sum(salary) from employees group by department_id having
sum(salary)>25000;
53
Nested Subqueries
A Subquery or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause. A subquery is used to return data that will be used in the
main query as a condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
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 clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.
An ORDER BY cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY 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 with multiple value operators,
such as the IN operator.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery; however, the BETWEEN
operator can be used within the subquery.
Subqueries with the SELECT Statement:
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Example:
Consider the employees table
54
Query:
select * from employees where employee_id in (select employee_id from employees
where salary > 4500);
55
Example:
Consider a table EMPLOYEES1 with two fields employee_id and department_id. Now
to copy the values of employee_id and department_id from EMPLOYEES table into
EMPLOYEES1, following query is executed:
Before executing the query create a table EMPLOYEES1 with CREATE table statement
and then insert the values.
Query: insert into employees1(employee_id, department_id) select employee_id,
department_id from employees;
56
Query: update employees set salary = salary * 0.25 where department_id in (select
department_id from employees where department_id=50);
This query is same as the previous but checks for a condition in the inner query.
Null values
NULL values represent missing unknown data. By default, a table column can hold
NULL values. If a column in a table is optional, we can insert a new record or update an existing
record without adding a value to this column. This means that the field will be saved with a
NULL value.
NULL values are treated differently from other values. NULL is used as a placeholder for
unknown or inapplicable or unavailable or unassigned values. It is not possible to compare
NULL and 0; they are not equivalent.
privilege_name is the access right or privilege granted to the user. Some of the access
rights are ALL, EXECUTE, and SELECT.
object_name is the name of an database object like TABLE, VIEW, STORED PROC and
SEQUENCE.
user_name is the name of the user to whom an access right is being granted.
user_name is the name of the user to whom an access right is being granted.
PUBLIC is used to grant access rights to all users.
ROLES are a set of privileges grouped together.
WITH GRANT OPTION - allows a user to grant access rights to other users.
For Example:
GRANT SELECT ON employee TO user1;
This command grants a SELECT permission on employee table to user1.You
should use the WITH GRANT option carefully because for example if you GRANT
SELECT privilege on employee table to user1 using the WITH GRANT option, then
user1 can GRANT SELECT privilege on employee table to another user, such as user2
etc. Later, if you REVOKE the SELECT privilege on employee from user1, still user2
will have SELECT privilege on employee table.
REVOKE Command:
The REVOKE command removes user access rights or privileges to the database objects.
The Syntax for the REVOKE command is:
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
For Example:
REVOKE SELECT ON employee FROM user1;
This command will REVOKE a SELECT privilege on employee table from user1.
When you REVOKE SELECT privilege on a table from a user, the user will not be able
to SELECT data from that table anymore. However, if the user has received SELECT
privileges on that table from more than one users, he/she can SELECT from that table
58
until everyone who granted the permission revokes it. You cannot REVOKE privileges if
they were not initially granted by you.
Privileges and Roles:
Privileges: Privileges defines the access rights provided to a user on a database object.
There are two types of privileges.
1) System privileges - This allows the user to CREATE, ALTER, or DROP database objects.
2) Object privileges - This allows the user to EXECUTE, SELECT, INSERT, UPDATE, or
DELETE data from database objects to which the privileges apply.
The above rules also apply for ALTER and DROP system privileges.
Few of the object privileges are listed below:
Object Privileges Description
INSERT Allows users to insert rows into a table.
SELECT Allows users to select data from a database object.
UPDATE Allows user to update data in a table.
EXECUTE Allows user to execute a stored procedure or a function.
Roles: Roles are a collection of privileges or access rights. When there are many users in a
database it becomes difficult to grant or revoke privileges to users. Therefore, if you define roles,
you can grant or revoke privileges to users, thereby automatically granting or revoking privileges.
You can either create Roles or use the system roles pre-defined by oracle.
Some of the privileges granted to the system roles are as given below:
For Example: To create a role called "developer" with password as "pwd",the code will be as
follows
CREATE ROLE testing
[IDENTIFIED BY pwd];
It's easier to GRANT or REVOKE privileges to the users through a role rather than
assigning a privilege directly to every user. If a role is identified by a password, then, when you
GRANT or REVOKE privileges to the role, you definitely have to identify it with the password.
60
Transactional control commands are only used with the DML commands INSERT, UPDATE
and DELETE only. They cannot be used while creating tables or dropping them because these
operations are automatically committed in the database.
Following is the example which would delete records from the table having age = 25 and then
COMMIT the changes in the database.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 25;
SQL> COMMIT;
As a result, two rows from the table would be deleted and SELECT statement would produce the
following result:
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
61
Example:
Consider the CUSTOMERS table having the following records:
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
Following is the example, which would delete records from the table having age = 25 and
then ROLLBACK the changes in the database.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 25;
SQL> ROLLBACK;
As a result, delete operation would not impact the table and SELECT statement would produce
the following result:
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
The SAVEPOINT Command:
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a
certain point without rolling back the entire transaction.
The syntax for SAVEPOINT command is as follows:
SAVEPOINT SAVEPOINT_NAME;
This command serves only in the creation of a SAVEPOINT among transactional
statements. The ROLLBACK command is used to undo a group of transactions.
Following is an example where you plan to delete the three different records from the
CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can
ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original state:
Example:
Consider the CUSTOMERS table having the following records:
ID NAME AGE ADDRESS SALARY
62
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
Now that the three deletions have taken place, say you have changed your mind and
decided to ROLLBACK to the SAVEPOINT that you identified as SP2. Because SP2 was
created after the first deletion, the last two deletions are undone:
Notice that only the first deletion took place since you rolled back to SP2:
SQL> SELECT * FROM CUSTOMERS;
63
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
6 rows selected.
Once a SAVEPOINT has been released, you can no longer use the ROLLBACK
command to undo transactions performed since the SAVEPOINT.
64
Features of Embedded SQL
(1) It is easy to use.
(2) It is ANSI/ISO standard programming language.
(3) It requires less coding
(4) The precompiler can optimize execution time by generating stored procedures for the
Embedded SQL statements.
(5) It is identical over different host languages, hence writing applications using different
programming languages is quite easy.
Dynamic SQL
Dynamic SQL is a programming technique which allows to build the SQL statements
dynamically at runtime.
Dynamic SQL statements are not embedded in the source program but stored as strings of
characters that are manipulated during a program's runtime. These SQL statements are
either entered by a programmer or automatically generated by the program.
Dynamic SQL statements also may change from one execution to the next without
manual intervention.
Dynamic SQL facilitates automatic generation and manipulation of program modules for
efficient automated repeating task preparation and performance.
65
Dynamic SQL facilitates the development of powerful applications with the ability to
create database objects for manipulation according to user input.
The simplest way to execute a dynamic SQL statement is with an EXECUTE
IMMEDIATE statement. This statement passes the SQL statement to the DBMS for
compilation and execution.
66