Name
: ______________________________________
Batch
: ______________________________________
Copyright 2008
Second Edition
Email:
info@sqlstar.com
No part of this publication may be reproduced (incl. photocopying) in any way, without prior
agreement and written permission of SQL Star International Ltd., Hyderabad. SQL Star
International Ltd., Hyderabad assumes no responsibility for its use, nor for any infringements
of patents or other rights of third parties which could result.
Table of Contents
Oracle 10g SQL
CHAPTER
No.
CHAPTER TITLE
PAGE
NO
1.
Database concepts
2.
40-49
3.
Data Retrieval
50-78
4.
79-95
5.
6.
129-154
7.
155-163
8.
Joins
164-186
9.
187-201
10.
202-221
11.
Data Manipulation
222-248
12.
Managing Tables
249-293
13.
Managing Constraints
294-318
14.
Views
319-342
15.
343-371
16.
372-380
17.
Security Implementation
381-393
18.
394-413
19.
Regular Expressions
414-422
1-39
96-128
ii
Chapter 1
Database Concepts
Typical Database Architecture
DBMS
Types of DBMS
Normalization
Entity Relationship (E-R) Model
Features of SQL
Objectives
At the end of this chapter, you will be able to:
Understand the basic concepts of a database
Identify the disadvantages with Flat File systems
Identify the advantages of a DBMS
Understand the types of DBMS
Understand the E-R Model
Understand the features of SQL
Describe the Codds Rules
Database Management
The approach of the new millennium has brought with it several challenges, thereby
making it mandatory for companies and organizations to maintain high level of
efficiency in their computing systems, so as to stand the test of time. Databases and
database systems are the essential backbone of any organization.
Database
When one hears the word Database, one thinks of a collection or a store of mutually
related data. The implicit properties of a database are as follows:
A database represents some aspects of the real world and any changes to the
real world should get reflected in the database. For example, a manufacturing
concern keeps details of the products it manufactures and also of the
suppliers it deals with. Any change in the pricing of the product or a change in
the suppliers who supply raw materials must be immediately reflected in the
database.
Now a day, the term database is mostly used in connection with computerized
databases. Therefore, today, a study of database systems involves also a study of the
associated technology employed to store, access and manipulate the computerized
databases.
Traditionally, data accessed through computers has been stored on different storage
media in the form of individual files. Files proved to be quite satisfactory as long as
computerization was limited to a few application areas and the use of computers
restricted to a privileged few. However, as actual users grew in number, especially
with the advent of online time-sharing systems, the file systems gave rise to many
serious problems. The discipline of database systems evolved in response to these
problems. Let us first consider what these problems are so as to understand the
different features of database systems more clearly.
Most of the problems with files arise out of the fact that files are specific to an
application, e.g., a set of files may be designed for the sales analysis system of a
company.
Programs of the same application system can use these files. However, if some
other new application needs source data from this system, there may be difficulties.
Therefore, in many cases, new files, with considerable data in common with the
existing files, may have to be designed for the new applications.
Therefore, as applications proliferate, the total number of computerized files grows
considerably. Also, as the number of actual users of the computer grows, the
number of applications increases, in turn resulting in an increase in the number of
files.
A large number of files give rise to the following problems:
Redundancy in data often results in inconsistency of data. The same data item
being used by different applications may exist in different versions. What is
worse, it may exist in different stages of update at different places and thus
may have different values. This may ultimately result in inconsistencies
amongst reports generated by the two application systems.
Individual files are not amendable to rapid changes, especially with respect to
the way the data items are structured within the file. If an application wants
data from an existing file but structured differently, it cannot be provided
quickly and easily. For this purpose, either conversion programs have to be
written or new files have to be created.
Major changes required in the system while modifying files increase the
maintenance load on Data Processing (DP) professionals substantially, thus
making them unavailable for development of new systems.
As has been already stated, Database Systems provide an effective solution to the
above problems. Let us see how.
Data Independence
You have just seen, files give rise to several problems because they are application
specific. Consequently, the applications become data-dependent, that is, they
depend upon the organization and access method for the data on the secondary
storage. This happens because, with conventional application development tools such
as COBOL, the application logic incorporates the knowledge of data organization and
access methods. Therefore, most changes in data organization or access methods
affect the application logic substantially. If problems arising due to this fact are to be
avoided, the data organization (and the access method) and the application logic
have to be made independent of each other. Database Systems do precisely this.
The first step towards this goal is to distinguish between data as is actually stored
(called as the physical representation of data) and data as is presented to an
individual user (called as the logical representation of data).
The Physical Representation of Data
The smallest named unit of data physically stored in the database is known as a
stored field, and a named collection of associated stored fields is known as a
stored or physical record. The named collection of all occurrences of one type of
physical record is known as a stored or a physical file. This concept will be clearer
after we discuss the logical representation of data.
The Logical Representation of Data
A logical field, record or file is, the field, record or file as it appears to the users, that
is, as it is defined in the users application programs. In all traditional systems, the
logical and physical data are practically the same, which is the root cause of all the
major problems with traditional files. This is not the case with database systems.
Similarly, the structure of stored and logical records can be different. A logical
record type may be obtained by selectively combining fields from different stored
records.
The logical and physical views of a file could also be different in terms of, say, the
key fields for sequencing the records in the file.
With such a separation of the logical and physical data, the database can be modified
and developed without affecting existing applications. The database architecture
achieves this separation.
Data Base Management System (DBMS) is software, which interacts with all the
three levels enabling the users to deal with the data in terms of its logical view
rather than the actual physical database.
A diagrammatic representation of the typical database architecture is shown below:
Let us now consider the different individual entities in the architecture in further
detail. Let us start with the actual or physical database and see how the architecture
brings about a transformation in the data representation to get the data to the user
in the form he wants.
The Internal Database
The internal (or physical) database is stored on secondary storage devices, mainly
the magnetic disk. It itself can be conceptually viewed at different levels of
abstraction. At its lowest level, it is stored in the form of bits with the associated
physical addresses on the secondary storage device.
At its highest level, it can be viewed in the form of files and simple data structures. It
is this level that we shall study when we discuss the physical organization for
databases in later chapters.
Standards for representation of data can be laid down and enforced among
different installations. This can significantly reduce the maintenance activity
and enhance data compatibility among installations.
With a centralized control of data, the database system may be designed for
an overall optimal performance from the viewpoint of the entire organization.
Ensuring data security becomes much easier as access to the database can be
controlled through establishment of proper channels and authorization
checks.
Designing scheme
Types of DBMS
The different types of DBMS available are:
Hierarchical DBMS
Hierarchical DBMS (HDBMS) is one of the oldest methods used for organizing and
storing data. In a hierarchical database, data is organized in a pyramid fashion, just
like the branches of a tree extending downwards.
In a hierarchical database you need to understand the concepts of record type and
parent-child relationship. Let us take an example.
In the order processing of any organization, orders are placed for different products.
Orders comprise the following information, which are also called field values:
Order number
Order date
10
Customer number
Product number
Product number
Product type
Product name
Quantity
Price
The collections of these field values are called records and groupings of records of
the same type are called record types. Related records are grouped together so that
you can have higher-level records and lower-level records, similar to how the parents
in a family tree sit above the subordinated children.
In the example, Orders and Products are the two record types and the relationship
between them is the parent-child relationship.
11
However, there are few limitations with HDBMS. The relationship between child
records is not permitted, even if they make a logical sense. Moreover, the
hierarchical design is so rigid that adding a new record requires the redefinition of
the entire database.
Network DBMS
Network DBMS (NDBMS) is an extension of HDBMS. Instead of having a pyramid like
structure, a NDBMS looks more like a spider web or interconnected network of
records. In NDBMS, child records are called members and parent records are called
owners.
In the network data model too, data is stored in records and descriptions of the
structure of a group of records of the same type are stored in record types. Record
types are linked or related using the set types. Set types consist of:
Consider an example of orders and its products. The two record types are Order
and Product, the former being the owner record type and the latter being the
member record type. They are linked using the set type ProdOrder, which
represents the relationship between orders and products in them. The relationship is
of 1:N meaning that there will be many set occurrences (or instances) where one
record from the owner record type Order relates to a set of records from the
member record type Product. That is, an order can have one or more products in it.
Therefore, set occurrences are composed of:
12
In the figure there are three set occurrences of ProdOrder set type, which are
Product1, Product2, Product3.
A record of the member record type cannot exist in more than one set occurrence,
that is, two customers cannot buy the same product.
Similar to HDBMS, NDBMS structure and relationship must be defined in advance.
The number of connections that can be made between records is also limited.
Relational Database
The databases mentioned so far including the file system databases were not very
efficient. In the file system, users cannot simultaneously access data from the same
file, managing the files is very tedious when data stored is complex and accessing
data from files is also very tedious. HDBMS and NDBMS does not allow many-tomany relationships, has complex path to navigate and retrieve data, and the
relationship between data is in the form of pointers.
The objective of developing the relational model was to overcome the shortcomings
of those systems that existed at the end of the 1960s, and to make DBMS products
more appealing to all kinds of users. Dr. E. F. Codd first introduced the relational
model when he proposed the relational model principles in a paper called A
Relational Model of Data for Large Shared Data Banks in June 1970.
A relational database stores data using relations where each relation resembles a
table of values. Each row in a table contains collection of data values. For example, a
university stores information about all its students using tables referred to as a
Student table and a Course table.
Student table contains details about students, such as, their student number, name
and the course code. As different data is stored in the two tables, we may need to
combine them to answer particular questions such as details about students pursuing
course Electronics. An RDBMS enables to relate data in one table to the data in
another. It does so by identifying the relationships, which are represented by
primary key and foreign key. A primary key is assigned to that column, which will
enable you to identify each row of data in a table uniquely. For example, in the
Course table the column containing the course code is the primary key as it helps to
identify each course uniquely. A foreign key is assigned to a column of a table, which
refers to the primary key column in another table. For example, in the Student table
the column containing the course code refers to CourseCode column of the Course
table.
13
RDBMS
Dr. E.F.Codd outlined the principles of the relational model, which formed the basis
for the evolution of the Relational Database Management System.
A Relational Database Management System is defined as a collection of tables
related to each other through common values.
Evolution of RDBMS
Before the acceptance of Codds Relational Model, database management systems
was just an ad hoc collection of data designed to solve a particular type of problem,
later extended to solve more basic purposes. This led to complex systems, which
were difficult to understand, install, maintain and use. These database systems were
plagued with the following problems:
They required large budgets and staffs of people with special skills that were
in short supply.
14
These database systems did not support the implementation of business logic
as a DBMS responsibility.
Hence, the objective of developing a relational model was to address each and every
one of the shortcomings that plagued those systems that existed at the end of the
1960s decade, and make DBMS products more widely appealing to all kinds of users.
The existing relational database management systems offer powerful, yet simple
solutions for a wide variety of commercial and scientific application problems. Almost
every industry uses relational systems to store, update and retrieve data for
operational, transaction, as well as decision support systems.
Tables: Tables are the basic storage structures of a database where data
about something in the real world is stored. It is also called a relation or an
entity.
Data values in columns are of the same kind. This property states that all
values in a given column must come from the same domain.
Oracle10g
Java
E-commerce
C++
15
Visual Basic
Let us name this list as course-names and define a rule that the contents of
CourseName should not only be characters of length less than 15, but must also
contain values that are part of course-names. Here, course-names becomes the
domain and CourseName is in the domain of course-names.
Each row is unique
Each column must have a unique name. As mentioned above, the sequencing
of columns being insignificant, columns must be referred by name and not by
their position.
Normalization
One of the methods of designing a database is normalization of steps.
What do you mean by normalization?
Normalization can be defined as the technique implemented to remove redundancy
from an existing set of tables.
Normalization helps:
Reduce redundancy
Increase integrity
Stabilize design
16
Add a foreign key column to the new table. It should be the same column at
the primary key in the base table.
All information of the borrowed book, such as its author, its publishers
information and the dates of issue and return
17
18
This table can be brought to the 1NF by removing all the repeating columns. The
repeating columns are put into separate tables in logical groups with a primary key
as a unique identifier for each table. The tables in the first normal form are:
1. Identify the non-primary key columns and remove them from the tables
2. Create another table to contain the removed columns, and the primary key column
that they are dependent on.
To bring a table to the 2NF, any of the following rules should be met:
The primary key column should not be a composite key. That is, it should be
made up of only one column.
All the non-primary key columns are dependant only on the primary key.
To bring the tables to 2NF, remove all those columns that are partially dependent on
the whole primary key. For instance the book availability does not depend on the
transaction ID but only on the book ID. So that column can be removed into a new
table.
Hence, the tables in the second normal form will be as follows:
19
The third normal form is defined as the level where the non-primary key columns in the
tables are directly dependent on the primary key and are not dependent on any other nonprimary key. If you remove the transitive dependencies from a table in 2NF, then it is
said that, the non-primary key columns are dependent on the primary key, the whole key
and nothing but the key.
Identify the non-primary key columns that depend on other nonprimary key columns.
2.
Remove the columns arrived at in the first step from the base table.
3.
Create another table with the removed columns and include the
non-primary key column that they are dependent on making it the
primary key.
4.
Create a foreign key in the base table linking it to the primary key in the
new table.
20
To bring the tables to their third normal form remove the non-primary keys that are
dependent on other non-primary keys and also remove the columns that are
calculated or derived columns. The membership fee a non-key column depends on
the age of a member, which is another non-key column. Tables in the third normal
form are:
21
Entities
An entity is a thing, which can be distinctly identified. What is important over here is
that it is individually distinct. Employees, a house, a supplier to an organization, are
all entities. Identification of entities relevant to a database is extremely important,
because, a database contains data about entities.
All entities of the same type form an entity set. All Employees form an entity set
and so do All Suppliers.
Distinguishing Entities
As has been stated, an entity is a distinctly identifiable thing. To distinguish one
entity or one member of an entity set from another, one needs some means of
distinction. This distinction is achieved through values of what is known as the Key
Attribute of the entity.
The database gives information about different entities contained in it. This
information is about different properties of each entity. For example, a personal
database contains information about employees (here employee is the entity). The
properties of an employee about which information is given, may typically be,
employee number, name, date of birth, date of joining the organization, total job
experience and educational qualifications. Each of these properties is called an
attribute of the entity.
Each of the attributes can take on a value from a range of possible values, which is
known as the domain of values for that attribute. The values need not be only
numerical but may also be character strings.
For each entity in an entity set, there is an attribute, which distinguishes the entity
uniquely from the other members of the set. This attribute is the key attribute for
the entity-set. For example, the employee entity-set discussed about can have either
the employee number or the employee name (assuming no two names are identical)
as the key attribute. In some cases, a combination of more than one attribute may
act as a key, instead of just one key attribute. Some times, an arbitrary serial
number may have to be supplied as the key attribute.
There are special cases where the members of an entity-set can be distinguished
from one another only by their relationship to entities of another type. The is a
relationship is one such relationship. These cases will be illustrated when we take up
an actual example of an E-R model later.
Relationships
In a real-world situation, certain entities are associated with each other. For
example, referring to the employee entity-set will have a relationship with the
department entity-set signifying the department for which an employee works. This
relationship can be expressed as the works-for relationship from employee to
department.
A relationship may exist amongst the members of the same entity-set. For example,
in the employee entity-set, some employees may be seniors of certain other
employees. Therefore a relationship senior-of may exist between one member of
the employee entity-set and a few other members of the same set.
22
Types of Relationship
Relationships between the entity-sets can be classified according to how many
members of one entity-sets are related to one member of the other entity-set and
vice-versa. Accordingly, the following types of relationships can be identified.
One-to-One Relationships
If a one-to-one relationship exists between two entity-sets A and B, it means that
with each entity in A, at most one entity in B is associated and vice-versa. Strict
one-to-one relationships are not very common in real world situations. Consider an
entity set of traffic policemen and another entity-set of traffic control points in the
city and assume that only one policeman controls one traffic control point. Under this
assumption, the relationship CONTROLS between the entity sets POLICEMEN and
CONTROL-SITES will be a one-to-one relationship.
Many-to-one Relationships
This type of relationship is quite common in everyday life. If, with each entity in the
entity-set B, zero or more entities in set A are associated, then we say that a
SQL Star International Ltd.
23
Determine the type of each of the relationships between the entity sets
Consider the situation in which the academic administration often finds itself. It has
to handle data regarding students, faculty, and the courses being offered, and
possible areas of specialization. It is worthwhile to consider the following points
before you move to construct the model:
From the above, the desirable entity-sets, their relevant attributes and the
appropriate key attributes can be determined.
1.
Entity-set : Students
Attributes : Roll No, Name, Date-of-birth, Basic Educational
Qualification, No. of years of work experience
24
Key
: Roll No.
2.
Entity-set : Faculty-members
Attributes : Name, Date-of-birth, Date of Joining, Educational
Qualifications, Total No. of years of Teaching Experience
Key
: Name
3.
Entity-set : Courses
Attributes : Course-No, Course-Name
Key
: Course-No
4.
5.
Entity-Set : Doctoral-students
Attributes : None
This entity-set is needed to identify individual doctoral students. The reason for
assigning a separate entity-set to doctoral students is to be able to establish the
relationship between these students and their faculty guides, in the actual database.
Now you are in a position to identify the different relationships amongst the above
entities. These are:
1. SPECIALISES_IN: This is a many-to-many relationship between STUDENTS
and AREAS
2. STUDIES: This is a many-to-many relationship between STUDENTS and
COURSES
3. INCLUDED_IN: This is a many-to-one relationship from COURSES to AREAS
4. BELONGS_TO: This is a many-to-one relationship from FACULTIES to
AREAS
5. GUIDES: This is a
DOCTORAL- STUDENTS
one-to-many
relationship
from
FACULTIES
to
25
As already stated earlier, the E-R model brings out the essential features of data
modeling for databases. Data modeling in practice, is done according to the one of
the three models, viz., hierarchical, network and relational. The three models too,
establish relevant entities and the relationships amongst them; they differ in the way
they represent data about both entities and their relationships. The E-R model,
however, is free from these relatively physical aspects of data representation and
therefore can be said to be a general view of the three models unifying them at a
relatively abstract level.
Case Study Entity Relationship for a Library
The library needs to create a database to automate its data storage and processes.
For this you first need to know how a library functions, what are its transactions and
what data it needs to store for activities. To arrive at an ERD for a library, list the
entities and its attributes you require before putting them into a diagram:
Member
Member ID
Name
26
Address
Area
Zipcode
phone number
Age
Date of membership
Age group
Branch ID
Book
Book ID
Book name
Author name
Publisher
Published Year
Category
Branch
No of copies in store
Transaction
Transaction ID
Member ID
Book ID
Issue date
Return date
Category ID
27
Category description
MemberFee
Minimum value
Maximum value
Fee amount
Branch
Branch ID
Branch name
Address
Location
Phone number
Date of establishment
Availability
Book id
Available copies
Publisher
Publisher Id
Name
Address
The list arrived at above can be depicted as an ERD in the following manner:
28
29
Features of SQL
The features of SQL are as follows:
SQL is a set-based language. Since the relational model uses the concept of a
mathematical relation that is, has its theoretical basis in set theory, SQL
supports set operations and relational algebra operations.
A set is an unordered collection of elements of the same type and structure. Tables
(or relations) are called sets and rows (or records) are their elements. In a relational
database the physical positioning of rows is not important, that is, they are
unordered, so SQL being a set-based language can operate on an entire table, or
multiple tables at once. In contrast, a procedural language can operate on a single
record at a time as it uses files which are similar to tables, wherein records have a
physical ordering.
Relational algebra operations can be divided into two groups:
Set operations from mathematical set theories including Union, Intersection
and Set Difference.
Operations developed for relational databases, which include Select, Project
and Join.
Set Operations
Set operations include the following:
Union
Intersection
SQL Star International Ltd.
30
Set Difference
Union
To understand the union operation look at the following two tables, States1 and States2:
The above two tables contain names of US states. Some of the state names are
common in both the tables.
In order to return the state names that are in States1 or in States2 or in both
States1 and States2, eliminating duplicate rows, you use the UNION operation.
The notation for union of tables is:
States 3
Hawaii
New Jersey
Indiana
Florida
Arizona
Colorado
California
Alaska
Texas
Georgia
Virginia
Wisconsin
Kansas
Kentucky
Iowa
The resulting table contains the state names that are contained in both the tables,
but displaying the common state names only once.
31
Intersection
The intersection operation returns state names that are in both States1 and
States2, that is, it returns a third table that contains only common state names.
The notation for intersection of tables is:
Set Difference
The result of the set difference operation returns state names that occur in the
States1 table but not in the States2 table, or also vice versa. The result is as
follows:
Relational Operations
In addition to set operations, certain other operations were developed for relational
databases, which include:
Selection
Projection
Joins
Selection
The selection operation enables you to retrieve only specific rows that match your
criteria. For example, you have two tables TabA and TabB.
32
Using the selection capability of SQL, you can retrieve those rows from TabB table
where Col1 value is 2.
The result set of rows would be:
Projection
The projection operation enables you to retrieve only specific columns from the total
columns available. For instance, using the projection capability of SQL, you can
retrieve only Col2 values from TabA table.
The result set would be:
Joins
The join operation enables you to retrieve data from more than one table. For
instance, using the joining capability of SQL, retrieve rows from both TabA and
TabB by joining Col1 as they have equivalent values.
The result set of joining the two tables would be:
33
It is an English-like language.
It is non procedural, that is, the user need not specify the method to be
followed to arrive at the result, rather just specify the result that needs to be
arrived at.
Codds Rules
Dr. E. F. Codd, was the creator of the relational data model. It was published as a twopart article in ComputerWorld (Codd, 1985). It contains a list of 12 rules that determine
whether a DBMS is relational and to what extent it is relational. These rules are a very
useful yardstick for evaluating a relational system. In the article, Codd mentions that
according to these rules, there is no database yet that is fully a relational system. He says
that rules 6, 9, 10, 11 and 12 are difficult to satisfy. Each of the twelve rules is briefly
discussed below.
34
Data definition
Data manipulation
Integrity definition
Transaction control
Data control
Accessing data files (files that contain the actual data), through a utility other than
an SQL interface, violates this rule.
35
Integrity Independence
In order to be considered as a relational database, all data integrity, which are
definable in the query language referred to in rule 5 must be stored in the database
as data in tables, that is, in the catalog and not in the application programs.
The data integrity rules that should apply to relational databases are:
Entity integrity- the primary key column cannot have missing values
Referential integrity- for every foreign key column value there must exist a
matching primary key column value.
Integrity rules are covered in depth in a later chapter.
Distribution Independence
In a relational database, data can be stored centrally (on a single system) or
distributed (across a network of systems). The data in a centralized database should
remain logically unaffected if they are distributed across systems. For example, a
user should be able to retrieve data from two tables distributed across two terminals,
the same way, as they would retrieve them if stored in the same terminal.
Non-subversion Rule
This rule states that there should be no other access path to the database, other
than SQL. Any other access language may bypass (or subvert) security or integrity
rules, which otherwise would be obeyed by the regular data access language.
36
Summary
In this chapter, you have learnt that:
Files prove to be inefficient due to their redundant nature when the number of
applications increase. This gave rise to the concept of Database.
Database is a collection of logically related data stored on media.
To handle and maintain this database, a software, Database Management
System (DBMS) came into existence.
They are of 4 types:
1. Hierarchical Database Management System
2. Network Database Management System
3. Relational Database Management System
4. Object Relational Database Management System
Out of these types, Relational Database Management System (RDBMS) was
popularly used which enables to relate data in one table to the data in
another.
Normalization is the process to stabilize a database, by reducing data
redundancy.
Data can be stabilized through 3 stages:
1. First Normal Form (1NF)
2. Second Normal Form (2NF)
3. Third Normal Form (3NF)
To access the stabilized data, Structured Query Language (SQL) is widely
used.
37
Lab Exercise
In the following questions, you are given the Universe of Discourse
(UOD, that is, the general outline at the level a user would normally
describe) of some typical enterprises. Please note that a UOD is not
a smooth and structured description.
Define:
1. LIBRARY SYSTEM:
A library keeps records of current loans of books to borrowers. Each borrower is
identified by a borrower number and each copy of a book by an accession number.
The name and address of each borrower is held so that overdue loan reminders etc.,
can be sent whenever necessary. The information required about books is the title,
authors, publishers, publication date, International Standard Book Number (ISBN),
purchase and price and current price. There is a restriction on the number of books
a borrower may have on loan at any one time, the limit depending on whether a
borrower is classified as having junior or adult status. Books out on loan may be
reserved by other borrowers pending their return. The library stocks only the
hardback version of the book. If a new edition of the book is published, all copies of
earlier editions are withdrawn from stock.
2. A UNIVERSITY:
A University has number of departments. Each department has its faculty.
Departments enroll students.
Each faculty member may conduct one or more courses and one or more students
register for a course. The courses are conducted in classrooms based on a prepared
time-table.
Each subject has a prescribed text book and host of recommended reference books.
A faculty member may guide more than one project and the same project may be
done by more than one student.
3. DEPARTMENTAL STORE:
The data in the database of a department store are defined as follows:
Each employee is represented. The data about an employee are his employee
number, name, address and the department he works for.
Each item sold is represented. The data about items are its name,
manufacturer, price, model number (assigned by the manufacturer) and an
internal item number (assigned by the store).
38
A MANUFACTURING ENTERPRISE:
A manufacturing company consists of a number of divisions. Each division, in turn,
consists of one or more departments. Each employee of the company belongs to
only one department at any point of time. Orders are executed at the divisional level.
Any number of departments (as required) may execute an order. A department may
also execute a number of orders.
Employees work on various orders in the
organization.
An order needs several parts. A part may be needed by several orders. A part itself
may consist of sub parts. Alternatively, a part may be a support of a super part.
Parts are housed in warehouses which are at specific locations. Suppliers supply
parts.
AN AIRLINES ENTERPRISE:
An Airlines company has a fleet of aircrafts which it employs to carry passengers
from one airport to another.
A flight (i.e. an aircraft assigned for trip from one airport to another) is manned by a
crew. A time table is available for all the flights. At each airport, based on the flight
schedules, service schedules are drawn up for the ground staff.
At each airport, for each flight, caterer(s) provide the necessary food and
refreshments according to the catering schedule.
The maintenance of aircrafts (that are not in operation and which have been tagged
for maintenance) is done by the maintenance staff according to the maintenance
schedule. Maintenance is conducted at the airports where hangar(s) are available for
the type of aircraft in question.
39
Chapter 2
Introduction to Oracle10g
Key features of Oracle10g
Components of Oracle10g
40
Objectives
At the end of the chapter, you will be able to:
41
Introduction
The traditional database systems such as relational, hierarchical and network have
been successful in developing the database for traditional business applications such
as banking and railway reservations. But, when it comes to more complex business
applications such as databases for scientific work, engineering designs,
telecommunications, geographic information system and multimedia, the traditional
database systems cannot handle them because the requirements and characteristics
of these applications are different from the traditional business applications. These
applications require more complex structures for:
New data types required for storing complex multimedia data such as audio
and video clips, images and maps.
Infrastructure Advantages
Data Availability
Internet database availability is critical for any eBusiness application. Oracle10g
focuses on the following areas to achieve continuous data availability:
42
Security
Oracle10g provides secure
platform by focusing on:
application
development
and
deployment
A strong three-tier security, which ensures that a user can access the database
only through the middle tier. As a result user identities are maintained securely
through all tiers of an application, with centralized user and privilege management in
Oracle Internet Directory.
Deep data protection through enhancements in Virtual Private Database, fine-grained
auditing and Oracle Label Security
Development Platform for eBusiness Applications
Oracle10g continues to provide the best development platform for both eBusiness
and traditional application development by focusing on the following key area
SQL Star International Ltd.
43
44
Oracle Flashback
Version Query
Changes in the table data can be viewed using VERSION
BETWEEN clause. A
changed transactions undo records stored in the database must be available.
Oracle Flashback Transaction Query
Changes made to the tables can be viewed using
FLASHBACK_TRANSACTION_QUERY view. This can be helpful for auditing,
debugging and even performance analysis.
compile
time
can
be
UTL_MAIL: This is an enhancement to UTL_SMTP package. Using this, Emails can be sent through a PL/SQL API.
UTL_COMPRESS: This package compresses and uncompressed RAW Byte
stream of Large Object (LOB) data.
DBMS_METADATA: This package extracts the metadata of a database
object, which can be obtained either in XML version or DDL.
45
Loss of critical pieces of information if they reside only on a disk drive in the
event of a break down
The need for upgrading the software every time a new version comes into the
market. This raises a lot of questions. Should the release of new versions
force the end users to upgrade their software? Whether to assume that endusers have the necessary skills to upgrade their software? Should the
support people go out to hundreds of PCs and upgrade them individually?
Isnt this a waste of their time when these activities are only for someone who
uses e-mail?
The above complexities of fat client led to the development of a more mature
network known as the Network Computing Architecture (NCA) wherein the end user
would not be required to deal with issues like loss of data due to virus attacks or deal
46
A thin client tier, which is the layer responsible for the presentation of the
information. It is the desktop PC running a web browser.
An application server tier (the middle tier), which is the layer wherein all the
application codes are processed.
A database server tier, which is the layer where all the data is stored.
A Three-tier Architecture
The NCA is designed such that the presentation layer is separated from the
application layer. The application layer resides on a server separate from the
database server and it is assumed that the application server is the fat client. The
application server as well as the database server can be moved to a machine with
more resources, which could be placed in a central location and be managed by
trained professionals thereby keeping the end users away from all the complexities.
Based on the above theoretical concepts, Oracle 10g release is broadly divided into:
47
Spreadsheets
Word documents
PowerPoint presentations
XML
Multimedia data types like MP3, graphics, video and more.
Oracle 10g facilitates two-tiered or multi-layered distributed database environments
like client/server and Web-based applications. Large objects need be stored inside
and outside the database. Oracle Database 10g has services which help to manage
and retrieve data, located outside the database.
Oracle10g continues Oracle Corporations lead on providing the best platform support
for business intelligence by focusing on the challenges raised by large volume of data
and the need for real time complex analysis in an eBusiness environment. Oracle10g
provides the business intelligence platform by enhancing and extending database
support for Online Analytical Processing (OLAP), data mining and extraction.
48
Summary
In this chapter, you have learnt that:
In Oracle10g, g stands for GRID. It is a technology, which groups similar
resources into a cluster. This allows efficient utilization of resources. This helps
us to design complex business applications.
Data availability, security and development for e-business applications are
some of the advantages with Oracle10g.
Oracle10g structure is based on NCA (Networked Computing Architecture),
which is developed to overcome the problems with fat client. It enables
development of applications within networked computing environment.
Oracle10g has come up with new features in SQL* Plus environment, new
commands in SQL language and improvements in PL/SQL, making it more user
friendly.
49
Chapter 3
Data Retrieval
iSQL*Plus Interface
Logging into Database using iSQL*Plus
Projection, Selection and Join Terminology
Differences Between SQL and iSQL*Plus
Review of Syntaxes for Basic SQL SELECT statements
Arithmetic and Concatenation Operators in SQL statements
Alternative Quote Operator
Saving SQL Statements to Script Files
50
Objectives
At the end of this chapter, you will be able to:
51
Retrieving Data
Oracle uses the Structured Query Language (SQL) to communicate with the
database. The SQL statement used to extract data from the database is the SELECT
statement. The SELECT statement informs the database about the data you want to
retrieve. SQL statements need to be executed in an environment where, they would
be interpreted and sent to database.
iSQL*Plus Environment
Executing SQL statements to extract, add, change and remove data from
the database
Storing SQL statements in files (called script files) so that they could be
used later. These files have .sql file extension.
52
53
54
55
After you log in to iSQL*Plus, you see the iSQL*Plus window as follows:
Within the windows browser, the key areas of the iSQL*Plus window are the:
Enter Statements field: The area to enter the SQL statements and iSQL*Plus
commands.
Execute button: Click to execute the statements entered in the Enter
Statements field.
Load Script button: Click to access the area for entering the name and
location of a file (containing the contents of the Enter Statements field, which
has been saved) or the URL for the script you want to execute.
Save Script button: Click to save the contents of the Enter Statements field to
a file. It is suggested that you save these files with .sql extension.
Cancel Button: Click to Cancel the query presently entered by the user. A
confirmation is displayed at the end.
Clear Screen button: Click to clear text from the Enter Statement field, output
and stops any script running at that time.
Help icon: Click to access iSQL*Plus help documentation.
56
iSQL*Plus Commands
iSQL*Plus has a set of commands, which are different from the SQL statements.
These commands need to be entered one line at a time. In order to continue a
command on the next line, end the current line with a hyphen (-).
iSQL*PLUS commands can be abbreviated. For instance, a command CLEAR BUFFER
could be written as CL BUFF. iSQL*Plus commands are not stored in the SQL buffer.
The main categories into which iSQL*Plus commands have been divided are:
File manipulation It saves SQL statements into script files and, loads and
runs them whenever needed.
Interaction It allows you to pass values into the SQL statements and
also print messages to the screen.
SQL Vs iSQL*Plus
SQL is the language used to communicate with the Oracle server from any
tool or application. iSQL*Plus is the Oracle tool, which recognizes and
submits the SQL statements to the Oracle server for execution, and based
on the query result, it produces formatted reports using its own commands.
The following diagram depicts the SQL and iSQL*Plus interaction:
57
Browser
SQL Statements
Oracle
Server
iSQL*Plus
iSQL*Plus Commands
Query Output
Formatted Report
It is an English-like language
58
SQL Statements
The SQL statements can be categorized with regard to their functionality.
They are:
CREATE
ALTER
DROP
RENAME
TRUNCATE
59
INSERT
UPDATE
DELETE
MERGE
COMMIT
ROLLBACK
SAVEPOINT
Data Control Language (DCL): used to secure the database. The DCL
statements are:
GRANT
REVOKE
60
There are two parts that are mandatory in a SELECT statement. They are:
The keyword SELECT (SELECT clause) followed by the columns that you
want to see.
The keyword FROM (FROM clause) followed by the table name from which
the columns have been specified in the SELECT clause.
The syntax of a simple SELECT statement is
SELECT {* | ,(column,)...}
FROM table;
Where,
*(Asterisk) is used to select all the columns from a table
column is used to select those columns specified after the SELECT statement
table specifies the table from which the columns are to retrieved
; is the termination character. The SQL statements are executed immediately on
encountering this character.
Typically enter all keywords in uppercase, tablename in Initcap and all the
words following them, such as column names, in lowercase.
61
62
63
64
46 rows selected.
You can specify multiple columns after the DISTINCT keyword. It would
affect all the columns selected and return a distinct combination of the
columns selected.
For example, to retrieve unique combinations of the year of publish of books
and the book names, issue the following SELECT statement:
SELECT DISTINCT dPublishedYr, cBookName
FROM Book;
All the basic arithmetic operators are available in Oracle, thereby enabling
arithmetic calculations to be performed on the selected data.
List of Arithmetic Operators
65
---------------
--------------
--------------
--------------
66
In the SELECT list, specify the column alias immediately after the column
name using the keyword AS or a space between the column name and
the alias.
Rules of Precedence
If there is more than one operator used in an arithmetic expression, then operator
precedence comes into picture. In operator precedence, multiplication and division
SQL Star International Ltd.
67
take precedence over addition and subtraction. If operators having the same priority
are there within an expression, then they are evaluated from left to right.
Suppose, the New Jersey Central Library wants to increase its total earnings by way
of membership fee. Assuming that at least 10 persons belonging to each grade enroll
as members of the library every year, the proposed earnings would be as follows:
SELECT cGrade grade, nFeeAmount+20 Revised fee/member,
10*12*nFeeAmount+20 Proposed Earnings
FROM MemberFee;
On the issue of the above query, the following result is displayed:
The above statement displays three columns: Grade, Revised fee/member and
Proposed Earnings. This SELECT statement calculates the revised fee to be paid by
each member (nFeeAmount+20) and the proposed earnings for the entire year
assuming each grade has 10 new members enrolled (10*12*nFeeAmount+20). You
will notice that in the expression 10*12*nFeeAmount+20, multiplication is performed
before addition. Since, there are two multiplication operations, they are performed
from left to right.
In order to override the rules of precedence, you can use parentheses. The
expression within the parenthesis would be evaluated first. We shall override the
precedence in the above statement using parentheses as follows:
SELECT cGrade, nFeeAmount+20 Revised fee/member,
10*12*(nFeeAmount+20)Proposed Earnings
FROM MemberFee;
On the issue of the above query, the following result is displayed:
In the above SELECT statement, parentheses have been used to cite the order in
which the operations are to be performed. The SELECT statement calculates the
proposed yearly income by incrementing the fee amount by 20 and then multiplying
the result by 12 (12 months) and 10 (assuming 10 members enroll in each grade).
68
While using arithmetic operations there are certain aspects you must be aware of.
For instance, the role DUAL table plays, in performing arithmetic operations and the
result of an arithmetic operation that involves a null value.
69
The result shows some null values in the proposed fine earnings column. This is
because the nFine column values for some rows are null.
Using the Concatenation Operator
In addition to performing arithmetic calculations or changing column headings, you
can also join two or more columns to further refine (or change) the query result.
Concatenation is the method used to join two or more column outputs into one, to
produce more descriptive output. The resultant column is a character expression. It
is advisable to give an alias to the resulting column. The concatenation operator is (
|| ) two pipe characters put together.
You can join columns to
Other columns
Arithmetic expressions
Constant values
For example, while retrieving details of members from the Member table, it would be
more appropriate to join the cFirstName and the cLastName columns to produce a
single member name column.
SELECT cFirstName||cLastName AS NAME
FROM Member;
On the issue of the above statement, the following result is displayed:
70
21 rows selected.
The above SELECT statement has joined the first name and the last names of
members using the concatenation operator.
What are Literal Character Strings?
A literal character string refers to any character, number or date included in the
SELECT statement, but which is neither a column name nor a column alias. They are
printed for each row returned by the query.
Date and character literals need to be enclosed within single quotation marks ( ) but
not number literals. For example, while retrieving the distinct book names along with
their respective authors from the Book table, concatenate the two columns with a
literal to make each row returned by the query more meaningful.
SELECT DISTINCT (cBookName ||written by
||cAuthorName) AS Book Details
FROM Book;
On the issue of the above statement, the following result is displayed:
49 rows selected.
71
In the example shown, the string contains a quotation mark, which is interpreted as
a delimiter. Using the q operator, the brackets [] are used as the quotation mark
delimiter. The string between the brackets delimiters is interpreted as a literal
character string.
Using Script Files
The statements entered in the Enter Statements field of iSQL*Plus can be saved to
text files (known as script files) as follows:
1. Enter the SQL statement in the Enter Statements field, and click the Save
Script button.
2.
Select Save this file to disk radio button from the file download dialog
box. Specify a file name and save it as shown below.
72
2. Click the Load Script button to load the file contents into the Enter
Statements field of iSQL*Plus.
SQL Star International Ltd.
73
74
CHAR holds a character data of fixed length with a default minimum size 1 and
maximum size 2000.
LONG holds Character data of variable length up to 2 gigabytes.
For example, names of members in the Member table are of CHARACTER datatype.
Number type
The datatype used to store only numeric data is the number datatype, NUMBER
(precision, scale). It can store both fixed and floating-point numbers. By precision we
mean the total number of digits and scale is the number of digits to the right of the
decimal point. For example, 779.69 is number datatype where the precision is 5 and
the scale is 2.
In the Member table, members age is stored as NUMBER datatype.
Datetype
In Oracle, date data are stored in the DATE datatype, which actually contains two
values: the date, and the time between January 1, 4712 BC and December 31, 9999
AD. The default date format in which Oracle stores date is DD-MON-YY where DD is
the day of the month (01), MON is the month (Jan) and YY is the year (two-digit
year-99).
Summary
In this chapter, you have learnt that:
To retrieve data from the database, a SELECT statement is used.
75
Lab Exercise
1.
Begin an iSQL*Plus session using the username and password provided to
you by
your instructor.
2.
Display the structure of the Departments table. Select all the rows from the
Departments table.
3.
Display the structure of the Employees table. Write a query to retrieve the
employee ID, first name, last name, job ID, hire date, department ID, and
salary from the Employees table. Save the query to script file. Name the file
as
LabEx03_03.sql.
4.
Look at the following SELECT statement. Can you identify any coding errors?
SELECT Employee_ID, First_Name, Job_ID
Salary x 0.1 Raised Salary
FROM Employees;
Execute the above statement and observe the error messages displayed.
5.
Write a query to display the unique department numbers and job IDs from
the Employees table.
76
6.
Write a query to display the last name, job ID, salary and annual
compensation of each employee. The annual compensation is to be calculated
by adding a bonus of $500 to monthly salary, and then multiplying the result
by 12. The query should combine the last name and job ID to make a single
column named as Employees. Give an alias Annual Compensation for the
calculated column. The query should display the result as follows:
7.
Copy the contents of LabEx03_03.sql file into the iSQL*Plus Edit window and
make the following modifications:
8.
77
9.
78
Chapter 4
79
Objectives
At the end of this chapter, you will be able to:
Use
Use
ORDER BY Clause
80
Comparison operators
Enclose all character and date values within single quotation marks.
Date values are format sensitive. The Oracle server stores date in
the default format, which is DD-MON-YY. Therefore, ensure that in
the WHERE clause the date values are entered in this default format.
Comparison Operators
As the name suggests, comparison operators are used in the WHERE condition to
compare one expression to another. Following are the comparison operators used:
81
To illustrate the usage of other operators look at the example given below.
For the purpose of preparing a report, which shows the IDs of all those members, who
are above the age of 21, the following SELECT statement is issued:
SELECT cMemberID
FROM Member
WHERE nAge >21;
The result of the above query is:
82
Similarly you can use >=, <, <= and <> operators in the WHERE clause.
Along with these operators there are other comparison operators such as,
The IN operator
BETWEEN Operator
Using the BETWEEN operator, you can extract rows which fall within the range of
values specified. The operator comprises of a lower range and an upper range. The
lower range must be specified first. Both the values specified in the operators are
inclusive.
For example, the following statement is written in order to satisfy a members query
regarding details of all those books published between 1965 and 1980:
SELECT DISTINCT (cBookName)
FROM Book
WHERE dPublishedYr BETWEEN 01-JAN-1965' AND 31-DEC-1980';
IN Operator
When you use the equal to (=) operator, you are equating an expression with a value.
Whereas, in case of the BETWEEN...ANDoperator a range of values is specified. But,
if you need to compare an expression to a list of values (may be the first, third, tenth
and sixteenth values), then you need to use the IN operator. The IN operator specifies
a list of values, which could be of any datatype.
SQL Star International Ltd.
83
LIKE operator
You may not always remember the exact value you are looking for in the database
tables. To overcome this problem, Oracle has provided the LIKE operator wherein you
can retrieve those rows that match the character pattern specified in the operator. The
method of matching expressions to a character pattern is called a wildcard search.
Following are the symbols used to create a character pattern:
% :- Sequence of zero or more characters preceding the character specified before the
symbol. Example,
To extract member names starting with A, use LIKE A% in the WHERE clause.
_ :- Any single character preceding the character specified before the symbol.
For example, to extract those member details who have the second letter in their
names as alphabet a, use LIKE _a% in the WHERE clause.
SELECT cFirstName||cLastName Member Name
FROM Member
WHERE cFirstName LIKE _a%;
SQL Star International Ltd.
84
But suppose you need to have an exact match for the actual symbols % and _, and
then you need to use the ESCAPE option. In this option you need to specify what the
ESCAPE character would be.
For example, suppose one of the branch names of the library that needs to be queried
is Dover_Brnc, then your ESCAPE option would be as follows:
SELECT cBranchID, cBranchName, cBranchLocation
FROM Branch
WHERE cBranchName LIKE %\_% ESCAPE \;
And the result set would be:
CBRANCH
CBRANCHNAME
----------------06DOVNJ
Dover_Brnc
CBRANCHLOCATION
--------------Dover
In this SELECT statement the ESCAPE option specifies the escape character to be
backslash (\). Since the escape character precedes underscore (_), it causes the
Oracle server to interpret the _ literally causing the statement to retrieve details of
branch Dover_Brnc.
IS NULL Operator
Since a NULL value stands for anything that is unavailable or unknown, you cannot use
the (=) operation because null cannot be equal or unequal to any value. So, you have
the
IS NULL operator to test for null values.
For example, the library resolves to bestow certain facilities such as extending the due
dates of all those members who are punctual in returning books. To identify these
members the following SELECT statement is issued:
SELECT cMemberID FROM Transaction
WHERE nFine IS NULL;
Which displays:
SQL Star International Ltd.
85
12 rows selected.
The statement retrieves those members who have not paid any fine amount.
Logical Operators
So far in the WHERE clause you have used only one condition. But, with logical
operators you combine conditions. Two or more conditions are joined together to
produce a single output by using the following logical operators:
AND
OR
NOT
AND Operator
86
If the desired rows are to be selected, then both the conditions specified in the WHERE
clause must necessarily evaluate to TRUE.
For example, to retrieve only those books that are stored in branch 04RANNJ and
which were published in the years ranging from 1965 to 1980, a SELECT statement
using the AND operator needs to be written as shown below.
SELECT cBookName
FROM Book
WHERE dPublishedYr BETWEEN 01-JAN-1965' AND 31-DEC-1980'
AND cBranchID = 04RANNJ;
The query displays the following result:
If a member has not paid fine, it does not necessarily mean that he has returned the
borrowed books on the due dates. This is because the nFine column of a member
whose due date is yet to come will also be null. Hence, to find out those members who
have not defaulted, the following SELECT statement using the NOT operator is to be
written as shown below.
SELECT cMemberID
FROM Transaction
WHERE nFine IS NULL
AND dActualReturnDt IS NOT NULL;
The result of the query is:
9 rows selected.
By using two conditions in the WHERE clause, the above statement returns those
members who have returned the borrowed books on their due dates. The two
conditions are:
To retrieve the IDs of those members who have not paid any fine
and
87
To retrieve the IDs of those members who have returned the borrowed
books.
The above statement uses the logical operator NOT in the WHERE clause as follows:
AND dActualReturnDt IS NOT NULL
The NOT operator specifies that the date on which the member returned his books is
not null (that is, he has returned the books).
The result of joining two expressions using the AND operator is given using the AND
TRUTH table.
OR Operator
In order to select the desired rows, either of the conditions specified in the WHERE
clause can be TRUE.
For example, to retrieve books written either by Nora Roberts or those that belong to
the mystery category, the following statement is issued:
SELECT DISTINCT(cBookName)
FROM Book
WHERE cAuthorName = Nora Roberts
OR cCategoryID = 01MYS;
The result of the query is as follows:
The result of joining two expressions using the OR operator is given below using the
OR TRUTH table.
88
NOT Operator
Operators such as BETWEEN, IN, LIKE and NULL can be used along with the NOT
operator. You have already seen its usage in one of the examples above. Here is
another one.
For example, the library database user issues the following statement in order to
display those books not written by Robert Louis Stevenson and Michael Crichton:
SELECT DISTINCT(cBookName)
FROM Book
WHERE cAuthorName NOT IN (Robert Louis Stevenson, Michael Crichton);
The result of using the NOT operator in a condition is given in the NOT TRUTH table.
Order of Evaluation
The order in which the operators are evaluated are:
89
The first condition mentions that branch ID must be 01ADLNJ and author
must be Charles Dickens.
The second condition mentions that the quantity of books stored must be 2.
But, to retrieve those books written by Charles Dickens and stored in branch
01ADLNJ or those books whose quantity stocked is 2,the above example could be rewritten as below:
As per the rule of precedence, the AND condition has a priority over the OR condition.
You can override this by using parenthesis as shown in the following statement:
SELECT cBookName
FROM Book
WHERE cBranchID = 01ADLNJ
AND (cAuthorName = Charles Dickens
OR nNoOfCopies = 2);
Which gives the result:
90
91
Number data values are fetched with the lowest values first.
Date data values are fetched with the earliest date first. For example,
17-OCT-97 will be displayed before 17-OCT-99.
92
93
Summary
In this chapter, you have learnt that:
Use of WHERE clause in a SELECT statement lets you to retrieve only the
required data and ORDER BY clause arranges the retrieved data in a specific
order.
Lab Exercise
1. Write a query to display details of employees whose job ID is ST_CLERK.
2. Write a query to display last name, job ID, department number and salary of
employees whose salary is greater than $14,000.
3. Write a query to display last name, department number and hire date of employees
hired between 01-JAN-1995 and 31-DEC-1999. Sort the query in descending order
of hire date.
4. Write a query to display the employee ID, first name and manager ID of employees
whose manager ID is 100, 108, 148 or 120.
5. Write a query to retrieve the last name and hire date of employees hired in 1997.
6.
Write a query to retrieve the employee ID, last name and job ID of employees
whose job ID contains the string SH_.
7. Write a query to retrieve the first name, job ID and Department number of
employees who do not have a manager.
8. Write a query to retrieve the employee ID, last name, salary and commission of all
employees who earn a commission. Sort the query result in descending order of
salary.
9. Write a query to retrieve the last name, department number, job ID and salary of
employees if, the job ID is SA_MAN or SA_REP, and if the employee earns a
salary more than $10,000. The query result should be as shown below:
94
8 rows selected.
10.
Write a query to retrieve the last name, job ID, department number and salary
for employees whose department ID is 50 or whose job ID contains the string MAN.
The query result should be as shown below:
11.
Write a query to display the details of employee whose last name is ernst.
95
Chapter 5
96
Objectives
At the end of this chapter, you will be able to:
Write queries requiring user input
Format the output using the various iSQL*PLUS commands
Create script files to run reports
97
On encountering this statement, the Oracle server returns the data that corresponds
to the cMemberID value specified in the WHERE clause.
On running the above query again, the Oracle server returns the same data because
its range is predetermined by the fixed WHERE clause of the query. This query will
not hold good if the librarian wants to retrieve address details of any other member.
Therefore, the query issued is not interactive. It could be made interactive by
enabling the librarian to enter values that determine the range of data to be
returned, at runtime. Oracle servers SQL processing mechanism contains features
that enable users to specify their own values to restrict the range of data to be
retrieved at runtime. This is achieved by using iSQL*Plus substitution variables (also
known as runtime variables).
Substitution Variables
Several times when developers execute a query, they do not know what value the
user may want to use to restrict the range of data to be returned. Therefore, they
hard code the value into the statement. However, it is cumbersome when the same
query is issued each time with a different value to restrict the range of data to be
retrieved.
For instance, when the librarian extracts the address details of a member with ID as
DDG019503, the cMemberID column value is hard coded in the WHERE clause.
However, if he uses the same statement to extract address details of a member with
ID as DCF058502, he would be unable to do so.
To make the statement reusable and flexible, substitute the hard-coded member ID
value with a variable that prompts the user to enter different member ID values each
time the statement is executed. These variables are known as substitution variables.
98
On entering the member ID value, the query displays the name and address for that
member.
SQL Star International Ltd.
99
Now the SQL statement is a flexible statement, therefore, you can re-execute it by
specifying a different member ID value at run-time.
You can verify the changes made on re-executing the statement by using the SET
VERIFY command. Setting SET VERIFY ON displays the value before and after it is
replaced by the substitution variable. Before a variable is replaced by the userprovided value, the old value is always the variable itself.
Points to remember while using substitution variables:
Enclose substitution variables within single quotes if they are used to store
character or date values. If they are used to store number datatypes, single quotes
are not required.
For example, WHERE cMemberID = &MemID;
Functions such as UPPER and LOWER can be used with substitution variables.
For example, using UPPER(&BkName) indicates that the user need not enter the
book name in uppercase.
You can use substitution variables not only in the WHERE clause but also in
the following clauses:
The ORDER BY clause
Column names in the SELECT clause
Table name in the FROM clause
Use substitution variables in the ORDER BY clause when you want to sort the result
of a query on the Book table according to a column of your choice.
SELECT cBookID,cBookName,&ColumnName
FROM Book
WHERE &Condition
ORDER BY &SortingColumn;
On executing the statement, the book ID, book name and any other column that the
user specifies at runtime into the ColumnName substitution variable, is displayed.
These columns are retrieved by the condition the user specifies into the Condition
substitution variable for their retrieval and the column by which the retrieved data is
to be sorted.
100
101
Many times a user may want to use the same variable value more than once without
being prompted for a value each time.
102
For example, the librarian may want to retrieve book details sorted by the same
column he specifies for the substitution variable. But by using single-ampersand
substitution variable, each time he re-executes the statement, he would be
prompted for the column value with which the data is to be sorted. In order to be
prompted for a value only once, the double ampersand (&&) substitution variable is
used in the following manner:
SELECT cBookID,cBookName,&ColName
FROM Book
ORDER BY &&Column;
Re-executing the above statement retrieves book Ids, book names and any other
column that the librarian specifies to be placed in the single ampersand ColName
substitution variable. But each time, the retrieved columns are sorted according to
the same column he specifies in the double ampersand Column substitution variable.
Yet, entering new values for single ampersand variables or using the same value that
is placed into the double ampersand variable may not always be useful. For example,
the values entered into the ColName can be used only in the statement in which they
have been defined. Once the variable value is used, iSQL*PLUS discards it, thereby,
requiring you to enter a new value each time. Instead of running the statement over
and over again so as to enter new values, you could predefine variables with values
before executing the statements. This is done by using two iSQL*PLUS commands:
DEFINE and ACCEPT.
Predefining variables
The two iSQL*Plus commands used to predefine variables are:
DEFINE
ACCEPT
The DEFINE Command
Values for runtime variables are defined by using the DEFINE command. The
command defines the input value of a variable before executing any statement.
Therefore, unlike substitution variables, any statement can use them, as the value is
not discarded once it is used in that session.
In case the library desk officer wants to make queries about information on books
from the Book table and from the Transaction table, she may do so by using a
substitution variable for accepting the ID of the book.
SELECT cBookName Books
FROM BOOK
WHERE cBookID=&BookID;
103
The disadvantage of this query is that other SELECT statements cannot use the
substitution variable used in the above SELECT statement.
Therefore, the DEFINE command can be used to avoid entering values for variables
each time the statements are run. It assigns a value to a CHAR datatype variable
that gets created.
The DEFINE command is issued with the following syntax:
DEFINE <variable_name> = value
To enhance the above statement, issue the DEFINE command in the Enter Statement
field and click the Execute button.
DEFINE BookID = MYS040000068
To display the values in a variable, use the command with the variable name as
shown.
DEFINE <variable_name>
DEFINE BookID
The above command displays the following result:
DEFINE BOOKID = MYS040000068 (CHAR)
The value you have defined into the BookID variable can now be used in any of the
statements as follows:
SQL Star International Ltd.
104
The variables defined with this command remain throughout the session unless you
undefine them. By un-defining the variables, they can be reused. To undefine the
BookID, give the following command.
UNDEFINE BookID
You can confirm the change made, by issuing the DEFINE command.
DEFINE BookID
Displays:
SP2-0135: symbol bookid is UNDEFINED
ACCEPT Command:
ACCEPT command requests value for a variable from the end
user.
Syntax:
ACCEPT <variable_name> datatype
Example: ACCEPT vage NUMBER
SQL Star International Ltd.
105
session
For example,
SET ECHO ON
This command sets the value of the session environment
variable ECHO to ON.
Later, you can verify the values of all the session environment
variables that are set, using the SHOW command. For instance,
you can check the value of the ECHO variable in the following
manner:
SHOW ECHO
Displays:
echo ON
In order to verify all the system variables that have been set,
use the SHOW ALL command.
SET Command Variables
106
107
108
The syntax for changing the way column data is display is:
COL[UMN] [{column|alias}] [option]]
Where,
column|alias is the column whose data display is to be
formatted
option specifies the way the column data is to be formatted.
The following commands are used to show or to clear the
current column formatting:
HEADING Option
This option sets the heading that appears for a particular
column.
For example, the heading of vAddress column could be
changed to make it more meaningful in the following way:
SQL Star International Ltd.
109
110
111
Description
Example
FORMAT
9999
Format
0999
$99,999
Result
2020acceptable
152550- not
acceptable. It
returns
1525##
789
$59,590
112
L9999
Rs7878
99,999
25,789
9999.99
4562.75
113
TTITLE
and
BTITLE
is
the
REPHEADER
and
You have seen the default behavior of the TTITLE command. This is
sufficient for most of your report generating activities. But, some reports
SQL Star International Ltd.
114
115
AND 31-DEC-2000';
10 rows selected.
116
117
By issuing the BREAK command, the same query result changes. You
will notice that the branch ID 01ADLNJ is printed only in line 1 and
is suppressed in lines 2 to 36, and from line 37 a different branch ID
is printed.
You will notice that the ordering of the query output by book name
hampers the suppression of duplicate branch IDs. This is because
you must always remember to order your query by the same column
on which you have issued the BREAK command.
But this report is quite incomplete without computing the total copies
of books that each branch stores. To do this, you need to just
mention what you want added up by using the COMPUTE command.
You will have to re-format the above query by incorporating the
COMPUTE command as follows:
COMPUTE SUM OF nNoOfCopies ON cBranchID
SELECT cBranchID, cBookName, nNoOfCopies
FROM Book
ORDER BY cBranchID, cBookName;
The output is:
118
All formatting commands remain in effect unless you end the current
iSQL*PLUS session or overwrite them.
After every report, remember to reset the iSQL*PLUS settings to their default
values.
In case, you have assigned an alias for a column, then reference the alias
name instead of the column name.
The entire standard SET commands, as well as, other iSQL*PLUS commands
required for every session are stored in a file known as the login.sql. As soon as you
CONNECT to a user in iSQL*PLUS environment, this file is read and all its commands
are implemented. These settings can be changed during the current session. But
they are in effect only for that session. Once you log out they are lost. Therefore, to
make them permanent add these settings to the login.sql file.
119
120
121
CLEAR COLUMN
TTITLE OFF
BTITLE OFF
CLEAR BREAK
10.
122
SPOOL Command
In SQL*Plus, the output of a script can be directed to a file
using SPOOL command.
Syntax to use SPOOL command is :
SQL> SPOOL <filename>
SQL> SPOOL OFF
Example:
SQL> SPOOL d:\test.txt
SQL> SELECT * FROM Member;
SQL> SPOOL OFF
The output of the above SELECT statement is stored in a text
file called test.
In Oracle10g, new option called APPEND has been added to
SPOOL command. It appends the output of a SPOOL command
to an existing file.
The above code can be re-written using APPEND command as
SQL> SPOOL d:\test.text APPEND
SQL> SELECT * FROM Memberfee;
SQL> SPOOL OFF
In the test.txt file, the output of the above SPOOL command
gets appended.
SET SQLPROMPT
New variables have been introduced to set the prompt in your
SQL*Plus session with this release of 10g.
_DATE : the current date or a pre-defined string.
_PRIVILEGE : level of connection, SYSDBA, SYSOPER or NULL for normal.
_USER : user you are connected to.
Example
123
If you want the column name in the condition to remain same each time you
execute the query, use &&-double ampersand Substitution Variables.
Variable can be pre-defined using DEFINE and ACCEPT command. DEFINE
stores Character value by default. Datatype of a variable can be specified
while using ACCEPT command.
Lab Exercise
1.
Display the Last Name, department and salary of the employees whose
employee id is 105.
124
After Execution:
2.Write a query that prompts the user to specify the condition and the
order by sorting column, using substitution variables.
125
6 rows selected.
3.Display employee details, by prompting the user to enter a column name he/she
wishes to view. The user should be prompted only once for the sorting column.
126
127
Once the script is run and the report generated, ensure that all the
settings are back to the default.
128
Chapter 6
129
Objectives
At the end of this chapter, you will be able to:
Use Character Manipulating Functions
Use Number Manipulating Functions
Use Date Manipulating Functions
Convert One datatype to another
Work with NULL values
130
Introduction
So far you have retrieved, restricted and sorted data from the database. But, the
real importance of the data lies in the way it is presented to the user. This requires
formatting the data. To format data values stored in the database, SQL provides
functions. Functions make your query more powerful.
Function Capabilities
Functions are an important and powerful feature of SQL that enables database users
to format the values stored in the tables. The following tasks are achieved using
functions:
Single-row functions: These functions format only single rows and return
one result for each row.
Single-row Functions
Single-row functions operate on single rows returning one value for every row
fetched by the query. Single-row functions can be used in SELECT, WHERE and
ORDER BY clauses. The syntax for issuing single-row functions is:
<function_name> (column | expr [, arg1, arg2, ])
Where,
function_name is the name of the function.
column is the database column name.
expr is any literal character string or calculated expression.
arg1, arg2 are arguments passed into the function.
Arguments mean:
131
Character functions
Number functions
Date functions
Conversion functions
General functions
Character Functions
Character functions format character data values by accepting them as inputs and
returning either character or number values as output. Character functions are
further divided into two categories, case conversion functions and character
manipulation functions.
Case Conversion Functions
These functions convert the case of the character data depending on the
requirement. The following are the case conversion functions:
Look at the following SELECT statement to identify the output derived using
conversion functions:
SELECT UPPER(cFirstName)||
enrolled as member on ||dMembershipDt Member Details
FROM Member;
The query result is as follows:
132
Function
CONCAT(column1| expr1,
column2| expr2)
Functionality
Joins the first argument (character value)
to the second. It is the same as the
concatenation operator (||) except that
you are limited to using only two
arguments.
133
LENGTH(column| expr)
INSTR(column| expr, m)
LPAD or
string)
RPAD(column|
expr,
n,
REPLACE(text,search_string, character
replacement_string)
TRANSLATE(string1, string_to_replace,
replacement_string)
Display the first and last names as a single name of members whose first name
starts with A. Display the length of the cFirstName column and find out the
numeric position of the vowel e in the first name. Issue the following SELECT
statement to see the output derived:
SELECT CONCAT(cFirstName,cLastName) Name with A,
LENGTH(cFirstName) Len,
INSTR(cFirstName, e) Instr
FROM Member
WHERE SUBSTR(cFirstName,1, 1) = A;
The query result is as follows:
134
This statement displays following data for those members whose first names start
with A:
Members first name joined with their last names
The length of their first names
The numeric position of the alphabet e in their first names.
Number Functions
Number functions accept number values as input and return only numeric values as
output. Following are the types of number functions:
135
To understand the usage of NUMBER functions, use the examples mentioned above
in SELECT statements from the DUAL table.
SELECT ROUND (67.238,2) ROUND,TRUNC (67.238,2) TRUNC
FROM DUAL;
Date Functions
In the Oracle database, dates are stored in an internal numeric format, which
represents the century, year, month, day, hours, minutes and seconds.
The default display for any date is DD-MON-RR. For example, the membership date
of the member Austin is displayed as 12-OCT-90. However, the date is not stored in
the database in this format. There is also time and century information associated
with it. The complete date might be October 12th, 1990 10:30:55 a.m.
Date functions accept date values and return a date or number datatype value.
Following are the date functions:
136
In addition to the functions mentioned above, you have the ROUND and TRUNC
number functions, which can be used with dates too.
ROUND (date, fmt) it rounds the date to the format model specified (month or
year). It rounds the date to the nearest day if the format is omitted. Given below are
some examples:
ROUND (16-FEB-99, MONTH) ->01-MAR-99
ROUND (16-FEB-99, YEAR) ->01-JAN-99
Syntax for the TRUNC function is:
TRUNC (date, fmt)
Examples for the same are:
TRUNC (16-FEB-99, MONTH) ->01-FEB-99
TRUNC (16-FEB-99, YEAR) ->01-JAN-99
After having understood the functionality of the various DATE functions, look at their
usage in the SELECT statement that follows.
Suppose the New Jersey Central Library management requests a complete report,
displaying the following details of all those persons who have been the members of
the library for greater than a year:
Number of months the person has been the member of the library and
137
SYSTIMESTAMP:
SYSTIMESTAMP returns the system date, including fractional seconds and time zone
of the system on which the database resides. The return type is TIMESTAMP WITH
TIME ZONE.
The following example returns the system date.
SELECT SYSTIMESTAMP FROM DUAL;
SYSTIMESTAMP
----------------28-MAR-00 12.38.55.538741 PM -08:00
138
Conversion Functions
In an Oracle database, in addition to Oracle datatypes, other datatypes can also be
defined. In such a case, the Oracle server automatically converts them to Oracle
datatypes.
Similarly, the Oracle Server can convert one Oracle datatype to another Oracle
datatype. These datatype conversions are of two types: implicit conversion by the
Oracle server or explicit conversion by the user.
139
While evaluating expressions in the WHERE clause, the Oracle Server can
automatically convert CHARACTER values to NUMBER values provided the character
string represents a number.
For example, in the following statement the Oracle Server automatically converts the
number of copies entered in the WHERE clause to NUMBER datatype and displays the
desired result:
SELECT cBookName
FROM Book
WHERE nNoOfCopies = 9;
140
TO_CHAR Function
The TO_CHAR function is used to explicitly convert number and date datatype values
into character types. It converts NUMBER and DATE types to VARCHAR2.
The uses of TO_CHAR function are as follows:
To display a number value as a character. The syntax for using the function with
numbers is:
TO_CHAR (number, fmt)
The syntax converts number values to character values using the format fmt
specified. The number formatting models are:
To understand the usage of the different number formats, look at the statement that
follows. The amount entered into the nFine column of the Transaction table is all of
number datatypes. But when you want to print a report displaying the amount the
library earns by way of fine, it would be more appropriate to prefix the amount with
a dollar symbol. This would require you to convert the values of the nFine column to
character datatype.
SQL Star International Ltd.
141
If the number of digits exceeds that provided in the format model, then
pound signs (#) are displayed in its place.
The decimal values are rounded to the number of decimal spaces provided
in the format model.
To convert date from the default format of DD-MON-YY to the format you specify.
The syntax for using TO_CHAR function with dates is:
TO_CHAR (date, fmt)
This syntax converts the DATE data to the format fmt specified. The date format
models are:
Format
CC or SCC
BC or AD
B.C. or
A.D.
YEAR or
SYEAR
YYYY or
SYYYY
YYY or YY
or Y
Q
Description
Century. Using S places a minus sign before BC
BC/AD indicator
BC/AD indicator with periods
MONTH
MON
MM
RM
WW
W
DAY
DDD
DD or D
DY
Year spelled out. For example, the year 2001 spelled as TWO THOUSAND
ONE
Year complete with all the four digits => 2001
The last three, two, or one digits of the year => 001, 01,1
Quarter of year January through March=>1
142
J
TH
SP
SPTH
AM or PM
A.M. or
P.M.
HH or
HH12 or
HH24
MI
SS
Julian day stands for the number of days since 31 December 4713 BC
Placed after any day of the months and are called ordinal number.E.g. for
4TH write DDTH
It is used to spell out a day and is called spelled-out number
E.g: for Four- Write SPDD
Spelled-out ordinal number. E.g for Fourth write DDSPTH
Meridian indicator
Meridian indicator with periods
Hour of the day, or hour (1-12) or (0-23)
Minute (0-59)
Second (0-59)
Any of the valid date formats can be included as the format model.
Use a comma to separate the date value from the format model.
The output is padded with blanks. Use the fm (fill mode) element to
remove the padded blanks.
143
Suppose you enter your date of birth as 20-JAN-79 (where the year of birth is 1979),
then Oracle interprets it as 20th Jan 2079 (according to YY format). This is incorrect.
However, with the RR format, it will be interpreted as:
144
Suppose the user querying the Member table is unaware of the default date format,
then while retrieving the IDs of members enrolled on 13 th March 1989, he/she would
issue the following SELECT statement:
SELECT cMemberID
FROM Member
WHERE
dMembershipDt = March 13,1989;
And encounter the following error message:
ERROR at line 3:
ORA-01861: literal does not match format string
This SELECT statement is erroneous because the date entered in the WHERE clause
is not in its default format. This can be rectified using the TO_DATE function as
follows:
SELECT cMemberID
FROM Member
WHERE dMembershipDt = TO_DATE (March 13,1989, Month dd,YYYY);
The result is now correct as given below:
General Functions
General functions work with any datatype and comprise of the following:
NVL function
NVL2 function
DECODE function
NULLIF
COALESCE
NVL Function
Performing calculations with null values result in null values. This situation can be
overcome using the NVL function.
NVL function converts a null value to a specified value. This function can be used
with any datatype be it character, number, or date. Its syntax is:
145
NVL2 Function
The NVL2 function returns the result based on its examining the first expression. If
the first expression is not null, the function returns the second expression. If the first
expression is null, the function returns the third expression. The syntax of NVL2
function is as follows:
NVL2 (expr1, expr2, expr3)
Where,
expr1 is the expression that may or may not contain null
expr2 is the expression returned in case expr1 is not null
expr3 is the expression returned in case expr1 is null
While retrieving transaction details of members, their fine status could be made
more descriptive, such that if fine is null, the text No fine dues is displayed, and if
fine is not null, the text Defaulter fine due is displayed. To achieve this, the
following SELECT statement is to be written:
SELECT cMemberID, dIssueDt,
dActualReturnDt, nFine,
NVL2(nFine,Defaulter - Fine due,
No fine due) Fine Status
FROM Transaction;
The result of the query is:
146
DECODE Function
The functionality of DECODE is similar to that of IF-THEN-ELSE logic used in most of
the programming languages.
Its syntax is:
DECODE (column / expr, search1, result1
[, search2, result2,,]
[, default])
The DECODE function compares the expr or the column values to each of the search
values and returns the result if the expr is same as the search value. If we omit the
default value, it returns a null value wherever the expr does not match the search
value.
Suppose the library increases the stock of books, but by different quantities for its
branches, the same could be illustrated using the DECODE function as follows:
SELECT cBranchID, nNoOfCopies,
DECODE (cBranchID, 01ADLNJ, nNoOfCopies+1,
02CHSNJ,nNoOfCopies+2, nNoOfCopies) revised copies
FROM Book
WHERE cBranchID IN (01ADLNJ,02CHSNJ);
The result of the query is.
147
70 rows selected.
In the statement, the cBranchID value has been decoded, that is,
In case of all other branches the number of copies is to remain the same.
NULLIF
COALESCE
Simple CASE Expression
The simple CASE expression is similar to the DECODE function. It does not support
comparison operators. Its syntax is as follows:
CASE expr WHEN comparison_expr1 THEN return_expr1
WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr
END
The CASE expression functions as follows:
The Oracle server searches for the first WHENTHEN pair for which expr is equal to
comparison_expr, and returns return_expr.
In case none of the WHENTHEN pairs meet this condition, and if an ELSE clause
exists, then Oracle server returns the else_expr, otherwise it returns null. [The literal
NULL cannot be specified for all return_exprs and else_exprs, and all the expressions
must be of the same datatype]
The Library management decides to revise the member fee structure as follows:
If grade is A then increase the fee by 5%
If grade is B then increase the fee by 10%
If grade is C then increase the fee by 15%
For all other grades there is no increase in fee
The effect of implementing the above changes could be viewed by writing the
following SELECT statement making use of the CASE expression:
SELECT cGrade,nFeeAmount,
CASE cGrade
WHEN A THEN nFeeAmount*1.05
WHEN B THEN nFeeAmount*1.10
WHEN C THEN nFeeAmount*1.15
SQL Star International Ltd.
148
ELSE nFeeAmount
END Revised Fee
FROM MemberFee;
The result of the query is:
149
150
32 rows selected.
COALESCE Function
The COALESCE function is a generalization of the NVL function. However, its
advantage over the NVL function is that it can take multiple alternate values.
The COALESCE function accepts any number of expressions, and returns the first
non-null expression in the list.
If the first expression is not null, the function returns it, or else, it does a coalesce of
the remaining expressions. The syntax of The COALESCE function is:
COALESCE (expr1, expr2, , exprn)
Where,
expr1 is returned if it is not null.
expr2 is returned if expr1 is null and this expression is not null.
exprn is returned if the preceding expressions are null.
The semantics of the COALESCE expression is defined as follows:
Summary
In this chapter, you have learnt that:
151
Date Functions are used to work with Date data and returns
number as well as date output.
Lab Exercise
1.
152
4.
Write a query to display the employee ID, hire date, and salary. The hire date
should appear as (For E.g.) Seventh of June 1994 12:00:00 AM. The salary should
appear as $15,000. Give each column an appropriate label as shown below.
5.
Write a query to display the employee ID, hire date and salary review date.
The salary review date is the first Friday after a year of service. Name the column as
Salary Review Dt.. The salary review dates should appear as Friday, 31 st of July,
2000.
153
6.
Write a query to retrieve the last name and hire date of employees hired
before 1994. The query should return the same result even if the query is run in
1994 or now.
Write a query to retrieve the employee ID, salary, commission, and annual
salary, which is calculated by multiplying salary by 12, and then adding the
commission percentage to the result. Annual salary is to be calculated for all
employees irrespective of whether they earn a commission or not.
7.
8.
Write a query to retrieve the employee ID, job ID and salary of all employees.
The query should calculate increase in salary on the following basis:
If job ID is SA_REP, salary increase is 10%
If job ID is ST_CLERK, salary increase is 15%
If job ID is IT_PROG, salary increase is 20%
For all other job IDs, there is no salary increase.
Perform the above query using CASE expression as well as DECODE function.
154
Chapter 7
155
Objectives
At the end of this chapter, you will be able to:
Use
Group functions
Use
GROUP BY clause
Restrict
Introduction
As we have seen earlier, SQL Functions are broadly categorized into two types,
Single row functions and Multiple row functions. We are familiar with the working of
single row function and its use. Let us now, look into the second category of the
functions-Group functions.
What are these group functions and why do we need them?
To explain the above question, let me relate a scenario. Suppose an organization
wants to calculate the total salary expenditure incurred for a particular month. Since,
the calculation has to be done on a collective data, we need to use Group Functions.
Further, if they need to know what is the salary disbursed in each department, the
table has to be split into groups and then, the Group Functions need to be applied on
it.
So, Group Function performs data operations on sets (or groups) of rows. The set
may be the whole table or table split into groups. It gives one result per group.
Group functions differ from single row functions, in that, they can manipulate several
rows at a time. This helps in obtaining summary details such as averages, sum,
maximum or minimum.
Group Functions
There are different group functions available. Some of them are:
AVG
SUM
MAX
MIN
COUNT
156
AVG Function
This function returns the average value of columns with numeric values. Its syntax
is:
AVG ([distinct| all] x)
It returns the average value of the values of column x, ignoring null values.
Where,
distinct is used in order to consider only non-duplicate values.
all is the keyword that causes the function to consider even duplicate values. This is
the default and hence, need not be mentioned.
For example, to calculate the average fine amount earned by the library, the
following statement is issued:
SELECT AVG(nFine) FINE
FROM Transaction;
Output:
FINE
-------5.475
But this statement ignores null values and calculates the average fine by dividing the
total fine amount by the number of people who have paid the fine. Therefore, use
the NVL function to include null values. Now, notice the difference in the average fine
amount.
SELECT AVG(NVL(nFine, 0)) FINE
FROM Transaction;
Displays:
FINE
-------3.421875
SUM Function
This function returns the total or sum of given values. Its syntax is:
SUM ([distinct|all] x)
Which returns the sum of all the values in column x, ignoring null values.
For example, to calculate the total number of copies of books stored in branch
04RANNJ the following SELECT statement is issued:
SELECT SUM(nNoOfCopies) TOTAL
FROM Book
WHERE cBranchID = 04RANNJ;
Displays:
TOTAL
-------165
MAX and MIN Functions
These functions return the maximum and the minimum values respectively for any
given expression. You can use these functions for any datatype unlike AVG and SUM.
Their syntax is:
SQL Star International Ltd.
157
MAX ([distinct|all] x)
MIN ([distinct|all] x)
Which returns the maximum and the minimum value for column x, ignoring null
values.
For example, in order to determine the senior most and junior most members of the
library based on their age as well as on their membership dates, issue the following
SELECT statement:
SELECT MAX(nAge) Oldest,
MAX(dMembershipDt) JuniorMem,
MIN(nAge) Youngest,
MIN(dMembershipDt) SeniorMem
FROM Member;
Displays:
COUNT Function
There are two formats for COUNT function. They are:
COUNT (*)
COUNT (column_name)
COUNT (*) returns the total number of rows in a table including both the duplicate
rows as well as those rows containing null values in any of the columns.
In case a WHERE clause is included in the SELECT statement, the function returns
the count of only those many rows that satisfy the WHERE condition.
For example, to retrieve the total number of members whose age is above twenty,
issue the following statement:
SELECT COUNT (*)
FROM Member
WHERE nAge > 20;
The query displays the following result:
COUNT(*)
------10
Using of COUNT (*) is not recommended because the processing mechanism is very
slow, as it has to first resolve all column names in the table. This step is unnecessary
when you need to know only the number of rows in a table. Instead, use COUNT (1)
or
COUNT (ROWID). A ROWID is a pseudo-column. It is not a part of the table. Rather,
they are unique numbers generated by the server to identify each row uniquely and
they are stored internally within Oracle.
COUNT (column_name) on the other hand, returns the number of rows in a table,
but excludes rows with null values in the column identified by column_name.
For example, to display the number of members who have paid fine for books issued
on 08-AUG-2000, issue the following statement:
SELECT COUNT (nFine)
FROM Transaction
WHERE dIssueDt = 08-AUG-2000';
Displays:
SQL Star International Ltd.
158
COUNT(NFINE)
-------2
The result gives a total of two rows (instead of three) because one of the members
who had issued books on 08-AUG-2000 did not have to pay fine as they had
returned the books on the due dates.
159
160
161
Summary
In this chapter, you have learnt that:
Group function takes a collective data for its operation and returns a single
value. The function can act upon character, number and date datatypes
respectively.
The functions discussed in this chapter are sum(), avg(), min() , max() and
count(). NULL values are ignored in all these functions except count (*).
To get a group wise resultant, the table is split into groups using GROUP BY
clause. Groups can be based on one or more columns.
Using WHILE clause, one can restrict the number of rows used in the
formation of the group.
Using HAVING clause, one can restrict the groups to be included in the final
resultant.
162
Lab Exercise
1.
Write a query to display the maximum, minimum, sum, and average salaries
for each job type. The query result should be as shown below.
2.
12 rows selected.
3.
Write a query to display the manager ID and the highest paid employee for
that manager. Exclude employees who do not have a manager.
4.
Write a query to display the job ID and total salary for each job that has total
salary exceeding $13000.
163
Chapter 8
Joins
Introduction to Joins
Discuss the Need of Joins
Types of Joins
Oracle Proprietary Join
SQL: 1999 syntax Join
Three way Join with the ON clause
Using Set operators
164
Objectives
At the end of this chapter, you will be able to:
Identify the need for joins
Obtain data from multiple tables using Oracle proprietary
syntax
Obtain data from multiple tables using SQL:1999 syntax
Use Set operators
165
Introduction to Joins
Organizations usually have large databases consisting of many tables. It is not
functionally efficient to have a single table storing all data. In the process of their
business, organizations require to prepare reports by extracting data that may be
from one or more than one table. You can retrieve data from more than one table by
creating joins. Joins are query statements made on more than one table using a
column, which is common to both tables.
Retrieving Data from Multiple Tables
View the report shown below to understand the use of a join:
The report displays names of books and their publishers. The book names are
derived from the Book table, where as the publisher names are derived from the
PublisherDetails table.
In order to join these two tables there must exist a relationship between them. For
instance, Book and PublisherDetails tables in the library database are related
through the column cPublisherID. You can query for the names of the books and
their publishers with a join as this column relates the two tables.
The syntax for creating a join is:
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1= table2.column2;
OR
SELECT t1.column, t2.column
FROM table1 t1, table2 t2
WHERE t1.column1= t2.column2;
Where,
SELECT table1.column, table2.column
SQL Star International Ltd.
166
lists the columns that you want to be displayed along with any of the functions or
column alias.
FROM table1, table2 specifies the table where the data is stored.
WHERE table1.column1= table2.column2 specifies the condition that needs to be
met for the join to be created.
In a table join, the FROM clause contains two or more tables which are the source for
data. The WHERE clause contains comparison operations, which specify how data
from the tables are to be joined.
Guidelines for writing Join conditions
The FROM clause contains names of more than one table each followed by an
alias. This reveals an interesting aspect of Oracle. Not only the columns in the
SELECT statement can have alias, but even the tables in the FROM clause can
have aliases.
The table alias given for a table in the FROM clause must be used for the
table name throughout the SELECT statement.
The table alias is valid only for the current SELECT statement.
Columns having common values in multiple tables may have the same column
names. For instance, the column-containing publisher Ids has the name
cPublisherID in both, the Book as well as the PublisherDetails table. This
makes it easier to identify them as columns with similar data. However, this
causes ambiguity as the Oracle SQL processing mechanism is unable to
resolve the column names (from which table is the column being referred to)
and therefore, would return an error. To avoid ambiguity, precede each
column with the table name or the alias.
You need not precede a column name with its table name or alias if it appears
only in one of the tables specified in the FROM clause. But, by prefixing the
table name or alias you would be able to enhance database access, as the
server would know exactly, in which table the column resides.
For joining n tables, you must have at least (n-1) join conditions. For
instance, to join three tables there must be a minimum of two join conditions.
What is a Cartesian product?
A Cartesian product results in the display of a large number of rows, that is, all
combination of rows, where all rows in the first table are joined to all the rows in the
second table. A cartesian product is generated as a result of:
167
Types of Joins
Tables are joined by including a join condition in the WHERE clause. There are
different ways of joining data.
The types of joins are:
Equijoin
Non-Equijoin
Outer join
Self join
Cross joins
Natural joins
Using clause
168
169
So far you have joined only two tables, but it is also possible to join more than two
tables when the need arises.
For example, if the desk officer wants to display the category name and the publisher
name of books written by Nora Roberts stored in branch 05PRINJ, you would have
to join three tables, namely the Book table, the PublisherDetails table and the
Category table as follows:
SELECT B.cBookName, C.cCategoryName,
P.cPublisherName
FROM Book B, Category C, PublisherDetails P
WHERE B.cCategoryID = C.cCategoryID
AND B.cPublisherID = P.cPublisherID
AND B.cAuthorName = Nora Roberts
AND B.cBranchID = 05PRINJ;
The result of the query is:
Creating Non-Equijoins
A non-equijoin relationship is established when a column in a table does not directly
correspond to any column in the other table. To establish a non-equijoin relationship
you need to use operators other than equal (=), such as <=, >=, or BETWEEN.
For instance, based on the age of a person the library charges the membership fee.
The MemberFee table contains details about the amount of fee payable by people
falling within the range of ages specified. To display the different membership fee
amounts, age must be specified between a range as shown below.
SELECT M.cMemberID, M.cFirstName,
M.dMembershipDt,M.nAge,MF.nFeeAmount
FROM Member M, MemberFee MF
WHERE M.nAge BETWEEN MF.nLowerAge AND
MF.nUpperAge;
The query displays the following result:
170
171
7 rows selected.
From the details displayed in the LibStaff table, to find the immediate superior of
Matt Scummen, you would have to:
Identify Matt Scummen from the cLibStaffName column.
Identify the manager (or superiors) number for Matt Scummen from the
nLibMgrNo column. On identifying, you get the manager number as 104.
Identify the name of the superior having staff number as 104 from the
cLibStaffName column. The staff member having staff number as 104 is Susan
Dudley. Therefore, Susan Dudley is Matt Scummens superior.
To identify Matt Scummens superior, you had to look at the LibStaff table twice.
First, to locate Matt Scummen and his superiors staff number. Second, to locate the
name of the superior having the staff number 104.
Rather than doing this manually, you could derive at the following result using self
join:
Subordinates
and Their Superiors
To join LibStaff table to itself, you would have to issue the following SELECT
statement:
SELECT Subordinate.nLibStaffNo,
Subordinate.cLibStaffName, Subordinate.nLibMgrNo,
Superior.nLibStaffNo, Superior.cLibStaffName
FROM LibStaff Subordinate,LibStaff Superior
WHERE Subordinate.nLibMgrNo=Superior.nLibStaffNo;
In the above self join, LibStaff table has been assigned two aliases, Subordinate
and Superior in the FROM clause. The join condition in the WHERE clause matches
the subordinates manager number with the staff number of the superior. The result
of the self-join is:
172
CROSS JOIN
NATURAL JOIN
Where,
table1.column is the table and column from which the data will be retrieved.
CROSS JOIN retrieves a Cartesian product from the two tables.
SQL Star International Ltd.
173
NATURAL JOIN joins two tables, based on the same column name.
JOIN table USING column_name
performs an equijoin based on the column
name.
JOIN table ON (table1.column_name = table2.column_name).
performs an equijoin based on the condition in the ON clause.
Cross Joins
The CROSS JOIN clause is used to retrieve the Cartesian product of two tables, which
is the cross product of two tables.
For example,
SELECT cBookName, cCategoryName
FROM Book CROSS JOIN Category;
The CROSS JOIN statement retrieves the names of books and categories from the
Book and Category tables. The keyword CROSS JOIN indicates that a Cartesian
product must be retrieved.
Natural Joins
A NATURAL JOIN takes into account all the columns in the two tables that have the
same name and the same datatype. In prior releases, to join two tables, you had to
explicitly specify the columns on which the JOIN was to be based. However, in case
of NATURAL JOINS you need not specify the column names explicitly. A natural join
is performed automatically based on all the columns in two tables that have same
name and same datatype. If you perform a natural join on two tables that have
columns with same names but different datatypes, a NATURAL JOIN syntax error is
returned.
When you perform a natural join on two tables, it selects rows that have equal
values in all the matched columns.
The keyword NATURAL JOIN is used to perform this type of join.
SELECT cTranID, dIssueDt, cBookName, dActualReturnDt
FROM Book NATURAL JOIN Transaction;
In the statement, Transaction table is joined to the Book table taking into account
cBookID column, which exists in both the tables. Had there been more common
columns, NATURAL JOIN would have used them.
The same result can be obtained using equijoin as follows:
SELECT cTranID, dIssueDt, cBookName,dActualReturnDt
FROM Book b, Transaction t
WHERE b.cBookID = t.cBookID;
A Natural Join with WHERE clause
You can also use WHERE clause with a NATURAL JOIN to restrict the result of your
query.
SELECT cTranID, dIssueDt, cBookName, dActualReturnDt
FROM Book
NATURAL JOIN Transaction
WHERE cBookID IN (SFI050001993,NFI040000224, HUM050001003);
In the statement a natural join is created on Book and Transaction tables. The
WHERE clause restricts the output of the query.
174
175
In the code you specify the join condition using the ON clause. The Book and the
Transaction tables are joined based on the column cBookID present in both the
tables.
Using ON Clause with Different Column Names
The ON clause can also be used to join tables using columns with different names. It
is similar to a self-join, wherein you join a table with itself. The following statement
shows the usage of the ON clause to create a join using columns with different
names:
SELECT S.nLibStaffNo StaffNo, S.cLibStaffName StaffName,
S.nLibMgrNo
MgrNo, M.cLibStaffName MgrName
FROM LibStaff S JOIN LibStaff M
ON S.nLibMgrNo = M.nLibStaffNo;
In the statement, the join condition is specified in the ON clause using different
columns.
The query displays the following result:
176
When a join not only returns the result of an inner join (the matched rows), but also
the unmatched rows, it is called an outer join. The unmatched rows are the ones that
are deficient in information and thus, do not match the condition specified in the join
condition. The information deficiency could be in the right side or the left side of the
join condition. Accordingly, the join is called right outer join or left outer join. If the
deficiency is on both sides of the join condition it is called full outer join.
Left Outer Join
Left outer join enables you to retrieve all rows (matched or unmatched) from the
table specified on the left side of join condition.
To retrieve information about the books that are issued as well as the books that are
not issued to any members, you can give a left outer join query as follows:
SELECT T.cMemberID, B.cBookName
FROM Book B
LEFT OUTER JOIN Transaction T
ON (B.cBookID = T.cBookID);
The query retrieves the names of the books that are issued to the members.
However, as there is a left outer join on the Book table, the query also retrieves all
books that are not issued to anybody. Book IDs of such books are however not found
in the Transaction Table.
From the output, you can identify member IDs to whom books have been issued.
The books that are available but have never been issued have no match in the
Transaction table and hence no member ID is retrieved along with it. You could get
the same result on issuing the Oracle proprietary syntax as follows:
SELECT T.cMemberID, B.cBookName
FROM Book B, Transaction T
WHERE B.cBookID= T.cBookID(+);
Right Outer Join
Right outer join enables you to retrieve all rows (matched or unmatched) from the
table specified on the right side of join condition. The following statement specifies
right outer join:
SELECT M.cMemberID, M.cLastName, T.cTranID, T.cBookID, T.dissueDt,
T.dreturnDt
FROM Transaction T
RIGHT OUTER JOIN Member M
ON (T.cMemberID = M.cMemberID);
The query retrieves the member details and transaction details based on the column
cMemberID. The query specifies right outer join, as there may be a deficiency in
the Member table (on the right side). All members may or may not issue a book
from the library; hence there may not be transaction details for each member.
Full Outer Join
In an outer join, there can be a deficiency on both the sides of the join condition. For
instance, some books may not belong to any category. This is the case when you
buy a new set of books for the library and may not put them in any category for
some time. You just make entries into the Book table, thereby assigning a book ID.
On the other hand, a particular category may not have any books. Such situation can
be handled using full outer joins, wherein both columns specified on either side of
the join condition have some deficiency.
SQL Star International Ltd.
177
To retrieve information about all books from the Book table (whether or not a book
belongs to a category) and information about all categories (whether or not it
contains any book), you can use a full outer join in the following manner:
SELECT B.cBookID, B.cBookName, C.cCategoryName
FROM Book B
FULL OUTER JOIN Category C
ON (B.cCategoryID = C.cCategoryID);
You can also give additional conditions with full outer joins
SELECT B.cBookID, B.cBookName, C.cCategoryName
FROM Book B
FULL OUTER JOIN Category C
ON (B.cCategoryID = C.cCategoryID)
AND B.cBookID = CLS010009970;
Mapping Joins
The Oracle proprietary joins can be mapped to SQL:1999 syntax joins as follows:
SET OPERATORS
You queried multiple tables by joining them using a single query statement. You
could also query multiple tables by joining two individual query statements on the
different tables. This is done using any of these operators:
o
o
o
UNION/UNION ALL
INTERSECTION
MINUS
For instance, to place a requisition for books from your supplier, you need the book
list from two tables. You could:
Order for all the books in both the tables a union
Order for only those books, which are there in both the tables an
intersection
Order for only those books, which are there only in one of the tables - minus
Assume that the New Jersey Central Library maintains two tables, BookArchive and
BookProspect. The former containing details of books that have been stored in the
library for quite sometime and the latter storing information of prospective books
(books they intend to buy). The lists of books in the two tables as seen with a simple
select statement is given below.
SELECT *
FROM BookArchive;
CBOOKNAME
SQL Star International Ltd.
178
179
The
The
The
The
The
Comedy of Errors
Fountainhead
Humor of Humor
Magic Faraway Tree
Prodigal Daughter
Minus operator
This operators causes the common values to be subtracted from one of the tables
and display the remaining values.
For instance, to place orders for those books that are contained only in the
BookProspect table and not in BookArchive, the following statement is issued:
SELECT cBookName
FROM BookProspect
MINUS
SELECT cBookName
FROM BookArchive;
The result of the statement is:
CBOOKNAME
--------David Copperfield
For the New Intellectual
Honour Among Thievesl
SQL Star International Ltd.
180
1. The number and the datatypes of the columns should always match with its
corresponding query.
2. The column name in resultant would be based on the outer most SELECT
statement.
Summary
In this session, you have learnt that:
Data present in the different tables can be extracted using the JOIN
functionality in SQL. Cartesian product result when there are no joining clauses in
the Statement.
Data can be obtained from multiple tables using Oracle proprietary syntax as
well as using SQL: 1999 syntax.
181
Lab Exercise
[Note: Questions 1 to 5 are based on Joins using the Oracle syntax. Questions 6 to
10 are based on the SQL 99 Join syntax]
1.
Write a query to retrieve the first name and last name of employees along
with their respective department numbers and department names as shown below.
3.
Write a query to retrieve the first name, last name, salary and job titles of
employees belonging to department numbers 100 and 30 as shown below.
182
12 rows selected.
4.
Write a query to retrieve the names of each employee and their managers.
result should be as shown below.
183
----------------7.
Write a query to retrieve the names of employees and their managers. The
result should be as shown below.
184
185
186
Chapter 9
Understanding Subqueries
Introduction to Subqueries
Types of Subqueries
Guidelines for using Subqueries
Single Row Subqueries using group functions
Identifying illegal statements with Subqueries
Multiple Row Subqueries
Multiple Row Comparison Operator
Impact of Null Value on the Outer Query
187
Objectives
At the end of this chapter, you will be able to:
Identify the need for subqueries
Write subqueries
188
Introduction to Subqueries
A subquery is a SELECT statement embedded within another SELECT statement. To
understand this, look at a scenario where in you need to find the details of those who
became members after Celena Stevenson.
To get the desired result, you would have to frame two queries, one query to find out
the membership date of Celena Stevenson and the other to find out those who
became members after Celena Stevenson. By combining these two queries, i.e by
placing one query inside the other you would be able to arrive at the desired result.
The inner query is known as the subquery where as the outer query is called the
main query. The value returned by the subquery is used as the search value in the
main query.
Subqueries are SELECT statements that are part of a clause of another SELECT
statement. Subqueries can be embedded in any of the following SQL clauses:
FROM clause
WHERE clause
HAVING clause
ORDER BY clause
Uses of a Subquery
A subquery has the following uses:
Values can be provided for the conditions specified in the WHERE, HAVING,
and the START WITH clauses used in the SELECT statements.
SQL Star International Ltd.
189
A set of rows can be defined and directly inserted into the target table of an
INSERT or CREATE TABLE statement.
A set of rows can be defined and included in a CREATE VIEW or SNAPSHOT
statement used for creating views and snapshots .
One or more values can be assigned to existing rows in the UPDATE
statement.
A table can be defined in a subquery and operations can be performed on it
by the outer query defined by the INSERT, UPDATE and DELETE
statements.
Types of Subqueries
The types of subqueries are:
Single-row subqueries :The inner SELECT statement returns only one row.
Multiple-row subqueries:The inner SELECT statement returns more than
one row.
Multiple-column subqueries:The inner SELECT statement returns more
than one column.
Using Subqueries
Before using the different types of subqueries, you must remember the guidelines to
be followed while using them.
SQL Star International Ltd.
190
Single-row Subqueries
In single row subqueries the inner SELECT statement returns only one row. Following
single row comparison operators are used in single row subqueries:
=
>
> =
<
< =
< >
Tifiny Keener, a member of the New Jersey Central Library makes a request to
furnish her with details of all those members who reside in the same area as
hers. But she does not mention the area where she resides. Therefore, to meet
this requirement the desk officer issues the following SELECT statement:
SELECT cFirstName||cLastName Name,cPhone
FROM Member
WHERE cArea = (SELECT cArea
FROM Member
WHERE cFirstName = Tifiny
AND cLastName = Keener);
The query displays the following result:
Since the library desk officer does not know the area where Tifiny resides, he
issues an inner query to retrieve Tifinys area and then uses the result to
execute the outer query.
SQL Star International Ltd.
191
The above example had only one condition in the WHERE clause. You can have
multiple conditions too in the WHERE clause.
For example, if you want to know all books written by the author who wrote the
book, which has the ID PSH030000017, and which was published after the year the
book having the ID NFI040000224 was published, issue the following statement:
SELECT DISTINCT(cBookName),cAuthorName
FROM Book
WHERE cAuthorName = (SELECT cAuthorName
FROM Book
WHERE cBookID = PSH030000017)
AND dPublishedYr > (SELECT dPublishedYr
FROM Book
WHERE cBookID = NFI040000224);
Here the query statement contains three queries: the outer query and two inner
queries. First, the two inner queries are executed returning the respective results:
Paul Harris and 20-JAN-89. Then the outer query is executed using the values
returned by the inner query to satisfy its search condition. So the query is to retrieve
all those books written by Paul Harris and which are published after 20-JAN-89.
192
In this example, the inner query returns the ID of Fiction category from the
Category table. The outer query then is processed using this value to extract the
book details of Hamilton branch from the Book table.
In this example, the inner query uses the MIN function to retrieve the lowest age of
members of the library. The age retrieved is then used as a result for the outer
query.
You can embed subqueries not only in the WHERE clause, but also in the HAVING
clause.
For example, suppose that the library management is interested in knowing about
those books, which are more in stock in the library, than the books written by
Charles Dickens. To fulfill this requirement the desk officer issues the following
statement:
SELECT cAuthorName, COUNT (cBookName)
FROM Book
GROUP BY cAuthorName
HAVING COUNT (cBookName)> (SELECT COUNT (cBookName)
FROM Book
WHERE cAuthorName = Charles Dickens);
SQL Star International Ltd.
193
6 rows selected.
Multiple-row Subqueries
Examine the query below:
SELECT cMemberID, nFine
FROM Transaction
WHERE nFine = (SELECT MAX (nFine)
FROM Transaction
GROUP BY dIssueDt);
The query displays an error in the following line:
WHERE nFine = (SELECT MAX (nFine)
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
This query attempts to retrieve the IDs of those members who have paid a fine
amount that is equivalent to the maximum amount of fine received on books
borrowed on issue dates taken as a group.
However, on executing this statement the Oracle Server displays an error message
saying, single-row subquery returns more than one row. This error is encountered
because in the example, the inner query contains the GROUP BY clause, which
causes more than one row to be returned, that is, one result for each group. The
result of the subquery is used in the WHERE clause of the outer query. But the
WHERE clause uses an equal (=) operator, which is a single-row comparison
operator. A single-row operator does not expect more than one value and hence
generates the error.
194
Therefore, subqueries that return more than one row is called multiple-row
subqueries.
You will have to change single-row comparison operator to multiple-row comparison
operator in order to overcome this error.
Following are the multiple-row operators:
You can modify the example using the multiple-row comparison operator IN as
follows:
SELECT cMemberID,dIssueDt, nFine
FROM Transaction
WHERE nFine IN (SELECT MAX (nFine)
FROM Transaction
GROUP BY dIssueDt);
The query displays the following result set:
ANY Operator
195
The ANY operator is used to compare values to each value that a subquery returns
and is used along with =, !=, > and < operators.
On the request of the library management to retrieve the Ids of those members who
have not borrowed books on 14-APR-01 and have paid a fine amount that is more
than any of the amounts received by way of fines on books issued on 14-APR-01,
the desk officer issues the following statement:
SELECT cMemberID,cBookID,
dIssueDt,nFine
FROM Transaction
WHERE nFine > ANY (SELECT nFine
FROM Transaction
WHERE dIssueDt = 14-APR-01')
AND dIssueDt <> 14-APR-01';
The query displays the following rows:
The outer query then returns those transaction details (other than the transactions of
14-APR-01) where the fine amount is greater than $3.
SQL Star International Ltd.
196
ALL Operator
The ALL operator is used to compare a value to every value that a subquery returns.
It is used along with the operators like > and <.
The desk officer issues the following statement to meet the request made by the
management to retrieve those transaction details wherein members have paid fine
amounts exceeding the average amount of fine received on all the dates the books
were borrowed from the library:
SELECT cMemberID,cBookID,nFine
FROM Transaction
WHERE nFine > ALL (SELECT AVG (nFine)
FROM Transaction
GROUP BY dIssueDt);
no rows selected
This statement compares the fine amount with the average fines received on issue
dates taken as a group.
Executing the subquery below, we get the average of fines.
SELECT dIssueDt,AVG(nFine)
FROM Transaction
GROUP BY dIssueDt;
The query displays the following result set:
197
When you write subqueries and execute them, you need to be very careful about a
few issues. There may not be any technical or syntactical error but the logic is
executed in such a way that will not get the required result.
Impact of Null Value on the Outer Query
To understand the impact of null values on outer query, look at the statement
written below that displays information about a book, which is not published in the
same year as books written by Enid Blyton.
SELECT cBookName,cAuthorName,dPublishedYr
FROM Book
WHERE dPublishedYr NOT IN (SELECT dPublishedYr
FROM Book
WHERE cAuthorName = Enid Blyton);
This query does not return any rows.
In the above example, you are trying to display those books, not published in the
same year books written by Enid Blyton were published. But, you get a statement
saying no rows selected. This is because one of the values returned by the inner
query is null as the year of publish of one of Enid Blytons book (The Magic Faraway
Tree) is unknown.
You already know that when conditions compare a value to a null value, they result
in null. Therefore, if null values are returned by the inner query, do not use the NOT
IN operator as it is equivalent to not all.
But, if null values are returned by the inner query, you can use the IN operator, as it
is equivalent to equal to any.
SELECT DISTINCT(cBookName), cAuthorName, dPublishedYr
FROM Book
WHERE dPublishedYr IN (SELECT dPublishedYr
FROM Book
WHERE cAuthorName = Enid Blyton);
The query displays the following result set:
198
By using the IN operator you are trying to display those books, that were published
in the same year as books written by Enid Blyton were published.
Summary
In this chapter, you have learnt that:
A subquery can return one or more number of values. Based on the return
values, comparison operators are divided into single row comparison operators
(relational operators) and multiple row comparison operators (ALL, ANY and IN).
Null value generated in a subquery nullifies the entire query resulting in no
row selected. It can be handled restricting the NULL values using IS NOT NULL
clause.
199
Lab Exercise
1.
Write a subquery to display the last name and salary of employees in the
samdepartment as Nancy.
6 rows selected.
2.
Write a query to retrieve the employee numbers, last names and salaries of all
Display the last name, department number and salary of all employees whose
location ID is 1400.
200
4.
Write a query to display the last name, department number, and salaries of all
5.
Write a query to display the last names, department ID, and salaries of all
employees whose department ID and salary match the department ID and salary of
all employees who earn a commission.
6 rows selected.
201
Chapter 10
Advanced Subqueries
Multiple column Subqueries
Pairwise and Non-Pairwise Comparison
Subquery in the FROM Clause
Working with Scalar Subqueries
Using Correlated subqueries
EXISTS and NON EXISTS operators
WITH clause
202
Objectives
At the end of this chapter, you will be able to:
Use
WITH clause
203
204
205
In pairwise column comparison, each row returned by the outer query must have
both the category Ids and the number of copies same as that for the books written
by Shakespeare.
206
FROM Book
WHERE cAuthorName= Shakespeare)
AND cAuthorName <> Shakespeare;
207
208
6 rows selected.
This example displays the member IDs, the book IDs, fine amount, average fines
and the issue dates of those members who have paid a fine amount, which is more
than the average fines paid for that issue date.
In the above statement, the Transaction table is given the alias T and the FROM
clause subquery (which returns value from the Transaction table) is given the alias
TR. This is similar to self-join. The WHERE clause compares the issue dates of the
Transaction table with the issue dates returned by the subquery and also the fine
amount is compared with the average fines for all issue dates that are returned by
the subquery.
Scalar Subqueries
A scalar subquery is a subquery that returns exactly one column value from one row.
It is used to specify a scalar value returned from an expression. You cannot qualify
multiple column subqueries as scalar subqueries.
The value of a scalar subquery expression is the number of rows returned by the
SELECT statement in the scalar subquery. The data type of the return value should
match the value being selected in the subquery.
If the number of rows returned by the scalar subquery expression is zero, the value
of the scalar subquery is NULL. If the number of rows returned by the scalar
subquery is more than one, the Oracle server returns an error. This is because, a
scalar subquery must return exactly one column value from one row.
The enhancements made in Oracle9i enable you to use scalar subqueries in all valid
expressions like:
SQL Star International Ltd.
209
It has an outer query that retrieves the IDs and the names of the books stored in
branch 04RANNJ from the Book table. The CASE expression evaluates the category
of the book depending on the result of the inner query. The inner query returns the
category ID of books belonging to the category fiction.
When you execute this scalar query, the inner query is executed first. The Oracle
server looks into each row of the Category table and compares the value of
SQL Star International Ltd.
210
The query is sorted based on the category name, which is retrieved by the scalar
subquery that matches the category ID in the Book table with the category ID in the
Category table.
The query retrieves the following output:
211
212
Correlated Subqueries
Correlated subqueries are also a type of nested subqueries, but execute differently.
In nested subqueries, the inner query executes first and the resulting value is used
by the outer query. However, in correlated subqueries, the execution is the opposite.
The outer query is executed first and the result is used by the inner query. In other
words, the inner query is driven by outer query. Correlated subqueries are used
whenever every row in a table must be processed and evaluated to generate a
result.
The Oracle server performs a correlated subquery whenever a subquery references a
column from a table specified in the outer query (or the parent statement). A
correlated subquery involves row-by-row processing of the table specified in the
parent statement. These rows are also known as candidate rows. It reads every
candidate row and compares the values in each row against a related data.
Following steps take place when a correlated subquery is executed:
1.
2.
3.
4.
The
The
The
The
candidate row is retrieved (from the outer query or the parent statement)
value of the candidate row is used by the inner query
result of the inner query is used to qualify or disqualify the candidate row
above steps are repeated for all candidate rows
213
FROM Member m
WHERE nAge >= (SELECT AVG(nUpperAge)
FROM MemberFee
WHERE cGrade = m.cGrade);
This query uses a correlated subquery where the cGrade column of the table from
outer query is used in the inner query. The inner query calculates the average upper
Age of all members belonging to each grade fetched by the outer query. The inner
query result is used by the outer query. The outer query looks into all records of the
Member table and displays only the records where the age of a member is greater
than or equal to the average upper age of members belonging to a grade. The result
of the query is as follows:
All types of logical operators can be used with nested subqueries. In addition to
these, you can use the EXISTS operator, which is mostly used with correlated
subqueries.
The EXISTS operator tests whether a row value exists in the resultset of inner query.
If the row value is found in the inner query, the condition is flagged as TRUE, and
the search is discontinued. This holds good for the first occurrence of the row value
in the inner query. If the row value is not found in the inner query, the condition is
flagged as FALSE and the search is continued in the inner query.
If you want to find out the Library staff members who have at least one person
reporting to them, you can use the following correlated subquery with an EXISTS
operator:
SELECT nLibStaffNo, cLibStaffName
FROM LibStaff l
WHERE EXISTS (SELECT x
FROM LibStaff
WHERE nLibMgrNo=l.nLibStaffNo);
On executing the query, the following result is obtained:
214
In the query, the nLibStaffNo and the cLibStaffName columns of the LibStaff
table are retrieved from the outer query. In the inner query, the search begins from
the first row of the nLibMgrNo column of the LibSatff table. The value in the
nLibMgrNo is matched with the value in the nLibStaffNo column of the LibStaff
table (of the outer query). If a match is found, the condition becomes TRUE and the
result of the outer query is displayed. The EXISTS operator ensures that the search
does not continue further.
Note that in the inner subquery, a constant x is used in the SELECT statement. This
is done for performance reasons. As the EXISTS operator does not return any
specific value but only checks for the existence of a value in a column, it is advisable
to give a constant in the SELECT statement. This is because, having a column name
in the SELECT statement causes a table scan for that column, which affects the
performance.
Alternatively, you can use the IN construct to get the same result as the EXISTS
operator.
SELECT nLibStaffNo, cLibStaffName
FROM LibStaff l
WHERE nLibStaffNo IN (SELECT nLibMgrNo
FROM LibStaff
WHERE nLibMgrNo IS NOT NULL);
The NOT EXISTS operator works in a manner opposite to the EXISTS operator. It
checks whether a row value selected in the outer query does not exist in the inner
query and flags a TRUE condition. If it exists, it flags FALSE.
You can use the NOT EXISTS operator to find the categories for which the library has
not stored any books.
SELECT cCategoryID, cCategoryName
From Category c
Where NOT EXISTS (SELECT x
SQL Star International Ltd.
215
FROM Book
WHERE
CCategoryID=c.cCategoryID);
The query is a correlated subquery. The outer query selects the category ID and the
category name from the Category table. The inner query selects constant value
from the Category table and compares the category ID with the category ID
selected from the outer query. If the two categories match, a FALSE condition is
flagged (as NOT EXISTS operator is used). If the category ID in the inner query does
not match with the category ID in the outer query, the condition becomes TRUE and
the columns from the outer query are returned. Thus, the above code retrieves all
categories that do not have an entry in the Book table, meaning those categories
that do not have any books. The result of the query is as follows:
Alternatively, you can use NOT IN operator in place of NOT EXISTS operator to get
the same result. This is shown in the following example:
WITH Clause
In a complex query, it is possible that a query block is used more than once or has
many references. Executing the same query block repeatedly is not advisable from
performance standpoint.
The WITH clause also known as subquery_factoring_clause can be used in such
cases. It:
216
Using a WITH clause you can improve the readability and maintainability of SQL
statements. It allows you to separate the business logic from data gathering.
Internally the WITH clause is resolved as an in-line view or a temporary table.
You can use a WITH clause to find out the issue dates on which the total fine
collected is greater than the average fine collected. Following is the code:
WITH TotFinePerIssDt AS (
SELECT dIssueDt, SUM(nfine) fine_tot
FROM Transaction
GROUP BY dIssueDt),
AvgFinePerIssDt AS (
SELECT SUM(fine_tot)/count(*) fine_avg
FROM TotFinePerIssDt)
SELECT *
FROM TotFinePerIssDt
WHERE fine_tot>
(SELECT fine_avg
FROM AvgFinePerIssDt)
ORDER BY dIssueDt;
In the code the WITH clause creates two query blocks TotFinePerIssDt and
AvgFinePerIssDt. The TotFinePerIssDt query block retrieves the total fine
collected on every issue date. The AvgFinePerIssDt query block calculates the
average of the total fine collected on each issue date. To calculate this,
AvgFinePerIssDt query block takes the total fine values from TotFinePerIssDt
query block. Finally, the query retrieves the total fine that is greater than the
average of the total fine collected on each issue date.
Without a WITH clause the query would be very complex, and the use of several
SELECT statements would affect the performance. By using a WITH clause, you can
write a code which is easy to read and maintain. In addition, the results of each
query block is stored in a temporary tablespace and used every time it is referenced,
thereby enhancing the performance.
SQL Star International Ltd.
217
There are some points you should remember while using a WITH clause. A WITH
clause can:
Summary
In this chapter you have learnt that:
218
219
Lab Exercise
1.
Write a query to display the last names, department ID and salaries of all
employees whose department ID and salary match the department ID and
salary of all employees who earn a commission.
----------------
3.
18 rows selected.
Write a query to display details of employees who have one or more
colleagues in their department who joined after them, with higher salaries.
220
4.
221
Chapter 11
Data Manipulation
Various types of INSERT statements
Create UPDATE statements
Generate DELETE statements
Single set Aggregate in DML RETURNING Clause
Transaction command to Save and discard changes
Locking Mechanism
Show how Read consistency works
222
Objectives
223
Manipulating Data
In the previous sessions, the library database users have only extracted the data
stored in the database. For instance, when a member wishes to see a list of all the
books stored in the library, the desk officer issued a simple SELECT statement as
follows:
SELECT *
FROM Book;
However, when the desk officer issues the same statement after a few days, he may
observe some noticeable variations in the resultant output. For instance, there would
be additions to the already existing list of books, or a particular book may no longer
be stocked. This reflects that the data stored in the database is dynamic in nature.
These changes involve:
Adding new data
Modifying existing data
Removing data
This chapter deals with how a database developer should implement these changes.
These changes are made possible by the use of Data Manipulation Language (DML).
The following DML statements are executed in order to manipulate the data in the
database:
INSERT
UPDATE
DELETE
MERGE
Insert Rows
In every organization, the new data or additional data must necessarily be
incorporated into the database. For example, when people enroll themselves as
members of the New Jersey Central Library, their details must be added to the
Member table. Similarly, details regarding new books ordered by the library should
be inserted into the Book table. In order to add new data to the tables, use the
INSERT statement. Its syntax is:
INSERT INTO table [(column [, column])]
VALUES(value [, value]);
SQL Star International Ltd.
224
Where,
table is the name of the table into which new column values are inserted.
(column [, column]) are the columns of the specified table into which the values
are inserted.
(value [, value]) these are the actual values that are inserted into the columns
specified.
There are different ways of inserting data into a table using the above syntax.
Inserting
Inserting
Inserting
Inserting
values
values
values
values
225
Implicit Method
226
In this method, you can omit those columns from the column list, into which values
are not to be inserted. The following INSERT statement specifies only those columns
into which values are to be inserted:
Explicit Method
In this method, specify the NULL keyword in the values list for those columns into
which values are not to be inserted. The above INSERT statement could be rewritten
as follows:
INSERT INTO Book
VALUES (FIC051111112,Roots,ALEX
HUXELY,01FIC,NULL,NULL,05PRINJ,NULL);
This statement omits the column list in the INSERT clause. In the VALUES clause, the
NULL keyword is specified in place of all those columns whose values are not known.
227
table. This is achieved by using a subquery instead of the VALUES clause in the
INSERT statement.
Its syntax is as follows:
INSERT INTO table [column (, column)]
subquery;
Remember that the number of columns and their datatypes specified in the column
list must be the same as the number of columns and their datatypes specified in the
subquery.
The following example illustrates how the usage of the subquery in the INSERT
statement populate the IncompleteTransaction table.
INSERT INTO IncompleteTransaction
SELECT cTranID, cMemberID, cBookID, dIssueDt, dReturnDt,
dActualReturnDt, nFine
FROM Transaction
WHERE dActualReturnDt IS NULL
AND (SYSDATE-dIssueDt) > 185;
2 rows created.
You can view the values inserted into the IncompleteTransaction table.
SELECT *
FROM IncompleteTransaction;
Displays:
CTRANID
CMEMBERID
CBOOKID
DISSUEDT DRETURNDT DACTUALRE
NFINE
-------------------------- -------- --------- ----T0000300100 CBW109702 ROM020003211 07-SEP-00 21-SEP-00
SQL Star International Ltd.
228
Update Data
Just as you enter details of new members in the Member table, similarly any change
in the members information, such as their address, requires you to incorporate
these changes effectively in the database tables. Such changes in the existing data
are made using the UPDATE statement.
The syntax for using the UPDATE statement is:
UPDATE table
SET column = value [, column = value,]
[WHERE condition];
Where,
table is the name of the table you are attempting to update.
column is the column in the specified table whose value is to be updated.
value is the new value with which the old column value is to be replaced.
condition specifies the condition for the rows to be updated
229
In this example if the WHERE clause had been omitted, all the members address and
phone numbers would have been updated.
However, it is advisable to use the primary key column in order to update a specific
row. This is due to the using of non-primary key columns (such as cFirstName and
cLastName) that may cause other rows to be updated too. For instance in the
above example, you used the member name to identify the specific row that is to be
updated. It works fine as long as there is only one member with the name Jessica
Hatcher. But it could cause a problem if the library has more than one member with
the same name.
Now use this syntax to update the above-mentioned change in the PublisherDetails
table.
UPDATE PublisherDetails
SET vPublisherAddress= (SELECT
vPublisherAddress
SQL Star International Ltd.
230
FROM PublisherDetails
Just as you can populate a table using values from an existing table, similarly you
can update rows by using subqueries that return values from other tables.
For example, suppose the New Jersey Central Library creates a table, RandolphBks
having a structure similar to the Book table and populated with details of books
stored only in the Randolph NJ Library.
The library management may decide to update the number of copies of books stored
in the above created table with the number of copies of book number
MYS010000001 from the Book table as follows:
UPDATE RandolphBks
SET nNoOfCopies = (SELECT nNoOfCopies
FROM Book
WHERE cBookID=MYS010000001');
38 rows updated.
231
the categories to which the books belong. Therefore, you put them in a common
category. Under such circumstances, you can set the default value for the category
ID in the Book table and make entries. Here, the default value is set as 01HUM.
First, alter the Book table to modify the category column to take the default value as
01HUM.
ALTER TABLE BOOK
MODIFY cCategoryID DEFAULT 01HUM;
Now, you can make new entries in the Book table as follows:
INSERT INTO Book(cBookID, cBookName, cCategoryID, cPublisherID, cBranchID)
VALUES (XYZ0135599555,Fish, default, PN0135, 01ADLNJ);
When you execute this query, book details are entered into the Book table.
However, for the category of the book, the default value is entered.
You can also use the DEFAULT keyword in UPDATE statements to set default values
for the columns. In the following query we will change the category ID of the book
Fun With Maths and Physics to the default value of category ID, that is, 01HUM.
UPDATE Book
SET cCategoryID=DEFAULT
WHERE cBookName=Fun With Maths and Physics;
Delete Data
SQL Star International Ltd.
232
233
In this example, the subquery returns the branch ID of Hamilton branch. This value
is then used in the WHERE clause of the DELETE statement.
234
235
Transactions
Transaction, in simple words is a series of data manipulation statements executed as
one logical unit of work.
But, why are DML statements executed as one logical unit of work?
This could be understood by looking at the outcome of treating data manipulating
statements as individual activities.
An act of a customer purchasing a product of certain quantity, is a transaction, which
comprises of two different activities:
But, after making the payment if the customer decides to change the quantity of the
product purchased, then Oracle would not be able to undo the stock level updates or
SQL Star International Ltd.
236
the cash received at entry, as these two activities have been saved to the Oracle
database individually. Hence, the changes will not be reflected in the respective
tables. This is because we have treated them as two individual activities independent
of each other.
But in reality when a product is purchased both the activities involved are performed
simultaneously.
Therefore, a collection of DML statements is executed as a single unit of work, which
is a transaction.
The act of purchasing a product affects more than one table. However, if you wanted
to reverse the transaction would it get reflected in all the required tables?
Controlling your transactions can solve this problem.
The different types of transactions are:
DML transactions that comprise any number of DML statements treated as
one unit of work
DDL (Data definition language) transactions that comprise only one DDL
statement
DCL (Data control language) transactions that comprise only one DCL
statement
Controlling Transactions
You can control transactions using transaction control mechanisms. Transaction
control mechanism comprises of a set of commands that define the beginning, end
and breakpoint of a transaction.
These commands are called transaction control commands and are as follows:
COMMIT permanently saves the data changes
ROLLBACK discards all data changes
SAVEPOINT divides a transaction into different sections
237
To control the transactions explicitly you need to use the COMMIT, ROLLBACK and
SAVEPOINT control statements. Each of these is explained below .
COMMIT
The COMMIT statement is issued when the user is satisfied with all the changes
made and therefore is ready to save them to the database. Issuing a COMMIT
statement ends the current transaction and begins a new one.
For example, the following DML statement is issued on the Member table to update
the phone number of Stanley Davenport:
UPDATE Member
SET cPhone=9865457989'
WHERE cFirstName=Stanley
AND cLastName=Davenport;
1 row updated.
But before issuing a COMMIT statement the state of data in the Member table would
be as follows:
Other users cannot change all those rows that are currently being changed by
another user. This is known as the locking of rows.
Changes made to the data get reflected only in the SQL buffer and not in the
database. Therefore the previous state of the data can be recovered if you
want to discard the changes made.
The current user can view the result of the DML statements, by using the
SELECT statement. But other users cannot view the changes, as the changes
have not yet been permanently saved to the database.
SELECT cPhone
FROM Member
WHERE cFirstName=Stanley
AND cLastName=Davenport;
CPHONE
---------9865457989
But after issuing a COMMIT statement as follows:
SQL Star International Ltd.
238
COMMIT;
Commit complete.
Changes to the data are made permanent in the database, hence, the
previous state of the data cannot be recovered.
All users can view the result of the DML operations as the changes are
permanently saved to the database.
Other users can now perform new data changes as the locks on the rows are
released.
ROLLBACK
The ROLLBACK statement is issued when the user wants to discard changes made to
the data before they are committed and made permanent. If the user deletes rows
that should not be deleted, the deletion could be undone by issuing the ROLLBACK
statement. For example, you may accidentally delete all the rows from the
Transaction table while attempting to delete only one record from the table.
DELETE FROM Transaction;
32 rows deleted.
Issuing the ROLLBACK keyword and then writing the correct transaction can undo
this.
ROLLBACK;
Rollback complete.
COMMIT;
Commit complete.
ROLLBACK statement not only undoes the current transaction, but also marks the
beginning of a new one.
State of the data before ROLLBACK is similar to that before COMMIT. But after
ROLLBACK,
239
SAVEPOINT
Issuing a ROLLBACK statement ends the current transaction by discarding all the
changes made to the data. But, a user may not always want to discard all the
changes made, simply because the last statement issued contains changes which are
unwanted. Therefore, divide the transaction into different segments so that you can
discard those segments you want to while keeping the rest intact. The syntax to use
SAVEPOINT is:
SAVEPOINT name;
For instance assume that you have executed the following transaction in the library
database:
INSERT INTO Transaction
VALUES (T000300501,BJH029405',
MYS010000065,11-MAY-2001',25-MAY-2001'
SQL Star International Ltd.
240
,NULL,NULL);
1 row created.
SAVEPOINT InsertTran;
Savepoint created.
UPDATE Transaction
SET dActualReturnDt= 27-MAY-2001'
WHERE cTranID=T000300501';
1 row updated.
SAVEPOINT UpdateTran;
Savepoint created.
DELETE FROM Transaction
WHERE cTranID=T000300501';
1 row deleted.
In the example, the transaction has been divided into small sections by using the
SAVEPOINT statement.
In case you want to rollback any of these transactions, then you will have to issue
the ROLLBACK TO SAVEPOINT clause, which rolls back the current transaction to the
specified savepoint. So, all the changes made after the savepoint are discarded. If
this clause were omitted, the ROLLBACK statement would discard the entire
transaction. Hence, to rollback the transactions to a particular point only, add the
following statement:
ROLLBACK TO UpdateTran;
Rollback complete.
In the example, the ROLLBACK TO UpdateTran clause rolls back the deletion made
after the issue of the savepoint UpdateTran, while keeping the insertion and
updation, made prior to the savepoint, intact.
and end?
A transaction begins when the Oracle server encounters the first executable SQL
statement written by the user on logging on to the SQL*PLUS environment. The SQL
statement could be DML, DDL or DCL statement.
A transaction ends on the occurrence of the following:
SQL Star International Ltd.
241
Saving or committing
Execution of a DDL or DCL statement because they automatically save the
activities performed
A user exiting from the SQL*PLUS environment.
Machine failure
When one transaction ends, the next transaction begins with the next executable
SQL statement.
Automatic Commit
Under the following circumstances, an automatic commit occurs:
Issue of a DDL or a DCL statement, or
Normal exit from SQL*PLUS environment, or
Keeping the SQL*PLUS, AUTOCOMMIT command on. Therefore, as soon as
each DML statement is executed, it is committed. Hence, they cannot be
rolled back.
Automatic Rollback
An automatic rollback occurs when the user exits from SQL*PLUS environment
abruptly, i.e. shutting down the machine without following the normal exit routine or
if the system crashes. In either case the entire transaction is rolled back and the
initial state of the data, as it was at the time of the last commit, is returned. This
prevents unwanted changes to be made to the data.
242
Locking Mechanism
Different users access the database to perform different operations. Database users
accessing the database perform the following two types of operations:
Read operations to retrieve data from the database but not make any
modifications to it (SELECT statement)
Write operations to perform modifications to the data in the database
(INSERT, UPDATE, DELETE statements)
Therefore it is necessary to ensure that the users reading from and writing to the
database get a consistent view of the data at all times. This is referred to as read
write consistency.
Read consistency is needed to ensure that,
Both, the database reader as well as the writer gets a consistent view of the
data.
Readers get to see the data that is not in the process of being changed.
A change made by one writer does not conflict with changes made by another
writer.
The tool used to achieve this is locking. In order to prevent data from being modified
by more than one user, the Oracle server uses the locking mechanism, which
requires no user effort. It is fully automatic and occurs for all SQL statements except
SELECT.
Locks are broadly classified into two types namely:
2. DDL Locks
What Happens when Row-Level Lock is placed on Row?
First a DML Lock is placed on the row.
No Other Transaction Can Lock Or Update this Row
This Lock will be released when the locking transaction commits or rollback.
Secondly, a DDL Lock is placed on the table, containing the row, to prevent anyone
from altering or dropping the table while the row is undergoing the update.
What happens when a Table level Lock is placed on the table?
SQL Star International Ltd.
243
Only the table Locking Transaction can Update Rows in the table
No rows in the table can be updated by other transactions
Other Transaction can query the table, including the one being updated in the
Read Consistency
Consider 2 users using the same table for two different operations as shown in the
table below.
244
When user2 queries the Member table after some time he gets the old telephone no
(9865457989) till the time user1 commits.
Summary
In this chapter, you have learnt that:
Data within the database can be manipulated using the Data Manipulation
command (DML).
245
INSERT: Adding new values into table, UPDATE: modifying the existing
values DELETE: removing the values and MERGE: adding and modifying
data based on the condition in a single statement.
These DML commands can be made interactive using the substitution
variables (&) in the scripts.
RETURNING INTO clause can be used in DML statement to view
modifications done by the DML operation. This feature avoids using another
SELECT statement to see the changed value.
Transaction can either be saved or undone using Data Transaction Control
Statement. COMMIT statements make the transaction permanent and
ROLLBACK statement reverses all the un-committed transaction.
Rows in a table undergoing a change, gets implicitly locked. Explicit locks can
also be applied on the tables.
Only committed data is viewed by all the users. This is known as read
consistency.
246
Lab Exercise
[Note: For questions 1 to 8, you are required to insert, update and delete data from
the OfficeStaff table.]
To work with the OfficeStaff table, run the LabEx11_00.sql script file. This creates
the OfficeStaff table. The structure of the table is as follows:
1.
From the sample data given above, insert the first row into the OfficeStaff
table without listing the columns in the INSERT clause.
247
2.
Insert the second row from the sample data into the OfficeStaff table by
explicitly listing the columns in the INSERT clause. Confirm the creation of
rows.
3.
Create a script file LabEx11_03.sql. Within the file write an INSERT statement
to populate the OfficeStaff table. The INSERT statement should be such that it
prompts you to enter the required values. To produce values for the Mail_ID
column, concatenate the first letter of first name with the last names.
4.
Now run the script file to insert the next two rows of sample data. Make the
data inserted permanent. Confirm the rows inserted into the OfficeStaff table.
5.
6.
Remove the details of Morgan Sheen from the OfficeStaff table. Confirm the
changes made. Commit the changes made.
7.
Insert the last row of the sample data. Mark a savepoint immediately after
inserting the row.
8.
Delete all the rows from the OfficeStaff table. Confirm that the table is empty.
Now,rollback the DELETE statement, but ensure that the earlier INSERT
statement is not discarded. Make the transaction permanent.
9.
248
Chapter 12
Managing Tables
Data Definition Language
Datatypes used in Defining columns
Modifying Table
Structure
Manipulating Table Definition
Flashback Table
249
Objectives
At the end of this chapter, you will be able to:
Create tables
Identify the datatypes used when defining the columns
Modify table structure
Understand Flashback table
250
Introduction
In order to store data in an organized manner, Oracle provides data containers
known as Database Objects. Oracle also provides a mechanism by which you can
create and manipulate them. To understand the creation of database objects, you
must first know the different types of database objects.
Database Objects
Any data structure created within the database for a specific purpose is known as a
database object. Oracle provides various database objects, which are as follows:
Table for storing data
View for providing a customized view of data from one or more tables
Sequence for generating numeric column values that serve as primary keys
Index to retrieve data faster from the database
Synonym to give alternative names to other database objects
In order to create the above listed database objects, you must use a language that
supports database object creation. The language provided by SQL for database
object creation, modification and removal is the Data Definition Language or DDL.
251
Creating Tables
A table is a database object that stores data. Here, the data is stored in the form of
rows and columns. Each row consists of one or more columns. The DDL statement
used to create a table in the database is CREATE TABLE.
CREATE TABLE Statement
To create a table with the CREATE TABLE statement, the syntax is as follows:
CREATE [GLOBAL TEMPORARY] TABLE [schema.] <table_name>
(Column datatype [DEFAULT expr] [,.] );
Where,
GLOBAL TEMPORARY specifies that the data in the table is temporary where as the
table definition is permanent. In other words, the data is visible only in the session in
which it was inserted, but the table definition is visible in all sessions.
schema is same as the user name
table_name is the name of the table
DEFAULT expr specifies a default value that a column should have
column is name of the column
datatype specifies the datatype and the length of a column
Given below is an example that creates a table named Branch, with columns to
store ID, name, address, location, phone and the year during which the branches
were established.
CREATE TABLE Branch
(cBranchID
NUMBER(7),
cBranchName
CHAR(25),
vBranchAdress
VARCHAR2(50),
cBranchLocation CHAR(15),
cPhone
CHAR(10),
dEstbl
DATE
);
When the table is created, the following message is displayed.
Table created.
252
To view the columns and their datatypes in the Branch table, issue the command
DESCRIBE in the following manner:
DESCRIBE Branch
Schema
When a database is created, several users access it. A single user does not create all
the database objects. Database objects are created by each user as and when
required. Database tables are usually created just once as they make up the basic
database.
When many users create objects, there is a security issue. To prevent the users from
tampering with each others objects, every user is allotted a separate work area. A
user is allowed to create database objects in his work area and use them. There is no
physical demarcation of memory area for different users. It is a logical allotment of
memory area. Database objects created by one user are logically separate from the
database objects created by other users. Such logical collection of database objects
such as tables, views or sequences is known as a schema.
For example, the tables named Member and Book are created by the DBA. Hence,
the DBA becomes the owner of the tables and the tables become a part of the DBA
schema.
To reference the objects created by a user, prefix the object name with the schema
or owner name.
For instance, to refer to the Member table you would refer to it as
DBA.Member
Table Naming Conventions
To improve readability and avoid common mistakes, some naming conventions need
to be followed.
They include:
Keeping short and descriptive names. For example, it would be hard to
comprehend a table named as Bk_iss_trans. Instead, you could name it
Transaction.
SQL Star International Ltd.
253
Giving same name to the columns having same values in different tables.
For example, category ID are present in Book as well as the Category table. So
give them the same name, cCategoryID.
Rules to be followed when naming tables are:
All tables must begin with an alphabet.
All column names must be in lowercase.
Names can be up to 30 characters long.
You cannot use non-alphanumeric characters except the underscore (_), the
dollar sign ($) and the hash mark (#).
The names must not be the same as that of any other object owned by the
user.
The names must not be an Oracle server keyword such as SELECT, CREATE,
INSERT etc.
Specifying default values for columns at the time of creating tables saves the task of
entering values for them during an insert. If a row is inserted without a value for a
column then this option prevents null values from being entered into that column.
Literal values, expressions or SQL functions can be used as the default value.
For example, in the Member table, a default value for the dMembershipDt column
can be defined, so that when new member details are to be inserted, their
membership date is set by default to SYSDATE.
dMembershipDt DATE DEFAULT SYSDATE
254
For example, the user DBA creates the IncompleteTransaction table for the library
to store details of those books, which have been issued six months back and have
not yet been returned by members. The data for this table comes from the
Transaction table. Inserting rows into the former table will prove to be quite
cumbersome, if there are large number of members who have not returned books in
the past six months. Instead, these details could be directly populated from the
Transaction table, as both the tables are similar in structure. The code to solve this
problem is:
CREATE TABLE IncompleteTransaction
AS
SELECT * FROM Transaction
WHERE dIssueDt>(SYSDATE 6)
AND dActualReturnDt IS NULL;
Table created.
The code selects all those details from the Transaction table, where members have
not returned books issued more than six months back and populates them into
IncompleteTransaction table.
SELECT *
FROM IncompleteTransaction;
SQL Star International Ltd.
255
Number Datatype
The datatype used to store only numeric data is the number datatype, NUMBER
(precision, scale). It can store both fixed and floating-point numbers. By
precision we mean the total number of digits and scale is the number of digits to the
right of the decimal point. For example, 779.69 is number datatype where the
precision is 5 and the scale is 2.
In the Member table, members age is stored as NUMBER datatype.
SQL Star International Ltd.
256
Date Datatype
In Oracle, dates are stored in the DATE datatype, which actually contains two values:
the date, and the time between January 1, 4712 BC and December 31, 9999 AD. The
default date format in which Oracle stores date is DD-MON-YY where DD is the day
of the month (01), MON is the month (Jan) and RR is the year (two-digit year-99,
01).
DATETIME Datatype
This datetime datatype had been introduced in Oracle9i. The datetime datatype
includes the time zone in the date and time data, as well as provide support for
fractional seconds. New datetime functions are also provided to use these datatypes.
These datatypes and functions ease the development of applications accessed
globally and significantly reduce the cost of developing and deploying applications.
Applications that are deployed across the globe include time zones and multilanguage support through Unicode. The datetime datatypes are time zone sensitive
and adjust the datetime as per the session time zone of the user.
The datetime datatypes are based on the concept of time zones and Greenwich Mean
Time (GMT), now known as Coordinated Universal Time (UTC). The time along the
prime meridian in Greenwich, England is known as Greenwich Mean Time (GMT). The
meridian line is an imaginary line that runs from the North pole to the South pole.
The earth is divided into 24 time zones depending on the hours of a day. UTC is the
standard time, which is same all around the year. It does not change by summer
time or daylight savings time. All time zones in the world refer to the UTC. When it is
noon in Greenwich, it is midnight along the international line.
257
TIMESTAMP Datatype
The TIMESTAMP data type allows time to be stored as a date with fractional seconds.
It is an extension of the DATE datatype and stores the year, month and day of the
DATE datatype along with the hour, minute and seconds. Apart from this, it also
stores the fractional value of seconds. The TIMESTAMP datatype has the following
form:
TIMESTAMP[(fractional_seconds_precision)]
Where,
fractional_seconds_precision specifies the number of digits in the fractional part of
the SECOND datetime field. It can be any number ranging from 0-9.
For instance, create a table AdmissionDetails with a column of type TIMESTAMP.
CREATE TABLE AdmissionDetails
( StudentName
Address
VARCHAR2(20),
VARCHAR2(20),
AdmissionDate TIMESTAMP(7),
Fees
NUMBER(7,2)
);
In AdmissionDetails table, the AdmissionDate column is of type TIMESTAMP and
of precision 7, which indicates the fractional seconds precision. If you do not specify
the precision, the default is 6.
Now, insert two rows into the table. When you execute the following query.
SELECT admissiondate FROM Admissiondetails;
The output is as follows:
25-NOV-20 01.00.00.0000000 AM
01-OCT-20 01.00.00.0000000 AM
258
VARCHAR2(20),
Address
VARCHAR2(20),
NUMBER(7,2)
);
Table created.
Insert some values into the table AdmissionDetails1 by selecting data from
AdmissionDetails table.
INSERT INTO AdmissionDetails1
SQL Star International Ltd.
259
Whereas, a client in Singapore retrieves the same data, the value displayed is
10-NOV-2001 11:30:00
260
VARCHAR2(20),
VARCHAR2(20),
NUMBER(7,2)
);
The code creates a table called AdmissionDetails2 with AdmissionDate of type
TIMESTAMP WITH LOCAL TIME ZONE and other columns of their respective
datatypes.
Insert some rows into the table as follows:
INSERT INTO AdmissionDetails2
SELECT * FROM AdmissionDetails1;
When you retrieve data within the column AdmissionDate from the table
AdmissionDetails2, the output shows the time as per current time zone:
SELECT AdmisionDate FROM AdmissionDetails2;
261
ADMISSIONDATE
------------------25-NOV-20 01.00.00.000000 AM
01-OCT-20 01.00.00.000000 AM
04-MAR-01 09.30.00.000000 PM
Change the time zone to Singapore time zone in the following manner:
ALTER SESSION SET TIME_ZONE = +8:00;
Session altered.
Now if you select the AdmissionDate column from AdmissionDetails2 table, you
get the following output:
SELECT AdmisionDate FROM AdmissionDetails2;
ADMISSIONDATE
------------------------25-NOV-20 03.30.00.000000 AM
01-OCT-20 03.30.00.000000 AM
05-MAR-01 12.00.00.000000 AM
As the difference between time zone of Singapore and time zone of India is two and
a half hours, the time displayed in the output shows a difference of two and a half
hours in each entry. Therefore, a user in Singapore will see the output according to
Singapore local time zone and a user in India will see the output according to Indian
local time zone. This is possible with the help of TIMESTAMP WITH LOCAL TIME
ZONE datatype.
The INTERVAL Datatype
The INTERVAL datatype is used to store a period of time. A period of time is the
difference between two datetime values. The period can be years, months, days,
hours, minutes or seconds. Therefore the interval datatype comprises of two types:
INTERVAL YEAR TO MONTH datatype
INTERVAL DAY TO SECOND datatype
The INTERVAL qualifier may specify a leading field precision, which specifies the
number of digits that can be present in a leading field.
262
The first INSERT statement inserts the value for year as 10. The year takes a
precision of 2 as specified during table creation. If you try to insert a value of three
SQL Star International Ltd.
263
digits, Oracle server gives an error. The second INSERT statement inserts a value for
month as 11. The third INSERT statement inserts values for both year as well as
month as 3-9.
264
Datetime Functions
You have seen the various datetime datatypes. Now you will learn about the
datetime functions, which enable you to retrieve the current session and database
time for all datetime datatypes. The datetime functions are:
CURRENT_DATE
CURRENT_TIMESTAMP
LOCALTIMESTAMP
SYSTIMESTAMP
DBTIMEZONE
SESSIONTIMEZONE
EXTRACT
FROM_TZ
TO_TIMESTAMP
TO_TIMESTAMP_TZ
TO_YMININTERVAL
TO_DSINTERVAL
TZ_OFFSET
CURRENT_DATE
CURRENT_DATE returns current date and time in the session time zone as DATE
datatype (without fractional seconds). The value returned is in the form of Gregorian
calendar date value. In earlier releases, current date and time could be retrieved
using SYSDATE. However, using CURRENT_DATE, you can see the time zone changes
as well. You can change the time zone of a session using the ALTER SESSION
command.
Retrieve the current date and time of the session by giving the following code:
ALTER SESSION SET NLS_DATE_FORMAT = DD-MON-YYYY HH24:MI:SS;
ALTER SESSION SET TIME_ZONE = +05:30;
SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;
The output is:
SESSIONTIMEZONE CURRENT_DATE
--------------- -----------+05:30
19-FEB-2002 15:10:19
Now you alter the session to time zone of Singapore.
SQL Star International Ltd.
265
CURRENT_TIMESTAMP
The CURRENT_TIMESTAMP function returns the current date and time of the session
in the form of TIMESTAMP WITH TIME ZONE datatype.
The following code retrieves the SESSIONTIMEZONE and CURRENTTIMEZONE from
DUAL:
ALTER SESSION SET TIME_ZONE = 02:00;
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;
The output is:
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- -----------------+02:00 19-FEB-02 12.21.06.000001 PM +02:00
As shown in the output, CURRENT_TIMESTAMP returns date and time as
TIMESTAMP WITH TIME ZONE type, with time zone as +2.
Now change the time zone to -5 as shown below:
ALTER SESSION SET TIME_ZONE = -05:00;
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;
266
SESSIONTIMEZONE
CURRENT_TIMESTAMP
---------------
-----------------
-05:00
The CURRENT_TIMESTAMP returns current date and time with time zone as -5.
LOCALTIMESTAMP
The LOCALTIMESTAMP function returns the current date and time in the session time
zone. However, the value returned is of TIMESTAMP datatype. The difference
between CURRENT_TIMESTAMP and LOCALTIMESTAMP functions is that the former
returns a value of type TIMESTAMP WITH TIME ZONE, whereas, the latter returns a
value of type TIMESTAMP. The following code illustrates this difference:
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
The output is:
CURRENT_TIMESTAMP
LOCALTIMESTAMP
------------------------19-FEB-02 05.55.41.000000 AM -05:00 19-FEB-02 05.55.41.000000 AM
Now alter the session:
ALTER SESSION SET TIME_ZONE = +02:00;
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
Displays:
CURRENT_TIMESTAMP
----------------19-FEB-02 01.01.59.000000 PM +02:00
01.01.59.000000 PM
LOCALTIMESTAMP
------------19-FEB-02
Note the difference between the values returned. The CURRENT_TIMESTAMP returns
the date and time along with the time zone, whereas the LOCALTIMESTAMP does
not.
SYSTIMESTAMP
The SYSTIMESTAMP function returns date and time of the server in TIMESTAMP
format, whereas the SYSDATE function (of previous releases) returns only server
system date and time.
SQL Star International Ltd.
267
SYSDATE
------------------
19-FEB-2002 16:40:05
DBTIMEZONE
The DBTIMEZONE function returns the default database time zone. You can set the
default database time zone at the time of database creation by specifying the
SET TIME_ZONE clause of the CREATE DATABASE statement. If not specified, the
time zone of the operating system where the database is installed is taken as the
default database time zone.
SELECT DBTIMEZONE FROM DUAL;
The output is:
DBTIME
-------05:00
The return value can have the following formats, depending on how the user has
specified the database timezone:
A character type time zone offset in the format [+ | - TZH:TZM]
A time zone region name
The output of the above select statement shows that the database time
zone is -5:00:
SESSIONTIMEZONE
The SESSIONTIMEZONE function returns the time zone of the current session. It can
have the following format, depending on the time zone specified during database
creation:
A character type time zone offset in the format [+ | - TZH:TZM]
A time zone region name
SQL Star International Ltd.
268
EXTRACT
The EXTRACT expression extracts the value of a specified datetime field from a
datetime or interval field value. Using the EXTRACT function, you can extract any of
the components specified in the EXTRACT function syntax:
SELECT EXTRACT([YEAR][MONTH][DAY][HOUR]
[MINUTE][SECOND][TIMEZONE_HOUR]
[TIMEZONE_MINUTE][TIMEZONE_REGION]
[TIMEZONE_ABBR]
FROM [datetime_value_expression]
[interval_value_expression]);
When you extract
A TIMEZONE_REGION or TIMEZONE_ABBR (abbreviation), the return value is a
string containing the appropriate time zone name or abbreviation
From a datetime with a time zone, the value returned is in UTC
Any other values, the value returned is in Gregorian calendar format.
The following query extracts the month from the system date:
SELECT EXTRACT (MONTH FROM SYSDATE) FROM DUAL;
SQL Star International Ltd.
269
FROM_TZ
270
Alternatively, you can also specify the TZR (time zone region) name in the query as
follows:
SELECT FROM_TZ(TIMESTAMP 2001-05-25 10:15:20, Asia/Singapore)
FROM DUAL;
TO_TIMESTAMP
The TO_TIMESTAMP function is used to convert a string of CHAR, VARCHAR2,
NCHAR, or NVARCHAR2 datatype to a TIMESTAMP datatype. The syntax is:
TO_TIMESTAMP (char,[fmt],[nlsparam])
Where,
fmt is optional. It specifies the format of char. However, if it is not specified, the
string must be in the default format of TIMESTAMP datatype.
nlsparam is also optional. It specifies the language in which day and month names
and abbreviations are returned. It can have the form:
NLS_DATE_LANGUAGE = language
If not specified, the TO_TIMESTAMP function takes the default language selected for
your session.
To convert a date specified as string to a TIMESTAMP value, give the following query:
SELECT TO_TIMESTAMP(2001-11-25 10:15:20,YYYY-MM-DD HH:MI:SS)
FROM DUAL;
271
Observe that the TO_TIMESTAMP function converts the date specified in YYYY-MMDD format to TIMESTAMP datatype format which is DD-MON-YY.
TO_TIMESTAMP_TZ
The TO_TIMESTAMP function is used to convert a string of CHAR, VARCHAR2, NCHAR
or NVARCHAR2 datatype to a TIMESTAMP WITH TIME ZONE datatype. The syntax is:
TO_TIMESTAMP_TZ (char,[fmt],[nlsparam])
Where,
- fmt is optional. It specifies the format of char. However, if it is not specified, the
string must be in the default format of TIMESTAMP WITH TIME ZONE datatype.
- nlsparam is also optional. It specifies the language in which day and month names
and abbreviations are returned. It can have the form:
NLS_DATE_LANGUAGE = language
If not specified, the TO_TIMESTAMP function takes the default language selected for
your session.
To convert a date specified as string to a TIMESTAMP WITH TIME ZONE value, give
the following query:
SELECT TO_TIMESTAMP_TZ(2002-01-12 10:15:20 +8:00,YYYY-MM-DD
HH:MI:SS TZH:TZM) FROM DUAL;
272
TO_YMINTERVAL
The TO_YMINTERVAL function is used to convert a string of CHAR, VARCHAR2,
NCHAR or NVARCHAR2 datatype to an INTERVAL YEAR TO MONTH datatype. The
INTERVAL YEAR
TO MONTH datatype stores a period of time using the YEAR and MONTH datetime
fields. The format of the INTERVAL YEAR TO MONTH is as follows:
INTERVAL YEAR [(year_precision)] TO MONTH
Where,
year_precision is the number of digits in the YEAR datetime field. The default is 2.
The syntax of TO_YMINTERVAL function is:
TO_YMINTERVAL(char)
Here, char is the string to be converted.
To calculate a lease period that is one year and two months after current date, give
the following query:
SELECT SYSDATE,SYSDATE+TO_YMINTERVAL(01-02') AS LEASE_PERIOD
FROM DUAL;
The output is:
SYSDATE
LEASE_PERIOD
------- ------------20-FEB-02 20-APR-03
In the above query, the current date is first retrieved. Then a period of one year and
two months is added to it and displayed as lease period.
273
TO_DSINTERVAL
TO_DSINTERVAL converts a character string of CHAR, VARCHAR2, NCHAR or
NVARCHAR2 datatype to an INTERVAL DAY TO SECOND type. The INTERVAL DAY TO
SECOND datatype stores a period of time using the DAY and SECOND datetime
fields. The format of the INTERVAL DAY TO SECOND is as follows:
INTERVAL DAY [(day_precision)] TO SECOND
Where,
day_precision is the number of digits in the DAY datetime field. The default is 2.
The syntax of TO_DSINTERVAL function is:
TO_DSINTERVAL(char)
Here, char is the string to be converted.
To calculate a lease period that is fifty days, ten hours after current date, give the
following query:
SELECT SYSDATE,SYSDATE+TO_DSINTERVAL(50 10:00:00') AS
LEASE_PERIOD
FROM DUAL;
The output is:
SYSDATE LEASE_PERIOD
------- -----------29-MAY-07 18-JUL-07
In the above query, the current date is first retrieved. Then a period of fifty days, ten
hours is added to it and displayed as lease period.
TZ_OFFSET
Every time zone across the globe has a corresponding offset. The TZ_OFFSET
function returns the corresponding time zone of the value entered depending on the
date when the statement is executed. For instance, if the return value of the
TZ_OFFSET function is 5:00, it indicates that the time zone from where the
statement was executed is
five hours after UTC. If it is +2:00, the time zone is two hours ahead of UTC.
The TZ_OFFSET function can take :
A valid time zone name
A time zone offset from UTC, (which returns itself)
The keyword SESSIONTIMEZONE
The keyword DBTIMEZONE
SQL Star International Ltd.
274
275
TZ_OFFS
-------05:00
The database time zone is five hours behind the UTC.
276
277
While adding a new column, you cannot specify where it is to appear in the
table. The new column is always the last column.
A new column added to a table (already containing rows of data) is initially
NULL for all the rows.
If a column with NOT NULL constraint is to be added, then:
1 The column must first be added to the table without the constraint.
2 It must be populated with data in all the rows.
3. Once the column values are not null, the NOT NULL constraint can be
added to it.
In a table, only one column can be of LONG and LONG RAW datatype.
Therefore, you cannot add columns of LONG and LONG RAW datatypes if the
table already has columns of these datatypes.
Modifying Columns
While creating tables, columns are defined along with their datatypes and lengths in
order to identify the type of data that the columns can hold. In case you want to
alter the column to hold values larger than the current ones then the new column
length must be made larger.
This can be done by using the ALTER TABLE statement with the MODIFY clause. The
syntax for modifying a column is:
ALTER TABLE <table_name>
MODIFY [column datatype [DEFAULT expr] [, column datatype]);
For example, in the Book table, the column cBookName is defined of length 20.
But, while attempting to enter details of book The strange case of Dr. Jekyll and Mr.
Hyde, the Oracle server would generate an error because the book name happens to
be longer than the length defined for the column. Therefore, modify the column
width so as to accommodate long book names.
ALTER TABLE Book
MODIFY (cBookName CHAR (100));
In the code, the length of the cBookName column has been changed from 20 to
100.
In addition to changing the column length, the column datatype and default value
can also be changed.
Points to remember:
278
Increasing column length using the MODIFY clause is OK, but decreasing them or
changing the column datatype tends to be tricky. For example, if the column length
of cMemberID were reduced from 9 to 7, then all those members having IDs of
lengths more than 7 would not satisfy the column definition and would therefore get
truncated.
ALTER TABLE Member
MODIFY cMemberID CHAR (7);
ERROR at line 2:
ORA-01441: column to be modified must be empty to decrease column
length
In order to overcome this problem, some special steps need to be taken.
Before reducing the column length, ensure that the column has all null
values.
But before deleting all the values from the column in question, create
a temporary table to store the values of the column. This can be done
by using the CREATE TABLE AS SELECT statement, where the SELECT
statement extracts data from the primary key column and the column
that is to be modified.
Changing the DEFAULT value of a column affects only succeeding insertions to
the column.
Dropping Columns
Columns are dropped when you no longer need them from a table. The DROP
COLUMN clause with the ALTER TABLE statement is used to remove a column. This
feature is available only from Oracle8i. The syntax for dropping columns is,
ALTER TABLE <table_name>
DROP COLUMN <column_name>;
Where,
<column_name> specifies the column that is to be dropped.
For example, the developer creates the Category table with the following columns:
cCategoryID
cCategoryCode
cCategoryName
279
cCategoryName
CHAR (15)
);
But after creating the table, the developer realizes that both the cCategoryID and
cCategoryCode columns hold the unique identity numbers given to each category.
Therefore, one of them must be removed from the table structure. This can be done
using the DROP clause as follows:
ALTER TABLE Category
DROP COLUMN cCategoryCode;
Points to remember
The dropped column may or may not contain data.
At a time only one column can be dropped.
The table must have at least one column left after one of its columns has been
dropped.
A dropped column cannot be recovered.
280
Therefore, to speed up the response time you could mark the columns to be dropped
as unused. Doing so does not restore the disk space. Instead, the columns so
marked could later be dropped when the requirement for the system resources is
less. The option used to mark one or more columns as unused is the SET UNUSED
option.
The syntax to mark columns as unused is,
ALTER TABLE <table_name>
SET UNUSED (<column_name>);
or,
ALTER TABLE <table_name>
SET UNUSED COLUMN <column_name>;
Where,
SET UNUSED COLUMN are the keywords to mark the column
table_name is the table name
column_name is the column name
For example, the New Jersey Central Library decides to drop the cMaritalStatus
column from the Member table. But the database developer or administrator
considers the option of dropping the column during the library hours as not
advisable. This is because the act of dropping the column could come in the way of
users accessing the table.
Therefore, they decide to mark it as unused and drop it probably after library hours
when the demand on the resource is less.
ALTER TABLE Member
SET UNUSED (cMaritalStatus);
Table altered.
281
Though the column data remains in the tables rows, you cannot access it by
using the SELECT * query. Also, the column will not be displayed even by
issuing the DESCRIBE command.
SELECT cMemberID,cBranchID,cMaritalStatus
FROM Member;
Displays the following error message:
SELECT cMemberID,cBranchID,cMaritalStatus
*
ERROR at line 1:
ORA-00904: invalid column name
It will also not be displayed when you describe the structure of the table.
DESCRIBE Member
Name
--------
Null?
Type
---------
-------
CMEMBERID
CFIRSTNAME
CLASTNAME
VADDRESS
CAREA
CZIPCODE
NOT NULL
NOT NULL
CPHONE
NAGE
CHAR(7)
CHAR(10)
NUMBER(2)
DMEMBERSHIPDT
CGRADE
CHAR(10)
DATE
CHAR(1)
CBRANCHID
CHAR(7)
A new column with the same name as the unused column can be added to the
table.
ALTER TABLE Member
ADD cMaritalStatus CHAR (1);
The disk space occupied by cMaritalStatus column, which is marked unused cannot
be restored. However, if you want to reclaim the disk space, you can drop the
SQL Star International Ltd.
282
unused column. Use the DROP UNUSED COLUMNS option to do this. The syntax for
the same is:
ALTER TABLE <table_name>
DROP UNUSED COLUMNS;
To drop the unused column from Member table:ALTER TABLE Member
DROP UNUSED COLUMNS;
This statement removes the marked column from the Member table.
However, if you would have dropped the cMaritalStatus column using the DROP
clause, then
The disk space used by the column is restored. This means that
values of cMaritalStatus are removed from each row in the
table and the space occupied by it is now available for any
further use.
As the disk space has to be restored, the response time taken
by executing the DROP COLUMN clause is slower.
The data stored in cMaritalStatus is lost and cannot be
recovered.
Executing a SELECT * query will not retrieve data from
cMaritalStatus.
283
284
You cannot roll back the DROP TABLE command. Being a DDL command it is
auto
commit by nature.
Flashback Table
A table can be reinstated automatically using a new 10g feature referred to as
Flashback table feature.
Here is a scenario that happens more often:
A user drops a very important table accidentally, and it needs to be recovered as
soon as possible.
Use the Flashback Table feature, which makes the recovery of a dropped table as
easy as the execution of a few statements.
Lets see with an example, how this feature works.
Drop the Member table of the library application
SQL> DROP TABLE Member;
Displays:
Table dropped.
Lets check the status of the table now.
SQL> SELECT * FROM tab;
TNAME
TABTYPE
CLUSTERID
----------------------------------------------cpndanfgMAAAAAANPw==$0
TABLE
The table Member is gone but note
BIN$04LhcpndanfgMAAAAAANPw==$0.
the
presence
of
the
new
table
-------------------
BIN$04LhcpndanfgMAAAAAANPw==$0 TABLE
2007-05-
This shows the original name of the table, RECYCLETEST, as well as the new name in
the Recycle Bin, which has the same name as the new table we saw created after
dropping the table. (Note: The internal Name given may be OS dependent) To
reinstate the table, all you have to do is use the FLASHBACK TABLE command:
SQL> FLASHBACK TABLE Member TO BEFORE DROP;
FLASHBACK COMPLETE.
SQL> SELECT * FROM TAB;
TNAME
TABTYPE
-------------- ----------MEMBER
TABLE
CLUSTERID
-----------
But what if you want to drop the table completely, without needing a flashback
feature. In that case, you can drop it permanently using:
DROP TABLE MEMBER PURGE;
This command will not rename the table to the Recycle Bin name, rather, it will be
deleted permanently, as it would have been pre-10g.
Managing the Recycle Bin
PURGE TABLE Member;
Or using its Recycle Bin name:
PURGE TABLE BIN$04LhcpndanfgMAAAAAANPw==$0;
This command will remove table MEMBER and all dependent objects such as indexes,
constraints, and so on from the recycle bin, saving some space. If, however, you
want to permanently drop an index from the recycle bin, you can do so using:
PURGE INDEX in_Member_01;
This will remove the index only, leaving the copy of the table in the recycle bin.
Truncating Tables
The TRUNCATE TABLE statement is a part of the Data Definition Language that
performs high-speed data deletion.
The TRUNCATE statement deletes data from tables just as the DELETE statement,
but with a slight difference. The difference being in the speed with which the deletion
is performed by the two statements.
The DELETE statement uses the transaction processing control commands for the
completion of its task. That is, the task performed by a DELETE statement is not
considered to be complete unless the task is committed to the database. This makes
SQL Star International Ltd.
286
Data from all rows are deleted, but the table definition including the definition of
constraints remains intact.
The storage space occupied by all rows is released.
As it is a DDL statement, data once deleted cannot be recovered.
287
288
Summary
In this chapter, you have learnt that:
Data Definition commands (DDL) are used to create and manage database
objects in a schema.
Schema is a collection of database object. The word SCHEMA and USER are
interchangeable.
DDL commands are CREATE: creates new database object, ALTER: modify
the structure of the database object, DROP: removes the database object
from the schema, RENAME: gives a new name to the database object,
TRUNCATE: removes rows permanently leaving the structure intact .
New tables can be created using an existing table using a subquery in the
CREATE TABLE statements.
Datatypes such as VARCHAR2, CHAR, NUMBER, DATA and various
TIMESTAMP determine the type of data a column can hold.
FLASHBACK statement can be used to restore the dropped objects.
Dropped objects of a schema can be viewed using SHOW RECYCLE BIN
command.
Table can be removed from the recycle bin using PURGE command.
289
Lab Exercise
1.
2.
3.
Create a table Dept_details based on the details given in the following chart.
Save the syntax in a script named LabEx12_01.sql. Execute the script and
confirm that the table has been created.
Populate the Dept_details table with data from Departments table. Insert
only the columns that you need.
Create a table Emp_details based on the following chart.
290
4. Modify the empFirstName and empLastName columns in the Emp_details
table to allow for longer names.
Table altered.
5.
Create a table emp2 based on the structure of employees table. Include the
columns employee_id, first_name, last_name, department_id, job_id,
and salary into the Emp2 table.
6.
7.
8.
291
Make the job_id column of Emp_details table UNUSED and confirm the
modification.
10
12. Modify the table New_Emp with hiredate as TIMESTAMP WITH TIMEZONE
datatype. View the structure of the table.
13.
292
293
Chapter 13
Managing Constraints
Introduction to Constraints
Defining Constraints at different level
Explain the different types of Constraints
Creating Constraints
Deferring Constraints
Dropping Constraints
Enabling and Disabling the Constraints
Querying Data Dictionary for Constraints
294
Objectives
At the end of this chapter, you will be able to:
Understand Integrity constraints
Maintain Integrity constraints
Understand Deferred Constraints
Understand Dictionaries related to constraints
295
Inserting,
Parent tables (those tables on which other tables are dependent) are not
deleted.
Defining Constraints
Constraints can be defined before or after the table creation. There are two levels for
defining a constraint:
Column level
Table level
296
Types of Constraints
The data integrity constraints used to enforce business rules are:
NOT
NULL: This constraint prevents null values from being entered into a
column.
FOREIGN
297
CHAR (9),
cZipcode
cPhone
CHAR (10),
nAge
CHAR (1),
cBranchID CHAR(7)
);
In the above code, all the NOT NULL constraints are defined at the column level.
Since the constraints are unnamed, the Oracle server will generate names for them.
You can however name the constraints while defining them along with the column
specification as follows:
CFirstName CHAR (20) CONSTRAINT MemberFNameNN NOT NULL,
298
A UNIQUE KEY constraint ensures that no two rows of a table have identical values in
a column or a collection of columns. UNIQUE KEY constraints allow the entry of null
values unless NOT NULL constraint is defined for the same column. A UNIQUE KEY
can be defined at the column level or table level.
How would you ensure that two rows of the Branch table do not have the details of
the same branch? You could do so with the following DDL statement.
CREATE TABLE Branch
(cBranchID
NUMBER (7),
cBranchName
CHAR (10),
DATE
);
In the code, the cBranchName column has been defined as UNIQUE specifying that
the column cannot have duplicate branch names. This is a column level constraint
definition.
A UNIQUE constraint is defined at the table level, usually if it comprises a collection
of columns.
A UNIQUE KEY constraint defined for a group of columns is known as a composite
unique key. It ensures that the combinations of values in the columns are unique.
To define a composite UNIQUE KEY constraint on the Book table to ensure that the
book name and the publishing year of each book are unique, create the Book table
in the following manner.
CREATE TABLE Book
(cBookID
cBookName
CHAR (13),
CHAR (20) NOT NULL,
CHAR (7),
299
cZipcode
cPhone
CHAR (10),
nAge
dMembershipDt DATE,
cGrade
CHAR (1),
cBranchID CHAR(7));
SQL Star International Ltd.
300
In the code, the PRIMARY KEY is defined at the column level for the column
cMemberID. This constraint ensures that, when a row is inserted in the Member
table, the value for cMemberID column is a non-null value and is not same as an
existing column value of another row.
FOREIGN KEY specifies the foreign key column in the child table.
REFERENCES identifies the column in the parent table that the foreign key
column is dependent on.
ON
DELETE CASCADE indicates that when a row in the parent table has
been deleted, its dependent rows in the child table are also deleted.
ON DELETE SET NULL converts foreign key values to null when parent
value is removed. But, without the ON DELETE CASCADE and ON DELETE SET
NULL, you cannot delete rows from the parent table if there are dependent
rows in the child table.
For example, there exists a parent-child relationship between the Book table and the
Transaction table. The Transaction table contains the issue details of those books
that are listed in the Book table. The foreign key constraint is defined on the
cBookID column of the Transaction table (child table or dependent table). It refers
to the cBookID column of the Book table (parent or referenced table), which is the
primary key column.
To ensure that the books issued to members are those that are available in the Book
table of the library, create the Transaction table as follows:
301
cMemberID CHAR(7),
cBookID
CHAR(13),
dIssueDt DATE,
dReturnDt DATE,
dActualReturnDt DATE,
nFine NUMBER(2),
CONSTRAINT TransactionBookIDFK FOREIGN KEY(cBookID)
REFERENCES Book(cBookID)ON DELETE CASCADE,
CONSTRAINT TransactionMemberIDFK FOREIGN KEY(cMemberID)
REFERENCES Member(cMemberID)ON DELETE CASCADE);
When the FOREIGN KEY constraint is defined at the column level, there is a slight
change in the syntax. The FOREIGN KEY keyword is not used while defining it.
The following example illustrates this:
Define a constraint on the Availability table to ensure that it stores the status of
availability of only those books that are stored in the library.
CREATE TABLE Availability
(cBookID CHAR(13) CONSTRAINT AvailabilityBookIDFK
REFERENCES Book (cBookID),
nAvailableCopies NUMBER (2),
cBranchID CHAR (7) CONSTRAINT AvailabilityBranchIDFK
REFERENCES Branch (cBranchID));
In the code, the FOREIGN KEY constraint has been defined at the column level for
both cBookID column and cBranchID column.
302
To check whether the age of persons being entered in the Member table is not
below five, issue the constraint as follows:
CREATE TABLE Member
(cMemberID CHAR (9) CONSTRAINT MemberMemberIDPK PRIMARY KEY,
cFirstName CHAR (20) NOT NULL,
cLastName CHAR (20) NOT NULL,
vAddress VARCHAR2 (50) NOT NULL,
cArea CHAR (10) NOT NULL,
cZipcode CHAR (7) NOT NULL,
cPhone CHAR (10),
nAge NUMBER (2) CONSTRAINT MemberAgeCK CHECK (nAge >5) NOT
NULL,
dMembershipDt DATE DEFAULT SYSDATE,
cGrade CHAR (1)
cBranchID CHAR(7) REFERRENCES Branch(cBranchID));
In the code, the CHECK constraint is defined for the nAge column, which ensures
that the age being entered is not below five.
Points to remember:
303
REFERENCES Category(cCategoryID) ,
cPublisherID CHAR(6) CHECK (cPublisherID IN (SELECT cPublisherID
FROM Publisher)),
dPublishedYr DATE,cBranchID CHAR(7)CONSTRAINT BookBranchIDFK
REFERENCES Branch(cBranchID),
nNoOfCopies NUMBER(2) CONSTRAINT BookNoOfCopiesCK
CHECK(nNoOfCopies < 11));
However, this code is not valid as it refers to the cPublishedID column of the
PublisherDetails table and it displays the following error message:
ORA-02251: subquery not allowed here
Multiple
304
VARCHAR2(20),
...
Job_ID VARCHAR2(10) NOT NULL
SQL Star International Ltd.
305
);
306
Deferred Constraints
Normally, constraints are checked as data is inserted or updated in the row. When a
row is inserted, all the constraints are checked and then, the row is either accepted
or rejected. You can, however, defer the constraint checking until the transaction
commits. In that case, the row is inserted into the table and constraints are not
checked until the COMMIT is issued. If the row then fails, it is removed from the
table. You can change the status of the constraints by setting them as DEFERRED or
IMMEDIATE. In order to change the constraint, it must be set to DEFERRABLE.
307
);
CREATE TABLE Dupdept
(Deptno NUMBER(2) CONSTRAINT dp_pk PRIMARY KEY,
Dname
Loc
VARCHAR2(20),
VARCHAR2(20));
If you try to enter values into Deptno column of Dupemp table, which does not lie
within the domain of the column values of Dupdept table, an error message is
thrown indicating the violation of foreign key constraint.
But, if you still want to add data into the Dupemp table, you can do it by changing
the status of the constraints by setting them as DEFERRED or IMMEDIATE. The
advantage of doing this is it defers constraint checking until the transaction is
committed. In order to change the constraint, it must be set to DEFERRABLE.
ALTER TABLE Dupemp
ADD CONSTRAINT dep_fk FOREIGN KEY(deptno) REFERENCES
dupdept(deptno)
ON DELETE CASCADE DEFERRABLE;
SQL> SET CONSTRAINT dep_fk DEFERRED;
Now, you insert values into detpno of Dupemp table directly without entering the
corresponding deptno value in the main table.
As an example, the above one works perfect. But, why at all we need a deferred
constraint?
Sometimes, it so happens that, we get the data for the secondary table, but not for
the main table. Since there is a referential integrity constraint present on these two
tables, we cannot enter data directly into the second table.
By deferring the foreign key constraints, we can atleast get the data loaded into the
secondary tables, and then validate the data against the main table once it is also
loaded. This saves time. When a COMMIT statement is issued against the secondary
table, constraints are checked and invalid data is rejected.
We can enable the deferred constraint by issuing the following command:
SQL> SET CONSTRAINTS dep_fk IMMEDIATE;
Dropping Constraints
If the organization decides to do away with a business rule, then you will have to
drop the constraint that enforced the rule. Use the DROP clause with the ALTER
TABLE statement to remove a constraint. The syntax for dropping a constraint is:
SQL Star International Ltd.
308
309
In the above codes, the primary key constraint is defined on the column nCol1 of
Tab1. The foreign key constraint is defined on the column nCol1 of Tab2. But the
creation of this constraint fails as it references cCol2 of table Tab1 instead of
nCol1. This automatically disables the Tab2nCol1FK constraint. The error message
displayed is as follows:
ORA-02267: column type incompatible with referenced column type
In the above case, the constraint was disabled automatically as its creation failed.
However, you can disable the constraint without dropping or re-creating it if you do
not want to enforce the rules defined for it. Using the DISABLE clause with the ALTER
TABLE statement can disable a constraint. Disabling a constraint renders it
ineffective, as if it had been dropped. The syntax for disabling a constraint is:
ALTER TABLE <table_name>
DISABLE CONSTRAINT <constraint_name> [CASCADE];
Where,
table_name is the table name
constraint_name is the constraint name
CASCADE option disables a dependent constraint if its primary key constraint is
disabled.
For example you can disable the unique key constraint defined on cBranchName
column of the Branch table.
ALTER TABLE Branch
DISABLE CONSTRAINT BranchBranchNameUK;
In this example you did not have to issue the CASCADE clause because the disabled
constraint did not have any dependent constraints. An example where you will have
to issue the CASCADE clause is to disable the primary key constraint defined on the
cBookID column of the Book table.
ALTER TABLE Book
DISABLE CONSTRAINT BookBookIDPK CASCADE;
This example uses the CASCADE clause because disabling the constraint on
cBookID column will affect the cBookID column constraint of the Transaction
table.
SQL Star International Ltd.
310
Points to remember:
You
can use the DISABLE clause with both the CREATE TABLE as well as
the ALTER TABLE statements.
Ensure that
Enabling Constraints
You must enable the disabled constraints after you have taken the steps needed to
correct the reasons that led to the failure of constraints during their creation. When
constraints are enabled the business rules that were defined would be enforced. The
syntax for enabling constraints is:
ALTER TABLE <table_name>
ENABLE CONSTRAINT <constraint_name>;
Where,
table_name is the table name
constraint_name is the constraint name
Points to remember:
You
can enable only those constraints which have been defined and which
are currently disabled.
When you attempt to enable a disabled constraint, ensure that the data
that had been loaded into the table does not violate the rule that had been defined.
This is because when you enable a constraint, that constraint applies to all the data
in the table.
You can use the ENABLE clause with both the CREATE TABLE as well as the
ALTER TABLE statements.
The disabled UNIQUE KEY constraint of the Branch table can be enabled as follows:
ALTER TABLE Branch
ENABLE CONSTRAINT BranchBranchNameUK;
In this code, the disabled unique constraint is enabled. But, if duplicate branch
names had been entered during the time the constraint was disabled, then the above
code would not execute and would display the following error:
SQL Star International Ltd.
311
The
All
312
If the columns referencing the constraints of the dropped columns are also dropped,
then you need not use the CASCADE CONSTRAINTS clause with the DROP COLUMN
clause.
SQL Star International Ltd.
313
without the
CON
SEARCH_CONDITION
-------------------------
SYS_C001297
NOT NULL
CFIRSTNAME
IS
SYS_C001298
SYS_C001299
VADDRESS
SYS_C001300
CAREA
SYS_C001301
MEMBERAGECK
nAge > 5
MEMBERMEMBERIDPK
MEMBERGRADEFK
IS NOT NULL
IS NOT NULL
8 rows selected.
SQL Star International Ltd.
314
In the code, you are retrieving the name of the constraint, the type of constraint and
the search conditions used with the check constraints. If the user has not named the
constraints, then the query retrieves the names assigned by the server. In the type
of constraints retrieved by the code, C stands for CHECK and NOT NULL, P for
PRIMARY KEY, R for FOREIGN KEY and U for UNIQUE KEY.
WHERE TABLE_NAME=MEMBER;
Displays the output:
CONSTRAINT_NAME
COLUMN_NAME
-------------- -------------MEMBERAGECK
NAGE
MEMBERGRADEFK
CGRADE
MEMBERMEMBERIDPK
CMEMBERID
SYS_C001297
CFIRSTNAME
315
SYS_C001298
CLASTNAME
SYS_C001299
VADDRESS
SYS_C001300
CAREA
SYS_C001301
CZIPCODE
8 rows selected.
Summary
In this chapter, you have learnt that:
Constraint can be defined before or after the table creation. These are
defined
either at the table level(after defining all the columns) or at
the column level(after
each column).
The different types of constraints are: NON NULL: defined only at column
level and
prevents NULL values, UNIQUE: prevents duplicate values,
PRIMARY KEY: prevents
duplicate and NULL values, FOREIGN
KEY: values referred by PRIMARY KEY domain
columns
only,
CHECK: allows values based on a criteria.
You can defer the constraints till the transaction commits using DEFFERED
statement.
316
Lab Exercise
1.
Add a table level PRIMARY KEY constraint to the new_emp table on the
empID column. Name the constraint as emp_id_pk. [Hint: Use the ALTER
TABLE command].
2.
3.
Add the column deptID to the NEW_EMP table. Add a foreign key constraint
on the NEW_EMP table using the deptID column. This ensures that the
employees are not assigned to a department that does not exist. Name the
foreign key constraint as emp_dept_fk.
4.
6.
317
A)
Modify the emp_details table to add the foreign key on department id column.
Also ensure that this constraint can be deferred.
B)
C)
D)
E)
Try to commit the inserted data. Is the data committed? If no, why?
318
Chapter 14
Views
What are
Simple vs Complex
Enforcing Constraints on
Creating Complex
Creating Inline
Data Access from
Views
Views
Views
Views
Views
Views
319
Objectives
At the end of this chapter, you will able to:
Describe the need for different types of views
Use views
320
Views provide security as they can restrict data being accessed by users. For
example, to implement data security, the library may create a view that will
allow the librarian to see only the details of the librarys members, thus,
preventing him from manipulating data pertaining to the books, or the
membership fee structure of the library.
321
Data dictionary views are widely used as they prevent users from directly
accessing the tables of the data dictionary. This is important due to the
following reasons:
To safeguard data stored in the data dictionary tables. If users manipulate
data dictionary tables it could result in loss of data or the table itself being
removed, thus causing serious damage to the Oracle database, even
rendering it completely ineffectual or unproductive.
Data stored within the data dictionary tables are hard to comprehend.
Therefore, dictionary views help to condense data into something easily
comprehensible and helpful.
Simple views
views Complex
Simple views are based on only one table whereas complex views are based
on more than one table. For example, the database developer could create a
simple view based on the Book table. Whereas, he could create a complex
view to show the books that belong to each category by joining the Book
table and the Category table.
Creating Views
To create a view, a subquery must be embedded within the CREATE VIEW
statement. The syntax for creating a view is:
CREATE [OR REPLACE] [FORCE| NOFORCE] VIEW <view_name> [(alias [,
alias])]
SQL Star International Ltd.
322
AS subquery
[WITH CHECK OPTION [CONSTRAINT <constraint_name>]]
[WITH READ ONLY];
Where,
OR REPLACE changes the definition of a view without dropping
or re-creating it.
FORCE forces the creation of a view even without the existence
of a base table.
NOFORCE this is the default option. It creates a view only if
the base table exists.
view_name is the name given to the view that is created.
alias specifies the names for the expressions which will hold the
values of the columns selected by the subquery.
subquery is the SELECT statement which retrieves the columns
that the view must have.
WITH CHECK OPTION is a type of CHECK constraint, which
specifies that only those rows that are selected by the view can
be inserted, updated or deleted.
CONSTRAINT constraint_name assigns a name to the CHECK
OPTION constraint.
WITH READ ONLY prevents any DML operations from being
performed on the view.
Points to remember while creating views:
The subquery can contain complex SELECT statements, that is, SELECT
statements, which include joins and group functions.
For example, in order to prevent the librarian of the Randolph NJ Library from
accessing or modifying the book details of other branches, the library database
developer creates a view that shows details of only those books that are stored in
the Randolph NJ Library. In order to create this view the developer would have to
execute the following code:
SQL Star International Ltd.
323
On executing the code, vwRandolpBook view gets created containing book details
of only the Randolph branch, which is identified by its ID 04RANNJ. The structure of
the vwRandolpBook view can be displayed by issuing the iSQL*Plus DESCRIBE
command.
DESCRIBE vwRandolpBook
324
in the Princeton branch (which has been assigned an ID 05PRINJ), the library
database developer decides to create a view that will contain details about all the
books the Princeton branch stores. But, the developer must ensure that the
librarian accessing the view does not make any changes to the data stored. To
ensure this the following code must be executed:
CREATE OR REPLACE VIEW vwPrincetonBks
AS SELECT *
FROM Book
WHERE cBranchID=05PRINJ
WITH CHECK OPTION CONSTRAINT PrincetonBkCK;
View created.
The code creates a vwPrincetonBks view with the WITH CHECK OPTION clause. By
specifying the WITH CHECK OPTION constraint, any attempt made by the librarian to
manipulate the branch ID for any row in the view will fail.
For example, if the branch ID of the book were changed to 01ADLNJ, the view
would no longer be able to select that book and therefore the following DML
operation performed fails:
UPDATE vwPrincetonBks
SET cBranchID=01ADLNJ
WHERE cBookID=FIC050003232;
Displays an error:
Error at line 1:
ORA-01402: view WITH CHECK OPTION where-clause violation
The WITH READ ONLY option
Using the WITH CHECK OPTION ensures that the DML operations being performed
are within the scope of the view, that is, DMLs are performed on rows that the view
can select. But, how to ensure that no DML operations are performed?
This can be ensured by using the WITH READ ONLY option. This option prevents any
DML operation from being performed on the view. If any attempt is made to perform
DMLs on any row in the view, the Oracle server will generate an error.
For instance, if the DBA creates a view for the clerk Brad Riley, he needs to ensure
that Brad Riley does not write into the view. The following code will help:
CREATE OR REPLACE VIEW vwHamiltonBks
AS SELECT * FROM Book
WHERE cAuthorName=Shakespear
AND cBranchID=03HAMNJ
WITH READ ONLY;
SQL Star International Ltd.
325
View created.
The view vwHamiltonBks is created by adding the WITH READ
ONLY option, which
prevents any DML operations from
being performed on it.
If Brad Riley attempts to delete any row from the view, he
encounters the following error:
DELETE FROM vwHamiltonBks
WHERE cBookName=Othello;
Error at line 1:
ORA-01752: Cannot delete from view without exactly one key-preserved table
Similarly, if he attempts to add or modify a row, the Oracle
server would generate the following error:
UPDATE vwHamiltonBks
SET nNoOfCopies=6
WHERE cBookName=Othello;
ERROR at line 2:
ORA-01733: virtual column not allowed here
The user cannot insert, update or delete data on the underlying table, if the
view itself cannot select that data. This happens if the WITH CHECK OPTION
is used.
The user cannot insert, update or delete data on the underlying table, if the
subquery that defines the simple view contains:
The user cannot perform any DMLs if any references to virtual or pseudo
columns such as ROWID, CURRVAL, NEXTVAL and ROWNUM are made by the
326
The user cannot perform any DMLs if the WITH READ ONLY option is used in
the subquery.
The user cannot add data into the underlying table if its NOT NULL column is
not selected by the subquery. For example, the librarian cannot insert data
into the Book table if the vwRandolpBook view does not select the
cBookID column. This is because cBookID is a primary key column implying
that it cannot contain null values. (Remember that you are attempting to
insert values directly into the base table through the view.)
GROUP BY clauses
Complex views are created for concealing complexities involved
in retrieving data such as the performance of joins or using
group functions to obtain the result set.
For example, members may request the librarian to provide a
list of all the books stored in the Randolph NJ Library branch
complete with their category names. To do so the librarian
would have to cross-check both the Book as well as the
Category tables. This proves to be quite taxing, especially if
queries of the similar kind increase. To overcome this, the
developer creates a view by joining the two tables. The librarian
then needs to only query this view without even knowing that it
is based on two tables. Executing the following code would
create the view:
CREATE OR REPLACE VIEW vwRandolphBooks
AS SELECT B.cCategoryID, C.cCategoryName, B.cBookID, B.cBookName,
B.cAuthorName,B.dPublishedYr,B.nNoOfCopies
FROM Book B,Category C
WHERE B.cCategoryID=C.cCategoryID
AND B.cBranchID=04RANNJ;
View created.
On the execution of the code, a complex view gets created
wherein the subquery contains a join condition.
327
SELECT cCategoryName,cBookName
FROM vwRandolphBooks;
CCATEGORYNAME
------------Math
CBOOKNAME
Children
.....
..............
.....
..............
38 rows selected.
But, if the library clerk wants to know the total number of
copies of books of each category that the library stores, then
the above view would not serve the purpose. To do so, the
developer would have to create a view by executing the
following code:
CREATE OR REPLACE VIEW vwBookCopies
(Category,TotalCopies)
AS SELECT C.cCategoryName,SUM(nNoOfCopies)
FROM Book B,Category C
WHERE B.cCategoryID=C.cCategoryID
GROUP BY C.cCategoryName;
View created.
On executing the code, a complex view vwBookCopies gets
created that contains a group function.
The clerk can display the contents of the view by executing a
SELECT statement.
SELECT *
FROM vwBookCopies;
The statement retrieves the following result set:
CATEGORY
---------Archaelogy
Children
TOTALCOPIES
--------25
136
328
Classic
92
Computers
36
Fiction
55
History
52
Humor
74
Math
56
Mechanical
31
Mystery
83
Non Fiction
58
Psychology
40
Romance
59
Science Fiction
45
14 rows selected.
329
Only unique, primary key and foreign key constraints can be specified on
views.
MAXCOPIES
--------
01ADLNJ
02CHSNJ
03HAMNJ
04RANNJ
05PRINJ
9
The developer would have to then cross check the query output
with the Book table in order to find out those books whose
number of copies stored is less than the maximum number of
copies of books stored in their branches.
330
---------
--------
------
01ADLNJ
2
..
...
-------01ADLNJ
. .
Oracle7 and Oracle8 did not allow the use of the ORDER BY clause within subqueries.
But since Oracle8i you can use the ORDER BY clause with inline views.
The following code will illustrate the use of the ORDER BY clause:
SQL Star International Ltd.
331
SELECT M.Name
FROM (SELECT cFirstName||cLastName Name
FROM Member
ORDER BY Name) M;
The query displays the following result:
NAME
----------------Andre
Cobb
Austin
Dinsmore
Bridgette
Wells
Celena
Stevenson
Cresinda
Finch
Darla
Green
Derian
Bates
Eveangela
Gammons
Ferdinand
Swander
----------
-------
21 rows selected.
The Inline view retrieves a sorted list of members names by
concatenating their first names with last names. This inline
view defines the data source for the SELECT statement. The
execution of the code does not result in any error despite the
use of the ORDER BY clause.
332
The ROWNUM pseudocolumn, which assigns successive values to each
of the rows returned by the subquery, and
The WHERE clause, which specifies the number of rows that are to be
returned. The WHERE clause must use a < or <= operator.
The outer query limits the number of rows by WHERE ROWNUM <= N.
Using Top-N Queries
If the developer has to find out the four most recently
registered members of the library, he would have to issue the
following SELECT statement:
SELECT ROWNUM NEW MEMBERS,M.Name, M.MembershipDt
FROM (SELECT cFirstName||cLastName Name, dMembershipDt MembershipDt
FROM Member
ORDER BY dMembershipDt DESC) M
WHERE ROWNUM <= 4;
In the code, the inline view M returns the names and the
membership dates from the Member table, which are sorted in
the descending order of the membership dates. The outer query
selects the pseudocolumn ROWNUM, which assigns sequential
values to the rows that are returned by the inline view. The
rows returned are limited by the
WHERE ROWNUM
<= 4 clause, which ensures that only four rows from the
resultant rows are displayed.
NEW MEMBERS
-------------
NAME
-----------------
MEMBERSHIP
--------333
Norbert
Cambell
Johna
03-MAR-01
Butler
Eveangela
Gammons
Ferdinand
Swander
19-SEP-00
10-APR-00
03-MAR-00
Using Views
The users using views find that accessing data from the views
is similar to accessing data from a table. They can even modify
or remove views just as they would do to a table.
Data Access from Views
Before you access data from a view, you need to confirm its
creation by querying the USER_VIEWS data dictionary table. In
order to display the contents of USER_VIEWS issue the
DESCRIBE command.
DESCRIBE USER_VIEWS
Name
---------
Null?
Type
---------
VIEW_NAME
---------
NOT NULL
VARCHAR2(30)
TEXT_LENGTH
TEXT
NUMBER
LONG
TYPE_TEXT_LENGTH
NUMBER
TYPE_TEXT
VARCHAR2(4000)
OID_TEXT_LENGTH
NUMBER
OID_TEXT
VARCHAR2(4000)
VIEW_TYPE_OWNER
VARCHAR2(30)
VIEW_TYPE
VARCHAR2(30)
334
335
definition of the view with the new one by overlooking the error
that arises on encountering an already existing view with the
same name. The following code illustrates the use of the
CREATE OR REPLACE VIEW:
For example, the user of the vwRandolpBook view may
suggest the developer that more meaningful names be
assigned to the columns as they currently have the same
names as the columns of the underlying Book table. To do so
the developer would have to modify the view by specifying
column aliases.
CREATE OR REPLACE VIEW vwRandolpBook
AS SELECT cBookID BookId,cBookName BookName,cAuthorName Author,
cCategoryID CatgID, cPublisherID PublisherID,
dPublishedYr PublishedYr, nNoOfCopies Copies
FROM Book
WHERE cBranchID=04RANNJ;
View created.
In the code, the OR REPLACE option replaces the old version of the
vwRandolpBook view with the new one. The execution of the above code
controls the column names by
specifying aliases in the subquery.
The column aliases can also be specified in the CREATE VIEW clause. But
they must be listed in the same order as the columns that are selected by
the subquery. The following code will illustrate how to specify column
aliases in the CREATE VIEW clause:
CREATE OR REPLACE VIEW vwRandolpBooK
(BookID,BookName,Author,CatgID,PublisherID,PublishedYr,Copies)
AS SELECT cBookID,cBookName,cAuthorName,cCategoryID,
cPublisherID,dPublishedYr,nNoOfCopies
FROM Book
WHERE cBranchID=04RANNJ;
View created.
Just as there is the ALTER TABLE statement to modify the table
structure, similarly there is an ALTER VIEW statement available
in the Oracle SQL language. The syntax is:
ALTER VIEW view_name;
But instead of being used to modify the view structure, it is used to recompile a
view that becomes invalidated either due to:
336
To rectify the invalidated view you must either recreate the underlying table and
issue the ALTER VIEW statement or modify the view by using the CREATE OR
REPLACE VIEW statement.
Removing Views
There may arise situations that would require the developer of
a view to remove it from the database.
The statement used to remove a view is the DROP VIEW
statement. Removing a view does not affect the table on which
it was based. Only the view definition is removed from the
database. The syntax for dropping a view is:
DROP VIEW <view_name>;
Where,
view_name is the name of the view that is to be dropped
For example, the developer creates a view to display the
addresses of all the library members, based on the requirement
made by the librarian for the purpose of sending mails to them.
The developer executes the following code to create the view:
CREATE OR REPLACE VIEW vwMemberAddress
AS SELECT cFirstName,cLastName,vAddress,
cArea,cZipcode
FROM Member;
View created.
After querying the vwMemberAddress view, the librarian
would be able to retrieve the addresses of members and
accordingly send mails to them.
SELECT *
FROM vwMemberAddress;
CFIRSTNAME CLASTNAME
SQL Star International Ltd.
VADDRESS
CAREA
CZIPCODE
337
--------
--------
Austin
-----------
--------
-------
Allenbury NJ01956
Fourth Cross Rd
Bridgette
Ridgeland
NJ04352
Redford Township
Celena Stevenson 19-22A, Free Gallop Villa,
Roxbury
NJ90231
Greenwich Hallway Rd
Ferdinand Swander 909D, Sunrise Marble Apts,
Stanhope NJ12543
Springwood Lane
---------------------21 rows selected.
After sending mails, the librarian would no longer need to query
the view as his purpose has been met. Therefore, the developer
(on being told by the librarian) would decide to remove the
view from the database.
The developer of the vwMemberAddress view would execute
the DROP VIEW statement to drop the view.
DROP VIEW vwMemberAddress;
View dropped.
Execution of this code removes the vwMemberAddress view
from the database.
338
Summary
In this chapter, you have learnt that:
Views are virtual tables based on the underlying database tables. They can
be visualized as a window to a base table.
Views provide restrictive access to the base table, hide the complexity of
joining multiple tables and can provide specific information for different
users.
Views based on single table are known as simple views. DML operations can
be perfomed on these views directly.
Views based on Multiple tables are known as complex views. DML operations
can be done on these views through triggers only.
Views are created using CREATE VIEW statement and modified using
CREATE OR REPLACE VIEW statement.
Constraints can be applied on views using WITH CHECK options and READ
ONLY clauses.
339
Lab Exercise
1.
2.
340
3.
4.
341
6.
Create an inline view to display the last name, department number, salary
and average salary for all those employees who earn a salary less than the
average salary in their respective departments.
On querying the view, the following result should be displayed:
7.
Write a query to display the last names and salaries of the top 10 salary
earners. The query should display the following result:
342
Chapter 15
343
Objectives
At the end of this chapter, you will be able to:
State the need for Indexes
Use Indexes
Define the role of Sequences
Use Sequences
Use Synonyms
Indexes
When you request for a particular row of data, the server does a full table search and
when the requested row is found, it is retrieved and displayed. The process of full
table scan takes a lot of time and also uses up a lot of the system resources. To
quicken the process, you can create an index. An index is a database object with
which you can increase the speed of data retrieval.
Following are the uses of indexes:
Indexes ensure that all data in a column with an index is unique.
Indexes ensure faster retrieval of data.
Creation of Indexes
Indexes are created to enforce integrity constraints for uniqueness. Indexes are
created either:
Automatically
Manually
Automatic Creation
Automatic indexes are called so, because the Oracle server creates them. These
indexes are created when a table with a column having the primary key or the
unique key constraint, has been created. When the constraint and its supporting
index is created and the column values are put into it, the index takes its name after
the constraint. In case you do not name the constraint when creating the table, the
system gives the index a name like SYS_28349240. Automatic indexes are created
basically to enforce the uniqueness check. The difference between an index created
with the primary key and that with a unique constraint index is that the former does
not allow NULL values and the latter does.
344
There could be requirements in a database for indexes other than the ones that are
automatically created. These need to be created manually.
Manual Creation
Manual indexes are created using the CREATE INDEX statement. They are not
different from the indexes that are created automatically. The only way you can
differentiate an automatically created index from a manually created one is from its
name. You would follow some naming convention of your own to name the index you
create. For example, an index of a primary key in the Member table of the library
database could be called idxMemberPK. The Oracle server follows its own
conventions for naming indexes.
The syntax for creating an index is as follows: [This is not the complete syntax. It is
dealt in depth later in this topic.]
CREATE INDEX <index_name>
ON <table_name> (column[, column]);
Where,
index_name is the name you assign to the index you are creating.
table_name is the name of the table whose column you are attempting to index.
column is the name of the column in the table you are trying to index.
You can create non-unique indexes to speed up the accessing of rows. For instance,
the library desk officer has to frequently query the Book table to retrieve books
based on their author names. To speed up the access time, the database developer
could create an index on the author name column using CREATE INDEX statement as
follows:
CREATE INDEX idxAuthorName
ON Book (cAuthorName);
On successful creation of the index, you get the following message:
Index created.
Types of Indexes
The types of indexes available are:
B-tree index
Bit-map index
Non Unique Index
Compressed Index
Function-based index
345
B-Tree Index
B-tree is the traditional index of the Oracle database and is based on a complex
algorithm of sorting. Data in a B-tree is stored in a top to bottom manner resembling
an inverted tree. This kind of data storage is based on the binary search tree
concept. The highest level of the data store is called the root-node and is depicted on
top of the inverted tree. The data is in a sort of a list and is arranged in a layered
and branching manner.
When the values are stored in this manner the data does not have to be searched in
a sequential manner, which is time consuming. It is suggested that data should be
stored in this manner when the column has many distinct values or when the column
is unique.
The B-tree consists of branch nodes, leaf nodes and data blocks.
The algorithm of a B-tree is as follows:
1. Data is stored in the form of a list with the midpoint of the list as the rootnode. Each sections midpoint is in turn made the next level and so on.
2. The data to be searched is compared with the values at the midpoint. If the
value is greater, then the values below the midpoint are eliminated. If the search
value is lower than the midpoint value, the values above the midpoint are
eliminated.
3. This process is performed with every successive section and its midpoint, till
the value is found.
346
From the above tree structure if we want to find the value of 45, we start from the
top of the tree and move towards the right. We would read the block and find that
we should go to the block in the range less than 50 to 40. This block will be the leaf
block and point to the rows containing the number 45. Note that the blocks in the
leaf nodes are doubly linked. This would help us in solving range scans because all
we need to do is to identify where to start in the leaf node and once its found doing
an ordered scan of values is very easy.
SQL Star International Ltd.
347
For example,
To solve the query,
SELECT * from <table name>
where x between 20 and 30;
Oracle will simply find the index block that contains 20 and from there the scanning
goes horizontally till 30. Here the advantage is that it does not have to navigate
through the entire tree structure again.
Bitmap
A bitmap index is structured like a matrix where the data value in a table column is
shown in columns along one axis of the matrix. The other axis contains the ROWIDs.
Every value is represented in one column; hence if a column in a table has too many
distinct values the index will become larger. Therefore, this kind of index is best
applied to a table where there are as few distinct values (also referred as low
cardinality columns) as possible.
Indexes are also classified as unique and non-unique depending on the data that
they store.
Consider the Member table in the Library example explained in this book. Two
columns are ideal candidates for bitmap index namely cMaritalstatus (Only Y/N) and
cGrade (A/B/C/D).
MEMBERID
--------
CGRADE
-------
CMARITALSTATUS
---------------348
CDB028504
BAD109001
CBW109702
ACS099903
DFS030005
BKF079702
CNC030103
BLB089904
...........
........
.........
etc
Once the bitmap index is created on CMARITALSTATUS, Oracle will store
something like the following table.
Cmaritalstatus_bitmap_ind
Considering bitmap index being created for Cgrade the structure would be
Cgrade_bitmap_ind
349
In the first row marital status is N so the bit N is 1, in the third row the marital
status is Y so the bit is 1 and the value for N is 0 so the bit is 0 and so on.
In the Bitmap index of cGrade, first row is C so the bit Corresponding is 1.
Similarly, in the 5th row, the value for grade is D; hence, it is equivalent bit in
column D is 1.
Consider a query,
SELECT count(*)
FROM Member
WHERE cmarital status = N AND Cgrade IN(C,B);
The above query involves the bitmap of N, C and B. While processing, the bitmap
N C and B will be taken as
N
1
1
1
0
0
1
1
0
0
1
AN
D
1
1
0
0
1
0
B
0
1
OR
0
0
0
1
0
1
The result of OR operator combined with AND is calculated to fetch the final
resultant.
350
From the Bitmap result there are 5 records satisfying the condition, because there
are 5 ones (1) in the result set.
As discussed earlier, the server creates unique indexes automatically, when a column
of a table is set with the unique or primary key constraint. The structure of a unique
index is a B-tree.
Non-unique
The non-unique index is manually created on a column, which is most frequently
used for queries. The data in the column may contain duplicate values. This could be
an index on a foreign key column of a table.
Compressed Index
SQL Star International Ltd.
351
This type of index saves the disk storage by storing the repeated portion of the
column value (Key), just once, depending on the degree of compression. There are a
lot of real applications that can use this type of index effectively.
Consider an application which maintains a table for storing object details created for
an application like owner, object type and object name. Whenever new objects are
created it gets a new entry in the table. When application wants to retrieve that
information it can have a compressed index, which saves lot of space in the disk.
Considering the three columns mentioned above the data would be something like
this:
SCOTT.TABLE.MYTAB
SCOTT.TABLE.EMP
SCOTT.TABLE.DEPT
SCOTT.PROCEDURE.P1
PROCEDURE.P2
....
...
An uncompressed index for the above data would be like the sample data above,
which is a concatenated index on all the three columns.
For a compressed index with a degree of compression 1 it will be
CREATE INDEX myindex ON <tablename>(OWNER,OBJECTNAME,OBJTYPE)
COMPRESS 1;
In this case the storage would be,
The above Index would definitly occupy less space ,since the key (username) is
stored only once.
352
Function-Based Index
Another kind of index is a function-based index. This index is created based on the
expression used in the SELECT statement or other DDL statements.
This query helps you to use the index to search for data rather than do an entire
table scan. To ensure this, you need to specify the WHERE clause with the condition
that the column of the values of the expression does not contain a NULL. If you do
not specify the clause, the server may scan the entire table.
For instance, the following query could have been implemented with a complete table
scan if the WHERE clause was not specified:
SELECT * FROM Transaction
WHERE nFine IS NOT NULL
ORDER BY nFine;
Prior to Oracle9i, all the columns used in the function-based index had to be declared
NOT NULL. But from the 9i release, even if the expression contains a NULL value, the
built-in operators recognize them as NOT NULL and give a result that is NOT NULL.
Therefore, the function-based index will contain NOT NULL values and the index-only
scan can be performed on it.
353
Creating an Index
The process of creating an index comes into picture when you need to create it
manually. Unique and primary indexes are automatically created by the system when
the respective constraints are set on table columns. These indexes can also be
manually created by using the CREATE INDEX statement. You need to specify the
type of index to be created with the CREATE INDEX statement. For instance, the
syntax to create a unique index is as follows:
CREATE UNIQUE INDEX <index name>
ON <table name> (column_name);
Where,
CREATE INDEX is the key word to create the index
UNIQUE is the keyword that specifies that a unique index is to be created
<index name> is a name for the index
<table name> is the table on which the index is to be created.
column_name is the name of the column on which the index has to be created
For example, for faster retrieval of data from the NonMember table, you can create
a unique index on the nSerialNo column as follows:
CREATE UNIQUE INDEX idxNonMemSer
ON NonMember(nSerialNo);
SQL Star International Ltd.
354
Index created.
You can also create an index:
On more than one column thus creating a composite index
Of bitmap type by using the BITMAP keyword instead of UNIQUE
However, creating an index is not very easy. You need to think about query
performance, database performance and size and requirement of the indexes to be
created. An index is needed to be created according to the requirements of a table.
You need to decide when to create a B-tree index and when to create a bitmap
index. If you create many indexes on a table it does not imply that the data retrieval
speed increases. On the contrary, any manipulations to the table data needs to be
reflected on the indexes and this could be a time taking process.
Some points to help you decide when it is best to create indexes are given below:
A particular column (or columns) is frequently used in the WHERE clause of a
SELECT statement or in a JOIN condition.
The column holds a large range of values or a large number of NULLS.
The table is of a large size and the result set of a query is less than 2-4 % of
the rows in the table.
There are some cases, like the following, where creating an index may not prove to
be useful:
If the table in question is small in size
If the columns of a table are not frequently used in a query
If the result set of a query is expected to be about 2-4 % of the rows in the
table
If the table requires frequent updates. In this case, every time the table is
updated all the indexes on the table need to be updated too.
Altering an Index
You cannot make many changes to an existing index directly. If you want to change
the structure of an index, you need to first remove the index completely and
recreate it with the new specifications.
View Index Structure
You can check if there are any indexes that have been created. Whether created
manually or automatically, all indexes are stored in the database with their structure
definition. You can confirm the existence of the indexes in a database by using the
USER_IND_COLUMNS. The information stored in this data dictionary is the name of
the index, the column name on which the index is created and the table name.
355
The ALL_INDEXES data dictionary view contains information about the storage of
indexes. The names of the tables of the indexes are also stored. The USER_INDEXES
data dictionary view displays the names of indexes and the type of index it is (that
is, its uniqueness).
Using the data dictionary views, confirm the creation of your indexes.
COLUMN_NAME
-----------
--------
COL_POS
UNIQUENES
----------
IDXAUTHORNAME
CAUTHORNAME
BOOKBOOKIDPK
CBOOKID
IDXNMEMSER
NSERIALNO
NONUNIQUE
UNIQUE
UNIQUE
Removing Indexes
Once indexes are created and if the users no longer require them, they can be
deleted. You can remove an index by using the DROP INDEX command. When an
index is dropped, it is removed from the data dictionaries and indexed searches are
no longer performed on the database. If the index was based on a constraint like
UNIQUE, then it will not enforce the constraint. The syntax for removing the index is:
DROP INDEX <index_name>
The unique index that you created on the NonMember table can be deleted as
follows:
DROP INDEX idxNMemSer;
Index dropped.
You can verify if the index is deleted by viewing the USER_INDEXES data dictionary
view.
SELECT index_name
SQL Star International Ltd.
356
FROM USER_INDEXES
WHERE table_name=NONMEMBER;
The above statement displays:
no rows selected.
357
Data Warehousing
Time-series applications
The syntax to create an Index Organized Table includes an additional
ORGANIZATION INDEX clause in the CREATE TABLE command.
358
Null?
--------
Type
--------------VARCHAR2(30)
NUMBER
DATE
VARCHAR2(4000)
VARCHAR2(30)
VARCHAR2(255)
VARCHAR2(128)
VARCHAR2(30)
VARCHAR2(30)
VARCHAR2(65)
NUMBER(38)
VARCHAR2(30)
VARCHAR2(255)
NUMBER
NUMBER(38)
NUMBER(38)
.....
.....
359
The above plan indicates that FULL access is done on the Member table.
We can also see the COST of executing the statement (COST=3), expected number
of rows to be returned and how many bytes of output will be returned.
Case 2.
Let us create an index on the cArea column and re-execute the SQL statement.
360
Explained.
SQL> @<Oracle_home>\RDBMS\admin\utlxpls.sql
PLAN_TABLE_OUTPUT
-------------------------------Plan hash value: 1648515940
--------------------------------------| Id | Operation | Name | Rows | BytesCos(%CPU)| Time |
-------------------------------------------| 0 | SELECT STATEMENT |
| 2 | 24 | 1 (0) |00:00:01 |
INDEX RANGE SCAN|MYINDEX | 2 | 24 | 1 (0) | 00:00:01 |
---------------------------------------------
|* 1 |
Here the output very clearly shows that the query uses index access (MYINDEX).
Note that the cost of executing the statement (COST=1) is much less here compared
to the previous case.
Sequences
A sequence is a database object used to automatically generate sequential integers
for primary key columns.
Sequences generate and store numbers, which are not dependent on any particular
table. Hence, sequences can be applied to any table that requires a unique identifier
for each row.
The purposes of generating sequences are:
The integer values are automatically generated by an internal Oracle server
routine and therefore, time is saved that would have been otherwise required
to write a sequence generating application code.
Sequences that are generated are independent of any table. Therefore,
multiple users can use the same sequence.
SQL Star International Ltd.
361
Sequences are most commonly used to populate those primary keys that are not
used for accessing data stored in tables. This is because there are drawbacks
associated with generating sequences to populate primary key columns.
For example, the IDs assigned to members, help to identify them as being unique
from one another. But if these unique IDs are created by sequences, they could
result in some gaps in the sequential values generated, thereby rendering the
primary key values meaningless.
These gaps in sequences result in the following circumstances:
If inserting sequence-generated values into the primary key column fails, the
statement gets rolled back and the number is lost.
If there is a system crash while generating sequential numbers, the number is
lost.
As the same sequence can be used for multiple tables, each table will have
different values for their primary keys. For example, sqSerialNo sequence is
used for inserting ten rows into the Book table. By doing so, its primary key
would have the values from 1 to 10. Later, if the same sequence were used to
insert ten rows into the Member table, its primary key would have values
beginning from 11 to 21. This shows that any subsequent use of the same
sequence by the Book or the Member table would generate different value
Using Sequences
You can create sequences based on some rules and clauses. These help the
sequences to generate unique integers in a special order. You can also ensure that
their performance is optimized.
Creating Sequences
The syntax to create a sequence is as follows:
CREATE SEQUENCE sequence
[START WITH n]
[INCREMENT BY n]
[(MAXVALUE n | NOMAXVALUE)]
[(MINVALUE n | NOMINVALUE)]
[(CYCLE | NOCYCLE)]
[(CACHE n | NOCACHE)];
Where,
sequence is the name of the sequence that will generate the numbers.
SQL Star International Ltd.
362
363
You can view and confirm the information on the structure of a sequence by using
the USER_SEQUENCES data dictionary.
The structure of USER_SEQUENCES can be verified by issuing the DESCRIBE
command.
DESCRIBE USER_SEQUENCES
Displays:
Name
------------
Null?
---------
SEQUENCE_NAME
Type
---------
NOT NULL
VARCHAR2(30)
MIN_VALUE
NUMBER
MAX_VALUE
NUMBER
INCREMENT_BY
NOT NULL
NUMBER
CYCLE_FLAG
VARCHAR2(1)
ORDER_FLAG
VARCHAR2(1)
CACHE_SIZE
LAST_NUMBER
NOT NULL
NOT NULL
NUMBER
NUMBER
You can retrieve the minimum and the maximum values specified for the sequences,
along with their incremental values by querying USER_SEQUENCES as shown below.
SELECT SEQUENCE_NAME,MIN_VALUE,
MAX_VALUE,INCREMENT_BY,LAST_NUMBER
FROM USER_SEQUENCES;
Displays the following result:
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAST_NUMBER
------------- --------- --------- ------------ ----------SQSERIALNO
1
1000
1
27
364
When you access NEXTVAL, the value in CURRVAL is the accessed value of NEXTVAL
and the previous value in CURRVAL is lost.
The values are generated according to some rules that are set when developing the
sequence.
Given below is a list of restrictions on sequences:
You can only display data from the pseudocolumns and not insert data into
them.
Avoid using the pseudocolumns in an INSERT and UPDATE triggers. This
action causes the session in which the trigger has been fired to end
abnormally.
You cannot use them when selecting from a view.
SELECT statements with the DISTINCT keyword, GROUP BY, HAVING or
ORDER BY clauses cannot include these columns.
They cannot be referenced in a DEFAULT expression in a CREATE TABLE or
ALTER TABLE statement.
You can, however, reference these values in:
The
The
The
The
The New Jersey Central Library maintains a table to store details of non-members,
that is, those, whose membership has been terminated either due to failure in
returning borrowed books after repeated reminders, or those who have terminated
SQL Star International Ltd.
365
the membership on their own will. The sequence sqSerialNo can therefore be used
for the purpose of inserting rows into the NonMember table as shown below.
INSERT INTO NonMember (nSerialNo, cMemberID,
cFirstName, cLastName, cPhone,dMembershipDt)
VALUES (sqSerialNo.NEXTVAL,CAC0989,
Angela,Clarke,9082233,12-SEP-89);
1 row created.
To view the current value issue the following statement:
SELECT sqSerialNo.CURRVAL
FROM DUAL;
CURRVAL
----------1
Some points that you need to remember while using sequences:
You can access the sequence values faster by caching the values. When a
reference is made to the sequence, some values are put into memory.
Sequence
values are retrieved from the cache each time it is requested.
Once the values in cache are over, the next request for a sequence value
causes another set of values to be cached.
Gaps in a sequence are not advisable. Sequences are generated in a
sequential manner, but incase a ROLLBACK is done, the value is lost. In the
event of a system crash the values in cache are lost. Sequences can be used
for more than one table and thus in such a case gaps in the sequences will be
seen in each table that it is used.
You can view the next value in a sequence without incrementing it, even if
you specified NOCAHCE while creating the sequence.
Manipulating Sequences
You have seen that you can alter a table in case you want to change the definition of
some column. Similarly, you can also change the definition of a sequence.
In case you want to increase the maximum value of the sequence, you can change
its definition to include a higher value.
The syntax do so is:
ALTER SEQUENCE sequence
[INCREMENT BY n]
SQL Star International Ltd.
366
[(MAXVALUE n | NOMAXVALUE)]
[(MINVALUE n | NOMINVALUE)]
[(CYCLE | NOCYCLE)]
[(CACHE n | NOCACHE)];
The syntax is the same as that for creating the sequence, but the name used is the
name of the sequence already created and needing modification. You can change any
parameter of a sequence except the START WITH parameter.
Some points to remember when modifying a sequence are:
You need to be the owner of the sequence and have the right to alter it.
You need to be careful when changing a sequence as the future values
generated are the ones that are affected by the change.
If you want to change the starting value of the sequence then you need to
drop and recreate the sequence as this value cannot be changed using the ALTER
SEQUENCE statement.
Care must be taken to ensure not to reset the maximum value to a value
lower than the current sequence number.
Suppose you have reached the maximum value specified for the sequence
sqSerialNo, then the sequence stops generating any additional numbers and
displays an error stating that the sequence has surpassed the MAXVALUE. If
however, you wish to continue using the sequence, then you need to modify it. But
while modifying, if you specify a maximum value lower than the current sequence
number generated, you would encounter an error. To view the error, issue the
following ALTER SEQUENCE statement:
Dropping a sequence
367
You can delete a sequence from the data dictionary by using the DROP SEQUENCE
statement.
The syntax is:
DROP SEQUENCE sequence;
If the purpose for which the sequence was created has been accomplished, you can
remove it.
DROP SEQUENCE sqSerialNo;
Synonyms
You now know how to create various database objects such as tables, views, indexes
and sequences. However, when it comes to accessing these objects owned by some
other users, then you need to prefix the object names with the names of their
owners. This proves to be quite cumbersome especially when the names are lengthy.
Hence, an object known as synonyms, which simplifies the accessing of objects
owned by other users can be created.
Synonyms enable the users to provide alternative names to objects and thereby do
away with the need to prefix object names with their owners name. The syntax to
give another name to an object is as follows:
CREATE [PUBLIC] SYNONYM <synonym_name>
FOR <object_name>;
Where,
PUBLIC specifies that the synonym created can be accessed by all users.
synonym_name is the alternative name given to the desired object.
368
object_name specifies the object (table, view, sequences or indexes) for which a
synonym is to be created.
Using Synonyms
Synonyms are of two kinds, PUBLIC and PRIVATE. Owners of database objects create
private synonyms, especially when their objects have lengthy names, which make
them difficult to remember. For instance, Lisa may decide to create a synonym for
the vwRandolphBook view created by her. To do so, she would create the following
private synonym:
Only the user DBA creates a public synonym, so that it could be accessible to all
users. For example, the user DBA decides to create a public synonym for Brads
NonMember table as follows:
CREATE PUBLIC SYNONYM NonMember
FOR Brad.NonMember;
After creating a synonym, the user or the user DBA could drop it using the DROP
SYNONYM statement. However, only the user DBA can drop a public synonym as
follows:
DROP SYNONYM BridgetonBk;
Synonym dropped.
Issuing this statement removes the synonym from the database.
369
Summary
In this chapter, you have learnt that:
An index is a database object with which you can increase the speed of data
retrieval. Indexes are created automatically when Primary key or Unique key
constraints are added to the table. Indexes can also be created manually.
Synonyms are database objects used to give alternative names to the exisit
objects.
370
Lab Exercise
Create a sequence that can be used with primary key column of the new_dept
table. The sequence should start with 200 and have a maximum value of 1000.
Name the sequence as NEW_DEPT_ID_SEQ and the sequence should increment by
20.
Display the information about your sequence- the sequence name, maximum value,
increment size and the last number.
1.
Insert three rows into the new_dept table. Use the sequence you created for
the deptID column. Add three departments namely, Marketing,
Administration and Education. Confirm whether the rows have been inserted
2.
3.
Display the indexes and the uniqueness that exist in data dictionary table for
the new_emp table.
371
Chapter 16
372
Objectives
At the end of this chapter, you will be able to:
Understand available Data dictionaries in Oracle
Query Data dictionary Views
Understand Dynamic Performance Views v$
373
Data Dictionary
A Data Dictionary is nothing but a set of read only tables that provides information
about its associated database. It provides information for all types of users right
from end users to administrators. It indirectly gets updated under the influence of a
DML or DDL Operation. There are hundreds of data dictionary tables and views.
These views are built on underlying base tables such as SYS.TAB$.
The static data dictionary views contain information about database objects such as tables,
indexes and procedures. They are called static because the contents of the views change only
when DDL operations are executed. For example, a row is added to the USER_TABLES view
when any user successfully executes a CREATE TABLE statement. (There are also dynamic
views, which we will discuss later in this chapter.)
There are three types of static views:
1. USER_
2. ALL_
3. DBA_
Where,
USER_ level views contain information about the current users objects.
ALL_ level views contain information about the users objects and objects in other schemas that
the user has some privilege on.
DBA_ level views are accessed by administrators.
374
Integrity Constraints
User Information
375
376
The DBA views encompass information about all database objects, regardless of the
owner. Only users with DBA privileges can access these views.
DBA_TABLES: tables of all users in the database
DBA_CATALOG
DBA_OBJECTS
377
ALL_COL_COMMENTS
USER_COL_COMMENTS
ALL_TAB_COMMENTS
USER_TAB_COMMENTS
DBA_TAB_COMMENTS, WHICH is
378
Summary
In this chapter, you have learnt that:
A data dictionary is nothing but a set of read only tables that provides
information about its associated database. It indirectly gets updated under the
influence of a
DML or DDL Operation.
Views created on these Data dictionary tables are accessed by the users for
retrieving metadata information.
A Data dictionary view can be a static view or a dynamic view. Static views
are USER_,(having specific user related information) ALL_,( owned as well as
accessible objects information) DBA_(all information accessed by DBA) and
dynamic views are V$.(information related to your database)
379
Lab Exercise
1. Write a query to check the available tables in your schema.
2. Write a query to see the different objects created in your schema.
3. Display the object names and types from the USER_OBJECTS data dictionary
view for the New_Emp and New_Dept tables.
4. Add a comment on the Employees table and view it.
5. Can you access DBA related tables in your schema. If no, what errors do you
get?
380
Chapter 17
Security Implementation
Introduction to user access control
Security Model of the Database
Creating users
Granting system privileges
Creating and Managing roles
Granting object privileges
Query Data Dictionary View for privileges
Revoking object privileges
381
Objectives
At the end of this chapter, you will be able to:
Identify the security requirements of a database
Identify the database system privileges
Create Database Users
Change User password for security
Create roles to grant privileges to users
Grant and revoke object privileges
382
Privileges
The right to execute particular SQL statements is referred to as privilege(s). The
DBA, who is the high-level user, has the power to grant access to the database and
its objects, to the users.
To gain access to the database, users require system privileges and to manipulate
the content of the objects within the database, they require object privileges.
383
System Privileges
System privileges are certain rights and restrictions that are implemented on the
database, so that you can control the access, people have to the database.
There are over 100 system privileges available. System privileges are provided
typically by the DBA. A few of the typical DBA privileges are listed below.
DBA System Privileges
Once a DBA creates a user, the DBA can assign specific system privileges to the
user. These are known as user system privileges. A few of the typical user privileges
are listed below.
User System Privileges
384
Creating Users
The first task that needs to be done is to allow the users to access the database.
Only then, will you be able to implement control on their access. To allow people to
use the database, you need to create users, as are understood by the database. All
personnel who have access to use the database will have a different name and
password assigned to them.
You can create users by using the CREATE USER command. The syntax is:
CREATE USER <user>
IDENTIFIED BY <{password | externally}>;
Where,
User is the name you want to give the user
Externally is specified when you want the system and database log on to be the
same. In this case you need not give a password and when you log on to your
machine, you will get connected to the database server without specifying the
username and password.
Password allows the user to access the database by giving the password specified.
An example of creating a user with a password is:
CREATE USER LISA
IDENTIFIED BY MYDATA;
User created.
A password is a secret character string, known only to the user. Passwords are set
after a user account is created. At first, users are given a default password. They can
change it with the following statement.
ALTER USER Lisa IDENTIFIED BY bugsbunny
User altered.
You could also use the PASSWORD command to change the password.
The PASSWORD command, when typed, prompts the user for the old password, the
new password and confirmation of the new password.
The password security does not allow too many attempts in case you have forgotten
your password and the account may be locked. It is a good practice to change your
password regularly but not too often. When you change your passwords ensure you
maintain a history of your passwords so that you do not reuse an old one. There is
also a feature that allows you to set the maximum and minimum size of a password.
SQL Star International Ltd.
385
The expiry of passwords depends on the profile assigned to your account. Profiles
determine the following:
A System privilege that is granted with the WITH ADMIN OPTION clause can be
passed on to other users by the grantee. SYSTEM privileges granted with the WITH
ADMIN OPTION clause are NOT revoked when the grantors privilege is revoked.
386
DBA
User1
User2
Roles
So far you have created a user account for Lisa and given her some system
permissions. You may create many such user accounts for all the personnel in the
organization and give them privileges. But when the size of the database and the
organization increases, it will be difficult to manage all the user accounts and the
privileges. This makes management of database security more critical. Hence, to
make this easier you can create roles.
Creating Roles for Database Access
A role is the functional part, one plays. For example, an actor or actress performs a
role in movies or a person of an organization plays a role like those of a finance
manager or marketing executive among many others. When used in relation to a
database a role means the same. Many privileges can be grouped under a particular
role. This enables easy management of privileges. Roles are also used as virtual
privileges.
The activities you need to perform to start using roles are:
1. Identifying the similar privileges and grouping them together.
2. Grouping together the various users of the database according to their usage
requirements.
3. Create the role.
4. Grant the identified set of privileges to the role.
5. Grant the role to the identified group of users.
The syntax for creating a role is:
CREATE ROLE <role_name>;
Where,
role_name is the name assigned to the role to be created.
Just as a user name is created with password protection, a role can also be created
with password protection. For example,
CREATE ROLE MktAnalysis IDENTIFIED BY analyzemarket;
SQL Star International Ltd.
387
Role created.
The DBA can now assign privileges to the role MktAnalysis using the GRANT
statement as follows:
GRANT CREATE VIEW TO MktAnalysis WITH ADMIN OPTION;
Grant succeeded.
The above role MktAnalysis can now be assigned by the DBA to users using the
GRANT statement as follows:
GRANT MktAnalysis TO Lisa, Maria;
Grant succeeded.
To assign roles to users, you need to classify the users based on the activities they
perform and the privileges they require to perform these activities. Hence based on
these, the roles will be granted privileges.
The three default roles are:
CONNECT: Enables the users to connect to the database and access data.
This role is not assigned the privilege to create tables in the database. The
privileges of this role can be further controlled by giving specific rights on
tables with different owners and specifying whether the role can perform a
query, an insert, update or delete on the tables.
RESOURCE: A role assigned to those users who use the database more
regularly. A user with this role has the right to create database objects.
Managing Roles
You can alter a role using the ALTER ROLE IDENTIFIED BY statement. You can also
delete a role using the DROP ROLE statement. When you drop a role, the user
belonging to that role will not have any of the privileges that were attached to the
role. To be able to perform these activities you need to have the CREATE ANY ROLE,
ALTER ANY ROLE and DROP ANY ROLE privileges. Privileges can be revoked from a
role as is done for users using the REVOKE statement.
By default, roles are enabled and would have to be disabled explicitly if required.
When a role is to be assigned to a user, the required role should be enabled. The
ALTER USER DEFAULT ROLE statement needs to be implemented if you want to
change the status of the role for a user in any of the following ways:
ALTER USER lisa DEFAULT ROLE ALL;
ALTER USER lisa DEFAULT ROLE ALL EXCEPT dba;
ALTER USER lisa DEFAULT ROLE mktanalysis, statdata;
ALTER USER lisa DEFAULT ROLE NONE;
388
Object Privileges
Once you create an object in a database, there might be situations where the object
would have to be changed. Not all users are allowed to manipulate database objects.
You (the owner) or the administrator have control over the objects and can
manipulate it. If a user has the GRANT ANY PRIVILEGE system privilege then he or
she can also control the object.
To be able to make changes to database objects you need to have privileges on an
object. These are called object privileges. Object privileges enable you to perform
specific actions on specific objects. Different objects have different privileges granted
for it.
Following is a list of object privileges and the objects they can be applied to:
EXECUTE: procedures
INDEX: tables
REFERENCES: tables
389
The syntax of granting Lisa the right to insert data into the Member table is given
below.
GRANT INSERT
ON Member
TO Lisa;
If you want to restrict the columns that she can update you may give the following
command:
GRANT UPDATE (vAddress, cPhone)
ON Member
TO Lisa;
Some points to remember about object privileges are:
Owners of objects have all the privileges associated with the objects.
Grantees can either grant the privileges to other users or not grant them to
others.
To grant a privilege, you must be the owner of schema or have the WITH
GRANT OPTION.
When you are granted privilege on an object in another schema, you need to access
the object by prefixing it with the name of the schema. For example, Lisa has been
given privilege to enter data into the Member table. The Member table is in the
schema owned by Jack. So Lisa will have to execute the following statement:
INSERT INTO Jack.Member (cFirstName, cLastName)
VALUES (Sandy, Prill);
390
not have privileges, the transaction will not be performed and an error message will
be displayed.
Revoking Privileges
You can also revoke privileges from users and roles, by using the REVOKE
statement. The syntax for revoking is as follows:
REVOKE <object_privilege, object_privilege, | ALL> [(columns)]
ON <object>
FROM {user, user, . . . | role | PUBLIC}
[CASCADE CONSTRAINTS]
While revoking, you can revoke more than one privilege from more than one user or
role or the public at a time.
CASCADE CONSTRAINTS is specified if you want to remove any integrity constraints
that come with granting the REFERENCES privilege.
If there has been a chain of grants enabled by the WITH GRANT OPTION from user
to user, then when the owner revokes the privilege from a user who in turn granted
the privilege,
the revoking has a cascading effect on all the privileges granted. Look at the diagram
shown below.
User 1
User 2
User 3
When User1 revokes the SELECT privilege from User2, then the privilege granted to
User3 by User2 is also revoked.
391
Summary
In this chapter, you have learnt that:
392
Lab Exercise
[Note: For this Lab exercise, you need to team up with other students in order to
implement controlling access to database objects.]
1.
Grant query access on your Employees table to another user. (Assume you
are user1) In turn have the user (Assume the user to be as user2) grant
query access on his/her Employees table to you (user1).
2.
Have user1 and user2 insert the following data into their respective
Employees table:
user1 : Should add 500 as employee ID, Nancy and Win as the first name
and last name respectively, nwin@somemail.com as the email ID, 15-Mar2002 as the hire date and ST_CLERK as the job ID.
user2 : Should add 501 as employee ID, Brown and Rhodes as the first name
and last name respectively, Brown@mailme.com as the email ID, 15-Mar2002 as the hire date and ST_CLERK as the job ID.
Will user1 be able to view the new row inserted by user2 on querying
user2.Employees table? Similarly, will user2 be able to view the new row
inserted by user1 on querying user1.Employees table? Why? How can they
view the new rows inserted by one another?
3.
Have user1 create a synonym user2 for user2.Employees table and have
user2 create a synonym user1 for user1.Employees table.
4.
As a DBA, how would you grant CREATE TABLE, CREATE SEQUENCE and
CREATE VIEW privileges to user1 and user2. Since, the same set of system
privileges are to be granted to both the users, is there any way to make your
job easier? What is the way?
5.
Have the users revoke the SELECT privilege on their respective Employees
table.
393
Chapter 18
394
Objectives
At the end of this chapter, you will be able to:
Perform Multi-table Inserts
Create Directories
Creating and querying External tables
Perform DML with Merge Statements
Tracking changes in DML using VERSION clause
395
Introduction
Multi-table INSERT, a feature introduced in Oracle9i, allows you to insert rows into
multiple tables as a part of a single DML statement. Multi-table INSERT statements
were especially designed with regard to data warehousing system. To facilitate
business analysis efficiently, data warehouses must be updated regularly. Multi-table
INSERT statements can be used in data warehousing systems to transfer data from
one or more operational sources to a set of target tables. Prior to Oracle9i, this was
done with independent INSERTSELECT statements. However, this approach was not
only cumbersome, but also created a heavy load on the Oracle server.
Multi-table INSERTs perform better than:
Unconditional INSERT
Conditional INSERT ALL
Conditional INSERT FIRST
Pivoting INSERT
Unconditional INSERT
In an unconditional Multi-table insert statement, there is no restriction on the
insertion of rows into different tables. There may be restriction on the rows that are
retrieved in the subquery. You must specify ALL followed by multiple
insert_into_clause to perform an unconditional Multi-table insert. The Oracle server
executes each insert_into_clause once for each row returned by the subquery.
396
For example, to insert details of members who were issued books before 1 st January
2001 and details of books issued before 1st January 2001, use an unconditional
Multi-table INSERT statement. First create two tables, namely:
TransMemberHistory, that holds the transaction details of members who
were issued books before 1st January 2001. The TransMemberHistory table
contains columns to store transaction ID, member ID, book ID, issue date
and fine amount.
TransBookHistory, that holds the details of books issued before 1 st January
2001. It contains columns to store transaction ID, book ID, issue date, actual
return date and fine amount.
To perform Multi-table insert into TransMemberHistory and TransBookHistory
tables, issue the following query:
INSERT ALL
INTO TransMemberHistory
VALUES (cTranID, cMemberID, cBookID,dIssueDt, nFine)
INTO TransBookHistory
VALUES (cTranID, cBookID, dIssueDt,
dActualReturnDt, nFine)
SELECT cTranID, cMemberID, cBookID,
dIssueDt, dActualReturnDt, nFine
FROM Transaction
WHERE dIssueDt<01-JAN-2001';
50 rows created.
In the query, first the transaction details before 1st Jan, 2001 are retrieved from
the Transaction table. Then, using INSERT ALL statement, the respective values are
inserted into TransMemberHistory and TransBookHistory tables. No condition is
specified in the INSERT ALL statement, hence known as unconditional Multi-table
insert statement.
397
32 rows created.
In the query, first the details of transaction that occurred before 1 st Jan, 2001 are
retrieved by the subquery. Only the details of transactions where a fine amount has
to be collected are inserted into the tables TransBookHistory and
TransMemberHistory. The condition for selective insert is specified by the
WHENTHEN clauses. As a single insert condition is specified in the above query, it
applies to both the tables. You can give separate insert conditions for different
tables.
398
For example if you want to perform conditional Multi-table insert using INSERT FIRST
statement in the query, first, create the tables TransHistory99, TransHistory00
and TransHistory. Insert the following values in the newly created tables:
in
the
year
1999
in
table
place
in
the
year
2000
in
table
The
following
query
performs
Multi-table
insert
TransMemberHistory, TransHistory99, TransHistory00
using INSERT FIRST statement.
into
and
the
tables
TransHistory
INSERT FIRST
WHEN nFine IS NOT NULL THEN
INTO TransMemberHistory
VALUES (cTranID, cMemberID, cBookID, dIssueDt, nFine)
WHEN dIssueDt LIKE (%99%) THEN
INTO TransHistory99
VALUES (cTranID, cMemberID, cBookID, dIssueDt, dActualReturnDt)
Pivoting INSERT
Suppose you retrieve a record from a non-relational database table and wish to store
it in a relational format, you can do so with the help of an operation called Pivoting
INSERT. Pivoting INSERT enables you to transform records retrieved from a non SQL Star International Ltd.
399
relational database table into a relational format. This is done by converting the
retrieved record into multiple records and storing it in a manner that resembles a
relational database table environment.
For instance, the table StudentMarksDetail contains the details of marks scored by
students in all courses. It is a non-relational database table. When you select records
from this table, it gives the following output:
NSTUDENTNO
---------101
CCOURSEID
NOPSYS
NBDMS NARTINT
------------------ ------- --------SE01
80
75
85
90
NPROGLANG
As this record is retrieved from a non-relational database table, you can give it a
relation format by implementing Pivoting INSERT on it. Because of pivoting INSERT,
the marks for each subject in the course will be inserted as a separate row in the
StudentMarksInfo table. First create another table called StudentMarksInfo that
contains columns for student number, course ID and marks. Following is the query to
perform Pivoting INSERT:
INSERT ALL
INTO StudentMarksInfo values (nStudentNo, cCourseID, nOpSys)
INTO StudentMarksInfo values (nStudentNo, cCourseID, nBDMS)
INTO StudentMarksInfo values (nStudentNo, cCourseID, nArtInt)
INTO StudentMarksInfo values (nStudentNo, cCourseID, nProgLang)
400
External Tables
External tables are read-only tables. Here data is actually stored outside the
database in flat files and only the metadata is stored in the database. If you want to
make a table as read-only, create it as an external table.
The Oracle server uses drivers to access the data from external files (stored outside
the database). However, with external tables you can perform only SQL queries. You
cannot perform DML operations (UPDATE/INSERT/DELETE) on external tables, nor
can you create indexes on them.
To access external tables, Oracle server provides two main access drivers:
ORACLE_LOADER, for reading data from external table using the Oracle
loader technology.
ORACLE_INTERNAL, the import/export access driver used for importing and
exporting of data using a platform independent format.
The data used by external tables is stored in data files stored under a directory.
Therefore, you must create a DIRECTORY object that corresponds to the directory on
the file system on which the data files reside. The syntax to create a directory object
is:
CREATE [OR REPLACE] DIRECTORY AS path_name;
Where,
CREATE, Allows you to create a new directory database object
REPLACE, Allows you to re-create or replace a directory object
DIRECTORY, Name of the directory object to be created, with a maximum length of
30 bytes. You cannot specify a schema name to a directory object.
path_name Specifies the full path name of the directory on the server where the
data files are located. Single quotes indicate that the path name is case sensitive.
As the above command indicates, the path_name is an alias for the directory
object. Whenever this directory object is referenced, it actually refers to path where
the data files are located.
SQL Star International Ltd.
401
402
The following code illustrates the use of all the aforesaid clauses and creates an
external table known as OldMembers:
CREATE TABLE OldMembers
SQL Star International Ltd.
403
(cMemberID char(9),
cFirstName CHAR(20),
cLastName CHAR(20),
dMembershipDt DATE
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY memdir
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
BADFILE bad_memdir:memXt%a_%p.bad
LOGFILE log_memdir:memXt%a_%p.log
FIELDS TERMINATED BY ,
MISSING VALUES ARE NULL
(cMemberID CHAR, cFirstName CHAR,
cLastName CHAR, dMembershipDt CHAR
date_format date mask dd-mon-yyyy
)
)
LOCATION (Members.txt)
)
PARALLEL 5
REJECT LIMIT 200;
In the above code, the Keywords ORGANIZATION EXTERNAL indicates that the table
to be created is an external table which stores data for member ID, first name, last
name and membership date. TYPE ORACLE LOADER specifies the access driver. The
default directory is memdir. The code also specifies the log directory and the bad
directory. The ACCESS PARAMETERS specify the format of the data file. LOCATION
specifies the name of the flat file where the data is actually stored. PARALLEL 5
specifies that five server process can simultaneously execute. The REJECT LIMIT is
200, indicating that 200 errors can occur before the query is aborted.
When you execute the above code successfully, it creates the external table
OldMembers.
Table created.
SQL Star International Ltd.
404
Now when select the data from external table OldMembers, the driver ORACLE_LOADER looks for the
data file Members.txt in the path d:\ext_data specified by the default directory memdir, fetches the
data from Members.txt file and displays it as follows:
CMEMBERID CFIRSTNAME CLASTNAME DMEMBERSH
--------- ---------- --------- --------A01010101
Anna
Jones
25-NOV-95
B02020202
Steve
Patterson
05-OCT-94
C03030303
Mark
Blair
12-JAN-96
Unloading Data Using External Tables in Oracle10g
Oracle10g has taken external tables a stage further by enabling an external table to
be created as a CTAS ( Create Table As Select ) operation, which enables a one-time
unloading of data. Oracle has in fact supplied a new access driver known as
ORACLE_DATAPUMP which means that Oracle will unload an external table to a
proprietary format usable by the Data Pump set of utilities (significant new 10g
feature to replace imp and exp). So, generating an ASCII, CSV file from external
tables is not possible, but this new feature is exciting nonetheless. The above feature
is demonstrated with a sample example below.
Step 1: Create a directory
SQL> CREATE DIRECTORY mydir AS /oracle/opt;
Step 2: Provide permission to one of the users say scott.
SQL> GRANT READ, WRITE ON DIRECTORY mydir TO scott;
Step 3: Create the external table.
CREATE TABLE demotab
ORGANIZATION EXTERNAL
(TYPE ORACLE_DATAPUMP default directory mydir
LOCATION (book_category.exp))
AS
SELECT b.cBookname, b.cAuthorname, c.cCategoryname
from Book b, Category c
WHERE b.cCategoryid=c.cCategoryid;
Merging Rows
SQL Star International Ltd.
405
Merging rows enables you to use the UPDATE and INSERT statements in the same
query. The MERGE statement (also called upsert) is an extension of the existing
SQL statements. The MERGE statement allows you to conditionally update and insert
rows in a table. If the row exists, it updates it, else, inserts it into the table.
You can perform these activities using PL/SQL loops and multiple DML statements.
However, MERGE is easier to use and can be written as a single SQL statement.
The MERGE statement avoids the use of multiple UPDATE statements, requiring few
scans of the source table, and thereby improving performance. This concept is
particularly useful in data warehousing, where data may be inserted from multiple
sources. There can be duplicates. If the row exists, it will be updated, else it will be
added.
For MERGE statements, you need two tables, a source table and a target table. Using
MERGE, you can not only update the values in the target table with the values in the
source table but also add new rows from the source to the target table.
You must have the INSERT and UPDATE object privileges on the target table and the
SELECT object privilege on the source table. To specify the DELETE clause of the
merger_update_clause, you must also have the DELETE object privilege on the
target table.
The MERGE statement is deterministic. You cannot update the same row of the
target table multiple times in the same MERGE statement.
For instance, if you wish to update the phone numbers in the Member table
(target), with the phone numbers in the MemberSrc table (source), use the query
given below. The MemberSrc table contains a new row for member Allen Scott.
The Member table does not contain this row. When you issue a MERGE statement,
the new row is inserted into the Member table. This is the advantage of using a
MERGE statement, where you can update and insert the rows using a single query.
You need not use multiple queries.
406
407
changed the row and 8 were committed between yesterday and today, and you
request all versions of that row in that time period, you will see 8 rows of outputone for each version. Changes that were rolled back do not generate a version.
Use the VERSIONS BETWEEN CLAUSE to code a flashback version query.
Oracle 10g supports MINVALUE and MAXVALUE in the VERSIONS BETWEEN clauses.
These expressions give access to the oldest and most recent data available.
Example of the flashback version query
SELECT nFine FROM Transaction WHERE cTranid=T0000290100;
Assume the output gives 15
UPDATE Transaction SET nFine=nFine+0.10 WHERE cTranid=T0000290100;
Now nfine is 15.1.
Now commit the above transactions using COMMIT command.
Now execute the following statement:
SELECT nFine FROM Transaction VERSIONS BETWEEN SCN MINVALUE AND
MAXVALUE
WHERE cTranid=T0000290100;
Now the output will be 15 and 15.1.
FLASHBACK_TRANSACTION_QUERY VIEW
It is easy to see the changes made to the tables with flashback_transaction_query
view. This can be helpful for auditing (i.e. who changed my table), debugging and
even performance analysis (how often is a table being changed).
The SELECT ANY TRANSACTION privilege allows the user to query the past
transaction by querying the above-mentioned view.
408
Summary
In this chapter, you have learnt that:
Multi-table insert allows you to insert rows into multiple tables as a part of a
single DML statement. These are designed with regard to data warehousing
system.
There are four types of Multi-table INSERT statements:
Unconditional INSERT
Conditional INSERT ALL
Conditional INSERT FIRST
Pivoting INSERT
External tables are read only tables whose data is stored in a flat file. It
stores only the metadata information in the database.
The flashback version query feature lets us see the table data as it changed
over time.
409
Lab Exercise
[Note: For questions 1 to 2, you are required to work with SalHistory,
ManagerHistory, SpecialSalary, SpecialSalHistory, HireDt00, HireDt99 and
HireDt_History tables.]
To work with these tables, you need to run the script file LabEx18_01.sql.
The structures of these tables are given below.
Structure of SalHistory
Structure of ManagerHistory
Structure of SpecialSalary
Structure of SpecialSalHistory
410
1.
Write a query to retrieve the employee ID, manager ID, hire date, and salary
of those employees whose employee ID is less than or equal to 115. Use the query
result set to do the following:
Insert into the SpecialSalary table the employee ID and salary of those
employees who earn a salary of more than $15,000. In case salary is less than
15,000, do the following:
Insert into the SalHistory table the employee ID, hire date,
and salary.
Insert into the ManagerHistory table the employee ID,
manager ID, and salary.
Confirm the rows created in all the three tables.
2.
Write a query to retrieve the department ID, sum of salary, and maximum
hire date, which are grouped according to the department ID. Use the result set to
perform the following:
In case the first WHEN clause returns TRUE, skip the subsequent WHEN
clauses for this row.
In case of the rows that do not satisfy the first WHEN clause, insert into:
411
Structure of SalesInfo
Write a query to retrieve the employee ID, week ID, and sales figures on Monday,
Tuesday, Wednesday, Thursday, and Friday from the SalesInfoFile table such that
the records retrieved are converted into multiple records for the SalesInfo table.
Confirm the records inserted into SalesInfo table.
4.
Create an External Table called All_objects_Xt, dump data into a .txt file.
Select the data from all_objects data dictionary.
Query the external table
5a.
5b.
412
Commit it
6.
Write a query to select the prevailing tax rate from the table.
7.
Write a query to list all the changes made to the Tax table.
413
Chapter 19
REGULAR EXPRESSION
Overview of Regular Expression Support
List of Meta-characters
Regular Expression Functions
414
Objectives
At the end of this chapter, you will be able to:
Use meta-characters
Use regular expression functions
Use NOVALIDATE Clause
415
416
Below are the various comparisons for the Character class, which is used in complex
string searching.
417
REGEXP_LIKE
Looks for a matching pattern in a character column.
It takes two arguments. First argument is the name of the character column. Second
argument is a regular expression string.
Following is an example of the syntax for this function: -
JERSEY
LIBRARY
STORES ,
( ){2,}, ) REGEXP_REPLACE
FROM DUAL;
Displays:
REGEXP_REPLACE
SQL Star International Ltd.
418
REGEXP_INSTR
This function searches a string for a given occurrence of a regular expression
pattern. You can specify which occurrence you want to find and the start position.
Syntax :
REGEXP_INSTR (srcstr, pattern [, position [, occurrence
[, return_option [, match_option]]]])
Example given below shows the position of @ in a given email id.
SELECT REGEXP_INSTR(email,@{1}) Position FROM MEMBER;
Displays:
Position
-------12
REGEXP_SUBSTR
This is similar to REGEXP_INSTR but instead of the position of the specified pattern it
returns a part of a string after matching the regular expression pattern.
Following is the syntax: REGEXP_SUBSTR (srcstr, pattern [, position [, occurrence
[, match_option]]])
Example to extract the username from the email of the members of New Jersey
Central Library:
Member;
419
Srcstr:- Indicates the Search value that can be an expression or a character column
name.
Pattern:- Regular expression
replacestr:- Character String replacing pattern.
Position:- Starting position to Search from.
Occurrence:- Number of Occurrences to search for.
Return_option:- Beginning or Ending position of occurrence
Match_option:-Option to change default matching; it can include one or more of the
following values:
c uses case-sensitive matching (default)
I uses case-insensitive matching
n allows match-any-character operator
m treats source string as multiple line
420
Summary
In this chapter, you have learnt that:
Regular expression works beyond the Portable Operating System For Unix
(POSIX) Standard.
421
Lab Exercise
1. A string contains three spaces in between. For example NEW
DELHI. Write a
query to replace the spaces with one. The output displayed should be as
NEW DELHI
2. Write a query, which extracts a part of a string. The regular expression searches
for a comma followed by a space; then zero or more characters that are not
commas, as indicated by [^,]*; and lastly looks for another comma.
The pattern will look similar to a comma-separated values string.
(first part, second part, third part)
3. Retrieve any values from ename that start with J, followed by any letter, then
N or M, then any letter, then S from Emp table
422