Вы находитесь на странице: 1из 146

FIRST STEPS TOWARDS ORACLE 10g

SOME THINGS WHAT WE SHOULD CONSIDER :


As per Oracle database is a collection of data in one or more files.
The database can contain Physical and Logical structure.
The Course of developing an application consists of
Creating structures (Tables & Indees!
Creating "ynonyms for the ob#ect names.
$ie% ob#ects in different databases
&estricting Access to the ob#ects.
'ternal Tables can be accessed for files outside the database( as if the ro%s in the files
%ere ro%s in the table.
An Oracle Instance Comprises of
A memory area called "ystem )lobal Area (")A!
*ac+ground processes %hich interact bet%een the ")A and the database files
on dis+.
The Oracle &ead Application Cluster (&AC! can ma+e more than one instance to
use the same database.
Database as per Orace :
,ith in the Oracle database( the basic structure is a table.
The different tables supported by Oracle -atabase are...
/! &elational Tables
0! Ob#ect &elational Tables
1! Inde Organi2ed Tables
3! 'ternal Tables
4! Partitioned Tables
5! 6ateriali2ed $ie%s
7! Temporary Tables
8! Clustered Tables
9! -ropped Tables
The Access to the data in the tables is tuned by indees( they are
/! *: Tree Indees
0! *itmap Indees
1! &everse ;ey Indees
3! <unction *ased Indees
4! Partitioned Indees
5! Tet Indees
ORACLE ST!LE FOR DATA STORAGE :
All the logical structures in the database must be stored in the database.
Oracle maintains a data dictionary( %hich records 6'TA-ATA about ob#ect.
The O*='CT" %hich need physical storage space( are allocated space %ithin a
Tablespace.
Oragle 10 g Material

1
Tabe Spaces :
It consist of one or more data files
A data file can be a part of one and only one table space
As per />g minimum table space is created are
"?"T'6 and "?"A@A B %hich are used to support its internal
management needs
In />g a b"g#"e tabespace can be created ( %hich can gro% to the si2e of tera bytes
The Oracle managed files concept can be used to simplify the creation and maintenance
of data files
Te$p%ra& Tabespace :
The actual table space is permanent ( but the segments saved in the table space are
temporary
It is used to support sorting operations such as inde creation and #oint processing.
Tables can be either -ictionary 6anaged C Logically 6anaged
Oracle maintains a '"t$ap in each data file of the table space to trace space availability
AUTOMATED STORAGE MANAGEMENT (ASM) :
It automates the layout of datafiles and other operating system level files ( used by the
database
It is done by distributing the files across all available dis+s.
It supports multipleing features( minimi2ing the possibility of data data loss
As ne% dis+s are added ( the database files are automatically redistributed access all
dis+s in the defined groups for optimal performance
AUTOMATIC UNDO MANAGEMENT :
The transaction are supported by dynamically creating and managing undo segments
It helps in maintaining prior images of the changed *loc+s and &o%s
The @D-O segments are managed directly %ithout the intervention of the -*A
<lashbac+ version Eueries can be eecuted to see the different versions of a ro% as it
changed during a specified time interval.
DROPPED DATA :
The Rec&ce '"* concept %as introduced %ith Oracle -atabase />g
This concept impacts the space reFuirements for the table spaces and datafiles
This architecture greatly simplifies recoveries of accidentally dropped tables
The concept may considerably increase the space used in the database
The PURGE Command is used to remove old entries from the recycle bin
PROGRAMMATICAL STRUCTURES IN ORACLE :
/! PL+S,L B
It is the most critical component of all the application implementations
Oragle 10 g Material

2
The component can be used to create stored procedures and functions
@sing this component the stored procedures and functions can be collected into single
unit called pac+ages
@sing the same component the -atabase trigger can also be designed.

0! D!NAMIC S,L B
@sing this concept "EL can be generated at runtime and pass it to procedures %hich
eecute it.
1! S,L - PLUS B
It provides simple interface to the Oracle database.
It supports &udimentary &eporting and is the best tool for scripting.
It provides consistent interface for
/. &etrieving data from data dictionary.
0. Creating database ob#ects.
3! .A/A 0 .D'C B
It supports =A$A and =-*C and help in using =A$A in the place of PLC"EL.
=A$A based stored procedures can be %ritten.
4! 1ML B
Oracles A6L interfaces and A6L types can be used to support inserting and retrieving of
data via A6L.
5! Ob2ect Or"e*te3 S,L 0 PL+S,L B
Oracle can be used to create and access ob#ect oriented structures( including user defined
data types( methods( large ob#ects ob#ect tables nested tables.
4) Data P5$p :
-ata Pump import and -ata Pump 'port( enhance the manageability and performance.
-ata Pump can be used to Fuic+ly etract data and move it to different databases %hile
altering the schema and changing the ro%s.
8! S,L - LOADER B
It is used to load flat files Fuic+ly into Oracle Tables.
A single flat file can be loaded into multiple tables during the same load( and loads can be
parallel 2ed.
9! E6ter*a Pr%gra$s a*3 Pr%ce35res B
"EL can be embedded %ith in eternal programs.
Procedural Libraries can be created and can be lin+ed later to Oracle.
/> ! UTL 7 MAIL B
It is a pac+age introduced in Oracle />g.
Oragle 10 g Material

3
It allo%s a PLC"EL application developer to see eGmails %ithout having to +no% ho% to use
the underlying "6TP Protocal stac+.
T!PES OF EDITIONS :
1) E*terpr"se E3"t"%* B

It contains all features and is the rich etensible version of the Oracle databse.
It includes features such as <lash *ac+ -atabase.
The licensed functionalities that are supported are B
Oracle "patial
Oracle OLAP
Oracle Label "ecurity
Oracle -ata 6ining
8) Sta*3ar3 E3"t"%* B
It provides some subset features of 'nterprise 'dition.
It includes features that are needed by a small business entity.
9) Pers%*a E3"t"%* :
It allo%s for the development of applications %hich run on either the standard or 'nterprise
'dition.
The 'dition is not suitable for Production 'nvironment.
DATA'ASE MANAGEMENT S!STEMS :
-*6" is a program %hich ta+es care of
"toring of Information.
&etrieving of Information.
6odification of Information.
-eletion of Information.
Type of -atabases

HH Iierarchical HH Det%or+ HH &elational
HH Ob#ect &elational HH Ob#ect Oriented
RELATIONAL MODELS :
The 6odel %as first outlined by -r. E: F: C%33 in /97>.
The components of &elational 6odel are B

Collection of ob#ects or relations that store the data.

Oragle 10 g Material

4
A set of operations that can act on the relations to produce other relations.
-ata integrity for accuracy and consistency
A &elational database uses relations or t%o dimensional tables to store information.
DATA MODELS :
The -ata 6odels help in eploring ideas and improve the understanding of the database design.
PURPOSE OF DATA MODELS :
-- Communicate HH Categori2e HH -escribe HH "pecify HH Investigate
HH 'volve HH Analy2e HH Imitate
O'.ECTI/E OF THE MODELS :
To produce a model that fits a multitude of uses
"hould be understood by an end user.
"hould contain sufficient detail for a developer to build the database system
RELATIONAL DATA'ASE PROPERTIES :
It can be accessed and modified by eecuting "tructured Euery Language statements.
It contains a collection of tables %ith no physical pointers.
It uses a set of operators.
,e need not specify the access route to the tables
There is no need to identify ho% the data is arranged physically.
COMMUNICATING WITH RD'MS :
The "tructured Euery Language is used to Communicate %ith &-*6".
STRUCTURED ,UER! LANGUAGE :
It allo%s the user to communicate as the server
It is 'fficient
It is easy to learn and use
It is functionally complete( by allo%ing the use to define( retrieve and manipulate the data.
A'OUT ORACLE ; :
It is the first ob#ect capable database.
It provides a ne% engine %hich supports
Ob#ect oriented programming
Comple data types
Comple business ob#ects
<ull compatibility %ith the relational concepts.
Oragle 10 g Material

5
It provides OLTP applications( %ith sharing of runtime data structures( large buffer caches and
deferrable constrains.
It supports Client "erver and ,eb based application %hich are distributed and multitired.
It can scale tens and thousands of concurrent users.
A'OUT ORACLE ;" :
It is the database of Internet Computing.
It provides advanced tools to manage all types of data in ,eb "ites.
The Internet <ile "ystem ( I<"! combines the po%er for ease of use a file system.
'nd users can easily access files and folders in O&ACL' I<" via a variety of protocols.
It enables the users to %ebGenable their multimedia data
It provides full( native integration %ith 6icrosoft Transaction "erver (6T"!.
It provides high performance Internet platform for eGcommerce and data %arehousing.
The core pieces of Oracle Internet Platform are B
*ro%ser based clients to process presentation.
Application servers to eecute business logic and serve presentation logic to
bro%ser based clients.
-atabases to eecute database intensive business logic and serve data.
ENTIT! RELATIONSHIP MODEL :
In an effective system data is divided into discrete categories or entities.
An '& J 6odel is an illustration of various entities in a business and the relationships bet%een
them.
It is built during the analysis phase of the "ystem -eveloping Life Cycle.
'& J 6odel separates the information reFuired & the business from the activities performed.
ER 7 MODEL 'ENEFITS :
It documents information for the organi2ation in a clear( precise format.
Provides a clear picture of the scope of the information reFuirement.
Provides an easily understood pictorial map for the database design
It offers an effective frame%or+ for integrating multiple application.
<E! COMPONENTS IN DIA ER 7 MODEL :
ENTIT! : It is a thing of significance about %hich the information need to be +no%n.
ATTRI'UTES : It is something that describes or Fualifies an entity.
-- 'ach attribute may be mandatory or optional.
RELATIONAL DATA'ASE TERMINOLOG! :
Oragle 10 g Material

6
ROW OR TUPLE B
It represents all data reFuired for a particular instance in an entity.
'ach ro% is an entity is uniFuely identified by declaring it as P&I6A&? ;'? or @DIE@' .
The order of the ro%s is not significant( %hile retrieving the data.
COLUMN OR ATTRI'UTE :
It represents one +ind of data in a table
The column order is not significant %hen storing the data.
A FIELD :
It can be found at the Intersection of a ro% and a column.
A <ield can have only one value( or may not have a value at all( the absence of value in
Oracle is represented as D@LL.
RELATING MULTIPLE TA'LES :
'ach table contains data that describes eactly only one entity.
-ate about different entities is stored in different tables.
&-*6" enables the data in one table to be related to another table by using the foreign +eys.
A <oreign ;ey is a column or a set of Column that refer to a Primary ;ey in the same table or
another table.
RATIONAL DATA'ASE PROPERTIES :
"hould not specify the access route to the tables( and should not reveal the physical arrange.
The -atabase is accessed using "tructured Euery Language ( "EL !
The language is a collection of set of operators.
STRUCTURED ,UER! LANGUAGE STATEMENTS :
O&CL' "EL Compiles %ith industry accepted standards.
The different categories into %hich the "EL statements fall are
Data Retr"e=a State$e*ts :
"'L'CT
Data Ma*"p5at"%* La*g5age State$e*t ( DML )
ID"'&T @P-AT' -'L'T'
Data De#"*"t"%* La*g5age State$e*t (DDL )

C&'AT' ALT'& -&OP
&'DA6' T&@DCAT'

Tra*sact"%* C%*tr% La*g5age State$e*t ( TCL )

CO66IT &OLL*AC; "A$'POIDT
Oragle 10 g Material

7

Data C%*tr% La*g5age ( DCL )

)&ADT &'$O;'
A'OUT THE S,L 'UFFER :
All Commands of "EL are typed at the "EL prompt.
Only one "EL "tatement is managed in the "EL *uffer.
The Current "EL statement replaces the previous "EL statement in the *uffer.
The "EL statement can be divided into different lines %ithin the "EL *uffer.
Only one line i.e.( the current line can be active at a time in the "EL *uffer.
At "EL prompt( editing is possible only in the current "EL *uffer line.
'very statement of "EL should be terminated @sing K
One "EL statement can contains only one.
To run the previous or current "EL statement in the *uffer type L C Lat "EL prompt.
To open the "EL 'ditor type ed at "EL prompt.
CREATING AND MANAGING TA'LES :
DATA'ASE O'.ECTS :
A Oracle database can contain multiple data structures.
The different -atabase ob#ects in Oracle are B
TA'LE : @sed to store date( *asic @nit
/IEW : Logically represents subsets of data from one or more tables.
SE,UENCE : @sed to )enerate Primary ;ey values.

INDE1 : It is used to improve the performance of some Fueries.
S!NON!M : @sed to give alternate names to ob#ects.
Tabes "* ORACLE ; + > + 10 :
Tables can be crated at any time( even %hen the users are using the database.
"i2e of the table need not be specified.
The structure of the table can be modified online.
R5es t% Create A Tabe :
The user should have permission or C&'AT' TA*L' command( and storage area.
The Table name should begin %ith a letter and can be / J 1> characters long.
Dames can contain B
HH A J M HH a G 2 HH > J 9 HH G ( N ( O
Dames can not be duplicated for another ob#ect in the same O&ACL' "erver.
Dames cannot be oracle servers reserved %ord
Oragle 10 g Material

8
Dames are not case sensitive
Create Tabe State$e*t B
S&*ta6 :

"EL P C&'AT' Table Q TableRDame P
(
ColumnRDame/ Q -ata type P (,idth!(
ColumnRDame0 Q -ata type P (,idth!(
B
B
ColumnRDamen Q -ata type P (,idth!
! K
'5"3"*g '%c?s %# S,L State$e*ts :
H -ata types H Literals H <ormat 6odels H Dulls
H Pseudo Columns H Comments H -atabase Ob#ects
H "chema Ob#ect Dames and Eualifiers
H "ynta for "chema ob#ects and parts of "EL "tatements.
Data T&pes "* Orace :
'ach value in O&ACL' is manipulated by a date type.
The data types value associates fied set of properties %ith the value.
The values of one data type are different from another data type.
The data type defines the domain of values that each column can contain.
The *uiltGin data types of O&ACL' are Categori2ed as
CIA&ACT'& -ATA T?P'"
D@6*'& -ATA T?P'"
LOD) AD- &A, -ATA T?P'"
-AT'TI6' -ATA T?P'"
LA&)' O*='CT -ATAT?P'"
&O,I- -ATAT?P'"
C@aracter Data T&pes :
They store character data %hich can be Alphanumaric data
The Information can be
,ords
<ree J <orm Tet
-atabase Character set
Dational Character set
They are less restrictive than other data types an have very fe% properties
The data is stored in strings %ith byte values
The information can belong to either
Oragle 10 g Material

9
7 J list A"CII Character set
'*C-IC
O&ACL' supports both single byte & multi byte character sets.
The different character data types are B
CIA& DCIA& $A&CIA&0 D$A&CIA&0
CHAR DATAT!PE B
It specifies fied length character string.
The si2e should be specified.
If the data is less than the original specified si2e( blan+ pads are applied
The default length is 1 '&te and the 6aimum is 8000 '&tes.
The si2e of a character can range from 1 '&te t% A '&tes depending on the database
character set.
NCHAR DATAT!PE B
It is first time defined in O&ACL' 9i( and contains @nicode data only.
The columnSs maimum length is determined by the Dational Character set definition.
The 6aimum si2e allo%ed is 8000 '&tes and si2e has to be specified.
If the data is shorter than the actual si2e then the blan+ pads are applied.
/arc@ar Datat&pe B
It specifies the variable length character string.
The 6inimum si2e is 1 '&te and the 6aimum si2e is A000 '&tes:
It occupies only that space for %hich the data is supplied.
N=arc@ar Datat&pe B
It is first time defined in O&ACL' 9i.
It is defined for @nicode only date.
The minimum for the si2e is 1 '&te and maimum is A000 '&tes:
NUM'ER DATAT!PES B
N5$ber Data T&pe :
It stores 2ero( positive and negative fied and floating point numbers.
The range of magnitude is
/.> H />
G/1>
to 9.9 ....9 H />
/04
The general declaration is
D@6*'& ( p ( s !

P It specifies the precision( i.e.( the total number of digits ( / to 18 !
" it specifies the scale( i.e.( the number digits to the right of the decimal
point. It can range from J83 to /07.
Oragle 10 g Material

10
F%at Data T&pe :
It specifies to have a decimal point any%here from the first to the last digit( or can have no
decimal point at all.
The scale value is not applicable to floating point numbers( as the number of digits that can
appear after the decimal point is not restricted.
S&*ta6 :

<loat It specifies a floating point number %ith decimal precision 18 or binary
precision of /05.
<loat (*! It specified a floating point number %ith binary precision b.

The precision can range from / to /05.
To convert from binary to decimal precision multiply L b L by 0:90109.
To convert from decimal to binary precision multiply the decimal precision by
9:981>9.
The maimum of /05 digits of binary precision is roughly eFuivalent to 18 digits of decimal
precision.
L%*g 0 RaB Data T&pes :
L%*g Data T&pe :
This datatype stores variable length charcgter strings.
It is used to store very lengthy tet strings
The length of LOD) values may be limited by the memory available on the computer.
LOD) columns can be referenced in
HH "'L'CT Lists HH "'T Clauses of @P-AT' statements
HH $AL@'" clauses of ID"'&T statements.
Restr"ct"%*s :
A single table can contain only one LOD) column.
Ob#ect types cannot be created on LOD) attribute.
LOD) columns cannot appear in ,I'&' clauses or in Integrity Constraints.
Indees cannot be created on LOD) Columns
LOD) can be returned through a functions( but not through a stored procedure.
It can be declared in a PLC"EL unit but cannot be referenced in "EL.
DATE 0 TIMME DATA T!PES :
Date Data T&pe :
It is used to store data and time information.
The dates can be specified as literals( using the )regorian Calender.
The information revealed by date is B
H Century H ?ear H 6onth
H -ate H Iour H 6inute H "econd
Oragle 10 g Material

11
The default date format in O&ACL' is --G6ODG??( and is specified in
DL" J -AT' J <O&6AT.
The default time accepted by O&ACL' date is /0 B >> B >> A6 ( 6idnight !
The default date accepted by O&ACL' data is the <irst day of the Current 6onth.
The -ate range provided by Oracle is
=AD@A&? /( 37/0 *C to -'C'6*'& 1/( 9999 A-.
T"$esta$p Data T&pe :
It is an etension of the -AT' data type.
It stores
- Da& - M%*t@ - !ear - H%5r
- M"*5te - Sec%*3
S&*ta6 : TI6'"TA6P T U <ractional J "econds J Precision V W
<ractional J "econds J Precision optionally specifies the number of digits in the
fractional part of the "'COD- datetime field.
It can be a number in the range of > J 9 ( %ith default as 5.
RaB 0 L%*g RaB Data T&pes :
&A, and LOD) &A, data types are intended for storage of binary data or *yte "trings.
&A, and LOD) &A, are variable length data types
They are mostly used to store graphics( sounds documents etc.
The O&ACL' converts the &A, & LOD) &A, data into Ieadecimal form.
'ach Ieadecimal character represents four bits of &A, data.
Large Ob2ect ( LO' ) Data T&pes :
The *uilt in LO* data types are
H *LO* H CLO* H DCLO*
These data types are stored inernally.
The *file is an LO* %hich is stored eternally
The LO* data types can store large and unstructured data li+e Tet( Image( $ideo and
"patial data.
The maimum si2e is upto 3 )*
LO* columns contain LO* locators( %hich can refer to outGofGline or inGline LO* values.
LO*Ss selection actually returns the LO*Ss locator.
'FILE Data T&pe :
Oragle 10 g Material

12
It enables access to binary file LO*Ss %hich are stored in the file systems outside O&ACL'.
A *<IL' column or the attributes stores the *<IL' locator.
The *<IL' locator maintains the directory alias and the filename.
The *inary <ile LO*Ss do not participate in transactions and are not recoverable.
The maimum si2e is 3 )*.
'LOG Data T&pe :
It stored unstructured *inary Large Ob#ects
They are *it streams %ith no character set semantics.
They are provided %ith full transactional support.
CLO' Data T&pe :
It stores single byte and multi byte character data
*oth fied %idth and variable %idth character sets are supported
They are provided %ith full transactional support.
NCLO' Data T&pe :
It stores @nicode data using the Dational Character set.
ROWID Data T&pe :
'ach ro% in the database has as address.
The ro%s address can be Fueried using the pseudo column &O,I-.
&O,I-Ss efficiently support partitioned tables and Indees.
'ample B
"ELP Create Table "tudents
(
"tuId Dumber (5!(
<name $archar0(1>!(
Lname $archar0(1>!(
-O* -ate(
-O= -ate(
<ees Dumber(7(0!(
)ender $archar0(/!
! K
"EL P Create Table Lab"pecification
(
ProdId Dumber(5!(
ProdPhoto *LO*(
Prod)raphic *<IL'(
Prod-esc LOD)
! K
P%p5at"*g t@e Data "*t% Tabes :
Oragle 10 g Material

13
INSERT "tatement B
The ID"'&T statement is used to add ro%s to a
Table
$ie%s *ase Tables
A partition of a Partition Table.
A subpartition of a CompositeRPartitioned Table
An ob#ect table
An ob#ect vie%Ss base Table.
Insertion of data into a table can be eecuted in t%o %ays.
o Conventional ID"'&T
o -irect J path ID"'&T
In Conventional Insert statement( O&ACL' reuses free space in the table into %hich the data in
being inserted and maintains referential integrity constraints.
In -irectGpath insert ( O&ACL' appends the inserted data after eisting data in the Table( the free
space is not reused.
S&*ta6 :
INSERT INTO C TA'LEDNAME E ( COL1F COL8F G:: )
/ALUES ( /ALUE1F /ALUE8FG::::) H
E6a$pes:
INSERTING DATA INTO ALL COLUMNS OF A TA'LE :
S,LE ID"'&T IDTO "T@-'DT"
$AL@'" (/01( L "A6PATIS( L;@6A&S( S09G=ADG8>S( 1>G6A&G94S( 04>>>(S6S! K
In this case the values should be provided to all the columns that eist inside the table.
The order in %hich the values are declared in the values clause should follo% the same order as
that of the original table order.
The char( varchar and data type data should be declared in single Fuotes.
INSERTING DATA INTO THE RE,UIRED COLUMNS:
S,LE ID"'&T IDTO "T@-'DT" ("tudno( <name( Lname( -o#( )ender!
$AL@'" (/014(S&a#S( L;umarS( S0>G<ebG84S( L6S! K
In this case the order of the columns declared in the "'L'CT list need not be the same order as
that of the original table.
The data values in the values clause should match %ith that of "'L'CT list.
Oragle 10 g Material

14
The columns for %hich the data is not supplied are filled %ith D@LL values.
If the columns that %ere not supplied %ith data are declared %ith DOT D@LL Constraints then the
ID"'&T "tatement is &e#ected.
INSERTING NULL /ALUES :
D@LL $alues can be inserted in t%o %ays
1. I6PLICIT Omit the column from the list.
2. 'APLICT "pecify the D@LL ;ey%ord.
"pecify 'mpty "tring L L ( only for character strings and dates.
The targeted column should not be set as DOT D@LL Constraint.
S,LE ID"'&T IDTO "T@-'DT" ("tudDo( <name( Lname( -O*( -O=( <ees( )ender!
$AL@'" (/013(S;rishnaS( D@LL( S08G<'*G>3S( D@LL( L6S! K
INSERTING SPECIAL /ALUES :
S!SDATE FUNCTION
It is a Pseudo column provided by the oracle.
The function returns the current date & time.
S,LE ID"'&T IDTO "tudents ("tudDo( <name( -O=( <ees( )ender!
$alues (/013( L6ohanS( "ysdate( 04>>>(S6S! K
USER FUNCTION :
It is special function( %hich records the current @"'& Dame.
S,LE ID"'&T IDTO "tudents ("tudDO( <name( Lname( -O=( -O*( <ees!
$alues (/013( L"ureshS( @ser( "ysdate( S04G#anG8>S( 1>>>>! K
SU'STITUTION /ARIA'LES :
These variables are used to stored values temporarily.
The values can be stored temporarily through
/. "ingle Ampersand (&!
0. -ouble Ampersand (&&!
1. -'<ID' and ACC'PT Commands
The "ingle Ampersand substitution variable applies for each instance %hen the "EL statement is
create or eecuted.
The -ouble Ampersand substitution variables applied for all instances until that "EL statement is
eisting.
USING SINGLE AMPERSAND SU'STITUTION /ARTIA'LE :
S,LE ID"'&T IDTO "tudents ("tudI-( <name( LDame( -ob( <ees!
$AL@'" (&"tudDo( LDameS( &Last Dame( L-ate Jof J*irth( 04>>>! K
USING DOU'LE AMPERSAND SU'STITUTION /ARIA'LE :
Oragle 10 g Material

15
S,LE ID"'&T IDTO "tudents ("tudDo( <name( Lname( -ob( -o#( <ees( )ender!
$alues (&"tudDo( L&<irstDameS( L& LastDameS( L-obS( "ysdate(
&&<ees( &)ender! K
DEFINING CUSTOMIED PROMPTS :
@sing the ACC'PT Command of "EL HPlus( the prompts can be customi2ed.
ACC'PT Command stores the values in a variable.
P&O6PT Command displays the Customi2ed tet.
After creating a script using the substitutions variable to a file it can called %ith ACC'PT and
P&O6PT %ith greater meaning.
E1AMPLE:
ACC'PT "tudRid P&O6PT LPlease 'nter the "tudent DumberBS
ACC'PT <irRname P&O6PT LPlease 'nter the <irst DameBS
ACC'PT CouRfees P&O6PT LPlease 'nter the Course <eesBS
S,LE ID"'&T IDTO "tudents ("tudDo( <name( <ees!
$AL@'" (&"tudRid (S&<irRnameS( &CouRfees! K
E1AMPLE:
ACC'PT -epartmentRId P&O6PT LPlease 'nter the -epartment DumberBS
ACC'PT -epartmentRDame P&O6PT LPlease 'nter the -epartment DameBS
ACC'PT Location P&O6PT LPlease 'nter the LocationBS
S,LE ID"'&T IDTO -'PT (-eptno( -name( Loc!
$AL@'" (&-apartmentRId( S&-epartmentRDameS( S&LocationS ! K
CREATING AN S,L SCRIPT FILE:
The "A$' Command is used to store the current contents of the "EL *uffer
Steps:
/. At "EL prompt type the full name of the path %here the file has to be created.
0. )ive the name of the file %ith :sI etension.
1. If the file has to be replaced %ith the same eisting name then use &'PLAC'
ALL clause.
E1AMPLE:
S,LE "A$' -BX6y -irX"ELscriptXInsert prompt.sFl
S,LE "A$' -BX6y -irX"ELscriptXInsert prompt.sFl &'PLAC' ALL
,UER!ING THE DATA FROM TA'LES:
,UER!: It is an operation that retrieves data from from one or more tables or vie%s.
SELECT STATEMENT:
The "'L'CT statement is used to retrieve data from one or more tables ( ob#ect tables (vie%s(
ob#ect vie%s or management vie%s.
Oragle 10 g Material

16
PRERE,UISITES :
The user must have the "'L'CT privileges on the specified ob#ect.
The "'L'CT AD? TA*L' allo%s to select the data from any recogni2ed ob#ect.
CAPA'ILITIES IF S,< SE<ECT STATEMENT :
The "'L'CT statement can be used to select or &etrieve data from the ob#ect using any one of
the follo%ing criteria.
o SELECTION :
It chooses the ro%s in a table that are epected to return by a Fuery.
o PRO.ECTION :
It chooses the columns in a table that are epected to return by a Fuery.
o .OIN :
It chooses the data in from one or more numbers of tables by creating a lin+
bet%een them.
'as"c SELECT S&*ta6 :
SELECT J DISTINCT K L - F C%5$* J A"as K F G:: M Fr%$ Tabe H
SELECT Identifies Columns( FROM Identifies Tables .
SELECT "pecifies a list of Column ( one C more !
DISTINCE "uppresses -uplicates
- "elect all Columns.
COLUMN "elects the Damed Column.
A"as )ives selected columns different columns.
FROM Tabe "pecifies the table containing the columns.
WRITING S,L STATEMENTS TO SELECT OR RETRI/E DATA FROM TA'LES :
T@e Sa$pe Tabes Use3 :
Tabe 1 : EMP
Oragle 10 g Material

17
C%5$* Na$e Data T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
E$p*% NUM'ER
E*a$e /ARCHAR
Dept*% NUM'ER
.%b /ARCHAR
Sa NUM'ER
C%$$ NUM'ER
Mgr NUM'ER
H"re3ate DATE
Tabe 8 : DEPT
C%5$* Na$e Data T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
Dept*% NUM'ER
D*a$e /ARCHAR
L%c /ARCHAR
Tabe 9 : SALGRADE
C%5$* Na$e Data T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
H"sa NUM'ER
L%sa NUM'ER
Gra3e NUM'ER
Retr"e="*g Data #r%$ A C%5$*s :
"Fl P "elect H from 'mpK
"Fl P "elect H from -eptK
"Fl P "elect H from "algradeK
In this the N - N is a pro#ection operator.
It pro#ects data from all the columns eisting in the table %ith all records.
The data is displayed in a table format.
Retr"e="*g Data #r%$ Spec"#"c C%5$*s :
"Fl P "elect empno( ename( sal from empK
"Fl P "elect ename( #ob( sal( deptno form empK
"Fl P "elect deptno( dname( loc from deptK
"Fl P "elect Iisal( Losal( )rade from "algradeK
"Fl P "elect empno( ename( sal ( hiredate from empK
Oragle 10 g Material

18
The column names need not be in the same order as table.
The columns should be separated using comma.
The column names can be separated onto different lines %ithin the "EL *uffer.
The casing of column names is not important.
C%5$* Hea3"*g De#a5ts :
The default #ustification of the data after it is retrieved from the table is ...
L'<T -ate and Character -ata
&I)IT Dumeric -ata
The default display of the data is al%ays is UPPER Casing.
The character and date column headings can be truncated( *ut numb ers cannot be
truncated.
App&"*g Ar"t@$et"ca Operat"%*s "* Seect State$e*ts :
Arithmetic 'pressions can be implemented through "'L'CT statement.
Arithmetic 'pressions can be implemented to
o 6odify the %ay the data is displayed.
o Perform calculations.
o Implement ,IAT J I< "cenarios.
An Arithmetic 'pression can contain
o "imple Column names
o Constant numeric values
o Arithmetic operators.
ARITHMETIC OPERATORS :
The Arithmetic operators can be used to create epressions on D@6*'& and -AT' data.
The Arithmetic operators supported are ...
o Addition :
o "ubtraction G
o 6ultiply H
o -ivide C
The Arithmetic operators can be used in any clause of a "EL statement. 'cept the <&O6
clause.
"EL H Plus ignores *ac+ "paces before and after the Arithmetic operator.
"Fl P "elect empno( ename( sal( sal : 4>> from empK
Oragle 10 g Material

19
"Fl P "elect empno( ename( sal( sal J />>> from empK
Operat%r Prece3e*ce :
6ultiplication and -ivision ta+e priority over addition and subtractions ( H C : G !
Operators of the same priority are evaluated from left to right.
To prioriti2e evaluation and to increase clarity parenthesis can be implemented.
"Fl P "elect empno( ename( sal( ( /0 H sal ! : />> from 'mpK
"Fl P "elect empno( ename( sal( /0 H ( sal : 4>> ! from 'mpK
Ha*3"*g N5 /a5es :
NULL : It is a value %hich is
@navailable
@nassigned
@n+no%n
Inapplicable
A NULL is not same as 2ero or blan+ space.
If a ro% lac+s the data for a particular column( than that value is said to be NULL or to contain
NULL:
SI E "elect ename( #ob( sal( comm <rom 'mp K
If any column value is an Arithmetic epression is D@LL( the overall result is also D@LL.
The above situation is termed as D@LL propagation and has to be handled very carefully.
"Fl P "elect ename( #ob( sal( comm( /0 H "al : comm <rom 'mp K
"Fl P "elect ename( #ob( sal( comm( /0 H sal : comm from 'mp K
N/L F5*ct"%* :
The D$L function is used to convert a D@LL value to an actual value.
S&*ta6 :

D$L ( 'pr/ ( 'pr0 !
'pr/ B is the source value or epression that may contain D@LL.
'pr0 B is the target value for converting D@LL.
D$L <unction can be used to convert any data type( the return value is al%ays the same as the
data type of 'pr/.
The -ata types of the "ource and -estination must match
o D$L ( Comm ( > !
o D$L ( Iiredate ( S>/G=@DG99S!
o D$L ( =ob ( LDot AssignedS!
Oragle 10 g Material

20
"Fl P "elect 'name( "al( Comm( "al : D$L ( Comm( > ! <rom 'mpK
"Fl P "elect 'name( "al( Comm( ( "al H /0 ! : D$L (Comm ( > ! <rom 'mpK
"Fl P "elect 'name( "al( Comm( ( "al : 4>> ! : D$L ( Comm ( > ! <rom 'mpK
W%r?"*g B"t@ A"ases :
An Alias is an alternate name given for any Oracle Ob#ect.
Aliases is Oracle are of t%o types
HH Column Alias HH Table Alias
Column Alias &enames a Column Ieading
The Column alias is specified in the "'L'CT list by declaring the Alias after the Column Dame by
using the space separator.
ALIA" Ieadings appear is @PP'& Casing by default.
The Alias should be declared in double Fuotes if it is against the specifications of Daming
Conversions.
The A" +ey%ord can be used bet%een the Column Dame and Alias.
An alias effectively renames the "'L'CT list item for the duration of the Fuery.
An alias cannot be used( any %here in the "'L'CT list for operational purpose.
"Fl P "elect 'mpno Dumbers(
'name Dame(
"al Y*asic "alaryZ(
=ob -esignation
<rom 'mpK
"FlP "elect -eptno A" Y -epartment Id Z (
-name A" Y -epartment Dame Y (
Loc A" Place
<rom -eptK
"FlP "elect Iisal As Y 6aimum &ange Y (
Losal As Y 6inimum &ange Y (
)rade
<rom "algrade K
LITERALS IN ORACLE B
A L"tera and a C%*sta*t value are synonyms to one another and refer to a fied data value.
The types of Literals recogni2ed by Oracle are
o Tet Literals
o Integer Literals
o Dumber Literals
o Interval Literals
Te6t L"teras :
Oragle 10 g Material

21
It specifies a tet or character literal.
It is used to specify values %henever LtetS or CIA& appear in
HH 'pression HH Condition HH "EL <unction HH "EL "tatements.
It should be enclosed in single Fuotes
They have properties of both CIA& and $A&CIA&0 data types.
A tet literal can have a maimum length of 3>>> *ytes.
' B L mployee Information L
L 6anagerZs "pecification L
D Ln char Literal L
Us"*g L"tera C@aracter Str"*gs :
A literal that is declared in a "'L'CT list can be a character( a Dumber( or a -ate.
A literal is not a column name or a column alias.
A literal is printed for each ro%( that is retrieved by the "'L'CT statement.
Literal strings of <reeG<orm test can be included in the Fuery as per the reFuirement.
-ate and character literals must be enclosed %ithin the single Fuotation mar+s L L
Literals increase the readability of the output.
"Fl P "elect 'name [ [ L B L [ [ L 6onth "alary \ L [ [ "al As "alaries <rom 'mp K
"Fl P "elect L The -esignation of L [ [ 'name [ [ L is L [ [ =ob As -esignation <rom
'mpK
"Fl P "elect L The Annual "alary of L [ [ 'name [ [ L is L [ [ "al H /0 As
AnnualR"alary <rom 'mp K
"Fl P "elect -name [ [ L -epartment is Located at L [ [ Loc <rom -ept K
"Fl P "elect 'name [ [ L =oined the Organi2ation on L [ [ Iiredate <rom 'mp K
"Fl P "elect 'name [ [ L ,or+s in -epartment Dumber L [ [ -eptno [ [ L as L
[ [ =ob <rom 'mp K
App&"*g C%*cate*at"%* Operat%r :
The Concatenation operator lin+s columns to other columns( Arithmetic epressions( or constant
values.
Columns on either side of the operator are combined to ma+e a single output column.
The resultant column is treated as an character epression.
The Concatenation operator is represented in Oracle by double pipe symbol ( [ [ !
"Fl P "elect 'mpno [[ 'name [[ L ( -esignation is L [[ =ob Y 'mployees Information Y
<rom 'mp K
"Fl P "elect L The *asic "alary of L [[ 'name [[ L is &s L [[ "al 'mployee <rom 'mp K
E"$"*at"*g t@e D5p"cate R%Bs #r%$ t@e O5tp5t :
@ntil it is instructed "EL H Plus displays the results of a Fuery %ithout eliminating duplicate ro%s.
Oragle 10 g Material

22
To eliminate the duplicate ro%s in the result( the -I"TIDCT +ey%ord is used.
6ultiple columns can be declared after the -I"TIDCT Fualifier.
The -I"TIDCT Fualifier affects all the selected columns( and represents a distinct combination of
the columns.
"Fl P "elect -I"TIDCT -eptno <rom 'mp K
"Fl P "elect -I"TIDCT =ob ( -eptno <rom 'mp K
"Fl P "elect -I"TIDCT -eptno ( =ob <rom 'mp K
F"ter"*g %# Rec%r3s :
The number of ro%s returned by a Fuery can be limited using the WHERE clause.
A ,I'&' Clause contains a condition that must be met and should directly follo% the <rom
Clause.
S&*ta6 :

"'L'CT T -istinct W U H [ Column T alias W ( ..V <rom Table T ,I'&' Condition(s! W K
The ,I'&' clause can compares
HH $alues in Columns
-- Literal $alues
-- Arithmetic 'pressions
-- <unctions
The components of ,I'&' clause are
HH Column Dame
HH Comparison Operator
HH Column Dame( constant or list of values.
The Character strings and dates should be enclosed in single Fuotation mar+s.
Character values are case sensitive and -ate values are format sensitive (--G6ODG??!
The Comparison operator are used in conditions that compare one epression to another.
The different comparison operators are
HH \ HH Q P ( ] \ ( ^ \ ( HH P HH Q HH P \ HH Q \
The format of the ,I'&' clause is ,I'&' 'pr operator value.
'amples B
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob \ L 6ADA)'& L
"Fl P "elect 'name( Iiredate ( -eptno( "al <rom 'mp ,here -eptno \ /> K
"Fl P "elect 'mpno( 'name( "al <rom 'mp ,here "al P \ 1>>> K
"Fl P "elect 'name [[ L =oined on L [[ Iiredate Y 'mployees =oining -ates Y
<rom 'mp ,here Iiredate \ L >/ J =AD J 94 L K
Oragle 10 g Material

23
"Fl P "elect 'name [[ L ,or+s in -epartment L [[ -eptno L 'mployees and
-eaprtments L <rom 'mp ,here -eptno Q P 0> K
"Fl P "elect 'name( "al( -eptno( =ob <rom 'mp ,here =ob Q P L CL'&; L K
"Fl P "elect 'name Dame ( "al *asic ( "al H /0 Annual <rom 'mp
,here "al H /0 P 5>>> K
L%g"ca Operat%rs :
The Logical operators combine the results of t%o components conditions to produce a single
result.
The Logical operators provided by Oracle are
HH AD- HH O& HH DOT

AND Operat%r B
It &eturns T&@' if both or all component conditions are T&@'.
It &eturns <AL"' if either is <AL"'( else returns un+no%n.
Tr5t@ Tabe :
AND TRUE FALSE NULL
TRUE T F NULL
FALSE F F F
NULL NULL F NULL
"Fl P "elect 'mpno( 'name( =ob( "al <rom 'mp ,here "al P \ //>> AD-
=ob \ LCL'&;S K
"Fl P "elect 'mpno( 'name ( =ob ( "al <rom 'mp ,here -eptno \ />
AD- =ob \ L6ADA)'&S K
"Fl P "elect 'name( "al ( =ob <rom 'mp ,here "al P \ /4>> AD- "al P 4>>> K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here ( "al P \ /4>> AD- "al Q \ 4>>> !
AD- =ob \ L6ADA)'&S K
OR Operat%r :
It returns T&@' if either component conditions is T&@'
It returns <AL"' if both are <AL"'( else returns un+no%n
Tr5t@ Tabe :
OR TRUE FALSE NULL
TRUE T T T
Oragle 10 g Material

24
FALSE T F NULL
NULL T NULL NULL
"Fl P "elect 'mpno( 'name( =ob( "al <rom 'mp ,here "al P \ //>> O&
=ob \ LCL'&;S K
"Fl P "elect 'mpno( 'name( =ob( "al <rom 'mp ,here -eptno \ />
O& =ob \ L6ADA)'&S K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here "al P \ /4>> O& "al P \ 4>>> K
"Fl P "elect 'name( "al( =ob( -eptno <rom 'mp ,here -eptno \ /> O& -eptno \ 0> K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob \ LCL'&;S O& =ob\S6ADA)'&S K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here ( "al Q \ 04>> O& "al P \ 4>>> ! O&
=ob \ L6ADA)'&S K
NOT Operat%r :
It returns T&@' if the follo%ing condition is <AL"'.
It returns <AL"' if the follo%ing condition is T&@'.
If the condition is @n+no%n( it returns @n+no%n.
Tr5t@ Tabe :
TRUE FALSE NULL
NOT F T NULL
C%$b"*at"%* %# AND a*3 OR Operat%rs :
"Fl P "elect 'mpno( 'name( =ob( "al <rom 'mp ,here ( "al P //>> O& =ob\LCL'&;S !
AD- -eptno \ 0> K
"Fl P "elect 'mpno( 'name( =ob( "al <rom 'mp ,here ( -eptno \ /> AD-
=ob \ L6ADA)'&S ! O& "al P \ 1>>> K
N%t Operat%r E6a$pes :
"Fl P "elect 'name( "al( =ob <rom 'mp ,here DOT =ob \ L6ADA)'&S K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here DOT "al P 4>>> K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here DOT "al Q 4>>> K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here Dot Iiredate\S0>G<'*G8/S K
"Fl P "elect 'name( =ob( "al( -eptno <rom 'mp ,here DOT
=ob \ L "AL'"6ADS AD- -eptno \ 1> K
S%$e T@"*gs T% N%te GG:
Oragle 10 g Material

25
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob P L6ADA)'&S K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob Q L6ADA)'&S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here Iiredate P 0>G<'*G/98/S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here Iiredate Q S0>G<'*G/98/S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here IiredateQPS0>G<'*G/98/S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here =ob Q P LCL'&;S K
"Fl P "elect 'name( "al( Comm <rom 'mp ,here Comm is D@LL K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here DOT =ob P L6ADA)'&S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here DOT
Iiredate \ S/7G-'CG/98>S K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here DOT
Iiredate P S/7G-'CG/98>S K
R5es %# Prece3e*ce :
The default Precedence order is
HH All Comparison Operators
HH DOT Operator
HH AD- Operator
HH O& Operator
The Precedence can be controlled using parenthesis.
S,L - PLUS Operat%rs :
'ETWEEN G: AND G: H NOT 'ETWEEN G:: AND G:
o This Operator is used to display ro%s based on a range of values.
o The declared range is inclusive
o The %Ber limit should be declared first.
"Fl P "elect 'name( "al( =ob <rom 'mp ,here "al *et%een />>> AD- /4>> K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here "al DOT *et%een />>> AD- /4>> K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob *et%een L6ADA)'&S AD-
Oragle 10 g Material

26
L"AL'"6ADS K
"Fl P "elect 'name( "al( =ob <rom 'mp ,here =ob DOT *et%een L6ADA)'& AD-
L"AL'"6ADS K
"Fl P "elect 'name( "al( =ob( Iiredate <rom 'mp ,here Iiredate *et%een
S/7G<'*G/98/S AD- S0>G=@DG/981S K
"Fl P "elect 'name( "al( =ob( Iiredate <rom 'mp ,here Iiredate DOT *et%een
S/7G<'*G/98/S AD- S0>G=@DG/981S K
IN Operat%r H NOT IN Operat%r :
The Operator is used to test for values in a specified list.
The Operator can be used upon any datatype.
"Fl P "elect 'name( "al( =ob <rom 'mp ,here 'name ID( L<O&-S( LALL'DS ! K
"Fl P "elect 'name ( "al( =ob <rom 'mp ,here 'name DOT ID( L<O&-S ( LALL'DS ! K
"Fl P "elect 'name( "al( -eptno <rom 'mp ,here -eptno ID( /> ( 1> ! K
"Fl P "elect 'name( "al( -eptno <rom 'mp ,here -eptno DOT ID( /> ( 1> ! K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here
Iiredate ID( S0>G<'I*G/98/S ( S>9G=@DG/98/S ! K
"Fl P "elect 'name( "al( Iiredate <rom 'mp ,here
Iiredate DOT ID( S0>G<'I*G/98/S ( S>9G=@DG/98/S ! K
IS NULL Operat%r H IS NOT NULL Operat%r :
The Operator tests for D@LL values
lt is the only operator that can be sued to test for D@LLSs.
D@LL means the value is unavailable( @nassigned( @n+no%n( or inapplicable.
"Fl P "elect 'name( -eptno( Comm <rom 'mp ,here Comm I" D@LL K
"Fl P "elect 'name( -eptno( =ob <rom 'mp ,here 6gr I" D@LL K
"Fl P "elect 'name( -eptno( Comm <rom 'mp ,here Comm I" DOT D@LL K
"Fl P "elect 'name( -eptno( Comm <rom 'mp ,here 6gr I" DOT D@LL K
LI<E Operat%r H NOT LI<E Operat%r :
The LI;' Operator is used to search to search for a matching character.
The character pattern matching operation is referred as a %ild card search
The available %ild cards in Oracle are
_ @sed to represent any seFuence of Mero or more characters.
C &epresents any single character( only at that position only.
Oragle 10 g Material

27
The ,ild Card symbols can be used in any combination %ith literal characters.
<or finding eact match for L _ L and L J L the '"CAP' option has to be used( %hich is L X L symbol
%ith '"CAP' Option.
"Fl P "elect 'name( =ob <rom 'mp ,here 'name LI;' L"_S K
"Fl P "elect 'name( =ob <rom 'mp ,here 'name DOT LI;' L"_S K
"Fl P "elect 'name( =ob <rom 'mp ,here 'name LI;' LRA_S K
"Fl P "elect 'name =ob <rom 'mp ,here 'name DOT LI;' LRA_S K
"Fl P "elect 'name "al <rom 'mp ,here 'name \ L"6_S K
"Fl P "elect 'name( "al <rom 'mp ,here L"6_S LI;' 'name K
"Fl P "elect 'name( Iiredate <rom 'mp ,here Iiredate LI;' L_G<'*G/98/S K
"Fl P "elect 'name( Iiredate <rom 'mp ,here Iiredate LI;' S>1G_G/98/S K
"Fl P "elect H <rom -ept ,here -name LI;' L_XR_S '"CAP' LXS K
Or3er"*g I*#%r$at"%* :
The Order of ro%s returned in a Fuery result is undefined.
The O&-'& *? Clause can be used to sort the ro%s
The O&-'& *? Clause should be the last clause in the order of all clauses in the "'L'CT
statement.
An epression or an alias can be specified to sort.
-efault ordering of -ata is Ascending
-- N5$bers 1 7 >>>
-- Dates Ear"est 7 Latest
-- Str"*gs A 7 O H NULLS Last:
S&*ta6 :
SELECT 'pr <rom Table Dame T ,I'&' Condition(s! W
T O&-'& *? U Column( 'pr V T A"C C -'"C W W K
The default ordering upon a column is Ascending( to change the default ordering -'"C should
be used after the column name.
"orting can be implemented on column aliases( and can also be implemented upon multiple
columns.
The controversy of sorting is bro+en only %hen there arises a conflict of consistency upon the
data in a column.
Oragle 10 g Material

28
"Fl P "elect 'name( =ob( -eptno( Iiredate <rom 'mp O&-'& *? Iiredate K
"Fl P "elect 'name( =ob( -eptno( Iiredate <rom 'mp O&-'& *? Iiredate -'"C K
"Fl P "elect 'name( =ob( "al <rom 'mp ,here =ob \ L6ADA)'&S O&-'& *? "al K
"Fl P "elect 'name( =ob( "al <rom 'mp ,here "al P \ 04>> O&-'& *? =ob(
'name -'"C K
"Fl P "elect 'mpno( 'name( "al( "al H /0 Annsal <rom 'mp O&-'& *? Annsal K
"Fl P "elect 'mpno( 'name( "al <rom 'mp O&-'& *? -eptno( "al( Iiredate K
"Fl P "elect 'mpno( 'name( "al <rom 'mp ,here "al P \ 0>>> O&-'& *? Iiredate(
"al -'"C K
The single ro% functions can appear in
HH "'L'CT List
HH ,I'&' List
HH "TA&T ,ITI Clause
HH CODD'CT *? Clause
The types of single ro% functions are
HH CIA&ACT'& HH D@6*'& HH -AT' HH COD$'&"IOD
M5t"pe R%B F5*ct"%*s :
These functions manipulate groups of ro%s to give one result per group of ro%s.
S"*ge R%B F5*ct"%*s :
They are used to manipulate data items.
They accept one or more arguments and return one value for each ro% returned by the Fuery.
An argument can be B
HH @ser "upplied Constant.
HH $ariable $alue.
HH Column Dame.
HH 'pression.
S&*ta6 :

<uncRDame ( Column C 'pr( T Arg/( Arg0( ..W !
S"*ge R%B F5*ct"%*s Feat5res :
Acts on each ro% returned in the Euery.
&eturn one result per ro%
6ay return a data value of a different type than that referenced
6ay epert one or more arguments.
Can be used in "'L'CT( ,I'&' and O&-'& *? Clauses.
Oragle 10 g Material

29
Can be nested.
Spec"#"cat"%* 'e@a="%r %# F5*ct"%*s :
C@aracter F5*ct"%*s : Accept Character input and can return both Character and
Dumber $alues.
N5$ber F5*ct"%*s : Accept Dumeric input and return Dumeric $alues.
Date F5*ct"%*s : Operate on values of date data type( can return both -ate and
Dumber.
C%*=ers"%* F5*ct"%* : Covert a value from one data type to another.
Ge*era F5*ct"%*s :
-- N/L Operates on D@LL $alues.
HH DECODE Operates on any data type and can return any data type.
C@aracter F5*ct"%*s :
They are functions that return character values( unless stated.
They return the data type $A&CIA&0( limited to a length of 3>>> *ytes
If the return value length eceeds( then the return value is truncated( %ithout an error.
The functions are categori2ed as
HH CIA&ACT'& 6ADIP@LATIOD <@DCTIOD".
HH CA"' COD$'&"IOD <@DCTIOD".
C@aracter Ma*"p5at"%* F5*ct"%*s :
L%Ber F5*ct"%* :
It converts Alpha character values to lo%er case
The return value has the same data type as argument char type ( CIA& or $A&CIA&0 !
S&*ta6 B LO,'& ( Column C 'pression !
"Fl P "elect LO,'&( LO&ACL' CO&PO&ATIODS ! <rom -ual K
"Fl P "elect 'name( LO,'& ( L 6? ID<O&6ATIODS ! <rom 'mpK
"Fl P "elect 'name( LO,'& ( 'name ! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect L The L [[ 'name [[ L Y s -esignation is L [[ =ob <rom 'mp
,here LO,'& ( =ob ! \ L managerS K
Upper F5*ct"%* :
It Converts the Alpha character values to @pper Case.
The return value has the same data type as the argument char.

S&*ta6 B @PP'& ( Column C 'pression !
Oragle 10 g Material

30
"Fl P "elect @pper ( L Oracle CorporationS ! <rom -ual K
"Fl P "elect 'name( @pper ( L 6y Information L ! <rom -ual K
"Fl P "elect 'name( Lo%er ( 'name !( @pper ( 'name ! <rom 'mp
,here =ob \ L6ADA)'&S K
"Fl P "elect 'name( =ob <rom 'mp ,here =ob \ @pper ( L6anagerS ! K
"Fl P "elect 'name( =ob( "al( "al H /0 <rom 'mp ,here =ob \
@pper ( Lo%er ( L6AD)'&S ! ! K
"Fl P "elect L The L [[ 'name [[ L Y s -esignation is L [[ Lo%er ( =ob ! ,here
=ob \ @pper ( LmanagerS ! Order *y "al K
"Fl P "elect @pper ( L The L [[ 'name [[ L Y s *asic "alary is &upees L [[ "al !
Y'm
ployee "alaries Y <rom 'mp ,here =ob In( L6ADA)'&S (
@pper ( Lcler+S ! ! Order *y "al -esc K
INITCAP F5*ct"%* :
It converts the Alpha character values to @ppercase for the first letter of each %ord( +eeping all
other letters in Lo%er case.
,ords are delimited by ,hite spaces or characters that are not alphanumeric.
S&*ta6 : INITCAP ( Column C 'pression !
"Fl P "elect Initcap ( Loracle corporationS ! <rom -ual K
"Fl P "elect L The =ob Title for L [[ Initcap ( 'name ! [[ L is L [[ lo%er ( =ob ! -etails
<rom 'mpK
"Fl P "elect 'name( @pper ( 'name ! ( Lo%er ( 'name ! ( Initcap ( 'name ! <rom 'mp K
"Fl P "elect 'mpno( Initcap ( 'name !( -eptno <rom 'mp ,here
'name \ @pper ( Lbla+eS ! K
CONCAT F5*ct"%* :
It Concatenates the first characters value to the second charactger value. Only t%o parameters
accept .
It return the character data type.
S&*ta6 : CONCAT ( Column/ C 'pr/( Column0 C 'pr0 !
"Fl P "elect Concat ( LOracleS ( LCorporationS ! <rom -ual K
"Fl P "elect 'name( =ob( Concat ( 'name( =ob ! <rom 'mp ,here -eptno \ /> K
Oragle 10 g Material

31
"Fl P "elect Concat ( L The 'mployee Dame is L ( Initcap ( 'name ! ! As
Y'mployee Dames Y <rom 'mp ,here -eptno in( />( 1> ! K
"Fl P "elect Concat ( Concat ( Initcap ( 'name !( L is a L ! ( =ob ! =ob <rom 'mp
,here -eptno in( />( 0> ! K
SU' STRING F5*ct"%* :
&eturns specified characters form character value( starting from a specified position
L m S ( to L n S characters long.
P%"*ts t% Re$e$ber GG:
o If m is > ( it is treated as /.
o If m is positive( Oracle counts from the beginning of char to find the first character.
o If m is Degative( Oracle counts bac+%ards from the end of the Character.
o If n is Omitted ( Oracle returns all characters to the end of Char.
o If n is less than / or > ( A D@LL is returned.
<loating point numbers passed as arguments to "ubstr are automatically converted to Integers.
S&*ta6 : SU'STR ( Col C 'pr( m( n !
"Fl P "elect "ubstr ( LA*C-'<)IS ( 1( 3 ! <rom -ual K
"Fl P "elect "ubstr ( LA*C-'<)IS ( G4( 3 ! <rom -ual K
"Fl P "elect "ubstr ( LA*C-'<)IS ( > (3 ! <rom -ual K
"Fl P "elect "ubstr ( LA*C-'<)IS ( 3 ! <rom -ual K
"Fl P "elect "ubstr ( LA*C-'<)IS( 3( > ! <rom -ual K
"Fl P "elect "ubstr ( LA*C-'<)IS( 3 ( G0 ! <rom -ual K
"Fl P "elect 'name( =ob <rom 'mp ,here "ubstr ( =ob( 3( 1 ! \ @pper( LageS ! K
"Fl P "elect Concat ( Initcap ( 'name ! (
Concat ( L is a L ( Concat ( Initcap ("ubstr ( =ob( /( 1 ! !( L'ater.S ! ! !
<rom 'mp ,here "ubstr ( =ob ( 3( 1 ! \ @pper ( LAgeS ! K

LENGTH F5*ct"%* :
&eturns the number of characters in a value.
If the char has data type CIA&( the length includes all trailing blan+s.
If the char is D@LL ( it return D@LL .
S&*ta6 : LENGTH ( Column C 'pression !
"Fl P "elect Length ( LO&ACL'S! <rom -ual K
"Fl P "elect Length ( 'name ! [[ L Characters eit in L [[ Initcap ( 'name ! [[ L Ys Dame.L
As YDames and Lengths Y <rom 'mp K
Oragle 10 g Material

32
"Fl P "elect Initcap( 'name ! ( =ob from 'mp ,here Length ( =ob ! \ 7 K
"Fl P "elect Inticap ( 'name ! ( =ob <rom 'mp ,here "ubstr ( =ob( 3( Length (
"ubstr ( =ob( 3( 1 ! ! ! \ LA)'S K
INSTRING F5*ct"%* :
It returns the numeric position of a named character.

S&*ta6 : INSTR ( Column C 'pression( Char ( n( m !
"earches for Column C 'pression beginning %ith its L n Lth character for the L m Lth occurrence of
char0( and returns the position of the character in char/( that is the first character of this
occurrence.
L n L can be positive or negative( if negative searches bac+%ard from the end of column C
'pression.
The value of L m L should be positive .
The default values of both m and n are /.
The return value is relative to the beginning of char/ regardless of the value of n( and is
epressed in characters.
If the search is unsuccessful( the return value is 2ero.
"Fl P "elect Instr ( L"tringS ( LrS! <rom -ual K
"Fl P "elect Instr ( LCO&PO&AT' <OO&S( LO&S( 1( 0 ! <rom -ual K
"Fl P "elect Instr ( LCO&PO&AT' <LOO&S( LO&S( G1( 0 ! <rom -ual K
"Fl P "elect =ob( Instr ( =ob( LAS( /( 0! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect =ob( Instr ( =ob( LAS( 0( 0 ! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect =ob( Instr ( =ob( LAS( 1( 0 ! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect =ob( Instr ( =ob( LAS( 0 ! <rom 'mp ,here =ob \ L6ADA)'&S K
LPAD F5*ct"%* :
Pads the character value right #ustified to al total %idth of n character positions.
The default padding character is space.

S&*ta6 : LPAD ( Char/( n( Char 0 !
"Fl P "elect Lpad ( LPage /S( /4( LH.S ! <rom -ual K
"Fl P "elect Lpad ( LPage /S( /4! <rom -ual K
"Fl P "elect 'name( Lpad ('name( />( LGL ! from 'mp ,here "al P \ 04>> K
RPAD F5*ct"%* :
Pads the character value left #ustified to a total %idth of n character positions.
The -efault padding character is space.
Oragle 10 g Material

33

S&*ta6 : RPAD ( Char/( n( Char 0 !
"Fl P "elect &pad ( LPage /S ( /4( LH.S ! <rom -ual K
"Fl P "elect &pad ( LPage /S ( /4 ! <rom -ual K
"Fl P "elect 'name( &pad('name( />( LGL ! <rom 'mp ,here "al P \ 04>> K
"Fl P "elect 'name( Lpad ( 'name( />( LGL ! ( &pad ( 'name( />( LGL ! <rom 'mp K
"Fl P "elect 'name( Lpad ( &pad ( 'name( />( LGL !( /4( LGL ! <rom 'mp K
LTRIM F5*ct"%* :
It enables to trim heading characters from a character string.
All the leftmost characters that appear in the set are removed.
S&*ta6 : LTRIM ( Char( set !
"Fl P "elect Ltrim ( Ly2AyLA"T ,O&-S( LyS ! <rom -ual K
"Fl P "elect =ob( Ltrim ( =ob ! <rom 'mp ,here =ob Li+e L6ADA)'&S K
RTRIM F5*ct"%* :
It enables the trimming of trailing characters from a character "tring.
All the right most characters that appear in the set are removed.
S&*ta6 : RTRIM ( Char( set !
"Fl P "elect &trim ( L*&O,DID)yAyS( LyS ! <rom -ual K
"Fl P "elect &trim ( =ob( L'&S ! ( =ob <rom 'mp ,here Ltrim ( =ob ( L6ADS ! Li+e L)'&S K
TRIM F5*ct"%* ( ; " )
It enables to trim heading or trailing characters or both from a character string.
If Lea3"*g is specified concentrates on leading characters.
If Tra""*g is specified concentrates on trailing characters.
If '%t@ or none is specified concentrates both on leading and trailing.
&eturns the varchar0 type.
S&*ta6 : TRIM ( Leading C Trailing C *oth( Trim character <rom Trim source !
"Fl P "elect Trim ( L"S <rom L6ITI""S ! <rom -ual K
"Fl P "elect Trim ( L"S <rom L""6ITIS ! <rom -ual K
"Fl P "elect Trim ( L"S <rom L""6ITI""S ! <rom -ual K
"Fl P "elect Trim ( Trailing L"S <rom L""6ITI""S ! <rom -ual K
"Fl P "elect Trim ( Leading L"S <rom L""6ITI""S ! <rom -ual K
Oragle 10 g Material

34
"Fl P "elect Trim ( *oth L"S <rom L""6ITI""S ! <rom dual K
REPLACE F5*ct"%* :
It returns the every Occurrence of search string replaced by the replacement string.
If the replacement string is omitted or null( all occurrences of search string are removed.
It substitutes one string for another as %ell as to remove character strings.
S&*ta6 : REPLACE ( Char( "earchR"tr( &eplaceR"tr !
"Fl P "elect &eplace ( L=ac+ and =ueS ( L=S( L*LS! <rom -ual K
"Fl P "elect 'name( &eplace(=ob( L6ADS( L-A6S ! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect =ob( &eplace ( =ob( LPS ! <rom 'mp ,here =ob \ LP&'"I-'DTS K
"Fl P "elect =ob( &eplace ( =ob( L6ADS( L'A'C@TI$'S ! <rom 'mp ,here
=ob \ L"AL'" 6ADS K
TRANSLATE F5*ct"%* :
@sed to Translate Character by character in a "tring .
S&*ta6 : TRANSLATE ( char( <rom( To !
It returns a char %ith all occurrences of each character in L <rom L replaced by its corresponding
character in L To L.
Characters in char that are not in <rom are not replaced.
The argument <rom can contain more characters than To.
If the etra characters appear in Char( they are removed from the return value.
"Fl P "elect =ob( Translate ( =ob( LPS( L L ! <rom 'mp ,here =ob \ LP&'"I-'DTS K
"Fl P "elect =ob( Translate ( =ob( L6DS( L-6S ! <rom 'mp ,here =ob \ L6ADA)'&S K
"Fl P "elect =ob( Translate ( =ob( LAS( LOS ! <rom 'mp ,here =ob \ L"AL'"6ADS K
CHR F5*ct"%* :
It returns a character having the binary eFuivalent to L n L.
It returns the eFuivalent for L nS in database character set or national character set.
S&*ta6 : CHR ( n ! CHR ( n @sing DCIA&RC" !
"Fl P "elect Chr ( 57 ! [[ Chr (54 ! [[ Chr ( 83 ! "ample <rom -ual K
"Fl P "elect Chr ( /57>4 @sing DCIA&RC" ! <rom -ual K
ASCII F5*ct"%* :
Oragle 10 g Material

35
It returns the decimal representation in the character database set of the first characters of the
Char.
S&*ta6 : ASCII ( Char !
"Fl P "elect Ascii ( LAS ! ( Ascii ( LAPPL'S! <rom -ual K
NUM'ER F5*ct"%*s :
These functions accept numeric input and return numeric values.
6any functions return values that are accurate to 18 decimal digits.
ROUND F5*ct"%* :
S&*ta6 : ROUND ( n( m !
It returns L n L rounded to n places right of the decimal point.
If L mS is omitted( n is rounded to >( places
L m L can be negative( and rounds off the digits to the left of the decimal point.
6 must be an integer.
"Fl P "elect &ound ( /4./91( / ! <rom -ual K
"Fl P "elect &ound ( /4./91( G/ ! <rom -ual K
"Fl P "elect &ound ( 34901( 0 ! ( &ound ( 34.901( > ! ( &ound ( 34(901( G/ ! <rom -ual K

TRUNCATE F5*ct"%* :
S&*ta6 : TRUNC ( n( m !
It returns n truncated to m decimal places.
If L m L is omitted( n is truncated to > places.
L n L can be negative to truncate m digits left to the decimal point.
"Fl P "elect Trunc ( /4.79( /! <rom -ual K
"Fl P "elect Trunc ( /4.79( G/ ! <rom -ual K
"Fl P "elect Trunc ( 34.901( 0 ! ( Trunc ( 30.901 !( Trunc ( 34.901( G/ ! <rom -ual K
CEIL F5*ct"%* :
S&*ta6 : CEIL ( n !
&eturns the smallest integer greater than or eFual to L n L .
The ad#ustment is done to the highest nearest decimal value.
"Fl P "elect Ceil ( /4.7 ! <rom -ual K
"Fl P "elect Ceil ( /3.07 ! ( Ceil ( /3.0 ! ( Ceil ( /3 ! <rom -ual K
FLOOR F5*ct"%* :
Oragle 10 g Material

36
S&*ta6 : FLOOR ( n !
&eturns the largest integer less than or 'Fual than n .
The ad#ustment is done to the lo%est nearest decimal values.
"Fl P "elect <loor ( /4.7 ! <rom -ual K
"Fl P "elect <loor ( /3.07 ! ( <loor ( /3.0 ! ( <loor ( /3 ! <rom -ual K
MODULUS F5*ct"%* :
S&*ta6 : MOD ( m( n !
It returns remainder m divided by n .
It returns m if n is >.
"Fl P "elect 6od ( //( 3 ! ( 6od ( /> ( 0 ! <rom -ual K
POWER F5*ct"%* :
S&*ta6 : POWER ( m( n !
&eturns m raised to the nth po%er.
The base m and the eponent n can be any numbers.
If m is negative( n must be an integer
"Fl P "elect Po%er ( 1( 0 !( Po%er ( G1( 0 ! <rom -ual K
"Fl P "elect Po%er ( 1( G0 ! ( Po%er ( G1( G0 ! <rom -ual K
"Fl P "elect Po%er ( G1.4( G0 !( Po%er ( 1( G0.4 ! <rom -ual K
"Fl P "elect Po%er ( G1.4( 0.4 ! ( Po%er ( 1.4( G0.4 ! <rom -ual K
S,UARE ROOT F5*ct"%* :
S&*ta6 : S,RT ( n !
It returns sFuare root of n.
The value of n cannot be negative
"E&T returns a real result.
"Fl P "elect "Frt ( 04 ! <rom -ual K
A'SOLUTE F5*ct"%* :
S&*ta6 : A'S ( n !
It returns the absolute value of n.
"Fl P "elect Abs ( G/4 ! <rom -ual K
"Fl P "elect "al( Comm( "al J Comm( Abs ( "al J Comm ! <rom 'mp
,here Comm \ /3>> K
Oragle 10 g Material

37
SIGN F5*ct"%* :
S&*ta6 : SIGN ( n !
It returns the "I)D( specification of a number.
If n Q > ( return J/
If n \ > ( return >
If n P >( return /
"Fl P "elect "ign ( G/4 !( "ign ( /4 ! ( "ign ( > ! <rom -ual K
"Fl P "elect "al( Comm( "ign( "al J Comm ! <rom 'mp ,here "ign( "al J Comm ! \ G/
WOR<ING WITH DATES :
Oracle stores dates in an internal numeric format.
The dates in Oracle range from =anuary /( 37/0 *C to -ecember 1/( 9999 A-.
The default display and input format for any date is --G6ODG??.
The numeric format represents
HH Century HH ?ear HH 6onth HH -ay HH Iours
HH 6inutes HH "econds
S!SDATE :
It is a date function that returns current date and time .
"?"-AT' is generally selected upon a -@66? Table.
"Fl P "elect "?"-AT' <rom -ual K
Date Ar"t@$et"c :
As database stores dates as numbers( Arithmetic operations can be implemented.
Dumber constants can be added or subtracted upon dates.
The operations that can be applied are
o Date : Dumber -ate K Adds number of days to a date
o Date G Dumber -ate B "ubtracts Dumber of days from a date.
o Date 7 -ate Dumber of days K "ubtracts one date from another.
o Date P Dumber C 03 -ate K Adds Dumber of Iours to a date.
"Fl P "elect "ysdate( "ysdate : 1 <rom -ual K
"Fl P "elect "ysdate( "ysdte J 1 ( "ysdate :R 70 C 03 <rom -ual K
"Fl P "elect 'name( Iiredate( Iiredate : 1 <rom 'mp K
"Fl P "elect 'name( Iiredate( Iiredate J 1 <rom 'mp K
"Fl P "elect 'name( Iidrdate( "ysdate J Iiredate <rom 'mp K
Oragle 10 g Material

38
"Fl P "elect 'name( ( "ysdate J Iiredate ! C 7 ,ee+s <rom 'mp ,here -eptno \ /> K
DATE F5*ct"%*s :
A33QM%*t@s F5*ct"%* :
S&*ta6 : A--R6ODTI" ( -( n !
It returns the date d plus n months
The argumennt n can be any integer
"Fl P "elect "ysdate ( AddRmonths ( "ysdate( 0 ! <rom -ual K
"Fl P "elect "al(l Iiredate( AddRmonths ( Iiredate( 0 ! <rom 'mp ,here
-eptno \ 0> K
M%*t@sQ'etBee* F5*ct"%* :
S&*ta6 : M%*t@sQ'etBee* ( D1F D8 )
It returns number of months bet%een dates d/ and d0.
If d/ is later than d0( the result is Positive( else Degative.
If d/ is and d0 are either the same days of the months or both last days of the months(
the result is al%ays an integer.
"Fl P "elect 'mpno( Iiredate( 6onthsR*et%een ( "ysdate ( Iiredate ! <rom 'mp
,here 6onthsR*et%een ( "ysdate( Iiredate ! Q 0>>> K
"Fl P "elect 'name( 6onthsR*et%een ( "ysdate ( Iiredate ! <rom 'mp K
Ne6tQDa& F5*ct"%*:
S&*ta6 : Ne6tQ3a&( 3F C@ar )
It returns the date of the first %ee+ day named by char( that is later than the date d.
The CIA& must be a day of the %ee+ in the sessions date language.
The day of the %ee+ can be full name or the abbreviation.
"Fl P "elect "ysdate( DetRday ( "ysdate( L,'-S ! <rom -ual K
"Fl P "elect "al( Iiredate( DetRday ( Iiredate( L6OD-A?S ! <rom -ual K
LastQDa& F5*ct"%* :
S&*ta6 : LastQ3a& ( d !
It returns the date of the last day of the month that contains d.
6ostly used to determine ho% many days are left in the current month .
"Fl P "elect "ysdate( LastRday ( "ysdate ! Last( LastRday ( "ysdate ! J "ysdate
-aysleft <rom -ual K
R%5*3"*g %# Dates :
Oragle 10 g Material

39
S&*ta6 : R%5*3 ( -ate( L<ormatS !
&eturns -ate rounded to the @nit specified by the format.
If format is omitted( -ate is rounded to the nearest day.
"Fl P "elect &ound ( "ysdate ( L-A?S ! <rom -ual K
"Fl P "elect &ound ( "ysdate( L6ODTIS ! <rom -ual K
"Fl P "elect &ound ( "ysdate ( L?'A&S ! <rom -ual K
Tr5*cat"*g Dates :
S&*ta6 : Tr5*c ( -ate( L<ormatS !
&eturn -ate %ith the time portion of the day truncated to the specified unit.
If format is omitted( date is truncated to the nearest day.
"Fl P "elect &ound ( "ysdate( L-A?S ! ( Trunc ( "ysdate( L-A?S ! <rom -ual K
"Fl P "elect &ound ( "ysdate( L6ODTIS !( Trunc ( "ysdate( L6ODTIS ! <rom -ual K
"Fl P "elect &ound ( "ysdate( L?'A&S ! ( Trunc ( "ysdate( L?'A&S ! <rom -ual K
C%*=ers"%* F5*ct"%*s :
The Conversion functions convert a value from one data type to another .
The -ata type conversion in Oracle is of t%o types
HH Implicit -ata type Conversion
HH 'plicit -ata type Conversion
I$p"c"t Data t&pe C%*=ers"%* :
Implicit -ata type Conversions %or+ according to the conversion specified by Oracle.
The assignment succeeds if the Oracle serves can convert the datatype of the value.
CIA& to D@6*'& Conversions succeed only if the character strings represents a valid
D@6*'&.
CIA& to -AT' Conversions succeed only if the character strings represent the default
format --G6ODG??.
IN ASSIGNMENTS IN E1PRESSIONS
- $archar0 C Char Dumber H $archar0C Char Dumber
H $archar0 C Char -ate H $archar0 C Char -ate
H Dumber $archar0
- -ate $archar0
E6p"c"t Data t&pe C%*=ers"%* :
Oragle 10 g Material

40
"EL provides three functions to convert a value from one data type to another.
The <unctions are
H ToRChar <or Conversion to Characters
H ToR-ate <or Conversion to -ates
H ToRDumber <or Conversion to Dumbers
T%QC@ar C%*=ers"%* F5*ct"%* :
This function can be used in t%o different flavors
ToRChar ( Dumber Conversion !
ToRChar ( -ate Concersion !
T%QC@ar ( Dumber Conversion ! B
S&*ta6 : T%Qc@ar ( Dumber ( fmt( LnlsparmsS !
o Converts number of number data type to a value of $archar0 data type.
o <mt is the optional number format( that can be used.
o The LnlsparmsS specifies the characters returned by the number format elemtns.
T%QC@ar ( -ate Conversion ! B
S&*ta6 : T%QC@ar ( -ate( fmt( LnlsparamsS !
Converts -ate of -AT' data type to a value of $archar0 data type in the format
specified.
<mt is the optional date format( that can be used.
The nlsparams specifies the language in %hich month and day names and
abbreviations are returned.
F%r$att"*g M%3es :
A formatting model is a character literal that describes the format of -AT' or Dumber data stored
in a character string.
D >>D>> B
o It returns the specified position of the decimal character.
o The default decimal delimites is a period ( . !
o Only one decimal character can be specified in a number format model.
"Fl P "elect ToRchar ( /013( L99999-99S ! <rom -ual K
"Fl P "elect ToRchar ( /013( L999-99S ! <rom -ual K
EEEE >:>EEEE :
o &eturns a value using scientific notation.
Oragle 10 g Material

41
"Fl P "elect ToRchar ( /013 ( L9.9''''S ! ( ToRchar ( /013( L9.99''''S ! <rom -ual K
G >G>>> :
o &eturns the specified position of the group separator
o 6ultiple group separators can be specified.
"Fl P "elect ToRchar ( /013457( L99)99)999S ! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( L99)999-99S ! <rom 'mp K
L L>>> :
o &eturns the specified position of the local currency symbol.
"Fl P "elect ToRchar ( /013( LL9999S! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( LL999999S ! Currency <rom 'mp ,here -eptno \ /> K
"Fl P "elect "al( ToRchar ( "al( LL99)999-99S( LDL"RC@&&'DC? \ Ind&upeesS !
"alary <rom 'mp ,here -eptno \ 0> K
MI >>>>MI :
o &eturns Degative value %ith a trailing minus sign ( G !.
o &eturn Positive value %ith a trailing blan+.
o 6I format should be declared as trailing argument only.
"Fl P "elect ToRchar ( G/>>>>( LL99)999-996IS ! <rom -ual K
"Fl P "elect "al( Comm( CommRsal( ToRchar ( Comm J "al ( LL999996IS ! <rom 'mp
,here -eptno in(/>( 0>( 1> ! K
PR >>>>PR :
o &eturns Degative value in Q angle *rac+ets P
o It can appear only as trailing declaration.
"Fl P "elect ToRchar ( G/>>> ( LL99)999-99P&S ! <rom -ual K
"Fl P "elect "al( Comm( Comm J "al ( ToRchar ( Comm J "al ( LL99999P&S ! <rom 'mp
RN Returns a value as &oman Dumber in @pper Case
r* Returns a value as &oman Dumber in Lo%er Case
The $alue can be an integer bet%een / and 9999.
"Fl P "elect ToRchar ( />>> ( L&DS ! ( ToRchar ( />>>( LrnS ! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( L&nS ! ( ToRchar ( "al( LrnS ! <rom 'mp K
S >>>>> :
o &eturns negative value %ith a leading minus sign
Oragle 10 g Material

42
o &eturns positive value %ith a leading plus sign
o >>>>S :
o &eturns negative value %ith trailing minus sign.
o &eturns positive value %ith a trailing plus sign.
" can appear as first or last $alue.
"Fl P "elect ToRchar ( />>>( L"9999S !( ToRchar ( G/>>>( L"9999S ! <rom -ual K
"Fl P "elect toRchar ( />>>( L9999"S ! ( ToRchar ( G/>>>( L9999"S ! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( L"99999S ! ( ToRchar ( "al( L99999"S ! <rom 'mp K
"Fl P "elect "al( Comm( ToRchar ( Comm G "al( L"99999S ! ( ToRchar ( Comm J "al (
L99999"S! <rom 'mp K
1 1111 :
6666 :
o &eturns the headecimal value of the specified number of digits.
o If the number is not an integer( Orcle rounds it to an integer.
o Accepts only positive values or >.
"Fl P "elect ToRchar(/>>>( LAAAAS! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( LAAAAAS ! <rom 'mp K
F ( C%$$a ) >F>>> :
o &eturns a comma in the specified position.
o 6ultiple commas can be specified.
"Fl P "elect ToRchar ( />>>>( S99(999-99S ! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( S99(999-99S ! <rom 'mp K
: ( Per"%3 ) >>:>> :
o &eturns a decimal point( at the specified position.
o Only one period can be specified in a number format model.
"Fl P "elect ToRchar ( />>>>( LL99(999.99S ! from -ual K
"Fl P "elect ToRchar ( "al( LL99(999.99S ! <rom 'mp K
R R>>>> :
o &eturns value %ith a leading -ollar "ign.
"Fl P "elect ToRchar ( />>>>( LN99(999.99S ! <rom -ual K
Oragle 10 g Material

43
"Fl P "elect "al( ToRchar ( "al( LN99(999.99S ! <rom 'mp K
0 ( Oer% ) 0>>> :
o &eturns leading Meros.
>>>0 :
&eturns Trailing Meros.
"Fl P "elect ToRchar ( />>>( L>999999S ! ( ToRchar ( />>>( L>999999>S ! <rom -ual K
"Fl P "elect "al( ToRchar( "al( LN>99(999.99S ! <rom 'mp K
> >>>> :
o &eturn value %ith a specified number of digits %ith a leading space %hen positive or
leading minus %hen negative.
"Fl P "elect ToRchar ( />>> J 5>> ( L99999S ! ( ToRchar( 5>> J />>> ( L99999S !
<rom -ual K
"Fl P "elect ToRchar ( 0>.04 J 0>( L99999S ! <rom -ual K
C C>>>> :
o &eturns specified position of the I"O Currency "ymbol.
"Fl P "elect ToRchar ( />>>( LC9999.99S ! <rom -ual K
"Fl P "elect "al( ToRchar ( "al( LC9999.99S ! <rom 'mp K
Date F%r$at M%3es :
The -ate format models can be used in the ToRchar function to translate a -AT' value from
original format to user <ormat .
The total length of a date format model cannot eceed 00 characters.
Date F%r$at Ee$e*ts :
A date format model is composed of one or more date format elements.
o <or input format models( format items cannot appear t%ice( and format items that
represent similar information cannot be combined.
Capitali2ation in a spelled out %ord( abbreviation( or &oman numeral follo%s capitali2ation in the
corresponding format element.
Punctuation such as hyphens( slashes( commas( periods and colons.
AD %r A:D: + 'C %r ':C: :
Oragle 10 g Material

44
o Indicates A- C *C %ith or ,ithout periods.
"Fl P "elect ToRchar ( "ysdate( LA-S! <rom -ual K
"Fl P "elect ToRchar ( "ysdate( L*.C.S! ( ToRchar ( "ysdate( LA.-.S ! <rom -ual K
"Fl P "elect "al( Iiredate( ToRchar ( Iiredate( LA.-.S ! <rom 'mp K
AM %r A:M: + PM %r P:M: :
o It indicates 6eridian Indicator %ith or %ithout periods.
"Fl P "elect ToRchar ( "ysdate( LA.6.S ! ( ToRchar ( "ysdate( LP6S! <rom -ual K
"Fl P "elect "al( Iiredate( ToRchar ( Iiredate( LA6S ! <rom 'mp K
CC + SCC :
o Indicates the century ( " prefies *C date %ith J
"Fl P "elect ToRchar ( "ysdate( L"CCGA-S ! <rom -ual K
"Fl P "elect "al( Iiredate( ToRchar ( Iiredate ( L"CCGA-S! <rom 'mp K
D -ay of the ,ee+ ( / J 7 !
"Fl P "elect ToRchar ( "ysdate( L-S ! <rom -ual K
"Fl P "elect Iiredate( ToRchar ( Iiredate( L-S ! <rom 'mp K
Da& Indicates spelled name of the ,ee+ -ay.
o Pads to a length of 9 Characters.
"Fl P "elect ToRchar ( "ysdate( L-A?S ! <rom -ual K
"Fl P "elect "al( Iiredate( ToRchar ( Iiredate( L-A?S! <rom 'mp ,here
ToRchar ( Iiredate( L-A?S! \ L,'-D'"-A?S K
DD It Indicates The -ay of the 6onth ( / J 1/ !
"Fl P "elect ToRchar("ysdate(S--G-A?S! from -ual K
"Fl P "elect Iiredate( ToRchar ( Iiredate(S--G-A?S ! <rom 'mp K
"Fl P "elect Iiredate( ToRchar ( Iiredate( S--G-A?S ! <rom 'mp ,here
ToRchar ( Iire-ate. S--G-A?S! \ S>1G,'-D'"-A?S K
H-A? "pecifies in @pper Casing.
H-A? "pecifies in Initcap Casing.
H-A? "pecifies in Lo%er Casing.
DDD It indicates the -ay of the ?ear ( / J 155 !
Oragle 10 g Material

45
"Fl E "elect "ysdate ( ToRchar("ysdate( S---S ! <rom -ual K
"Fl P "elect 'name( Iiredate ( ToRchar ( Iiredate( S---S ! <rom 'mp
,here ToRchar ( Iiredate( S-A?S ! \ S,'-D'"-A?S K
D! It indicates the abbreviated name of the day.
"Fl P "elect "ysdate ( ToRchar ("ysdate( S-G-?G-A?S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( S-G-?G-A?S ! from 'mp
,here -eptno in( /> ( 0> ! K
IW "pecifies the %ee+ of the year ( / G 40 or / J 41 !based on the I"O "tandard.
"Fl P "elect "ysdate( Iiredate( ToRchar ( "ysdate( LI,S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( LI ,S ! <rom -ual K
I!!! "pecifies 3 -igit year based on the I"O "tandard .
I!!
I!
"Fl P "elect "ysdate( ToRchar ( "ysdate( LI???S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( LI???S ! <rom 'mp ,here
ToRchar ( Iiredate( L-A?S ! \ L,'-D'"-A?S ! K
!!!! + S!!!! &eturns <our -igit year( L"S Prefies *C dates %ith L J L .
!!! + !! + !
! F !!! &eturns year %ith comma in this position.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L????S ! <our( ToRchar ( "ysdate( L???S !
Three ( ToRchar ( "ysdate( L?(???S ! Comma <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( L????S ! <rom 'mp
,here -eptno \ 0> K
!EAR + S!EAR &eturns the spelled out year.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L?'A&S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( L?'A&S ! <rom 'mp K
W "pecified the %ee+ of the 6onth ( / J 4 !. ,ee+ starts on the first day of the
6onth and ends on the seventh.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L,S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( L,S! <rom 'mp K
WW "pecifies the %ee+ of the ?ear ( / J 41 ! . ,ee+ / "tarts on the first day of the year and
continues to the seventh day of the year.
Oragle 10 g Material

46
"Fl P "elect "ysdate( ToRchar ( "ysdate( L,,S ! <rom -ual K
"Fl P "elect 'name( Iiredate( toRchar ( Iiredate( L,,S ! <rom 'mp K
, &eturns the Euarter of the ?ear.
"Fl P "elect "ysdate( ToRchar ( "ysdate( LES ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( LES ! <rom 'mp ,here
ToRchar ( Iiredate( LES ! \ 3 K
. &eturns the =@LIAD -A?.
i. It is the Dumber of days since =anuary /( 37/0 *C.
ii. Dumbers specified %ith L=S must be integers.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L=S ! <rom -ual K
"Fl P "elect 'name( ToRchar ( Iiredate( L=G---G--G--S ! <rom 'mp K
MM returns the digit numeric abbreviation of the 6onth.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L66G????S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( L66G????S ! <rom 'mp ,here
ToRchar ( Iiredate ( L66S ! \ /0 K
MON &eturns the Abbreviated Dame of the 6onth.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L66G6ODS ! <rom -ual K
MONTH "pells the Dame of the 6onth( padded to a length of 9 characters .
"Fl P "elect "ysdate( ToRchar ( "ysdate( L6ODG6ODTIS ! <rom -ual K
"Fl P "elect 'nbame( Iiredate( ToRchar ( Iiredate( L6ODTI( ????S ! <rom 'mp K
HH + HH18 &eturns the Iour of the day in t%elve hour cloc+ mode.
"Fl P "elect "ysdate( ToRchar ("ysdate( LIIS !( ToRchar ("ysdate( LII/0( A6S !
<rom -ual K
"Fl P "elect 'name( Iiredate ( ToRchar ( Iiredate( LII/0 B A6S ! <rom 'mp K
HH8A &eturns the Iour of the day in t%enty four hour cloc+ mode . ( > J 01 !
"Fl P "elect "ysdate( ToRchar ( "ysdate( LII03S ! <rom -ual K
MI &eturns the 6inutes from the given date ( > J 41 ! .
"Fl P "elect "ysdate( ToRchar ( "ysdaste( L6IS !( ToRchar ( "ysdate( LIIB6IS ! <rom -ualK
"Fl P "elect 'name( "al( ToRchar ( Iiredate( LIIB6IS ! <rom 'mp ,here =ob\LCL'&;S K
Oragle 10 g Material

47
RM &eturns the &oman Dumeral 6onth ( I J AII ! .
"Fl P "elect "ysdate( ToRchar ( "ysdate( Y&6Z ! ( ToRchar ( "ysdate(
L--G&6G??S ! <rom -ual K
"Fl P "elect 'name( "al(. ToRchar ( Iiredate( L--G&6G??S ! <rom 'mp K
SS &eturns "econds from the given date ( > J 49 !
"Fl P "elect "ysdate( ToRchar ( "ysdate( L""S !( ToRchar ( "ysdate( LIIB6IB""S !
<rom -ual K
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--G6ODTIG????( IIB6IB"" A.6.S !
<rom -ual K
"Fl P "elect 'name( "al( Iiredate( ToRchar ( Iiredate( LII03B6IB""S ! <rom 'mp
,here -eptno In( />( 1> ! K
SSSS -isplay "econds past 6idnight ( > J 85199 ! .
"Fl P "elect "ysdate( ToRchar ( "ysdate( L""""S ! <rom -ual K
"Fl P "elect 'name( Iiredate( ToRchar ( Iiredate( L--G66G??( IIB6IB""( """"S !
<rom 'mp K
The Punctuation mar+s that can be used in -ate formats are
D H + H S H : H H H : H Nte6tT
RR Date F%r$at Ee$e*t :
The && date <ormat element is similar to the ?? date format element.
The && format element provides additional fleibility for storing date values in other
Centuries.
The && date format element allo%s to store the date to the previous as %ell as the net
Centuries .
If the Last 0 -igits of The Current ?ear are B
0 D A> U0 D >>
0 D A> Returns date is in the The return date is in the
Current Century. Preceding Century .
U0 D >> The return date is in The return date is in the
t@e net Century Current Century .
Date F%r$at Ee$e*t S5##"6es :
o TH "pecifies the Ordinal Dumber
E6 : DDTI 80
TH
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--TI( 6ODTI( ????S ! <rom -ual K
Oragle 10 g Material

48
"Fl P "elect 'name( "al( Iiredate( ToRchar ( Iiredate( L--TI( 6ODTI( ????S !
<rom 'mp K
o SP "pells Dumbers.
E6 B L--"P T,'DT?
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--"P ( 6ODTI( ????S ! <rom -ual K
"Fl P "elect 'name( "al( ToRchar ( Iiredate( L--"P( 6ODTI( ????S ! <rom 'mp K
o "pelled( Ordinal Dumber
' B --"PTI T,'DTI'TI
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--"PTI( 6ODTI( ????S ! <rom -ual K
"Fl P "elect 'name( "al( ToRchar ( Iiredate( L--"PTI( 6ODTI( ????S ! <rom 'mp K
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--"PTI 6ODTI ????"PS ! <rom -ual K
Date F%r$at Ee$e*ts Restr"ct"%*s :
o The suffies %hen added to date return value al%ays in 'nglish.
o -ate suffies are valid only on output( hence cannot be used to insert a date into the
database.
F%r$at M%3e M%3"#"ers :
o FM Y <ill 6ode Y( It suppresses blan+ padding in the return value of the
ToRchar <unction.
o F1 Y <ormat 'act Y ( It specifies eact matching for the character argument
and date format model.
"Fl P "elect "ysdate( ToRchar ( "ysdate( L--"PTI 6ODTI ????"PS ! (
ToRchar ( "ysdate( L<6--"PTI 6ODTI ????"PS ! <rom -ual K
TOQNUM'ER F5*ct"%* :
S&*ta6 : T%QN5$ber ( C@arF #$tF N*spara$T )
o It Converts a char( value of CIA& or $A&CIA&0 data type containing a Dumber in the
format specified by the optional format model fmt( to a value of D@6*'& data type .
"Fl P "elect ToRnumber ( LN/>(>>>.>>S( LL99(999.99S ! <rom -ual K
"Fl P "elect ToRnumber ( LN/(>>>.>>S( LL9(999.99S ! : 4>> <rom -ual K
Oragle 10 g Material

49
TOQDATE F5*ct"%* :
S&*ta6 : T%QDate ( C@arF #$tF N*spara$T )
o Converts char of CIA& or $A&CIA&0 datatype to a $alue of -AT' data type .
o The fmt is a date format specifying the format of char.
"Fl P "elect 'name( Iiredate( A--R6ODTI" ( ToRchar ( S/7G-'CG/98>S(
L--G6ODG??S ! ( 1 ! <rom 'mp ,here Iiredate \ S/7G-'CG/98>S K
Spec"#"cat"%*a E6a$pes :
"Fl P "elect ToRchar ( A--R6ODTI" ( Iiredate( / !( L--G6ODG????S !
YD'AT 6ODTIZ <rom 'mp ,here 'name \ L"6ITIS K
"Fl P "elect Concat ( Concat ( 'name( Lis aS!( =ob ! -esignation <rom 'mp
,here 'mpno \ 79>> K
"Fl P "elect Trunc( ToR-ate ( S07GOCTG90S ( L--G6ODG??S !( L?'A&S ! YD', ?'A&Z
<rom -ual K
"Fl P "elect ToRchar ( A--R6ODTI"( LA"TR-A? ( Iiredate ! ( 4 !( L--G6ODG????S !
Y<I$' 6ODTI"Z <rom 'mp ,here 'name \ L6A&TIDS K
"Fl P "elect 6onthsR*et%een ( ToR-ate ( S>0G>0G/994S ( L66G--G????S !(
ToR-ate ( S>/G>/G/994S( L66G--G????S ! ! 6ODTI" <rom -ual K
"Fl P "elect DetR-ay ( S/4G6A&G98S( LT@'"-A?S ! Y Det -ay Y <rom -ual K
"Fl P "elect 'name( D$L ( ToRChar ( Comm! ( L Dot Applicable L ! Y Commission Z
<rom 'mp ,here -eptno \ 1> K
"Fl P "elect &ound ( ToR-ate ( S07GOCTG90S ( L?'A&S ! Y De% ?ear Y <rom -ual K
"Fl P "elect ToRChar ( Iiredate ( L6ODTI -- ????S ! <rom 'mp ,here
'name \ L*LA;'S K
"Fl P "elect 'name( ToRchar ( Iiredate( Lfm 6onth --( ????S ! ( Iiredate <rom
'mp ,here -eptno \ 0> K
"Fl P "elect ToRchar ( ToR-ate ( S07GOCTG98S( L--G6ODG&&S !( L????S ! ?ear
<rom -ual K
"Fl P "elect ToRchar ( ToR-ate ( S07GOCTG/7S( L--G6ODG&&S !( L????S ! ?ear
<rom -ual K
o Ass5$pt"%* : Eueries Issued bet%een /94> J /999.
"Fl P "elect ToRchar ( ToR-ate ( S07GOCTG98S( L--G6ODG&&S !( L????S ! ?ear
<rom -ual K
"Fl P "elect ToRchar ( ToR-ate ( S07GOCTG/7S( L--G6ODG&&S ! ( L????S ! ?ear
<rom -ual K
o Ass5$pt"%* : Eueries Issued bet%een 0>>> J 0>39
Oragle 10 g Material

50
"Fl P "elect ToRchar ( "ysdate( Lfm --TIS ! [[ L of L [[ ToRchar ( "ysdate( L6onthS !
L ( L [[ ToRchar ( "ysdate( L????S ! Idea <rom -ual K
"Fl P "elect ToRchar ( "ysdate( Lfm-ayS ! [[ L Ys "pecial L ! Y 6'D@ Y <rom -ual K
"Fl P "elect 'name( =ob( D$L ( ToRchar ( 6gr !( L "upreme Authority L ! Y 6anagers Y
<rom 'mp Order *y "al -esc K
Aggregate %r Gr%5p F5*ct"%*s :
o These functions return a single ro% based on groups of ro%s.
o These functions can appear in "'L'CT lists and IA$ID) clauses.
o These functions operate on sets of ro%s to give one result per group.
o The sets may be the %hole table or the table split into groups.
G5"3e"*es t% 5se Gr%5p F5*ct"%*s :
o -I"TIDCT ma+es the function consider only non duplicate values.
o ALL ma+es the function to consider every value including duplicates.
S&*ta6 : Gr%5pQF5*ct"%*QNa$e ( -istinct C ALL C n !
o The data types for arguments may CIA&( $A&CIA&( D@6*'& O& -AT'.
o All group functions ecept CO@DT( H ! ignore D@LL values. To substitute a value for
D@LL values( use the D$L function.
o ,hen a group function is declared in a "'L'CT list ( no single ro% columns should be
declared.
o ,hen a group function is declared in a "'L'CT list( other columns can be declared( but
they should be grouped columns( and all the non functional columns should be declared
into a )&O@P *? clause.
A=erage F5*ct"%* :
S&*ta6 : A/G ( -I"TIDCT C ALL C n !
o It returns the Average $alue of n.
o It ignores D@LL values.
"Fl P "elect A$) ( "al ! ( A$) ( -I"TIDCT "al ! <rom 'mp K
"Fl P "elect A$) ( Comm ! ( A$) ( -I"TIDCT A$) ! <rom 'mp K
SUM F5*ct"%* :
S&*ta6 : SUM ( -I"TIDCT C ALL C n !
o It returns the "um of the values of n.
o It ignores D@LL values.
o
"Fl P "elect "um ( "al !( "um ( -I"TIDCT "al ! <rom 'mp K
"Fl P "elect "um ( Comm ! ( "um ( -I"TIDCT Comm ! <rom 'mp K
Oragle 10 g Material

51
MA1"$5$ F5*ct"%* :
S&*ta6 : MA1 ( -I"TIDCT C ALL C n !
o It ignores the 6aimum value of n.
o It ignores D@LL values.
"Fl P "elect 6a ( "al !( 6a ( -I"TIDCT "al ! <rom 'mp K
"Fl P "elect 6a ( Comm ! ( 6a ( -I"TIDCT Comm ! <rom 'mp K
MIN"$5$ F5*ct"%* :
S&*ta6 : MIN ( -I"TIDCT C ALL C n !
o It returns the 6inimum value of the n.
o It ignores D@LL values.
"Fl P "elect 6in ( "al ! ( 6in ( -I"TIDCT "al ! <rom 'mp K
"Fl P "elect 6in ( Comm !( 6in ( -I"TIDCT Comm ! <rom 'mp K
Sta*3ar3 De="at"%* F5*ct"%* :

S&*ta6 : STDDE/ ( -I"TIDCT C ALL C n !
o It returns the "tandard -eviation of the n.
o It ignores D@LL $alues.
"Fl P "elect "tddev ( "al ! ( "tddev ( -I"TIDCT "al ! <rom 'mp K
"Fl P "elect "tddev ( Comm !( "tddev ( -I"TIDCT Comm ! <rom 'mp K
/ar"a*ce F5*ct"%* :
S&*ta6 : /ARIANCE ( -I"TIDCT C ALL C n !
o It returns the $ariance of D.
o It ignores the D@LL values.
"Fl P "elect $ariance ( "al ! ( $ariance ( -istinct "al ! <rom 'mp K
"Fl P "elect $ariance ( Comm !( $ariance ( -I"TIDCT Comm ! <rom 'mp K
COUNT F5*ct"%* :
Oragle 10 g Material

52
S&*ta6 : COUNT ( H C -I"TIDCT C ALL C n !
o It returns the number of ro%s in the Fuery.
o D evaluates to something other than D@LL.
o It H is used returns all ro%s( including duplicated and D@LLs.
o It can be used to specify the count of all ro%s or only distinct values of n.
"Fl P "elect Count ( H ! <rom 'mp K
"Fl P "elect Count ( =ob ! ( Count ( -I"TIDCT =ob ! <rom 'mp K
"Fl P "elect Count ( "al ! ( Count ( Comm ! <rom 'mp K
"Fl P "elect Count ( 'mpno ! ( Count ( -I"TIDCT 6gr ! <rom 'mp K
Creat"*g Gr%5ps %# Data :
o The )roup *y Clause is used to decide the ro%s in a table into groups.
S&*ta6 : Seect C%5$*F GrpQF5* ( C%5$* )
Fr%$ Tabe
W@ere C%*3"t"%*
GROUP '! GrpQ'&QE6pr
Or3er '& C%5$* H
G5"3e"*es t% Use Gr%5p '& Ca5se :
If the )roup function is included in a "'L'CT clause( %e should not use individual results
columns.
The 'tra not group functional columns should be declared in the )&O@P *? Clause.
@"ID) ,I'&' Clause( ro%s can be pre ecluded before dividing them into groups.
Column Aliases cannot be used in )roup *y Clause.
*y default( ro%s are sorted by ascending order of the columns included in the )roup *y
List.
The column applied upon )&O@P *? Clause need not be part of "'L'CT list.
"Fl P "elect -eptno( Avg( "al ! <rom 'mp )roup *y -eptno K
"Fl P "elect -eptno( Avg ( "al ! <rom 'mp )roup *y -eptno Order by Avg ( "al ! K
"Fl P "elect -eptno( 6in ( "al ! ( 6a ( "al ! <rom 'mp )roup *y -eptno K
"Fl P "elect -eptno( =ob( "um ( "al ! <rom 'mp )roup *y -eptno( =ob K
The Above specification falls under the principle of )roups %ithin )roups.
"Fl P "elect -eptno( 6in ( "al ! ( 6a ( "al ! <rom 'mp ,here =ob \ LCL'&;S
)roup *y -eptno K
"Fl P "elect -eptno( "um ( "al !( Avg ( "al ! <rom 'mp ,here =ob \ LCL'&;S
Oragle 10 g Material

53
)roup *y -eptno K
E6c53"*g Gr%5ps %# Res5ts :
o Ha="*g Ca5se :
It is used to specify %hich groups are to be displayed.
S&*ta6 : SELECT C%5$*F Gr%5pQF5*ct"%*
Fr%$ Tabe
J WHERE C%*3"t"%* K
J GROUP '! Gr%5pQ'&QE6pr K
J HA/ING Gr%5pQC%*3"t"%* K
J ORDER '! C%5$*QNa$e + A"as K H
Steps Per#%r$s '& Ha="*g Ca5se :
&o%s are )rouped
The )roup <unction is applied to the group.
The )roups that match the criteria in the IA$ID) Clause are displayed.
o The IA$ID) Clause can precede )roup *y Clause( *ut it is more logical to declare it
before )roup *y.
o )roup *y Clause can be used( %ithout a )roup <unction in the "'L'CT list.
o If ro%s are restricted based on the result of a group function( %e must have a )roup *y
Clause as %ell as the Iaving Clause.
"Fl P "elect -eptno( Avg ( "al !R<rom 'mp )roup *y -eptno Iaving
6a ( "al ! P 09>> K
"Fl P "elect =ob( "um ( "al ! Payroll <rom 'mp ,here =ob DOT LI;' L"AL'"_S
)roup *y =ob Iaving "um ( "al ! P 4>>> Order *y "um ( sal ! K
"Fl P "elect -eptno( 6in ( "al !( 6a ( "al ! <rom 'mp ,here =ob \ LCL'&;S
)roup *y -eptno Iaving 6in ( "al ! Q />>> K
"Fl P "elect -eptno( "um ( "al ! <rom 'mp )roup *y -eptno Iaving
Count ( -eptno ! P 1 K
"Fl P "elect -eptno( Avg ( "al ! ( "um ( "al !( 6a ( "al !( 6in ( "al ! <rom 'mp
)roup *y -eptno Iaving Count ( H ! P 1 K
"Fl P "elect -eptno( Avg ( "al ! "um ( "al ! <rom 'mp )roup *y -eptno
Iaving Avg ( "al ! P 04>> K
"Fl P "elect -eptno( =ob( "um ( "al ! ( Avg ( "al ! <rom 'mp )roup *y -eptno( =ob
Iaving Avg ( "al ! P 04>> K
Nest"*g %# Gr%5p F5*ct"%*s :
o )roup functions can be nested to a depth of t%o.
"Fl P "elect 6a ( Avg ( "al ! ! <rom 'mp )roup *y -eptno K
"Fl P "elect 6a ( "um ( "al ! ! ( 6in ( "um ( "al ! ! <rom 'mp )roup *y -eptno K
Oragle 10 g Material

54
"Fl P "elect 6a ( "um ( "al ! ! ( 6in ( Avg ( "al ! ! <rom 'mp )roup *y =ob K
M"scea*e%5s F5*ct"%*s :
o GREATEST F5*ct"%* :
S&*ta6 : GREATEST ( 'pr/( 'pr0( ...!
&eturns the greatest of the list of 'pr.
All 'prs after the first are implicitly converted to the data type of the first 'pr(
before the comparison.
Oracle compares the 'prs using Don padded comparison semantics.
Character comparison is based on the value of the character in the data base
character list.
"Fl P "elect )reatest ( LIA&&?S ( LIA&&IOTS ! <rom -ual K
"Fl P "elect )reatest ( />>>( 0>>>( 0>> ! <rom -ual K
"Fl P "elect )reatest ( S/>G=@LG>4S( S0>G=@LG>4S ! <rom -ual K
o LEAST F5*ct"%* :

S&*ta6 : LEAST ( 'pr/( 'pr0( ...!
It returns the Least of the List of 'prs.
All 'prs after the first are implicitly converted to the data type of the first epr
before the comparison .
"Fl P "elect Least ( LIA&&?S ( LIA&&IOTS ! <rom -ual K
"Fl P "elect Least ( />>>( 0>>>( 0>> ! <rom -ual K
"Fl P "elect Least ( S/>G=@LG>4S( S0>G=@LG>4S ! <rom -ual K
o USER F5*ct"%* :
S&*ta6 : USER
It returns the Current Oracle users %ithin the $A&CIA&0 data type.
The function cannot be used in the condition of the CI'C; Constraint .

"Fl P "elect @ser <rom -ual K


o UID F5*ct"%* :
S&*ta6 : UID
It returns an integer that uniFuely identifies the Current @ser.
"Fl P "elect @id <rom -ual K
"Fl P "elect @ser( @id <rom -ual K
Oragle 10 g Material

55
o USEREN/ F5*ct"%* :
S&*ta6 : USEREN/ ( Opt"%* )
&eturns information of $A&CIA&0 data type above the current session .
o The $alues in Option are
NISD'AT &eturns LT&@'S if -*A &ole is 'nabled.
LLANGUAGET &eturns the Language and territory used in Current "ession .
LTERMINALT &eturns the O" identifier for the Current "essionSs Terminal .
LSESSIONIDT &eturns the Auditing "ession identifier.
NENTR!IDT &eturns the available auditing entry identifier.
LLANGT &eturns the I"- abbreviation for the language name.
LINSTANCET &eturns the instance identification number of the
Current Instance.
LCLIENTQINFOT &eturns upto 53 *ytes of @ser "ession Information .
"Fl P "elect @ser'nv ( LLAD)@A)'S ! <rom -ual K
o /ISIOE F5*ct"%* :
S&*ta6 : /SIOE ( E6pr )
It returns the Dumber of bytes in the internal representation of 'pr.
If 'pr is D@LL( <unction returns D@LL .
"Fl P "elect 'name( $si2e ( 'name ! <rom 'mp K
o SOUNDE1 F5*ct"%* :
S&*ta6 : SOUNDE1 (CHAR )
It returns a character string containing the phonetic representation of char.
It allo%s comparison of %ords that are spelled differently( but sound ali+e in enlish.
"Fl P "elect 'name <rom 'mp ,here "oundes ( 'name ! \ "ounde ( L"6ITIS! K
"Fl P "elect 'name( =ob <rom 'mp ,here "oundes ( =ob ! \ "ounde ( LCL'&;S ! K
DATA INTEGRIT! IN DATA 'ASES :
Data I*tegr"t& :
Oragle 10 g Material

56
It is a state in %hich all the date values stored in the data base are collect.
'nforcing -ata Integrity ensures the Fuality of the data in the data base.
CATEGORIES OF DATA INTIGRIT! :
E*t"t& I*tegr"t& :
It defines a ro% as a @DIE@' entity for a particular table
'ntity integrity enforces the integrity of the identifies columns( or the P&I6A&? ;'? of
a table.
D%$a"* I*tegr"t& :
-omain Integrity validates the entries for a given column.
It can enforced through.
&estricting type ( -ata Types !
*y <ormat ( CI'C; Constraint !
*y &ange of Possible $alues ( <O&'I)D ;'?( CI'C;( -'<A@LT( DOT D@LL !
Re#ere*t"a I*tegr"t& :
It preserves the defined relationship bet%een tables %hen records are entered or deleted.
It ensures that +ey values are consistent across tables.
,hen referential integrity is enforced( it prevents from ....
H Adding records to a related table if there is no associated record in the
Primary Table.
H Changing values in a Primary Table that result in Orphaned records in a
related table.
H -eleting records from a Primary Table if there are matching related records.
UserQDe#"*e3 I*tegr"t& :
It allo%s to define specific business rules that do not fall into any one of the other integrity
categories.
CONSTRAINTS :
Constraints are used to define an Integrity Constraint( as rule that restricts the values in a
data base.
As per Oracle these are "i Types of Constraints B
1) A NOT NULL C%*stra"*t
8) A UNI,UE C%*stra"*t
9) A PRIMAR! <E! C%*stra"*t
A) A FOREIGN <E! C%*stra"*t
Oragle 10 g Material

57
U) A CHEC< C%*stra"*t
V) A REF C%*stra"*t
In Oracle Constraints can be declared in t%o different styles.
/! C%5$* Le=e : They are declared as part of the definition of an individual
column or attribute. The style is called as IDLID' specification.
0! Tabe Le=e : T@ey are declared as part of the table definition. The style is
called as O@TRO<RLID' specification.
N%te : NOT NULL Constraint should be declared as IDLID' only.
The Constraint Clause can appear in ....
o C&'AT' Table
o ALT'& Table
o C&'AT' $ie%
o ALT'& $ie%
Oracle does not support Constraints on Columns or attributes %hose type is ..
o @"'&R-'<ID'- O*='CT
o D'"T'- TA*L'"
o $A&&A?
o &'<
o LO*
E6cept"%*s :
o NOT NULL Constraint are supported for a column or attributes %hose type is
@"'&R-'<ID'- ob#ect $A&&A?( &'<( LO* .
o NOT NULLF FOREIGN <E!F a*3 REF Constraints are supported on a column of
type &'<.
NOT NULL C%*stra"*ts :
A DOT D@LL Constraint prohibits a column from containing D@LLs .
DOT D@LL should be defined using only IDLID' specification or only at COL@6D Level
The default if not specified is D@LL .
To satisfy a DOT D@LL constraint( every ro% in the table must contain a value for the
column.
Restr"ct"%*s :
Oragle 10 g Material

58
D@LL or DOT D@LL Cannot be specified in a vie% constraint.
NULL %r DOT D@LL cannot be specified for an attribute of an ob#ect.

E6a$pe :
"Fl P Create Table "tudents
(
"tudDo Dumber ( 5 ! Constraint "tudnoRDD DOT D@LL(
"tudDame $archar0 ( 04 ! Constraint "tudDameRDD DOT D@LL(
CourseDame $archar0( 04 ! Constraint CourseDameRDD DOT D@LL(
=oin-ate -ate Constraint =dateRDD DOT D@LL
! K
UNI,UE C%*stra"*ts :
The @DIE@' Constraint designates a column as a @DIE@' ;ey.
A composite @DIE@' +ey designates a combination of columns as the @DIE@' ;ey.
A composite @DIE@' +ey is al%ays declared at the table level or out of line.
To satisfy a @DIE@' constraint( no t%o ro%s in the table can have the same value for the
@DIE@' +ey.
@DIE@' +ey made up of a single column can contain D@LLs.
Oracle creates an Inde implicitly on the @DIE@' +ey column.
Restr"ct"%*s :
A Table or $ie% can have only one @DIE@' ;ey.
@DIE@' +ey cannot be implemented on columns having ..
o LO* H LOD) H LOD) &A, H $A&&A? H D'"T'- TA*L'
o O*='CT H *<IL' H &'< H TI6'"TA6P ,ITI TI6' MOD' .
A composite @DIE@' +ey cannot have more than 10 columns
"ame column or combination of columns cannot be designated as both P&I6A&? ;'?
and @DIE@' ;'?.
,e cannot specify a @DIE@' +ey %hen creating a subtable or subvie% in an Inheritance
hierarchy.
The @DIE@' +ey can be specified only for the top level ( &oot ! table or $ie%.
E6a$pe :
S,L E Create table Promotions/
(
PromoRI- Dumber ( 5 ! Constraint PromoRI-R@DE @DIE@' (
PromoDame $archar0( 0> ! (
PromoCategory $archar0 ( /4 ! (
PromoCost Dumber ( /> ( 0 ! (
Oragle 10 g Material

59
Promo*eg-ate -ate (
Promo'nd-ate -ate
! K
S,L E Create table Promotions0
(
PromoRI- Dumber ( 5 !
PromoDame $archar0( 0> ! (
PromoCategory $archar0 ( /4 ! (
PromoCost Dumber ( /> ( 0 ! (
Promo*eg-ate -ate (
Promo'nd-ate -ate (
Constraint PromoRI-R@DE @DIE@' ( PromoI-!
! K
S,L E Create table ,areIouse
(
,areIouseI- Dumber ( 5 ! (
,areIouseDame $archar0( 1> ! (
Area Dumber ( 3 ! (
-oc+Type $archar0( />> ! (
,aterAccess $archar0( /> ! (
&ailAccess $archar0( /> ! (
Par+ing $archar0( /> ! (
$clearance Dumber ( 3 ! (
Constraint ,areIouseR@DE @DIE@' ( ,areIouseI-(
,aareIouseDame !
! K
PRIMAR! <E! C%*stra"*ts :
A Primary ;ey constraint designates a column as the Primary ;ey of a table or $ie%.
A CO6PO"IT' P&I6A&? ;'? designates a combination of columns as the P&I6A&?
;'?.
,hen the COD"T&AIDT is declared at column level or inline only P&I6A&? ;'?
;ey%ord is enough.
A Composite Primary ;ey is al%ays defined at Table Level or Out of Line only.
A Primary ;ey Constraint combines a DOT D@LL and @DIE@' Constraint in one
declaration.

Restr"ct"%*s :
A Table or $ie% can have only one P&I6A&? ;'? .
P&I6A&? ;'? Can not be implemented on Columns having...
o LO* H LOD) H LOD) &A, H $A&&A? H D'"T'- TA*L'
o O*='CT H *<IL' H &'< H TI6'"TA6P ,ITI TI6' MOD' .
The si2e of a P&I6A&? ;'? cannot eceed appro. one -atabase *loc+ .
A composite P&I6A&? ;'? cannot have more than 10 columns.
Oragle 10 g Material

60
The same column or combination of columns cannot be designated both as P&I6A&?
;'? and @DIE@' ;'?.
P&I6A&? ;'? cannot be specified %hen creating a "ubTable or "ub$ie% in an
Inheritance Iierarchy.
The P&I6A&? ;'? can be specified only for the top level( &OOT !Table or $ie%

E6a$pe :
SI E Create Table Locations
(
LocationI- Dumber ( 3 ! Constraint LocRI-RP; P&I6A&? ;'?(
"tAddress $archar0( 3> ! DOT D@LL (
PostalCode $archar0( 5 ! Constraint PCRDD DOT D@LL (
City $archar0 ( 1> ! Constraint CityRDD DOT D@LL
! K
SI E Create Table Locations/
(
LocationI- Dumber ( 3 ! (
"tAddress $archar0( 3> ! DOT D@LL (
PostalCode $archar0( 5 ! Constraint PCRDD DOT D@LL (
City $archar0 ( 1> ! Constraint CityRDD DOT D@LL (
Constraint LocRI-RP; P&I6A&? ;'? ( LocationI- !
! K
SI E Create Table "alesInfo
(
ProdI- Dumber ( 5 ! (
CustI- Dumber ( 5 ! (
"ale-ate -ate DOT D@LL (
"ale-esc Long DOT D@LL (
Constraint ProdRCustRI-P; ( ProdI- ( CustI- !
! K
FOREIGN <E! C%*stra"*t :
It is also called as &'<'&'DTIAL IDT')&IT? COD"T&AIDT.
It designates a column as <oreign ;ey and establishes a relation bet%een the <oreign
;ey and a specified Primary or @niFue ;ey.
A Composite <oreign ;ey designates a combination of columns as the <oreign ;ey.
The Table or $ie% containing the <oreign ;ey is called the Child Ob#ect.
The Table or $ie% containing the &'<'&'DC'- ;'? is called the Parent Ob#ect.
The <oreign ;ey and the &eferenced ;ey can be in the same Table or $ie%.
The corresponding column or columns of the <oreign ;ey and the &eferenced ;ey must
match in order and data type.
A <oreign ;ey Constraint can be defined on a single +ey column either inline or out of
line.
A Composite <oreign ;ey on attributes should be declared at table level or out of line
style.
A Composite <oreign ;ey Constraint( must refer to a Composite @niFue ;ey or a
Composite Primary ;ey in the Parent Table or $ie%.
Oragle 10 g Material

61
The same column or combination of columns can be designated as both a <oreign ;ey
and a Primary or @niFue ;ey.
,e can designate the same column or combination of columns as both a foreign +ey and
a Primary or @niFue ;ey.
Restr"ct"%*s :
The <oreign ;ey Columns cannot be applied on ..
o LO* H LOD) H LOD) &A, H $A&&A? H D'"T'- TA*L'
o O*='CT H *<IL' H &'< H TI6'"TA6P ,ITI TI6' MOD' .
The &eferenced @niFue C Primary ;ey Constraint on the Parent Table on $ie% must
already be deleted.
A Composite <oreign ;ey cannot have more than 10 columns.
The Child and Parent Tables must be on the same database.
To enable &eferential Integrity across nodes of a distributed database -atabase
Triggers are used.
REFERENCES Ca5se :
The &eferences Clause "hould be used %hen the <oreign ;ey Constraint is IDLID'.
,hen the Constraint is O@T O< LID'( %e must specify the <oreign ;ey ( ;ey %ords.
ON DELETE Ca5se :
The OD -'L'T' Clause determines ho% oracle automatically maintains
&'<'&'DTIAL IDT')&IT? if the &'<'&'DC'- P&I6A&? or @DIE@' ;'? value
is removed.
CA"CA-' option can be specified if %e %ant O&ACL' to remove -ependent
<O&'I)D ;'? values.
"pecify "'T D@LL if %e %ant Oracle to convert dependent <O&'I)I ;'? values to
D@LL.
OD -'L'T' Clause cannot be specified for a vie% constraint.
Steps t% be F%%Be3 #%r Creat"*g F%re"g* <e& C%*stra"*t are as F%%Bs :
Step 1 : Create Primary 6asterSs C Pure 6asterSs
These are Tables %hich contains only Primary ;ey Columns.
"Fl P Create Table -ept
(
-eptno Dumber( 0 ! Constraint -eptnoRP+ P&I6A&? ;'?(
-name $archar0( 0> ! Constraint -nameRDD DOT D@LL(
Location $archar0( 0> ! Constraint LocRDD DOT D@LL
! K
Step 8 : Create -etailed C Child Table
Oragle 10 g Material

62
These are Tables %hich can contain Primary ;ey of their o%n as %ell as <oreign
;eySs referring to other Primary 6asterSs or to them selves.
These tables are also called as dependent tables or &eferential tables.
SI E Create Table 'mployee
(
'mployeeI- Dumber ( 5 !
Constraint 'mpRI-RP; P&I6A&? ;'? (
'name $archar0 ( 1> !
Constraint 'nameRDD DOT D@LL (
-esignation $archar0 ( 1> !
Constraint -esigRDD DOT D@LL (
6anagerI- Dumber ( 5 !
Constraint 6grRI-R<;R"elf
&'<'&'DC'" 'mployee ( 'mployeeI- !
OD -'L'T' "'T D@LL (
Iire-ate -ate
Constraint Iire-ateRDD DOT D@LL (
Commission Dumber ( 7( 0 !(
-eptI- Dumber ( 0 !
Constraint -eptI-R<;
&'<'&'DC'" -ept( -eptno !
OD -'L'T' CA"CA-'
! K
W%r?"*g W"t@ C%$p%s"te PRIMAR! <E! :
Step 1 : Create Pure 6aters
"Fl P Create Table "ample6aster/
(
"ampleI-/ Dumer ( 3 ! Constraint "ampRI-RP; P&I6A&? ;'? (
"ampDame $archar0 ( 0> ! Constraint "ampDameRDD DOT D@LL (
"amp-ate -ate Constraint "amp-ateRDD DOT D@LL
! K
"Fl P Create Table "amp6aster0
(
"ampleI-0 Dumer ( 3 ! Constraint "ampRI-0RP; P&I6A&? ;'? (
"ampDame0 $archar0 ( 0> ! Constraint "ampDame0RDD DOT D@LL (
"amp-ate0 -ate Constraint "amp-ate0RDD DOT D@LL
! K
Step 8 : Create The Pure -etails
"Fl P Create Table "amp&ef
(
Oragle 10 g Material

63
"ampI-&ef/ Dumber ( 3 !
Constraint "ampI-&efR<;
&'<'&'DC'" "amp6aster/( "ampI-/ ! (
"ampI-&ef0 Dumber ( 3 !
Constraint "ampI-&ef0R<;
&'<'&'DC'" "amp6aster0( "ampI-0 ! (
"ampDame&ef $archar0 ( 0> ! (
"amp-ate&ef -ate (
Constraint "amp&efRCompRP;
P&I6A&? ;'? ( "ampI-&ef/( "ampI-&ef0 !
! K
CHEC< C%*stra"*t :
It defines a condition that each ro% must satisfy.
To satisfy the constraint( each ro% in the table must ma+e the condition either T&@' or
@D;DO,D.
O&ACL' does not verify that CI'C; conditions are not mutually eclusive.
Restr"ct"%*s :
The Condition of a CI'C; Constraint can refer to any column in the table( but it cannot
refer to columns of other tables.
The constructs that cannot be included are ...
o Eueries to refer to values in other ro%s
o Calls to functions "?"-T'( @I-( @"'&( @"'&'D$.
o The pseudo Columns C@&&$AL( D'"T$AL( L'$'L or &O,D@6.
o -ate constant that are not fully specified.
A single column can have multiple CI'C; constraint that reference the column in the
definition.
There is no limit to the number of CI'C; constraints that can be defined on a column.
The CI'C; constrains can be defined at the column level or Table level.
E6a$pe :
SI E Create Table -ept
(
-eptno Dumber ( 0 ! Constraint CI;R-eptno
CI'C; ( -eptno *'T,''D /> AD- 99 ! (
-name $archar0( /4 ! Constraint CI;R-nameRup
CI'C; ( -name \ @PP'& ( -name ! !
-I"A*L' (
Loc $archar0 ( /4 ! Constraint CI;RLoc
CI'C;( Loc ID( L-ALLA"S( L*O"TODS(
LD', ?O&;S ( LCIICA)OS ! !
! K
Oragle 10 g Material

64

SI E Create Table 'mp
(
'mpno Dumber ( 3 !
Constraint P&R'mpno P&I6A&? ;'? (
'name $archar0( 04 ! DOT D@LL
Constraint CI;R'name
CI'C; ( 'name \ @PP'& ( 'name ! ! (
=ob $archar0( 1> !
Constraint =obRDD DOT D@LL (
Constraint CI;R=ob
CI'C; ( =ob \ @PP'& ( =ob ! ! (
6gr Dumber ( 3 ! (
Iire-ate -ate -'<A@LT "?"-AT'(
"al Dumber ( 7( 0 !
Constraint "alRDD DOT D@LL (
Constraint CI;R"al
CI'C; ( "al *'T,''D 0>>> AD- />>>>> ! ! (
Comm Dumber ( 7( 0 ! (
-eptno Dumber ( 0 ! (
Constraint TotR"alRCh+ CI'C; ( "al : Comm Q \ />>>>> !
! K
DEFAULT Opt"%* :
The -'<A@LT Option is given to maintain a default value in a column.
The option prevents D@LL $alues from entering the Columns( if a ro% is inserted %ithout a value
for a column.
The -'<A@LT value can be a literal( an epression or a "EL <unction.
The -'<A@LT 'pression must match the data type of the Column.

A33"*g C%*stra"*ts t% a Tabe :
A constraint can be added to a table at any time after the table %as created by using ALT'&
TA*L' "tatement( using A-- Clause.
S&*ta6 :
SI E ALT'& TA*L' Q TableRDame P
A-- T Constraint Q ConstraintRDame P W
ConsRType ( ColumnRDame ! K
The Constraint Dame synta is optional( but recommended.
G5"3e"*es :
,e can A--( -&OP ( 'DA*L'( or -I"A*L' a Constraint ( but modify the structure.
Oragle 10 g Material

65
A DOT D@LL can be added to eisting column by using the 6O-I<? Clause of the ALT'&
TA*L' "tatement.
DOT D@LL can be defined only %hen the table contains no ro%s.
E6a$pe :
SI E ALT'& Table 'mp A-- Constraint 'mpR6grR<; <O&'I)D ;'? ( 6gr !
&'<'&'DC'" 'mp ( 'mpno ! K
DROPPING C%*stra"*ts :
To -rop a constraint identify the constraint name from the @"'&RCOD"T&AIDT" and
@"'&RCOD"RCOL@6D" -ata dictionary vie%s.
The ALT'& TA*L' "tatement is used %ith the -&OP Clause.
The CA"CA-' Option of the -&OP Clause causes any dependent constraints also to be
dropped.
,hen a constraint is dropped( the constraint is no longer enforced and is no longer available in
the data dictionary.
S&*ta6 : ALT'& Table Q TableRDame P
-&OP P&I6A&? ;'? C @DIE@' ( Column ! C
COD"T&AIDT ConstraintRDame T CA"CA-' W K
E6a$pe :
SI E ALT'& Table -ept -&OP P&I6A&? ;'? CA"CA-' K
SI E ALT'& Table 'mp -&OP COD"T&AIDT 'mpR6grR<; K
DISA'LING C%*stra"*ts :
The constraints can be disabled %ithout dropping it or recreating it.
The ALT'& TA*L' "tatement is used %ith the -I"A*L' Clause.

S&*ta6 : ALT'& Table Q TableRDame P
-I"A*L' COD"T&AIDT Q ConstraintRDame P T CA"CA-' W K
G5"3e"*es :
The -I"A*L' Clause can be used both in C&'AT' TA*L' and the ALT'& TA*L'
statement.
The CA"CA-' clause disables dependent integrity constraints.

E6a$pes :
SI E ALT'& Table 'mp -I"A*L' COD"T&AIDT 'mpR'mpnoRP; CA"CA-' K
ENA'LING C%*stra"*ts :
The Constraints can be enabled %ithout dropping it or reGcreating it.
The ALT'& TA*L' statement %ith the 'DA*L' Clause.
Oragle 10 g Material

66

S&*ta6 : ALT'& Table Q TableRDame P
'DA*L' COD"T&AIDT Q ConstraintRDame P K
G5"3e"*es :
Enabling a Constraint applied to all the data in the table.
,hen an @DIE@' or P&I6A&? ;'? Constraint is 'DA*L'-( the @DIE@' or P&I6A&?
;'? Inde is automatically created.
The 'DA*L' Clause can be used both in C&'AT' TA*L' as %ell as ALT'& TA*L'
"tatements.
E6a$pes :
SI E ALT'& Table 'mp 'DA*L' COD"T&AIDT 'mpR'mpnoR<; K
/IEWING C%*stra"*ts :
To $ie% all Constraints on a table by Euerying the @"'&RCOD"T&AIDT" table.
The Codes that are revealed are ..
o C CHEC<
o D PRIMAR! <E!
o R REFERENTIAL INTEGRIT!
o U UNI,UE <E!
E6a$pe :
SI E "'L'CT COD"T&AIDTRDA6'( COD"T&AIDTRT?P'( "'A&CIRCOD-ITIOD
<&O6 @"'&RCOD"T&AIDT" ,I'&' TA*L'RDA6' \ L'6PS K
/IEWING T@e C%5$*s Ass%c"ate3 W"t@ C%*stra"*ts :
The Dames of the columns that are involved in constraints can be +no%n by Fuerying the
@"'&RCOD"RCOL@6D" -ata -ictionary $ie%.
E6a$pe :
SI E "'L'CT COD"T&AIDTRDA6'( COL@6DRDA6' <&O6
@"'&RCOD"RCOL@6D" ,I'&' TA*L'RDA6' \ L'6PS K
.OINS :
A =oin is Fuery that combines ro%s from t%o or more tables( vie%s( or 6ateriali2ed vie%s.
A =oin is performed %henever multiple tables appear in the Fueries <&O6 Clause.
The Eueries "'L'CT list can select any columns from any of these tables.
Oragle 10 g Material

67
The common column names %ithin the tables should Fualify all references to these columns.
.OIN CONDTIONS :
6any =oin Eueries contain ,I'&' Clause( %hich compares t%o columns( each from a different
table.
The applied condition is called a =oin Condition.
To eecute a =oin ...
o Oracle combines pairs of ro%s( each containing one ro% from each table( for %hich the
=oin condition evaluated to T&@'.
The Columns in the =oin Conditions need not be part of the "'L'CT list.
The ,I'&' Clause of =oin Euery can also contain other conditions that refer to columns of only
one table.
To 'ecute a =oin of three or more tables
o Oracle first =oins t%o of the tables based on the =oin Conditions comparing these
columns and then =oinSs the result to another.
.OIN 1
TA'LE 1 TA'LE 8

.OIN 8
RESULT TA'LE 9
RESULT
T@e Oracle Optimi2er determines the older in %hich Oracle =oins tables based on the
o .OIN C%*3"t"%*
o INDE1ES %* Tabes
o STATISTICS #%r t@e Tabes:
The LO* Columns cannot be specified in the ,I'&' Clause( ,hen the ,I'&' Clause
contains any =OID".
S&*ta6 : Tabe1:C%5$*1 W Tabe8:C%5$*8 :
G5"3e"*es :
,hen %riting a "'L'CT statement that =oinSs tables( precede the Column Dame %ith the
Table Dame for clarity and enhance database Access.
If the same column name appears in more than one table( the column name must be prefied
%ith the table name.
To =oin n tables together( %e need a minimum of * D1 =oin Conditions .
The above rule does not apply ( if the table contains a concatenated Primary ;ey.
E,UI.OINS + SIMPLE .OINS + INSER .OINS :
Oragle 10 g Material

68
An 'E@I=OID is a =oin %ith a =oin Condition containing an eFuality operator.
It combines ro%s that have eFuivalent values for the specified columns.
The total si2e of columns in the eFui #oin condition in a single table may be limited to the si2e of a
data bloc+ minus some overhead.
The si2e of the data bloc+ is specified by the initiali2ation parameter -*R*LOC;R"IM' .
E6a$pe :
SI E Seect 'mp.'mpno( 'mp.'name( 'mp.-eptno ( -ept.-eptno( -ept.-name(
-ept.Loc <rom 'mp( -ept ,here 'mp.-eptno \ -ept.-eptno K
,5a"#&"*g A$b"g5%5s C%5$* Na$es :
The names of the column names should be Fualified in the ,I'&' Clause( %ith the table
name to avoid ambiguity.
If there are no common column names bet%een the t%o tables( the Fualification is not
necessary.
E6a$pe :
SI E "elect 'mpno( 'name( 'mp.-eptno ( Loc <rom 'mp( -ept ,here
'mp.-eptno \ -ept.-eptno AD- =ob \ @PP'& ( LmanagerS ! K
USING TA'LE ALIASES :
Tables Aliases can be used instead of table names.
A table Alias gives an alternate name for the eisting Fueried table.
Table Aliases help in +eeping the "EL Code smaller( hence using less 6emory.
The Table alias is specified in the <&O6 Clause.
To specify a Table Alias( specify the Table Dame in full follo%ed by space and then the Table
Alias.
G5"3e"*es :
A table alias can be upto 1> character in length.
If a Table Alias is used for a particular Table Dame in the <rom Clause( then that Table Alias
must be substituted fro the Table Dame through out the "'L'CT statement.
A Table Alias should be meaningful .
A Table Alias is valid only for the Current "'L'CT statement .
E6a$pes :
SI E "elect '.'mpno( '('name( -.-eptno( -.-name <rom 'mp '( -ept -
,here '.-eptno \ -.-eptno K
"Fl P "elect '.'name( '.=ob( -.-eptno( -.-name ( -.Loc <rom 'mp '( -ept -
,here '.-eptno \ -.-eptno AD- '.=ob ID(LADAL?"TS( L6ADA)'&S ! K
"Fl P "elect '.'name( '.=ob( -.-name ( -.Loc <rom 'mp ' ( -ept -
,here '.-eptno \ -.-eptno AD- -.-name Q P L*O"TODS K
SELF .IONS :
Oragle 10 g Material

69
It is a =oin of a table to itself.
The same table appears t%ice in the <&O6 Clause and is follo%ed by table aliases.
The Table Aliases must Fualify the Column names in the =oin Condition.
To perform a "elf =oin( Oracle combines and returns ro%s of the table that satisfy the =oin
Condition .
E6a$pe :
SI E "elect '/.'name Y 'mployee Dame Y (
'0. 'name Y 6anagers Dame Y (
<rom 'mp '/( 'mp '0
,here '/.6gr \ '0.'mpno K
SI E "elect '/.'name [[ L Ys 6anagers is L [[
'0.'name Y 'mployees And 6anagers Y
<rom 'mp '/( 'mp '0
,here '/.6gr \ '0.'mpno K
SI E "elect '/.'name [[ L,or+s <ir L [[
E0.'name Y 'mployees And 6anagers Y
<rom 'mp '/ ( 'mp '0
,here ( '/.6gr \ '0.-eptno ! AD- '/.=ob \ LCL'&;S K
CARTESIAN PRODUCTS :
The CA&T'"IAD P&O-@CT is a =oin Euery( that that no =oin Condition.
-uring Cartesian Product Oracle Combines each ro% of one table %ith each ro% of the other.
It tends to generate a large number of ro%s and the result is rarely useful.
E6a$pe :
SI E "elect 'name( =ob( -name <rom 'mp( -ept K
"Fl P "elect 'name( =ob( -name <rom 'mp( -ept ,here =ob \ L6ADA)'&S K
NON E,UI .OIN :
It is a =oin Condition that is eecuted %hen no column is one table corresponds directly to a
column in the other table.
The -ata in the tables in directly not related but indirectly or logically related through proper
values.
E6a$pe :

SI E "elect '.'name ( '."al ( ".)rade <rom 'mp '( "al)rade "
,here '."al *'T,''D ".Losal AD- ".Iisal K
SI E "elect '.'name( '."al( ".)rade <rom 'mp '( "al)rade "
,here ( '."al P \ ".Lo"al AD- '."al Q \ ".Ii"al ! AD- ".)rade \ / K
OUTER .OINS :
Oragle 10 g Material

70
An Outer =oin etends the result of a simple =oin.
An O@T'& =oin returns all ro%s that satisfy the =oin Condition and also those ro%s from one
table for %hich no ro%s from the other satisfy the =oin Condition.
To perform an O@T =oin of tables A and * and returns all ro%s from A( apply the outer #oin
operator ( : ! to all columns of *.
<or all ro%s in A that have no matching ro%s in *( Oracle returns D@LL for any select list
epressions containing columns of *.
S&*ta6 :
SI E "elect Table/.Column( Table0.Column <rom Table/( Table 0
Where Table/.Column ( : ! \ Table0.Column K
SI E "elect Table/.Column( Table0.Column <rom Table/( Table 0
Where Table/.Column \ Table0.Column ( : ! K
R5es A*3 Restr"ct"%*s :
The ( : ! operator can appear only in the ,I'&' Clause.
The ( : ! operator can appear in the contet of the left correlation in the <rom Clause( and
can be applied only to a column of a Table or $ie%.
If A and * are #oined by multiple #oin conditions( %e must use the ( : ! operator in all of these
conditions.
The ( : ! operator can be applied only to a column( not to an arbitrary epressions.
A condition containing the ( : ! operator cannot be combined %ith another condition using the
O& Logical Operator.
A condition cannot use the ID Comparison operator to compare a column mar+ed %ith the ( :
! operator %ith an epression.
A condition cannot compare any column mar+ed %ith the ( : ! operator %ith a
sub Fuery.
E6a$pes :
"Fl P "elect '.'name ( -.-eptno( -.-name <rom 'mp '( -ept -
,here '.-eptno ( : ! \ -.-eptno Order *y '.-eptno K
"Fl P "elect '.'name( -.-eptno ( -.-name <rom 'mp '( -ept -
,here '.-eptno ( : ! \ -.-eptno AD- '.-eptno ( : ! \ />
Order *y '.-eptno K
"Fl P "elect '.'name( -.-eptno( -.-name <rom 'mp '( -ept -
,here '.-eptno \ -.-eptno ( : ! And '.-eptno ( : ! \ />
Order *y '.-eptno K

.OINING Data Fr%$ M%re T@a* O*e Tabe :
=OID" can be established on more than t%o tables.
The =oin is first eecuted upon the t%o most relevant tables and then the result is applied upon
the third table.
Oragle 10 g Material

71
E6a$pe :

SI E "elecgt C.Dame( O.OldI-( I.ItemI-( I.Itemtot( O.Total
<rom Customer C( Ord O( Item I
,here C.CustI- \ O.CustI- AD-
O.OldI- \ I.OldI- AD-
C.Dame \ LT;* "PO&T "IOPS K
SU' ,UERIES + NESTED SELECT + SU' SELECT + INNER SELCT :
A "ub Euery ans%ers multipleGpart Fuestions.
A "ub Euery in the <rom Clause of a "'L'CT statement is called as IDILID' $I',.
A "ub Euery in the ,I'&' Clause of a "'L'CT statement is called as D'"T'- "@*E@'&?.
A "ub Euery can contain another "ub Euery.
Oracle imposes no limit on the number of "ub Euery levels in the <rom Clause of the topGlevel
Fuery.
,ithin the ,I'&' Clause upto 044 "ub Eueries can be Dested.
A "ub Euery can be part of a Column( in the "'L'CT list.
To ma+e the statements easier for readability( Fualify the columns in a "ub Euery %ith the Table
Dame or Table Alias.
P5rp%se %# A S5b ,5er& :
To define the set of ro%s to be inserted into the target table of an ID"'&T or C&'AT'
TA*L' statement.
To define the set of ro%s to be included in a vie% a materiali2ed vie% in a C&'AT' $I', or
C&'AT' 6AT'&IALIM'- $I', statement.
To define one or more values to be assigned to eisting ro%s in an @P-AT' statement.
The provide values for conditions in a ,I'&' clause( IA$ID) Clause( "TA&T ,ITI
Clause of "'L'CT ( @P-AT'( and -'L'T' statements.
To define a table to operated on by a containing Fuery.
S5b ,5er& Pr"*c"pe :
"olve a problem by combining the t%o Fueries( placing one Fuery inside the other Fuery.
The inner Fuery or the sub Fuery returns a value that is used by the outer Fuery on the main
Fuery.
USAGE G::
They are practically very useful %hen %e need to select ro%s from a table %ith a condition
that depends on the data in the table itself.
S&*ta6 :
Select "electRList <rom TableRDame
,here TableRDame
( "elect "electRList <rom TableRDame ! K
The 'pr . Operator can be of
o "ingle ro% Operators ( P ( \ ( P \ ( Q ( Q \ !
Oragle 10 g Material

72
o 6ultiple ro% Operators ( ID ( AD? ( ALL !
T!PES OF SU' ,UERIES :
S"*ge R%B S5b ,5er& :
o These Eueries return only one &o% from the inner "'L'CT "tatement.
M5t"pe R%B S5b ,5er& :
o These Eueries return more than one &o% from the inner "'L'CT "tatement.
M5t"pe C%5$* S5b ,5er& :
o These Eueries return more than one Column from the inner "'L'CT "tatement.
G5"3e"*es T% F%%B G::
A "ub Euery must be enclosed in parenthesis.
A "ub Euery must appear on the right side of the comparison operator.
"ub Eueries should not contain an O&-'& *? Clause.
Only one O&-'& *? Clause can be implemented for the total "'L'CT "tatement.
T%o clauses of comparison operations can be used in sub Fueries.
- S"*ge R%B - M5t"pe R%B
S"*ge R%B S5b ,5er"es :
They return only one ro% from the IDD'& "'L'CT "tatement.
It uses only single ro% operator.
E6a$pes :
SI E "elect 'name( "al( =ob <rom 'mp
,here "al P ( "elect "al <rom 'mp ,here 'mpno \ 7455 ! K
SI E "elect 'name( "al( =ob <rom 'mp
,here =ob \ ( "elect =ob <rom 'mp ,here 'name \
@pper ( LsmithS! ! Order *y "al K
SI E "elect 'mpno( 'name( Iiredate( "al <rom 'mp
W@ere Iiredate P ( "elect Iiredate <rom 'mp
,here 'name \ @PP'& ( LturnesS ! ! Order *y "al K
SI E "elect 'mpno( 'name( "al( =ob <rom 'mp
,here -eptno \ ( "elect -eptno <rom -ept ,here -name \ L"AL'"S ! K
SI E "elect 'mpno( 'name( "al( Comm( "al : D$L( Comm( > ! <rom 'mp
,here -eptno \ ( "elect -eptno <rom -ept ,here Loc \ L-ALLA"S ! K
APPL!ING GROUP FUNCTIONS IN SU' ,UERIES :
The data from the main Fuery can be displayed by using a )roup <unction is a "ub Euery.
Oragle 10 g Material

73
As a )roup <unction returns a single ro%( the Fuery passes through the success state.
The inner sub Fuery should not have a )&O@P *? Clause in this scenario.
E6a$pes :
SI E "elect 'name( =ob ( "al <rom 'mp
,here "al \ ( "elect 6a( "al ! <rom 'mp ! K
SI E "elect 'name( =ob( "al <rom 'mp
,here Sa W ( "elect 6in ( "al ! <rom 'mp ! K
SI E "elect 'name( =ob( "al <rom 'mp
,here Sa P ( "elect Avg ( "al ! <rom 'mp ! K
SI E "elect 'name( =ob( "al <rom 'mp
,here "al Q ( "elect "T--'$( "al ! <rom 'mp ! K
App&"*g HA/ING Ca5se W"t@ S5b ,5er"es :
A "ub Euery can be also applied in IA$ID) Clause.
The Oracle "erver eecutes the sub Fuery( and the results are returned into the IA$ID) Clause
of the 6ain Euery.
E6a$pe :
SI E "elect -eptno( 6in ( "al ! <rom 'mp
Gr%5p '& -eptno
Ha="*g 6in( "al ! P ( "elect 6in ( "al ! <rom 'mp
W@ere -eptno \ 0> ! K
SI E "elect =ob( Avg ( "al ! <rom 'mp
Gr%5p '& =ob
Ha="*g Avg( "al ! \ ( "elect 6in ( Avg ( "al ! !
Fr%$ 'mp Gr%5p '& =ob ! K
SI E "elect =ob( Avg ( "al ! <rom 'mp
Gr%5p '& =ob
Ha="*g Avg ( "al ! Q ( "elect 6a ( Avg ( "al ! !
Fr%$ 'mp Gr%5p '& =ob ! K
SI E "elect =ob( Avg ( "al ! ( ToRChar ( Avg ( "al ! ( LL99(999.99S ! <rom 'mp
Gr%5p '& =ob
Ha="*g Avg ( "al ! Q ( "elect 6a ( "al ! !
Fr%$ 'mp Gr%5p '& -eptno ! K
S5b ,5er"es Ret5r*"*g M%re T@a* O*e R%B :
The "ub Eueries that return more than one ro% are called as 6@LTIPL' &O, "@* E@'&I'".
In This case a multiple ro% operator should be used.
The multiple ro% operators epect one or more values.
The multiple ro% operator are ID ( AD?( ALL(
o IN E,UAL TO AN! MEM'ER INDIA THE LIST :
Oragle 10 g Material

74
o AN! COMPARES /ALUE TO EACH /ALUE RETURNED S
'! SU' ,UER! : !
N
o ALL COMPARES /ALUE TO E/ER! /ALUE RETURNED O
'! THE SU' ,UER! : N
!
S
E6a$pes :
SI E "elect 'name( "al( -eptno <rom 'mp
,here "al In( "elect 6in ( "al ! <rom 'mp )roup *y -eptno ! K
SI E "elect 'name( "al( -eptno <rom 'mp
W@ere "al In ( "elect 6a ( "al ! <rom 'mp )roup *y -eptno ! K
SI E "elect 'name( "al ( -eptno ( =ob <rom 'mp
,here "al In( "elect 6a ( "al ! <rom 'mp )roup *y =ob ! K
E6a$pes A*& Operat%r :
SI E "elect 'mpno( 'name( =ob <rom 'mp
,here "al Q Any( "elect "al <rom 'mp ,here =ob \ LCL'&;S ! K
N%te : C It means less than the 6aimum $alue .
SI E "elect 'mpno( 'name( =ob( "al <rom 'mp
,here "al Q Any ( "elect "al <rom 'mp
,here -eptno \ 0> AD- =ob Q P LCL'&;S K
SI E "elect 'mpno( 'name( =ob <rom 'mp
,here "al P Any ( "elect "al <rom 'mp =ob \ LCL'&;S ! K
N%te : E AN! It means more than the 6inimum.
SI E "elect 'mpno( 'name( =ob <rom 'mp
,here "al \ Any ( "elect "al <rom 'mp ,here =ob \ LCL'&;S ! K
N%te : W AN! It is 'Fuivalent to ID Operator .
E6a$peTs ALL Operat%r :
SI E "elect 'mpno( 'name( =ob( "a+l <rom 'mp
,here "al P ALL ( "elect Avg ( "al ! <rom 'mp )roup *y -eptno ! K
N%te : E ALL It means more than the 6aimum .
SI E "elect 'mpno( 'name =ob( "al <rom 'mp
,here "al Q All ( "elect Avg( "al ! <rom 'mp )roup *y -eptno ! K
N%te : C ALL It means less than the 6inimum .
Oragle 10 g Material

75
S5b ,5er"es Ret5r*"*g M5t"pe C%5$*s :
In "un Eueries multiple Columns can be compared in the ,I'&' Clause( by %riting a
Compound ,I'&' Clause using logical operators.
6ultiple Column sub Fueries enable us to combine the duplicate ,I'&' condition into a single
,I'&' Clause.
S&*ta6 :
Select Column/( Column0( ...
<rom TableRDame
,here ( Column a( Column b ( ...! ID
( "elect Column a( Column b .. ! <rom TableRDame
,here Condition ! K
E6a$pe :
SI E "elect OrderRI-( ProductRI- ( Euantity <rom Item
W@ere ( ProductRI-( Euantity ! ID
( "elect ProductRId( Euantity <rom Item
,here OrderRI- \ 5>4 AD- OrderRI- Q P 5>4 K
The Column comparisons in a multiple Column sub Fuery can be
o Pair %ise Comparison
o Don Pair %ise Comparison
In Pair ,ise comparisons each candidate ro% in the "'L'CT statement must have both the
same values associated %ith each column in the group.
The Don Pair ,ise comparison also called cross product( %e can use a ,I'&' Clause %ith
multiple conditions.
In Don Pair ,ise comparison( the candidate ro% must match the multiple conditions in the
,I'&' Clause but the values are compared individually.
E6a$pe :
SI E "elect OrderRI-( ProductRI-( Euantity <rom Item
,here ProductRI- ID("elect ProductRI- <rom Item
,here OrderRI- \ 5>4 !
AD- Euantity ID("elect Euantity <rom Item
,here OrderRI- \ 5>4
AD- OrderRI- Q P 5>4 K
Ha*3"*g NULL /a5es "* S5b ,5er"es :
Oragle 10 g Material

76
If one of the values returned by the inner Fuery is D@LL value( then the entire Fuery returns Do
&o%s.
All Conditions that compare a D@LL $alue result is a D@LL .
,henever a D@LL could be part of a "ub Euery( it is better not to use DOT ID Operator
As it is eFuivalent to ] \ D@LL .
E6a$pe :
SI E "elect '.'name <rom 'mp '
,here '.'mpno ID( "elect 6.6gr <rom 'mp 6 ! K
App&"*g S5b ,5er& "* Fr%$ Ca5se :
A "ub Euery in the <rom Clause is eFuivalent to a vie%.
The "ub Euery in the <rom Clause defines a data source for that particular "'L'CT statement
and only that "'L'CT statement.
E6a$pes "* ,5er"es :
SI E "elect '.'name( '."al ( '.-eptno( '/."alAvg <rom 'mp '(
( "elect -eptno( Avg ( "al ! "alAvg <rom 'mp
)roup *y -eptno ! '/ ,here '.-eptno \ '/.-eptno
AD- '."al P '/."alAvg K
SI E "elect T/.-eptno( -name( "taff <rom -ept T/(
( "elect -eptno( Count( H ! As "taff <rom 'mp
)roup *y -eptno ! T0
,here T/.-eptno \ T0.-eptno
AD- "taff P \ 4 K
SI E "elect -eptno( "um ( "al ! ( "um ( "al ! C TotR"al H />> Y"alary Percentage Y
<rom 'mp( ( "elect "um ( "al ! TotR"al <rom 'mp !
)roup *y -eptno( TotR"al K
SI E "elect H <rom ( "elect 'mpno( 'name( Iiredate <rom 'mp
Order *y Iiredate -esc ! K
SI E "elect 'name( "al( ( "elect Avg( "al ! <rom 'mp ! Y Organi2ation AverageZ
<rom 'mp K
SI E "elect 'name( "al( ( "elect 6a( "al ! <rom 'mp ! Y Organi2ation 6aimum Y (
( "elect 6in ( "al ! <rom 'mp ! Y Organi2ation 6inimum Y
<rom 'mp K
SI E "elect '.'mpRCount( -.-eptRCount
<rom ( "elect Count ( H ! 'mpRCount <rom 'mp ! '(
( "elect Count( H ! -eptRCount <rom -ept ! - K
SI E "elect A.-eptno Y -epartment DumberZ (
( A.DumR'mp C *.TotalRCount ! H />> Y _'mployeesZ (
( A."alR"um C *.TotalR"al ! H />> Y _"alary Y
<rom ( "elect -eptno( Count( H ! DumR'mp(
"um ( "al ! "alR"um <rom 'mp
)roup *y -eptno ! A (
Oragle 10 g Material

77
( "elect Count ( H ! TotalRCount(
"um( "al ! TotalR"al <rom 'mp ! * K
CORRELATED SU' ,UERIES :
It is another %ay of performing Eueries upon the data %ith a simulation of =oins.
In this the information from the outer "'L'CT statement participates as a condition in the IDD'&
"'L'CT statement.
S&*ta6 :
Select "electRList <rom Table/ <RAlias/
,here 'pr. Operator
( "elect ColumnRList <rom Table0 <RAlias0
,here <RAlias/.Column Operator <RAlias0.Column ! K
E6a$pe :
SI E "elect -eptno( 'name( "al <rom 'mp A
,here "al P ( "elect Avg( "al ! <rom 'mp
,here .-eptno \ -eptno !
Order *y -eptno K
Steps Per#%r$e3 :
<irst the Outer Euery is eecuted.
Passes the Fualified column value to the inner gueries ,I'&' Clause.
Then the inner Fuery or candidate Fuery is eecuted.( and the result is passed to the outer
Fueries ,I'&' Clause.
-epending on the supplied value the condition is Fualified for the specific record.
E1ISTS Operat%r "* C%rreate3 S5b ,5er"es :
The 'AI"T" Operator tests for eistence of ro%s in a subFuery.
It returns T&@' if a subFuery returns at least one ro%.
E6a$pe :
SI E "elect -eptno( -name <rom -ept -
,here 'AI"T" ( "elect H <rom 'mp ' ,here -.-eptno \ '.-eptno ! K
SI E "elect -eptno( -name <rom -ept -
,here DOT 'AI"T" ( "elect H <rom 'mp ' ,here -.-eptno \ '.-eptno ! K
SI E "elect '.'name <rom 'mp '
,here 'AI"T" ( "elect H <rom 'mp '/ ,here '/.'mpno \ '.6gr ! K
SI E "elect '.'name <rom 'mp '
,here DOT 'AI"T" ( "elect H <rom 'mp '/ ,here '/.'mpno \ '.6gr ! K
SI E "elect '.'name <rom 'mp '
,here 'AI"T" ( "elect H <rom 'mp '/ ,here '/.6gr \ '.'mpno ! K
Oragle 10 g Material

78
SI E "elect '.'name <rom 'mp '
,here DOT 'AI"T" ( "elect H <rom 'mp '/ ,here '/.6gr \ '.'mpno ! K
HIERARCHICAL ,UERIES :
These are Fueries that are eecuted upon tables that contain hierarchical data.
To eecute the hierarchical Fueries( %e need the follo%ing Fueries.
o START WITH : It specifies the root ro%s of the hierarchy.
o CONNECT '! : It is used to specify the relationship bet%een parent ro%s and
child ro%s of the hierarchy.
o WHERE : It is used to restrict the ro%s returned by the Euery %ithout
affecting other ro%s of the hierarchy.
Steps F%%Be3 :
ORACLE selects the root ro% ( s ! of the hierarchy( %hich satisfy the condition of the "TA&T
,ITI Clause.
ORACLE selects the child ro%s of each root ro%. 'ach child ro% must satisfy the condition of the
CODD'CT *? Clause( %ith respect to one of the root ro%s.
ORACLE selects successive generations of child ro%s.
ORACLE selects children by evaluating the CODD'CT *? condition %ith respect to a current
parent ro%.
I# the Fuery contains a ,I'&' Clause( O&ACL' removed all ro%s from the hierarchy that do not
satisfy the condition of the ,I'&' Clause .
Ge*era Represe*tat"%* :
R%%t
C@"3
Lea#s
Restr"ct"%*s :
o They cannot be used to perform =oins.
o They cannot select data from a $ie%( ,hose Euery lperforms a =oin.
o If O&-'& *? Clause is used( then the ro%s are returned as per the specification in the
O&-'& *? Clause.
Oragle 10 g Material

79
3
2
1
9
7
1
0
1
2
1
4
5 6
1
1
8
T@e Ge*era H"rarc@& %# E$p Data :
<ING 4;9>
.ONES 4UVV 'LA<E 4V>; CLAR< 44;8
SCOTT 44;; FORD 4>08 ALLEN 4A>> MILLER 4>9A
ADAMS 4;4V SMITH 49V> WARD 4U81

MARTIN 4VUA
TURNER 4;AA
.AMES 4>00
Ge*era Str5ct5re %# H"erarc@"ca Tree :
LE/EL 1 :
Le=e 8 :
Le=e 9 :

Le=e A :
N%te : T% -efine Iierarchical Eueries Properly %e must use the follo%ing Clauses.
o "TA&T ,ITI
o CODD'CT *?
START WITH Ca5se :
o It identifies the ro%(s! to be used as the &oot ( s ! of a hierarchical Euery.
Oragle 10 g Material

80
Root / Parent
Root / Parent Root / Parent
Parent / Child Parent / Child Child / Leaf
Child / Leaf Child / Leaf Child / Leaf
o It specifies a condition that the roots must specify.
o If "TA&T ,ITI is omitted( Oracle uses all ro%s in the table as &OOT ro%s.
o A "TA&T ,ITI Condition can contain a "ubFuery.
CONNECT '! Ca5se :
o This clause specifies the &elationship bet%een Parent and Child &o%s( in a hierarchical
Fuery.
o This caluse contains a condition that defines a relationship
o This condition can be any condition as defined by the synta decription.
o ,ithin the condition( some part of the condition must use the P&IO& operator( %hich
refers to the Parent ro%.
o The forms of P&IO& operators is
PRIOR 'pr Comparison J operator 'pr.
'pr Comparison J operaor 'pr.
o The Clause can contain other conditions to further filter the ro%s selected by the Fuery.
o It cannot contain a "ub Euery.
E6a$pe :
SI E "elect 'name( 'mpno( 6gr( =ob <rom 'mp
CONNECT '! PRIOR 'mpno \ 6gr K
SI E "elect 'name( 'mpno( 6gr( =ob <rom 'mp
START WITH =ob \ LP&'"I-'DTS
CONNECT '! PRIOR 'mpno \ 6gr K

SI E "elect 'name( 'mpno( 6gr ( =ob <rom 'mp
START WITH 'name \ L;ID)S
CONNECT '! PRIOR 'mpno \ 6gr K
SI E "elect 'name( 'mpno( 6gr( =ob( "al <rom 'mp
START WITH "al \ 4>>>
CONNECT '! PRIOR 'mpno \ 6gr K
PSEUDO C%5$*s :
Pseudo columns behave li+e a table column( but is not actually stored in a table.
@pon Pseudo columns only "'L'CTSs can be implemented( *ut ID"'&T( @P-AT' or -'L'T'
Cannot be implemented.
The Available Pseudo Columns are ..
o CURR/AL
o NE1T/AL
o LE/EL
o ROWID
o ROWNUM
CURR/AL A*3 NE1T/AL :
Oragle 10 g Material

81
These Pseudo columns are applied upon the "'E@'DC' "chemaOb#ect.
CURR/AL returns the Curent $alue of a "eFuence.
NE1T/AL Increments the seFuence and returns the Det $alue.
The CURR/AL And NE1T/AL can be used only in ..
o The "'L'CT list of a "'L'CT "tatement
o The $AL@'" Clause of an ID"'&T "tatement
o The "'T Clause of an @P-AT' "tatement.
Restr"ct"%*s :
The C@&&$AL and D'AT$AL cannot be used in ...
o A "ub Euery
o A $ie%Ss Euery or "DAP"IOTSs Euery.
o A "'L'CT "tatement %ith the -I"TIDCT operator
o A "'L'CT "tatement %ith a )&O@P *? or O&-'& *? Clause.
o A "'L'CT "tatement that is combined %ith another "'L'CT "tatement %ith
@DIOD( ID'&"'CT( 6ID@" "'T Operator.
o The ,I'&' Clause of a "'L'CT "tatement
o The -'<A@LT $alue of Column in a C&'AT' TA*L' or ALT'& TA*L'
"tatement
o The Condition of a CI'C; Constraint.
In a single "'L'CT "tatement ( all referenced seFuences( LOD) Columns( @pdated
tables( and loc+ed tables( must be located on the same database.
,I'D a "'E@'DC' I" Created( %e can define its IDITIAL $AL@' and the
IDC&'6'DT bet%een its values.
The <irst refrence to the D'AT$AL returns the "'E@'DC'" Initial $alue.
*efore the C@&&$AL can be used for a "'E@'DC' is a session( first the "'E@'DC'
should be incremented %ith D'AT$AL.
A "'E@'DC' can be incremented only once in a single "EL "tatement.
A "'E@'DC' can be accessed by many users concurrently %ith no ,AITID) or
LOC;ID).
C@&&$AL and D'AT$AL should be Fualified %ith the name of the "eFuence.
S&*ta6 :
SE,UENCENAME:CURR/AL &eturns the Current $alue of the "eFuence.
SE,UENCENAME:NE1T/AL Increments the "eFuence $alue by the -eclar
Specification.
SE,UENCE :
A "'E@'DC' is a schema ob#ect that cn generate uniFue seFuential $alues.
The "'E@'DC' $alues are often user for P&I6A&? ;'?S" and @DIE@' ;'?Ss.
To refer to the Current or Det $alue of a "'E@'DC' in the "chema of another used. The
follo%ing privileges should be available
o SELECT O'.ECT PRI/ILAGE
o SELECT AN! SE,UENCE
Oragle 10 g Material

82
F%r "'E@'DC'" in other "chema the Eualifying "ynta is
o SCHEMANAME:SE,UENCENAME:CURR/AL
o SCHEMANAME:SE,UENCENAME:NE1T/AL
To refer to the value of a "'E@'DC' on a &emote -atabase( The "'E@'DC' should be
Fualified %ith a complete or partial name of the -atabase Lin+.
o SCHEMANAME: SE,UENCENAME :CURR/ALXD'LIN<
o "CI'6ADA6'."'E@'DC'DA6'.D'AT$AL`-*LID;
CREATING SE,UENCES :
P5rp%se :
A* ob#ect from %hich multiple users may generate @niFue Integers.
Can be used to generate P&I6A&? ;'? values automatically.
S&*ta6 :
CREATE SE,UENCE SeI5e*ceNa$e
INCREMENT '! I*teger
START WITH I*teger
MA1/AL I*terger + NOMA1 /ALUE
MIN/AL I*teger + NOMIN /ALUE
C!CLE + NOC!CLE
CACHE I*teger + NOCACHE
ORDER + NOORDER H
INCREMENT '! :
o "pecifies the interval bet%een the "eFuence Dumbers.
o $aule can be Positive or Degetive( but can not be >.
o If the value is positive it is Incrementeal "eFuence else it -ecremental "eFuence.
o If omitted defaults to incrementation of /.
MIN/ALUE :
o Specifies the "eFuencesSs 6inimum $aule.
NOMIN/ALUE :
o Specigfies a minimum value of / for an ascending seFuence or J ( /> !
05
<or decending seFuence.
MA1/ALUE :
o Specifies the 6aimum vaue that can be generated.
NOMA1/AULE :
Oragle 10 g Material

83
o "pecifies a maimum value of />
07
for ascending seFuece or J/ for -escending
"eFuence.
STARTWITH :
o "pecifies the first seFuence Dumber to be generated.
o <or Ascending seFuences the default value is "'E@'DC'"Ss 6IDI6@6 v alue( $ice
$ersa.
C!CLE :
o "pecifies the seFuence %ill continue to generate values after reaching either maimum or
minimum value.
NOC!CLE :
o "pecifies the "'E@'DC' cannot genral more values after the targeted limit.
CACHE :
o Specifies the Preallocation of "'E@'DC' Dumbers( The minimum is 0.
NOCACHE :
o "pecifies the values of a "'E@'DC' are not Preallocated.
If the above parameters are not specified by default 0> values are cached.
ORDER :
o )uarantees the seFuence numbers to be generated in the order of reFuest.
NOORDER :
o -oesnot guarantee the seFuence Dumber %ithout Pre order -efault .
Sa$pe Tabe :
SI E Create Table "ample
(
"ampI- Dumber ( 3 !
Constraint "ampI-RP; Primary ;ey(
"ampDame $archar0( 04 ! (
"amp-ate
! K
Creat"%* %# I*cre$e*ta SeI5e*ce B"t@ N%C&ce :
SI E Create "'E@'DC' "ample"eF
IDC&'6'DT *y /
"TA&T ,ITI >
6AA$AL@' 4
DOCACI'
DOC?CL' K
Oragle 10 g Material

84
Act"=at"*g a*3 Attac@"*g t@e SeI5e*ce t% a Tabe :
SI E Insert into "ample ( "ampI-( "ampDame( "amp-ate !
$alues ( "ample"eF.Det$al( L"ampleS( S1/GA@)G>4S ! K
N%te :
o <or every eample -&OP TA*L' and -&OP "'E@'DC' .
o Create the sample table again %ithout P&I6A&? ;'?.
Creat"*g A SeI5e*ce W"t@ C!CLE :
SI E Create "eFuence "ample"eF
IDC&'6'DT *? /
"TA&T ,ITI >
6AA$AL@' 4
DOCACI'
C?CL' K
o <ollo% the Insert statement as previous and chec+ the Inserted $alues by eecuting the
"'L'CT statement on "ample Table.
Creat"%* %# Decre$e*ta SeI5e*ce :
SI E Create "eFuence "ample"eF
IDC&'6'DT *? J/
"TA&T ,ITI 4
6AA$AL@' 4
6ID$AL@' >
DOCACI'
DOC?CL' K
G5"3e"*es F%r Ater"*g A SeI5e*ce :
o The ALT'& previlege should be available.
o Only the future seFuence numbers are affected by the ALT'& "'E@'DC' "tatement.
o The "TA&T ,ITI option cannot by changed using ALT'& "'E@'DC'.
o To change the "TA&T ,ITI. Option( drop the "'E@'DC' and then recreate the
"'E@'DC'.
o "ome validation performed( i.e.( A D&' 6AA$AL@' cannot be imposed that is
less than the current "'E@'DC' number.
Le=e Pse53%c%5$* :
o This Pseudocolumn return / for &OOT Dode( 0 for a child of a &OOT and so on.
Child Any DonR&oot Dode.
&oot Iighest Dode %ithin an Inverted Tree.
Parent Any Dode C &o% that has children.
Leaf Any Dode %ithout Children.
Oragle 10 g Material

85
M%3"#&"*g a SeI5e*ce :
o The ALT'& Command can be used to change the present status of a "'E@'DC'.
o The ALT'& "'E@'DC' Command can be used to change ...
Increment $alue
6aimum $alue.
6inimum $alue
Cycle Option
Cache Option
S&*ta6 :
ALTER SE,UENCE "eFuenceDame
T IDC&'6'DT *? n W
T U 6AA$AL@' n C DO6AA$AL@' V W
T U 6ID$AL@' n C DO6ID$AL@' V W
T U C?CL? C DOC?CL' V W
T U CACI' n C DOCACI' V W K
E6a$pe :
SI E ALTER SE,UENCE "ample"eF
6AA$AL@' />
CACI'
DOC?CL' K
C%*#"r$"*g SeI5e*ces :
All "'E@'DC'" that have been created are documted in the dta dictionary.
The data dictionary in %hich the information of "'E@'DC'" are stored is @"'&RO*='CT".
The sttings of the "'E@'DC' can be confirmed by "'L'CTID) on @"'&R"'E@'DC'".
"Fll P "elect "eFuenceRDame( 6inR$alue( 6aR$alue( Increment *y( LastRDumber
<rom @serR"eFuences K
/"eB"*g t@e C5rre*t /a5e %# a SeI5e*ce :
SI E "elect "ample"eF.C@&&$AL <rom -ual K
Dr%pp"*g A* E6"st"*g SeI5e*ce :
A "'E@'DC' can be -&OPP'- at any time.
Once removed( the "'E@'DC' can no longer be referenced.
"Fl P -&OP "'E@'DC' "ample"eF K
To establish the Iierarchical relationship %ith L'$'L %e need.
o "TA&T ,ITI
o CODD'C? *?
Oragle 10 g Material

86
"Fl P "elect 'name( =ob( 6gr( Level <rom 'mp K
"Fl P "elect Lpad ( L L ( 0 H ( L'$'L J / ! ! OrgRLevel(
'name( 'mpno( 6gr( =ob <rom 'mp
"TA&T ,ITI =ob \ LP&'"I-'DTS
CODD'CT *? P&IO& 'mpno \ 6gr K
"Fl P "elect Lpad ( L L ( 0 H ( L'$'L J / ! ! [[ 'name OrgRChart (
'mpno( 6gr( =ob <rom 'mp
"TA&T ,ITI =ob \ LP&'"I-'DTS
CODD'CT *? P&IO& 'mpno \ 6gr K
"Fl P "elect Lpad ( L L ( 0 H ( L'$'L J / ! ! [[ 'name OrgRChart (
'mpno( 6gr( =ob ( "al <rom 'mp
,I'&' =ob ] \ LADAL?"TS
"TA&T ,ITI =ob \ LP&'"I-'DTS
CODD'CT *? P&IO& 'mpno \ 6gr K
"Fl P "elect Lpad ( L L ( 0 H ( L'$'L J / ! ! [[ 'name OrgRChart (
'mpno( 6gr( =ob( "al <rom 'mp
"TA&T ,ITI =ob \ LP&'"I-'DTS
CODD'CT *? P&IO& 'mpno \ 6gr AD- L'$'L Q \ 0 K
Seect"*g Nt@ H"g@est /a5e Fr%$ Tabe :
S&*ta6 :
S'L'CT L'$'L( 6AA ( ColDame !
<&O6 TableDame
,I'&' Level \ & L'$'LDO
CODD'CT *? P&IO& ColDame P ColDame
)&O@P *? L'$'L K
E6a$pe :
SI E "'L'CT L'$'L( 6AA ( "AL ! <&O6 '6P
,I'&' L'$'L \ & L'$'LDO
CODD'CT *? P&IO& "AL P "AL
)&O@P *? L'$'L K
Seect"*g Nt@ L%Best /a5e Fr%$ Tabe :
S&*ta6 :
S'L'CT L'$'L( 6ID ( ColDame !
<&O6 TableDame
,I'&' Level \ & L'$'LDO
CODD'CT *? P&IO& ColDame Q ColDame
)&O@P *? L'$'L K
E6a$pe :
SI E "'L'CT L'$'L( 6ID ( "AL ! <&O6 '6P
,I'&' L'$'L \ & L'$'LDO
CODD'CT *? P&IO& "AL Q "AL
Oragle 10 g Material

87
)&O@P *? L'$'L K
ROWNUM Pse53% C%5$* :
<or each &o% returned by a Fuery( the &O,D@6 pseudocolumn returns a number indicated the
order in %hich Oracle selects the r%o from a set of =oined ro%s.
The <irst &o% selected has a &o%num of /( the second has 0. and so on.
The &o%num can be used to limit the Dumber of ro%s returned by the Euery.
,hen O&-'& *? Clause follo%s a &o%nu( the ro%s %ill be recordered by O&-'& *y Clause .
If O&-'& *? Clause is embedded in a "ub Euery and &o%num condition is palced in the
topRlevel Fuery( then the &o%nyum condition can be forced to applied after the ordering of the
ro%s.
Conditions testing for &o%num values greater than a positive integer are al%ays <alse.
"Fl P "elect Lpad ( L L ( &o%Dum( H ! <rom 'mp K
"Fl P "elect &o%Dum( 'name( "al <rom 'mp K
,5er&"*g #%r T%p N N N Rec%r3s :
,e can As+ for Dth largest or smallest values of a column.
Dever use &o%num and O&-'& *? together as Oracle first fetches the ro%s according to
&O,D@6 and then "O&T the found ro%s.
<rom Oracle 8i ( O&-'& *? can be used in $I'," and IDLID' $I',".
"Fl P "elect &o%Dum( 'name( "al <rom 'mp
,here &o%Dum Q 5
O&-'& *? "al -esc K CH ,rong ,ay HC
"Fl P "elect H <rom ( "elect H <rom 'mp O&-'& *? "al -'"C !
,here &o%Dum Q 5 K CH Proper ,ay HC
ROWID Pse53%C%5$* :
This Pseudo Column returns a &%oSs Address for each ro% in the database.
&O,I- $alues copntain information necessary to locate a ro%
o The &o% belongs to %hich data bloc+ in the data file.
o The &o% belongs to %hich ro% in the -ata *loc+ ( <irst ro% is > !
o The ro% belongs to %hich -ata <ile ( <irst <ile is / !
T@e R%Bs in different tables that are stored together in the same cluster can have the same
&o%id.
The -ate type of the $alues belonging to the &o%I- are of &O,I- -ata type.
Uses %# ROWID /a5es :
&O,I- is the fastest means of accessing a single ro%.
&O,I- can sho% ho% a tables &o%Ss are stored.
&o%I-Ss are @niFue identifiers for a &o% in a Table.
A &o%I- can never change during the life time of its &o%.
Oragle 10 g Material

88
&o%I-Ss should bnot be assigned as P&I6A&? ;'?Ss as there is a chance of &O,I- to change
%hen the database is 'ported or Imported.
,hen a &o% is -'L'T'-( O&ACL' may reassign its &o%id to a ne% &o% that is inserted.
The &O,I- can never be ID"'&T'-( @P-AT'- and -'L'T'-.
The &O,I- Pseudo Column can be used in "'L'CT and ,I'&' Clauses.
"Fl P "elect &o%I-( 'name( =ob <rom 'mp
,here 'mpno \ 0> K
"Fl P "elect 'name( "al( =ob <rom 'mp
,here &o%id \ LAACEEAACAAAA'IAAAS K
"Fl P "elect 'name( "al( =ob <rom 'mp
,here &o%id Q LAACEEAACAAAA'IAAAS K
Cac5at"*g t@e C5$5at"=e Saar& :
SI E "elect *."al( "um ( A."al ! Y Cumulative "alary Y
<rom 'mp A( 'mp *
,here A.&O,I- Q \ *.&O,I-
)roup *y *.&O,I-( *."al K
SET OPERATORS :
These operators are used to Combine information of similar date type from one or more than one
table.
-ata type of the corresponding columns in all the "'L'CT statement should be same.
The different types of "'T Opeators are
o UNION - INTERSECT - UNION ALL
o MINUS
"'T operatiors can copmbine t%o or more Fueries into one result.
The O&-'& *? Caluse %hen iomplemented should be applied only upon the column Dumbers in
the "'L'CT list.
UNION Operat%r :
The @DIOD Operator merges the outputs of t%o or more Fueries into a single set of ro%s and
columns.
S&*ta6 :

Select "tatement /
@DIOD
"elect "tatement 0
T O&-'& *? Clause W
The Eueries are all eecuted independently but their output is merge.
Only final Fuery ends %ith a semicolon.
E6a$pes :
"Fl P "elect =ob <rom 'mp
Oragle 10 g Material

89
,here -eptno \ 0>
@DIOD
"elect =ob <rom 'mp
,here -eptno \ 1> K
P%"*ts t% P%*3er :
The t%o select statement may not contain an O&-'& *? Clause( the final result of the entire
@DIOD operation can be ordered.
The number of columns retrieved by the first "'L'CT statement must be eFual to the number of
columns retrieved by the second "'L'CT( and -ata types must match.
The columns used for ordering must be defined through a number.
"Fl P "elect 'mpno( 'name <rom 'mp
,here -eptno \ /> K
UNION :
SI E "elect 'mpno( 'name <rom 'mp
,here -eptno \ 1>
O&-'& *? / K
SI E "elect 'mpno( 'name( =ob <rom 'mp
,here -eptno \ ( "elect -eptno <rom -ept
,here -name \ L"AL'"S !
UNION
"elect 'mpno( 'name( =ob <rom 'mp
,here -eptno \ ( "elect -eptno <rom -ept
,here -name \ LACCO@DTID)S !
O&-'& *? / K
UNION ALL :
It is similar to @DIOD( %here @DIOD suppresses the deplicates.
If the duplicate records have also to be displayed then @DIOD ALL is used.
"Fl P "elect 'mpno( 'name <rom 'mp
,here -eptno \ />
UNION ALL
"elect 'mpno( 'name <rom 'mp
,here -eptno \ 1>
O&-'& *? / K
INTERSECT Operat%r :
This operator returns the ro%s that are common bet%een t%o sets of ro%s.
S&*ta6 :
Select "tatement /
IDT'&"'CT
"elect "tatement 0
T O&-'& *? Clause W K
E6a$pe :
Oragle 10 g Material

90
SI E "elect =ob <rom 'mp
W@ere -eptno \ 0>
INTERSECT
"elect =ob <rom 'mp
,here -eptno \ 1> K
"Fl P "elect =ob <rom 'mp ,here -eptno \ 0>
INTERSECT
"elect =ob <rom 'mp ,here -eptno \ 1> K
MINUS Operat%r :
This operators returns the ro%s @niFue to the first Fuery.
S&*ta6 :
"elect "tatement /
MINUS
"elect "tatement 0
T O&-'& *? Clause W K
E6a$pe :
SI E "elect =ob <rom 'mp ,here -eptno \ 0>
MINUS
"elect =ob <rom 'mp ,here -eptno \ /> K
SI E "elect &o%Dum( 'name <rom 'mp ,here &o%Dum Q 7
MINUS
"elect &o%Dum( 'name <rom 'mp ,here &o%Dum Q 5 K
/IEWS IN ORACLE :
/IEW :
It is a logical table based on one or more tables or $ie%s.
A $ie% in practicality contains no data by itself
The tables upon %hich a vie% is based are called as *A"' TA*L'".
$ie%s can be created as Ob#ect $ie%s or &elational $ie%s.
The Ob#ect $ie%s support
o LO*Ss H Ob#ect Types H &'<Ss
o Dested Tables H $arrays
Ob#ect $ie% is a vie% of a user defined type( %here each ro% contains ob#ects( and each ob#ect
%ith a ob#ect Identifier.
Oragle 10 g Material

91
PRERE,UISITES :
"hould have C&'AT' $I', or C&'AT' AD? $I', "ystem privilege.
To create a subvie%( %e need @D-'& AD? $I', system privilege or @D-'& O*='CT privilege
on the "@P'&$I',.
The O,D'& of the "chema should have "'L'CT( ID"'&T( @P-AT' or -'L'T' &o%s from all
tables or $ie%s on %hich the $I', is based.
The above privileges must be granted the privileges directly( rather than a role.
S&*ta6 :
Create T O& &'PLAC' W $I',
J U <O&C' C DO<O&C' V W $I', $ie%Dame
J ( AliasDame T ( AliasDame ...W ! W As "ubEuery
J ,ITI U CI'C; OPTIOD C &'A- ODL? V
COD"T&AIDT ConstraintDame W K
OR REPLACE :
o "pecifies the vie% has to be replaced if already 'isting.
FORCE :
o "pecifies the $I', has to be created even if the base tables does not eist.
NOFORCE :
o "pecifies the $ie% "hould not be created if the base table does not 'ist( %hich is
default.
A"asQNa$e :
o "pecifies the Dame of an alias for an epression in the "ubEuery.
WITH CHEC< OPTION :
o "pecifies that only ro%s that %ould be retrieved by the "ubEuery can be ID"'&T'-(
@P-AT'- or -'L'T'-.
CONSTRAINT :
o ConstraintRDame specifies the name of the ,ITI CI'C; Option or &'A- ODL?
Constraint.
WITH READ ONL! :
o "pecifies that ro%s my only read from the base tables.
RESTRICTIONS :
Oragle 10 g Material

92
I# a $ie% has ID"'&T'- of Triggers( then only $ie% created on it must have ID"T'A- O<
Triggers( even if the $ie%s are Inherantly @pdatable.
A* Alias cannot be specified %hen creating an O*='CT $ie%.
T!PES OF /IEWS :
S"$pe /"eBs :
o ,hich contains a "ubEuery that retrieves from one base table.
C%$pe6 /"eBs :
o Contain a "ubEuery that can perform any of these actions.
o &etrieving from multiple base tables.
o )roups ro%s using a )&O@P *? or -I"TIDCT Clause.
o Contains a function call.
S"$pe /"eBs :
o These $ie%s access one *ase Table.
E6a$pe :
SI E Create $I', 'mployees As
SELECT 'mpno Y I- Dumber Y (.
'name Dame (
"al Y *asic "alary Y (
=ob -esignation
<rom 'mp K
Seect"*g Data Fr%$ A /"eB :
SI E "elect Dame( =ob <rom 'mployees K
"Fl P "elect Y I- Dumber Y ( 'name( Y *asic "alary Y H /0
<rom 'mployees K
"Fl P "elect Y I- Dumber Y( Dame (
ToRChar ( Y *asic "alary Y ( S99(99(999.99S ! 6onthly
Y *asic "alary Y H /0 Annual
<rom 'mployees
,here Y *asic "alary Y P 04>> K
C%$pe6 /"eBs :
o Comple $ie%s can contain "ubEueries.
o The "ubEuery can contain( Eueries
That &etrieve from multiple *ase Tables
)roup &o%s using a )&O@P *? or -I"TIDCT Clause.
Oragle 10 g Material

93
Containg a <unctional Call .
E6a$pe :
SI E Create $I', 'mpInfo As
"elect '.'mpno 'mployeeI-(
'.'name Dame (
-.-eptno -epartmentI- (
-.-name -eparatmentDame
<rom 'mp '( -ept -
,here -.-eptno \ '.-eptno
O&-'& *? -.-eptno K
SI E Create $I', 'mp)rades As
"elect '.'name Dame (
'."al *asic (
".)rade )rade
<rom 'mp ' ( "algrade "
,here '."al *'T,''D
".Lo"al AD- ".Ii"al
O&-'& *? ".)rade K
SI E Create or &eplace $I', 'mp6anagers A"
"elect &o%Dum "erialDo (
Initcap ( '.'name ! [[ L ,or+s @nder L [[
6.'name Y 'mployee And 6anagers Y
<rom 'mp '( 'mp 6
,here '.6gr \ 6.'mpno K
SI E Create or &eplace $I', 'mpAccounts As
"elect 'name( -eptno ( "al 6onthly (
"al H /0 Annual
<rom 'mp
,here -eptno \ ( "elect -eptno
<rom -ept
,here -name \ LACCO@DTID)S !
O&-'& *? Annual K
SI E Create or &eplace $I', Cum"um As
"elect *."al ( "um ( A."al ! As CumR"al
<rom 'mp A( 'mp *
,here A.&o%I- Q \ *.&o%I-
)&O@P *? *.&o%I- ( *."al K
SI E Create or &eplace $I', Org-esignations As
"elect =ob <rom 'mp ,here -eptno \ />
@DIOD
"elect =ob <rom 'mp ,here -eptno ID( 0>( 1> ! K
Oragle 10 g Material

94
/IEWS "* Data D"ct"%*ar& :
Once the $ie% has been created( %e can Fuery upon the -ATA -ICTIODA&? table called
@"'&R$I'," to see the Dame and definiton of the $ie%.
The tet of the "'L'CT statement that constitures the $I', is stroed in a LOD) Column
Data Access Us"*g /"eBs :
The "teps or operations performed by the Oracle "erver( %hen data is accessed using a $I',
are
o Retrieves the $I', definition from the -ata -ictionary table @"'&R$I',".
o Chec+s the Access privileges for the vie% base table.
o Converts the vie% Fuery into an eFuivalent opration on the underlying base table or
tables.
M%3"#&"*g A /"eB :
O& &'PLAC' Option is used to 6odify an eisting $I', %ith a ne% definition.
A $I', can be altered %ithout -ropping( recreating( and regranting ob#ect privileges.
The assigned column Aliases in the C&'AT' $I', Clause( are listed in the same order as the
columns in the "ubEuery.
Creat"*g /"eBs B"t@ C%5$*s Decarat"%*s :
,hen a $I', is being crated( %e can specify the Dames of the Columns( that it can pro#ect(
along %ith the $I',Ss definition.
E6a$pe :
SI E Create $I', -ept"al"ummary
( -epartmentDame F
6inimum"alary (
6a"alary (
Average"alary(
"alary"um
! As
"elect -.-name( 6in ('."al ! ( 6a ( '."al ! ( Avg ( '."al ! ( "um ( '."al !
<rom 'mp '( -ept -
,here '.-eptno \ -.-eptno
)roup *y -.-name K
SI E Create $I', Insert-ept/> As
"elect H <rom 'mp
,here -eptno \ /> K
I*sert"*g T@e T%ta Data %# E$p%&ees Fr%$ Depart$e*t 10 Us"*g /"eB :
SI E Create Table -ept/>
As "elect H <rom Insert-ept/> K
SI E Create Table 'mp)rades ( 'mployee (
Oragle 10 g Material

95
-esignation (
*asic"alary (
)rade
! As
"elect 'name( =ob( "al( )rade <rom 'mp '( "al)rade "
,here '."al *'T,''D ".Lo"al AD- ".Ii"al K
DROPPING A /IEW :
The -&OP $I', statement is used to &emove a $ie% permanently.
-ropping a $ie% has no affect on the tables upon %hich the $ie% is created.
$I'," or APPLICATIOD" based on deleted $ie%s become invalid.
,e need -&OP AD? $I', privilege to remove the $I',".
S&*ta6 :
DROP /IEW /"eBNa$e H
E6a$pe :
DROP /IEW I*sertDept H
INLINE /IEWS :
An IDLID' $I', is a "ubEuery %ith an ALIA" ( Called as CO&&'LATIOD DA6' !( that can be
used %ithin a "EL "tatement.
An IDLID' $I', is similar to using a named "ubEuery in the <&O6 Clause of the 6ain Euery.
An IDLID' $I',' is not a "chema Ob#ect.
An IDLID' $I', in the <&O6 Clause of a "'L'CT statement defining -ata "ource for the
"'L'CT statement.
E6a$pes :
SI E Select '/.'name ( '/."al ( '/.-eptno( '0.6a"al
<rom 'mp '/ ( ( "elect -eptno( 6a ( "al ! 6a"al
<rom 'mp )&O@P *? -eptno ! '0
,here '/.-eptno \ '0.-eptno
AD-
'/."al Q '0.6a"al K
R5es #%r Per#%r$"*g DML Operat"%*s O* A /"eB :
-6L operatons can be perfomed upon a table through $I',.
A ro% can be removed from a $I', unless it contains.
o )&O@P <unction
o A )&O@P *y Clause
o The &O,D@6 P'"@-O COL@6D
o The -I"TIDCT ;ey ,ord
o The Columns defined by 'pressions.
Oragle 10 g Material

96
-ata can be added through a vie%( unless it contains any of the above rules and there are DOT
D@LL Columns( and %ithout -'<A@LT $alue.
E6a$pe :
SI E Create $ie% Insert-ept ( -eptI-(
DeptNa$e F
Pace
) AS
Select -eptno( -name( Loc <rom -ept K
Us"*g B"t@ CHEC< Opt"%* Ca5se :
To ensure that -6L on the vie% stays %ithin the domain of the $I', by using the ,ITI CI'C;
option Clause.
$I'," ma+e it possible to perform &eferential Integrity Chec+s.
@sing $I'," %e can enforece constraints at database level.
@sing $I'," %e can protect the -ATA IDT')&IT?( *ut the use is very limited.
The ,ITI CI'C; Option Clause specifies that ID"'&T"( & @P-AT'" performed through the
$aI', are not allo%ed to create ro%s( that the $I', cannot "'L'CT.
$I'," allo% Integrity Constraits and data validation chec+ to be enforced on data being Inserted
or @pdated.
E6a$pe :
SI E Create Or &eplace $I', '-ept1>
AS
"elect H <rom 'mp ,here -eptno \ 1>
,ITI CI'C; OPTIOD COD"T&AIDT '-ept1>Ch+$ie% K
"Fl P Create Or &eplace $I', 'manager
AS
"elect H <rom 'mp ,here =ob \ L6ADA)'&S
,ITI CI'C; OPTIOD COD"T&AIDT 'manager$ie% K
App&"*g W"t@ READ ONL! OPTION :
*y Adding the ,ITI &'A- ODL? option %e can ensure that no -6L operations are eecuted.
An attempt to perform a -6L operation results in O&ACL' "'&$'& '&&O&.
E6a$pe :
SI E Create Or &eplace $ie% 'dpet&ead
( 'mpId( Dame( -esignation !
AS
"elect 'mpno( 'name( =ob <rom 'mp
,here -eptno \ 0>
,ITI &'A- ODL? K
/IEW CONSTRAINT :
In practicality Oracle does not enforce $ie% constrains( but operations on $I'," are sub#ect to
the IDT')&IT? COD"T&AIDT" defined on the underlying base tables.
Oragle 10 g Material

97
COD"T&AIDT" on $I'," can be enforced through COD"T&AIDT" n base tables.
Restr"ct"%*s O* /"eB C%*sta"*ts :
T@e $I', Constraints area subset of TA*L' COD"TAIDT".
Only @DIE@'( P&I6A&? ;'? And <O&'I)D ;'? COD"T&AIDT" can be specified on
$I','".
The CI'C; COD"T&AIDT is imposed using ,ITI CI'C; OPTIOD.
As $I', COD"T&AIDT" are not enforced directly( %e cannot specify IDTIALL? -'<'&&'- or
-'<&''A*L'.
$I', CODT&AIDT" are supported only in -I"A*L' DO$ALI-AT' mode.
INITIALL! DEFERRED :
It Indicated that Oracle should chec+ this COD"T&AIDT at the end of "@*"'E@'DT
transactions.
N%te :
INTIALL! DEFERRED "s not valied if the COD"T&AIDT is declared to be
DOT-'<'&&A*L'
DOT -'<&''A*L' COD"T&AIDT is A@TO6ATICALL? IDITIALL? I66'-IAT' and can
never be IDITIALL? -'<'&&'-.
/ALIDATE + NOT/ALIDATE :
The *ehavior of $ALI-AT' and DO$ALI-AT' al%ays depends on %hether the COD"T&AIDT is
'DA*L'- C -I"A*L'-( either 'APLICITL? or by -'<A@LT.
REL! CLAUSE :
&'L? and DO&'L? are valid only %hen %e are modifying an eisting COD"T&IDT.
REL! :
It activates an eisting COD"T&AIDT is DO$ALI-AT' mode for Fuery re%rite( in an
unenforced Fuery re%rite Integrity mode.
DEFERRA'LE Ca5se :
The -'<'&&A*L' and DOT-'<'&&A*L' parameters indicate %hether or not( in the
subseFuent transaction( constraint chec+ing can be deferred until the end of the transction using
the "'T COD"T&AIDT statement.
The default is DOT -'<'&&A*L'.
NOTDEFERRA'LE :
It indicates that in subseFuent transctions( %ee cannot use the "'T COD"T&AIDT(s! Clause
to defer chec+ing the COD"T&AIDT @DTIL the transaction is committed.
The chec+ing of a DOT-'<'&&A*L' COD"T&AIDT can Dever be -'<'&&'- to the end
of the transactions.
Oragle 10 g Material

98
DEFERRA'LE :
It indicated that in subseFuent transctions %e can use the "'T COD"T&AIDT(s! Clause to
defer chec+ing the COD"T&AIDT until after the transaction is committed.
The setting is effect lets the user to disable the COD"T&AIDT temporarily %hile ma+ing
changes to the database that might violate the COD"T&AIDT until all the changes are
complete.
To alter the COD"T&AIDT" -eferability( %e must -&OP the COD"T&AIDT and recereate it.
Restr"ct"%* O* T@e DEFERRA'LE Ca5se :
The parameters cannot be specified for a $I', COD"T&AIDT.
INITIALL! Ca5se :
It established the default chec+ing behavior for COD"T&AIDT" that are -'<'&&A*L'.
The IDITIALL? setting can be overridden by a "'T COD"T&AIDT(s! statement in a
subseFuent Transaction.
I*"t"a& I$$e3"ate :
It indicated that Oracle should chec+ this COD"T&AIDT at the end of each
subseFuent "EL statement .
It "s t@e 3e#a5t sett"*g:
,e cannot specify the @singRIndesRClause the 'ceptionsRClause or the OD
-'L'T' Clause of the &eferenceRClause.
$I', COD"T&AIDT" cannot be defined on attribute of an ob#ect column.
"Fl P Create $ie% 'mp"alary
(
'mpI-( 'name( 'mail @DIE@' &'L? -I"A*L' DO$ALI-AT' (
Constraint I-RP; P&I6A&? ;'? ( 'mpI- ! &'L? -I"A*L'
DO$ALI-AT' ! A"
"elect 'mpno( 'name( 'mail <rom 'mp K
Constraint "tate specifies ho% and %hen O&ACL' should enforce the
COD"T&AIDT.
The constraint state clauses can be specified in any order( but each state can be
specified only once.
Restr"ct"%* %* REL! Ca5se :
,e cannot set a DOT D@LL COD"T&AIDT to &'L?.
E6a$pe :
SI E Create Table )ames ( "cores Dumber Chec+ ( "cores P \ > ! ! K
N%te : I* the above case the default constraint is DOT -'<'&&A*L'
IDTITALL? I66'-IAT' constraint CI'C;.
"Fl P Create Table )ames/
(
"cores Dumber (
Oragle 10 g Material

99
Constraint "coRDumR@nF @DIE@' ( "cores !
IDITIALL? -'<'&&'- -'<'&&A*L' ! K
WOR<ING WITH INDE1ES :
INDE1 :
It is a "chema ob#ect %hich contains an entry for each value that appears in the indeed
column(s! of the table or cluster.
It provides -irect( <ast Access to &o%s.
T&pes %# INDE1ES :
NORMAL INDE1ES :
o They are default Indees.
o They are created %ith *GTree principle.
'ITMAP INDE1ES :
o They store &o%I-Ss associated %ith a +ey value as a *itmap.
PARTITIONED INDE1ES :
o They contain partitions containing an entry for each value that appears in the Indeed
columns of the table.
FUNCTION 'ASED INDE1ES :
o They are based on epressions.
o 'nable Fuery to evaluate value retuned by an epression.
DOMAIN INDE1ES :
o They are ID-'A'" %hich are instances of an application specific inde of type
Indetype.
PRE RE,UISITES :
The table or CL@"T'& to be ID-'A'- must be in the O%n "chema.
ID-'A ob#ect parivilege should be available on the table to ID-'A'-.
Cerate any inde "?"T'6 privilege must be available.
@DLI6IT'- TA*L'"PAC' system privilege or "PAC' E@OTA on TA*L' "PAC'" must be
available.
<or -O6AID ID-'A'"( 'A'C@T' ob#ect privilege on the ID-'AT?P' should be available.
<or <@CTIOD *A"'- ID-'A'"( the <@DCTIOD used for Indeing must be mar+ed as
-'T'&6IDI"TIC.
RESTRICTIONS :
If ID-'A is locally partitioned then the TA*L' msut be partitioned.
If the TA*L' is ID-'A O&A)ADIM'-( then a "'COD-A&? ID-'A is created.
Oragle 10 g Material

100
If TA*L' is T'6PO&A&? TA*L'( then ID-'A is also T'6PO&A&? %ith the same scope( as
that of the TA*L'.
Restr"ct"%* O* INDE1 C%5$*s :
An ID-'A cannot be created on Columns or ATT&I*@T'" %hose type is ..
o USER DEFINED
o LONG
o LONG RAW
o LO'
o REF
An ID-'A on &'< tyhpe columns Or attributes have to be declared %ith a "COP' Clause.
S"$pe I*3e6 E6a$pe :
SI E Create ID-'A 'mp'mpnoI- On 'mp ( 'mpno ! K
"Fl P Create ID-'A -ept-eptnoI- On -ept ( -eptno ! K
S&*ta6 :
Create T @DIE@' W ID-'A IndeDame
or
T *IT6AP W OD
TableDame ( ColumnRname T ( ColumnDame ..W !
TA*L'"PAC' Table"paceDame K
U*"I5e :
- Specify @DIE@' to indicate that the value of the column or columns upon
%hich the Inde is based must be @DIE@'.
Restr"ct"%*s :

- ,e cannot specify both @DIE@'and *IT6AP.
- UNI,UE Cannot be specified for a domain Inde.
'ITMAP :
Specify *IT6AP to indicate that ID-'A has to be careated %ith a *IT6AP for each -I"TIDCT
;'?.
*IT6AP Indees store the &o%I-Ss associated %ith a +ey value as a *IT6AP.
'ach *IT in the *IT6AP corresponds to a possible &o%I-.
Restr"ct"%*s :
*IT6AP cannot be specified %hen creating a global partitioned Inde.
*IT6AP "'COD-A&? ID-'A canot be created on an ID-'A O&)ADIM'- TA*L' unless the
ID-'A O&)ADIM'- table has a mapping table Associated %ith it.
Oragle 10 g Material

101
,e cannot specify both @DIE@' and *IT6AP.
*IT6AP cannot be specified for a -O6AID ID-'A.
CLUTER 7 INDE1 7 CLAUSE :
It identifies the CL@"T'& for %hich a CL@"T'& ID-'A has to be created.
CL@"T'& ID-'A Cannot be created for a IA"I CL@"T'&.
TA'LE 7 INDE1 7 CLAUSE :
It identifies the TA*L' upon %hich the ID-'A is being defined.
An ID-'A on a D'"T'- TA*L' COL@6D can be created by creating the ID-'A on the
D'"T'- storage Table.
CREATING UNI,UE INDE1ES :
SI E Create @DIE@' ID-'A 'mp'mailI-@DE OD 'mp ( 'mail ! K
"Fl P Create @DIE@' ID-'A "tuPhnoI-@DE OD "tudent ( PhoneDo ! K
CRATING COMPOSITE INDE1ES :
CO6PO"IT' ID-'A is an ID-'A on multiple Columns.
"Fl P Create ID-'A "tudR<nameRLnameRId On "tudent ( <name( Lname ! K
"Fl P Create ID-'A 'mpRDameRPlaceRI-A OD 'mp ( 'name ( "treetDame ! K
CREATING FUNCTION 'ASED INDE1ES :
These are indees based on epressions.
The Inde 'presions are *uilt from table columns( contancts "EL <unctions and @ser -efined
<unction.
<unctin based Indees defined %ith the @PP'& ( ColumnRDame ! or LO,'& ( ColumnRDame !
allo% case Insensitive "earches.
To ensure that Oracle uses the Inde rather than performing a full table scan( %e shoulKd be sure
that the value of the <unctins is not DOT D@LL in subseFuent Eueries.
Oracle treats ID-'A'" %ith columns mar+ed -'"C as <unctins *ased Indees.
E6a$pe :
SI E Create ID-'A 'mp@pperCaseI- OD
'mp ( @PP'& ( 'name ! ! K
Dote B The <unction *ased Indees are used only %hen the Euery "tatement is
eecuted through the specified function.
'ITMAP INDE1ING :
These Indees are used to tune Eueries that use non selective columns in their limiting
conditions
*IT6AP ID-'A'" should be used only %hen the data is infereFuently updated.
Oragle 10 g Material

102
*IT6PAT ID-'A'" add to the cost of all data manipulation transaction against the tables they
ID-'A.
The O&ACL' OPTI6IM'& can dynamically convert *IT6AP ID-'A'" entries to &O,I-Ss
during the Fuery processing.
E6a$pes :
SI E Create *IT6AP ID-'A 'mp*it6ap=ob OD 'mp ( =ob ! K
L"$"tat"%*s :
'ITMAP INDE1ES should not be used for tables involved in ODLID' T&AD"ACTIOD
P&OC'""ID) applications due to the Internal 6echanisms Oracle user to maintain them.
&estricted %ith usage to tables involved in *atch transactions.
Spec"#"cat"%* %# A* INDE1 :
ID-'A I" A "chema Ob#ect.
ID-'A is used by the O&ACL' "'&$'& to speed up &etrieval of ro%s by using a Poiter.
ID-'A &educes the -is+ I C O by using rapid path Access method to locate the data Fuic+ly.
ID-'A'Ss are Independent of the table it ID-'A'"( both logically and physically.
ID-'A is used and maintained automatically by the O&ACL' "'&$'&.
ID-'A'Ss can be created or -ropped at any time and have no effect on the base tables or other
ID-'A'".
,hen a table is droopped( the Corresponding ID-'A'" are also dropped.
INDE1 Creat"%* "s %# TB% T&pes :
1 ) A5t%$at"c 8 ) Ma*5a
One Table more than one Inde can be created( *ut this does not mean that( more the Indees
lead to more faster performance.
'ach -6L operations that is committed on a table %ith Indees means that the Indees must be
updated.
WHEN TO CREATE AN INDE1 :
The Column is used freFuently in the ,I'&' Clause or ina =oin Condition.
The Column contains a %ide range of values.
The column contains a large number of D@LL values.
T%o or more columns are freFuently used together in a ,I'&' clause or =oin Condition.
The table is large and most Fueries are epected to retrieve less than 0 J 3 _ of the ro%s.
WHEN NOT TO CREATE AN INDE1 :
The table is too small
The columns are not often used as condition in the Fuery
6ost Fueries are epected to retrieve morethan 0 J 3 _ of the ro%s.
The table is updated freFuently
The indees can be confirmed form the @"'&RID-'A'" -ata -ictionarly.
The Column that are involved in an ID-'A by Fuerying @"'&RID-RCOL@6D".
To -&OP an ID-'A -&OP command is used
Oragle 10 g Material

103
-&OP ID-'A ID-'ARDA6' K
DATA'ASE SECURIT! :
The "'C@&IT? upon databases is applied by defining and describing separate "CI'6A
O*='CT" and )&ADTID) reFuired P&I$IL')'" upon them.
In O&ACL' the Privileges can be ranted as %ell as &evolved.
GRANT COMMAND :
S&*ta6 :
GRANT C P&I$ILA)'" DA6' P ( (((((((
ON
Q Ob#ectRDame P To Q @ser Dame P
)&ADT command is used %hen %e %aFnt the database to be shared %ith other users.
The other users are )&ADT'- %ith certain type of rights.
)&A*T cinnabd cab be issued not only on TA*L' O*='CT( but also on $I',"( "?DOD?6"(
ID-'A'"( "'E@'DC'" etc.(.
E6a$pe :
SI E )&ADT "'L'CT OD '6P TO 'D-@"'&" K
"Fl P )&ADT ID"'&T( "'L'CT( -'L'T' OD '6P TO OP'&ATO&" K
"Fl P )&ADT ID"'&T ( 'mpno( 'name( =ob ! OD 'mp To 'nd@sers K
RE/O<E COMMAND :
S&*ta6 :
RE/O<E C P&I$IL')' DA6' P ( ... OD Q O*='CT DA6' . TO Q @"'& DA6' .
&'$O;' is usede %hen %e %ant one database to stop sharing the information %ith other users.
&'$O;' privileges is assigned not only on table on#ect( but also $ie%s( "ynonyms( Indees
etc.(
E6a$pe :
SI E &'$O;' ID"'&T ( -'L'T' OD '6P <&O6 Operators K
T&pes %# Pr"="eges :
S&ste$ Pr"="eges :
o They allo% a user to perform certain actions %ithin the database.
Ob2ect Pr"="eges :
o An Ob#ect Privilege allo%s a user to perform cetain actions on database ob#ects.
C@ec?"*g t@e Ob2ect Pr"="eges Gra*te3 :
Oragle 10 g Material

104
The schema ob#ect that stores the information about the privileges is
o USERQTA'QPRI/QMADE
The Column of @"'&RTA*RP&I$R6A-'
o GRANTEE TA'LEQNAME
o GRANTOR PRI/ILEGE
o GRANTA'LE HIERARCH!
C@ec?"*g Obect Pr"="eges Rece"=e3 :
The schema ob#ect that stores the information about the P&I$IL')'" received are.
o USERQTA'QPRI/SQRECD
The column of @"'&RTA*RP&I$"R&'C- are ...
o OWNER TA'LEQNAME
o GRANTOR PRI/ILEGE
o GRANTA'LE HIERARCH!
MA<ING USE OF O'.ECT PRI/ILEGES :
Once a particular @"'& has benn granted an ob#ect privilege( the specific @"'& can perform the
Tas+s.
"teps to be performed
o Connect to the reFuired user.
o 'ecute the reFuire "EL "tatement using the ob#ect Iierarchy.
E6a$pe :
SI E CODD'CT "ATI"I C ?'LLAD;I
"Fl P "'L'CT H <rom "COTT.'6P K
WOR<ING WITH ROLES :
A &OL' is a group of P&I$IL')'" that can be assigned to a @"'& or another &OL'.
A3=a*tages :
&ather than assigning Privileges one at a time directly to a @"'&( %e can C&'AT' a &OL'(
assign P&I$IL')'" to that &OL'( and then )&ADT that &OL' to multiple @"'&" and &OL'".
Oragle 10 g Material

105
,hen you A-- or -'L'T' a P&I$IL')' from a &OL'( all @"'&" and &OL'" assigned that
automatically receive or lose that P&I$IL')'
,e can Assign multiple roles to a @"'& or &OL'.
A &OL' can be assigned %ith a Pass%ord.
ROLETs CREATION :
To C&'AT' a &OL' %e should have the C&'AT' &OL' "?"T'6 privilege.
The "teps in Implementing the &OL'"
o ROLE CREATION
o GRANTING PRI/ILEGES TO ROLES
o GRANTING ROLES TO USERS %r O'.ECTS
S&*ta6 :
C&'AT' &OL' Q &ole Dame P I-'DTI<I'- *? Q Pass%ord P W K
E6a$pe :
SI E C&'AT' &OL' "alesR6angers I-'DTI<I'- *? "alesAudit K
Gra*t"*g Pr"="eges t% ROLE :
The &OL'" are granted privileges using the )&ADT statement.
A &OL' can be granted both "?"T'6 as %ell as O*='CT privileges
E6a$pe :
SI E )&ADT "'L'CT( ID"'&T( @P-AT'( -'L'T'
OD '6P TO "alesR6anager K

Gra*t"*g A ROLE T% a USER :
SI E )&ADT "alesR6anager to "cott K
Gra*t"*g M5t"pe ROLES t% A*%t@er R%e :
SI E )&ADT &OL'/( &OL'0 ( ... TO Q TA&)'TR&OL'RDA6' P K
C@ec?"*g ROLES Gra*te3 t% a USER :
The "CI'6A O*='CT @"'&R&OL'RP&I$" specifies the roles granted to a @"'&.
The "CI'6A O*='CT @"'&R&OL'RP&I$" contains the follo%ing columns.
USERNAME GRANTEDQROLE
ADMINQOPTION DEFAULTQROLE
OSQGRANTED
C@ec?"*g S&ste$ Pr"="eges Gra*te3 t% A ROLE :
Oragle 10 g Material

106
The "chema Ob#ect &OL'R"?"GP&I$" specifies the "?"T'6 P&I$IL')'" )ranted to a
&OL'.
The columns present are..
H ROLE - PRI/ILEGE - ADMINQOPTION
C@ec?"*g Ob2ect Pr"="eges Gra*te3 t% a R%e :
The "chema Ob#ect ( &OL'RTA*RP&I$" specifies the O*='CT P&I$IL')'" granted to a
&OL'.
The columns present are ..
ROLE OWNER TA'LEQNAME COLUMNQNAME
PRI/ILEGE GRANTA'LE
Re=%?"*g a ROLE :
SI E RE/O<E "alesR6anagers <rom "cott K
Re=%?"*g Pr"="eges Fr%$ A ROLE :
SI E RE/O<E ALL OD 'mp <rom "alesR6anagers K
Dr%pp"*g A R%e :
S&*ta6 : DROP ROLE C &oleRDame P
E6a$pe : DROP ROLE "alesR6anagers K
WOR<ING WITH S!NON!MS :
A "ynonym is schema ob#ect( %hich acts as an alternate name for an eisting ob#ect.
*y using a "ynonym( %e can avoid the entry of the schema Dame( %hen referencing upon
ob#ects that belong to other schema.
The C&'AT' "ynonym Privileges is necessary to eecute the creation of a "ynonym.
S&*ta6 :
Create S&*%*&$ C "ynonym Dame P FOR C "chemaDame P. Q Ob#ectDame P K
E6a$pe :
Create S&*%*&$ 'mpInfo <O& "COTT.'6P K
S&*%*&$ T&pes :
The "ynonyms are practically of t%o types
PRI/ATE S!NON!M PU'LIC S!NON!M
Oragle 10 g Material

107
,e should have C&'AT' P@*LIC "?DOD?6 privilege( and it can be accessed by all @"'&".
E6a$pe :
SI E Create PU'LIC S!NON!M 'mpInfo <O& SCOTT:'mp K
OLAP FEATURES IN ORACLE :
Some features for Euery processing is O&ACL' include the use of ODLID' ADAL?TICAL
P&OC'""ID) ( OLAP ! upon the -ata base.
OLAP features are useful for -ATA ,A&'IO@"ID) and -ATA 6A&T applications.
The OLAP operations are performance enhancements.
TOPQN ,UERIES
GROUP '!
CU'E
ROLL UP
ROLL UP :
It is a )&O@P *? operation and is used to produce subtotals at any level of the
Aggregation.
The generated substotals Y &OLL @P Y to produce grant total .
The totaling is based on a one dimensional -ata Iierarchy of grouped Information.
S&*ta6 :
GROUP '! ROLLUP ( C%5$*1F C%5$*8 G:)
Ge*era I$pe$e*tat"%* :
E6a$pe :
SI E "elect -eptno( "um ( "al ! <rom 'mp
Gr%5p '& R%Up ( -eptno ! K
SI E "elect =ob( "um ( "al ! <rom 'mp
Gr%5p '& R%Up ( =ob ! K
Pass"*g M5t"pe C%5$*s T% R%Up :
,hen multiple columns are passed to &OLL@P( the &OLL @P ( groups the ro%s into bloc+s %ith
the same column values.
E6a$pe :
SI E "elect -eptno( =ob( "um ( "al ! "alary <rom 'mp
Gr%5p '& R%Up ( -eptno( =ob ! K
SI E "elect =ob( -eptno( "um ( "al ! "alary <rom 'mp
Gr%5p '& R%Up ( =ob( -eptno ! K
SI E "elect =ob( -eptno( Avg ( "al ! Average <rom 'mp
Gr%5p '& R%Up ( =ob( -eptno ! K
Oragle 10 g Material

108
N%te :
o NULL $alues in the output of &OLL@P operations typically mean that the ro% contains
subtotal or grant total information.
o @se the Dvl ( ! <unction for proper meaning.
CU'E :
It is an etension similar to &OLL@P.
C@*' allo%s to ta+e a specified set of grouping columns and crate sub totals for all possible
cominatins of them.
The result of C@*' is a summary that sho%s subtotals for every cominiation of columns or
epressions in the )&O@P *? clause .
The implementation of C@*' is also called as nGdimensional C&O""RTA*@LATIOD.
E6a$pe :
SI E -eptno( =ob( "um ( "al ! "alary <rom 'mp
Gr%5p '& CU'E ( -eptno( =ob ! K
SI E =ob( -eptno ( "um ( "al ! "alary <rom 'mp
Gr%5p '& CU'E ( =ob( -eptno ! K
App&"*g Gr%5p"*g ( ) F5*ct"%* :
T@e )&O@PID) ( ! <unction accepts a column and returns > or /.
)&O@PID) ( ! <unction returns / %hen the column value is D@LL( and returns > %hen the
column value is DOT D@LL.
)&O@PID) ( ! <unction is used only upon Fueries that use &OLL@P or C@*'.
)&O@PID) ( ! <unction is useful %hen %e %ant to display a value %hen a D@LL %ould other%ise
be returned.
E6a$pe :
SI E "elect )&O@PID) ( -eptno ! ( -eptno ( "um ( "al ! <rom 'mp
)roup *y &oll@p ( -eptno ! K
SI E "elect )&O@PID) ( =ob ! ( =ob ( "um ( "al ! <rom 'mp
)roup *y &oll@p ( =ob ! K
DECODE F5*ct"%* :
It is a single ro% function.
The function %or+s on the same principle as the if J then J else.
,e can pass a variable number of values into the call of the -'CO-'( ! function.
The first item is al%ays the name of the column that need to be -'CO-'-.
Once all valueGsubstitute paris have been defined( %e can optionally specify a -'<A@LT $alue.
S&*ta6 :
"elect -'CO-' ( ColumnDame( $alue /( "ubstitute/( $alue 0(
Oragle 10 g Material

109
"ubstitute0( ... &eturn -efault ! <rom TableDame K
The function has no restrictuon on the input and output data type.
E6a$pe :
SI E "elect 'name( -'CO-' ( -eptno( /> ( LACCO@DTID)S (
0> ( L&'"'A&CIS (
1> ( L"AL'"S (
3> ( LOP'&ATIOD"S (
LOTI'&S ! -epartments
<rom 'mp O&-'& *? -epartments K
SI E "elect 'name [[ L Ta+es Care of L [[
DECODE ( =ob( LADAL?"TS ( LADAL?"I"S (
NCL'&;S ( L<I&ID)S (
N6ADA)'&S ( L6ADA)ID)S (
LP&'"I-'DTS ( LA-6IDI"T&ATIODS (
L"AL'"6ADS ( L"AL'"S ! &esponsibilities
<rom 'mp K
SI E "elect 'name( =ob( -'CO-' ( =ob (
LCL'&;S ( L'S (
L"AL'"6ADS ( L-S (
LADAL?"TS ( LCS (
L6ADA)'&S ( L*S (
LP&'"I-'DTS ( LAS (
LOS ! )&A-'"
<&O6 '6P O&-'& *? =O* K

SI E "elect Count ( H ! Total (
"um ( -'CO-' ( ToRChar ( Iiredate( L????S ! ( /98>(/(> ! ! Y/98>Z (
"um ( -'CO-' ( ToRChar ( Iiredate( L????S ! ( /98/(/(> ! ! Y/98/Z (
"um ( -'CO-' ( ToRChar ( Iiredate( L????S ! ( /980(/(> ! ! Y/980Z (
"um ( -'CO-' ( ToRChar ( Iiredate( L????S ! ( /981(/(> ! ! Y/981Z
<rom 'mp K
SI E "elect =ob( "al( -'CO-' ( =ob( LADAL?"TS ( "al H /./ (
LCL'&;S ( "al H /./4 (
L6ADA)'&S ( "al H /.0 ( "al
! Y&'$I"'- "ALA&? Y
<rom 'mp K
SI E "elect Count ( -'CO-' ( -eptno( /> ( L H L ( D@LL ! ! -/>RCO@DT (
"um( -'CO-' ( -eptno( /> ( "al ( D@LL ! ! -/>R"al (
Count ( -'CO-' ( -eptno( 0> ( L H L ( D@LL ! ! -0>RCO@DT (
"um( -'CO-' ( -eptno( 0> ( "al ( D@LL ! ! -0>R"al (
Count ( -'CO-' ( -eptno( 1> ( L H L ( D@LL ! ! -1>RCO@DT (
"um( -'CO-' ( -eptno( 1> ( "al ( D@LL ! ! -1>R"al
<rom 'mp K
SI E "elect =ob(
"um ( -'CO-' ( -eptno ( /> ( "al ! ! Y -eptno /> Y (
"um ( -'CO-' ( -eptno ( 0> ( "al ! ! Y -eptno 0> Y (
"um ( -'CO-' ( -eptno ( 1> ( "al ! ! Y -eptno 1> Y (
Oragle 10 g Material

110
"um ( "al ! YTotal Y
<rom 'mp )&O@P *? =ob K
Gr%5p"*g W"t@ DECODE ( ) :
The -'CO-' ( ! <unction can be used to convert / and > returned through )&O@PID) ( ! into a
meaningful outpout.
E6a$pes :
SI E "elect -'CO-' ( )&O@PID) ( -eptno ! (
/( LAll -epartmentsS ( -eptno ! -epartments (
"um ( "al ! <rom 'mp )roup *y &oll@p ( -eptno ! K
SI E "elect -'CO-' ( )&O@PID) ( =ob ! (
/( LAll -esignationsS ( =ob ! -esignations (
"um ( "al ! <rom 'mp )roup *y &oll@p ( =ob ! K
DECODE ( ) a*3 GROUPING ( ) #%r C%*=ert"*g M5t"pe C%5$* /a5es :
E6a$pe :
SI E "elect -'CO-' ( )&O@PID) ( -eptno ! (
/ ( LAll -epartmentsS ( -eptno ! -epartments (
-'CO-' ( )&O@PID) ( =ob ! (
/( LAll -esignations L ( =ob ! -esignations (
"um ( "al ! <rom 'mp
)roup *y &oll@p ( -eptno ( =ob ! K
GROUPING ( ) W"t@ DECODE ( ) a*3 CU'E :
E6a$pe :
SI E "elect -'CO-' ( )&O@PID) ( -eptno ! (
/ ( LAll -epartmentsS ( -eptno ! -epartments (
-'CO-' ( )&O@PID) ( =ob ! (
/( LAll -esignations L ( =ob ! -esignations (
"um ( "al ! <rom 'mp
)roup *y C@*' ( -eptno ( =ob ! K
App&"*g Gr%5p"*g SETs Ca5se :
The )&O@PID) "'T" Clause is used to get the "ubtotal ro%s.
E6a$pe :
SI E "elect -eptno( =ob( "um ( "al ! <rom 'mp
)roup *y )&O@PID) "'T" ( -eptno( =ob ! K
Oragle 10 g Material

111
W%r?"*g B"t@ CASE E6press"%* :
The CA"' 'pression can be used to perform if J then J else logic in "EL.
CA"' is similar to -'CO-' byut is AD"I J Compliant.
CA"' 'pressions are of t%o types
o S"$pe CASE E6press"%*s
o Searc@e3 CASE E6press"%*s:
S"$pe CASE E6press"%*s :
These 'pressions are used to determine the retuned value.
S&*ta6 :
CASE Searc@QE6pr
WHEN E6pr 1 THEN Res5t 1
WHEN E6pr 8 THEN Res5t 8
:
:
ELSE De#a5tQRes5t
END H
E6a$pe :
"Fl P "elect 'name( -eptno
CA"' -eptno
,I'D /> TI'D LACCO@DT"S
,I'D 0> TI'D L&'"'A&CIS
,I'D 1> TI'D L"AL'"S
,I'D 3> TI'D LOP'&ATIOD"S
'L"' LDOT <O@D-L
'D-
<rom 'mp K
SEARCHED CASE E6press"%*s :
They use conditions to determine the returned value.
S&*ta6 :
CASE
WHEN C%*3"t"%* 1 THEN Res5t 1
WHEN C%*3"t"%* 8 THEN Res5t 8
:
WHEN C%*3"t"%* * THEN Res5t *
ELSE De#a5tQRes5t
END
E6a$pe :
Oragle 10 g Material

112
SI E "elect 'name( -eptno (
CA"'
,I'D -eptno \ /> TI'D L ACCO@DTID)S
,I'D -eptno \ 0> TI'D L &'"'A&CIS
,I'D -eptno \ 1> TI'D L "AL'"S
,I'D -eptno \ 3> TI'D L OP'&ATIOD"S
'L"' L Dot "pecified L
'D-
<rom 'mp K
SI E E*a$eF SaF
CASE
WHEN Sa E W ;00 AND Sa C W 8000 THEN
N LOWEST PA! N
WHEN Sa E W 8000 AND Sa C W A000 THEN
N MODERATE PA! N
ELSE N HIGH PA! N END Fr%$ E$p H
MATERIALIOED /IEWS :
Materiali2ed $ie%s are used in -ATA ,A&'IO@"'".
They are used to increase the speed of Fueries on $ery large databases
,5er"es Ma?"*g Use %# M: /"eBs :
A))&')ATIOD on a "ingle Table.
=OID" *et%een Tables.
A))&')ATIOD" and =OID".
6ateriali2ed $ie%s can be used to replicate data.
P&IO& to 6ateriali2ed $ie%s( the Concept of "DAP"IOT %as Implemented.
,5er& ReBr"te :
Materiali2ed $ie%s improve Fuery performance by P&'CALC@LATID) 'pensive =OID
and A))&')ATIOD operations on the -ATA*A"' P&IO& to 'ecution time and stores
the results in the -ATA*A"'.
The Euery OPTI6IM'& can ma+e use of 6AT'&IALIM'- $I'," by automatically
recogni2ing %hen an 'isting 6AT'&IALIM'- $I', can and should be used to satisfy a
&eFuest.
After above process is completed then the E@'&? OPTI6IM'& transparently re%rites
the reFuest to use the 6AT'&IALIM'- $I',.
E@'&I'" are then directed to the 6AT'&IALIM'- $I', and not to the underlying
-'TAIL TA*L'" or $I',".
&',&ITID) E@'&I'" to yuse 6AT'&IALIM'- $I'," rather than detail relations(
results in a significant performance gain.
PRERE,UISITES FOR MATERIALIOED /IEWS PRI/ILEGES :
SI E )&ADT E@'&? &',&IT' TO "COTT K
SI E )&ADT C&'AT' 6AT'&IALIM'- $I', TO "COTT K
SI E ALT'& "'""IOD "'T E@'&?R&',&IT'R'DA*L'- \ T&@' K

Set t@e I*"tS"3:ORA F"e :
Oragle 10 g Material

113
OPTI6IM'&R6O-' \ CIOO"'
=O*RE@'@'RIDT'&$AL \ 15>>
=O*RE@'@'RP&OC'""'" \ /
E@'&?R&',&IT'R'DA*L'- \ T&@'
E@'&?R&',&IT'RIDT')&IT? \ 'D<O&C'-
E6a$pe %# Mater"a"Ye3 /"eB W"t@ Aggregat"%* :
SI E Create 6AT'&IALIM'- $I', 'mpR"um
'DA*L'- E@'&? &',&IT'
A"
"'L'CT -eptno ( =ob( "um ( "al ! <rom 'mp
)roup *y -eptno ( =ob K
Creat"*g Opt"$"Yer Stat"st"cs a*3 Re#res@"*g Mater"a"Ye3 /"eBs :
SI E 'A'C@T' -*6"R@TILIT?.ADAL?M'R"CI'6A
( L"COTTS ( L'"TI6AT'S ! K
SI E 'A'C@T' -*6"R6$I',.&'<&'"I ( L'mpR"umS ! K
Test"*g Mater"a"Ye3 /"eB :
SI E "'T A@TOT&AC' OD 'APLAID K
SI E SELECT Dept*%F S5$ ( Sa ) Fr%$ E$p
Gr%5p '& Dept*%F .%b H
E6a$pes %# Mater"a"Ye3 /"eB B"t@ .%"* + Aggregat"%* :
SI E C&'AT' 6AT'&IALIM'- $I', 'mpR-eptR"um
'DA*L' E@'&? &',&IT'
A"
"'L'CT -name( =ob( "um ( "al !
<rom 'mp ' ( -ept -
,here '.-eptno \ -.-eptno
)roup *y -name( =ob K
Creat"*g Opt"$"Yer Stat"st"cs a*3 Re#res@"*g Mater"a"Ye3 /"eB :
SI E 'A'C@T' -*6"R@TILIT?.ADAL?"'R"CI'6A ( L"COTTS ( L'"TI6AT'L ! K
SI E 'A'C@T' -*6"R6$I',.&'<&'"I ( L'mpR-eptR"umS ! K
Test"*g t@e Mater"a"Ye3 /"eBs :
SI E "'T A@TOT&AC' OD 'APLAID
SI E "'L'CT -name( =ob( "um ( "al ! <rom 'mp '( -ept -
,here '.-eptno \ -.-eptno
)roup *y -name( =ob K
P5tt"*g t@e T@"*gs B"t@ R%Up :
Oragle 10 g Material

114
SI E Create 6ateriali2ed $ie% 'mpR-eptRAgg
'DA*L' E@'&? &',&IT'
A"
"'L'CT -eptno( =on( Count ( H ! ( "um ( "al !
<rom 'mp
)roup *y &oll@p ( -eptno( =ob ! K
GROUPINGQID ( ) FUNCTION :
The function is used to <ILT'& &O," using a IA$ID) Clause to eclude ro%s that do not
contain a subtotal n total .
The <unction accepts one or more columns and returns the decimal eFuivalent of the
)&O@PID) bit vertor.
The )&O@PID) bit vector is computed by combining the results of a call to the )&O@PID)( !
function for each column in order.
COMPUTING THE GROUPING 'IT /ECTORS :
)&O@PID) ( ! <unction returns / %hen the column value is D@LL( else returns > . *ased on
this.
)&O@PID)RI- ( ! returns > ( %hen -eptno and =ob are DOT D@LLSs
)&O@PID)RI- ( ! returns / ( if -eptno is DOT D@LL and =ob is D@LL.
)&O@PID)RI- ( ! returns 0 ( if -eptno is D@LL and =ob is DOT D@LL
)&O@PID)RI- ( ! returns 1 ( if -eptno is D@LL and =ob is D@LL.
SI E "elect -eptno ( =ob
)rouping(-eptno! )-PT(
)rouping (=ob ! )=O* (
)roupingRI- ( -eptno( =ob ! )&PI-(
"um ( "al !
<rom 'mp
)roup *y Cube ( -eptno ( =ob ! K
GROUPINGQID ( ) a*3 HA/ING Ca5se :
SI E "elect -eptno( =ob
)roupingRI- ( -eptno( =ob ! )&PI-(
"um ( "al !
<rom 'mp
)roup *y Cube ( -eptno ( =ob !
Iaving )roupingRI- ( -eptno( =ob ! P > K
Represe*t"*g C%5$* M5t"pe T"$es "* a Gr%5p '& Ca5se :
A Column can be represented multiple times in a )roup *y Clause .
SI E "elect -eptno ( =ob ( "um ( "al ! <rom 'mp
Oragle 10 g Material

115
)roup -eptno ( &oll@p ( -eptno( =ob ! K
App&"*g GROUPQID F5*ct"%* :
The )&O@PRI- ( ! <unction is used to remove the deplicate ro%s returned by )&O@P *?
Clause.
The )&O@PRI- ( ! does not accept any parameters.
If L n L duplicate eist for a particular grouping( )&O@PRI- ( ! returns numbers in the range > to n
J /.
SI E "elect -eptno( =ob( )&O@PRI-( ! ( "um ( "al ! <rom 'mp
)roup *y -eptno( &oll@p ( -eptno( =ob ! K
SI E "elect -eptno( =ob( )&O@PRI- ( ! ( "um ( "al ! <rom 'mp
)roup *y -eptno( &oll@p ( -eptno( =ob !
Iaving )&O@PRI- ( ! \ > K
ANAL!TIC FUNCTIONS :
The *uilt in Analytic <unctions enable to perform comple calculations.
A*a&t"c F5*ct"%*s Categ%r"es B
o Ra*?"*g F5*ct"%*s :
They 'nable to calculate &an+s( Percentiles and nGtiles .
o I*=erser Perce*t"e F5*ct"%*s :
Enable to calculate the value corresponding to a Percentile.
o W"*3%B F5*ct"%*s :
Enable to calculate cumulative and 6oving Aggregates.
o Rep%rt"*g F5*ct"%*s :
Enable to Calcuate area li+e mar+et "hares.
o LAG a*3 LEAD F5*ct"%*s :
Enable to get a value in a ro% %here that ro% is a cetain number of ro%s
a%ay from the current ro%.
o F"rst a*3 Last F5*ct"%*s :
Enable to get the first and last values in an ordered group.
o L"*ear Regress"%* F5*ct"%*s :
Enable to fit an ordinary J least J sFuares regression line to a set of
number pairs.
Oragle 10 g Material

116
o H&p%t@et"ca Ra*? a*3 D"str"b5t"%* F5*ct"%*s :
Enable to calculate the ran+ and percentile that a ne% ro% %ould have if
a value is inserted into a table.
TEST TA'LES :
ITEM T!PES TA'LE :
Used to store the names of the Item Types that are stoc+ed by the store.
"Fl P Create Table ItemTypes
(
ItemTypeI- IDT')'&
Constraint ItemTypeP; P&I6A&? ;'?(
ItemTDame $archar0 ( /> !
Constraint ItemTDame DOT D@LL
! K
ITEMS TA'LE :
Use3 to store detailed information about the Items to be sold.
SI E Create Table Item
(
ItemI- IDT')'&
Constraint ItemI-P; P&I6A&? ;'?(
ItemTypeI- IDT')'&
Constraint ItemType<;
&eferences ItemTypes ( ItemTypeI- ! (
ItemDame $archar0 ( /> !
Constraint ItemDameDD DOT D@LL (
-escription $archar0( />> ! (
Price Dumber ( 4( 0 !
! K
DI/ISIONS TA'LE :
@sed to store different divisions eisting.
"Fl P Create Table -ivision
(
-ivid Char ( 1 !
Constraint -ividP; P&I6A&? ;'?(
-ivDame $archar0 ( /4 ! DOT D@LL
! K
.O'S TA'LE :
Oragle 10 g Material

117
@sed to store different =ob Catogories 'isting in the Organi2ation.
"Fl P Create Table =obs
(
=obI- Char ( 1 !
Constraint =obI-P; P&I6A&? ;'?(
=obDame $archar0 ( 0> !
Constraint =obDameDD DOT D@LL
! K
EMPLO!EES TA'LE :
@sed to store the details of the 'mployees %or+ing in the stores.
"Fl P Create Table 'mp"tores
(
'mpI- IDT')'&
Constraint 'mp"toresP; P&I6A&? ;'? (
"al Dumber ( 5( > ! (
<irstDame $archar0 ( /> !
Constraint <nameDD DOT D@LL (
LastDame $archar0 ( 0> !
Constraint LnameDD DOT D@LL (
=obI- Char ( 1 !
Constraint =obI-<;
&eferences =obs( =obI- ! (
-ivid Char ( 1 !
Constraint -ivI-<;
&eferences -ivision ( -ivid !
! K
ALL SALES TA'LE :
@sed to store the sum of all sales by specified amount( for a particular ?'A&( 6ODTI( IT'6
T?P' and '6OLO?''.
"Fl P Create Table All"ales
(
?ear IDT')'&
Constraint ?earDD DOT D@LL (
6onth IDT')'&
Constraint 6onthDD DOT D@LL (
ItemTypeId IDT')'&
&eferences ItemTypes ( ItemTypeI- ! (
'mpI- IDT')'&
Constraint 'mpI-As
&eferences 'mp"tores ( 'mpI- ! (
"alAmt Dumber ( 8( 0 ! (
Constraint All"alesP; P&I6A&? ;'? ( ?ear( 6onth(
ItemTypeI-( 'mpI- !
! K
RAN<ING FUNCTIONS :
The -ifferent types of &an+ing <unctins are
Oragle 10 g Material

118
/ ! RAN< ( ) F5*ct"%* :
- It returns the ran+ of items in a group.
H &AD; ( ! leaves a gap in the seFuence of ran+ings in the event of a tie.
0 ! DENSEQRAN< ( ) F5*ct"%* :

- It returns the ran+ of items in a group.
H -'D"'R&AD; ( ! doesnSt leave a gap in the seFuence of ran+ings in the vent of
tie.
"Fl "elect ItemTypeI-( "um ( "aleAmt! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt ! -'"C ! &AD; (
-'D"'R&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt ! -'"C ! -'D"'R&AD;
<&O6 All"ales
,here ?ear \ 0>>1 AD- "aleAmt I" DOT D@LL
)roup *y ItemTypeI-
O&-'& *? ItemTypeI- K
PARTITION '! CLAUSE :
The Clause is used to divide groups into subgroups.
"Fl P "elect ItemTypeI-( 6onth( "um ( "aleAmt ! (
&AD; ( ! O$'& ( PA&TITIOD *? 6onth O&-'& *? "um ( "aleAmt !
-'"C ! &AD;
<rom All"ales
,here ?ear \ 0>>1 AD- "aleAmt I" DOT D@LL
)&O@P *? ItemTypeI-( 6onth
O&-'& *? ItemTypeI-( 6onth K
APPL!ING ROLLUPF CU'E AND GROUPING SETS :
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! &AD;
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? &oll@P(ItemTypeI-!
O&-'& *? ItemTypeI- K
SI E "elect ItemTypeI-( 'mpI-( "um ( "aleAmt ! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! &AD;
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? C@*'(ItemTypeI-( 'mpI-!
O&-'& *? ItemTypeI- ( 'mpI- K
SI E "elect ItemTypeI-( 'mpI-( "um ( "aleAmt ! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! &AD;
<rom All"ales
Oragle 10 g Material

119
,here ?ear \ 0>>1
)&O@P *? )&O@PID) "'T" (ItemTypeI-( 'mpI-!
O&-'& *? ItemTypeI- ( 'mpI- K
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! &AD; (
-'D"'R&AD;( ! O$'& ( O&-'& *? "um ( "aleAmt ! -'"C !
-'D"'R&AD;
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? ItemTypeI-
O&-'& *? ItemTypeI- K
NULLS FIRST AND NULLS LAST CLAUSE :
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
&AD; ( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C D@LL LA"T ! &AD; (
-'D"'R&AD;( ! O$'& ( O&-'& *? "um ( "aleAmt ! -'"C D@LL" LA"T !
-'D"'R&AD;
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? ItemTypeI-
O&-'& *? ItemTypeI- K
CUME Q DIST ( ) F5*ct"%* :
It returns the position of a specified value relative to a group of values.
The function represents the Cumulative distribution of the data.
PERCENTQRAN< ( ) F5*ct"%* :
It returns the Percent &an+ of a $alue relative to a group by $alues.
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
C@6'R-I"T( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! Cumulative (
P'&C'DTR&AD;( ! O$'& ( O&-'& *? "um ( "aleAmt ! -'"C !
Percent
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? ItemTypeI-
O&-'& *? ItemTypeI- K
NTILE ( ) F5*ct"%* :
The DTIL' ( *uc+ets ! is used to calculate n J tiles.
*uc+et specifies the number of buc+ets into %hich groups of ro%s are placed.
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
DTIL' ( 3 ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! A" Dtile
<rom All"ales
,here ?ear \ 0>>1 AD- "aleAmt I" DOT D@LL
Oragle 10 g Material

120
)&O@P *? ItemTypeI-
O&-'& *? ItemTypeI- K
ROWQNUM'ER ( ) F5*ct"%* :
The <unction is used to return a number %ith each ro% in a group.
The ro% number starts at /.
SI E "elect ItemTypeI-( "um ( "aleAmt ! (
&O,RD@6*'&( ! O$'& ( O&-'& *? "um ( "aleAmt !
-'"C ! &o%RDumber
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? ItemTypeI-
O&-'& *? ItemTypeI- K
IN/ERSE PERCENTAGE F5*ct"%* :
These functions are used to get the value corresponding to a percentile.
The types of ID$'&"' percentile functions are
o PERCENTILEQDIST( ) :
It eamines the cumulative distribution values in each group until it finds one that
is greater than or eFual to .
o PERCENTILEQCONT( ) :
It eamines the percent ran+ values in each group until it finds one that is greater
than or 'Fual to .
SI E "elect P'&C'DTIL'RCODT ( >.5 !
,ITIID )&O@P ( O&-'& *? "um ( "aleAmt !
-'"C PercentileRCont (
P'&C'DTIL'R-I"T ( >.5 !
,ITIID )&O@P ( O&-'& *? "um ( "aleAmt !
-'"C PercentileR-ist
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? ItemTypeI- K
WINDOW FUNCTIONS :
The ,ID-O, <unctions are used to calculate Cumulative "ums and moving averages %ithin a
specified range of ro%s.
The functions athat can be applied %ith %indo%s are
SUM ( ) A/G ( ) MA1 ( ) MIN ( )
COUNT ( ) /ARIANCE ( ) STDDE/ ( )
FIRSTQ/ALUE ( ) LASTQ/ALUE ( )
Oragle 10 g Material

121
CALCULATING CUMULATI/E SUM :
SI E "elect 6onth( "um ( "aleAmt ! 6onthlyAmt (
"um ( "um ( "aleAmt ! ! O$'&
( O&-'& *? 6onth
&O," *'T,''D @D*O@D-'-
P'&C'-ID) AD- C@&&'DT &O, !
CumulativeAmount
<rom All"ales
,here ?ear \ 0>>1
)roup *y 6onth
Order *y 6onth K
U*'%5*3e3 Prece3"*g :
o "pecifies that the %indo% starts at the first ro% of the partition.
U*'%5*3e3 F%%B"*g :
o "pecifies that the %indo% starts at the last ro% of the partition.
C5rre*t R%B :
o "pecifies that the %indo% begins at the Current &o% or $alue.
SI E "elect 6onth( "um ( "aleAmt ! 6onthlyAmt (
"um ( "um ( "aleAmt ! ! O$'&
( O&-'& *? 6onth
&O," @D*O@D-'-
P'&C'-ID) !
CumulativeAmount
<rom All"ales
,here ?ear \ 0>>1 AD- 6onth *'T,''D 5 AD- /0
)roup *y 6onth
Order *y 6onth K
CALCULATING MO/ING A/ERAGES :
SI E "elect 6onth( "um ( "aleAmt ! 6onthlyAmt (
Avg ( "um ( "aleAmt ! ! O$'&
( O&-'& *? 6onth
&O," *'T,''D 1 AD- C@&&'DT &O, !
6ovingAverage
<rom All"ales
,here ?ear \ 0>>1
)roup *y 6onth
Order *y 6onth K
CALCULATING CENTERED A/ERAGE :
SI E "elect 6onth( "um ( "aleAmt ! 6onthAmt( Avg ( "um ( "aleAmt ! ! O$'&
( O&-'& *? 6onth &o%s *'T,''D / P&'C'-ID) AD- / <OLLO,ID) ! 6ovAvg
Oragle 10 g Material

122
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? 6onth
O&-'& *? 6onth K
D"spa&"*g F"rst A*3 Last R%Bs Us"*g F"rstQ/a5e( ) a*3 LastQ/a5e ( ) :
SI E "elect 6onth( "um ( "aleAmt ! 6onthAmt( <I&"TR$AL@' ( "um ( "aleAmt ! ! O$'&
( O&-'& *? 6onth &o%s *'T,''D / P&'C'-ID) AD- / <OLLO,ID) !
P&&$6onthAmt (
LA"TR$AL@' ( "um ( "aleAmt ! O$'&
( O&-'& *? 6onth &o%s *'T,''D / P&'C'-ID) AD- / <OLLO,ID) !
DAT6onthAmt
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? 6onth
O&-'& *? 6onth K
W%r?"*g W"t@ REPORTING F5*ct"%*s :
The &eporting function can be used to perform calculations across groups and partitions %ithin
groups.
The functions that can be used are ..
SUM ( ) A/G ( ) MA1 ( ) MIN ( )
COUNT ( ) /ARIANCE ( ) STDDE/ ( )
RATIOQTOQREPORT ( )
SI E "elect 6onth( ItemTypeI-( "um ( "um ( "aleAmt ! !
O$'& ( PA&TITIOD *? 6onth ! TotR6onthRAmt (
"um ( "um ( "aleAmt ! !
O$'& ( PA&TITIOD *? ItemTypeI- ! TotRPesdRTypeRAmt !
<rom All"ales
,here ?ear \ 0>>1 AD- 6onth Q \ 1
)&O@P *? 6onth( ItemTypeI-
O&-'& *? 6onth( ItemTypeI- K
RATIOQTOQREPORT ( ) F5*ct"%* :
The function is used to compute the ratio of a value to the sum of a set of values.
SI E "elect 6onth( ItemTypeI- (
"um ( "aleAmt ! ItemTypeAmt (
&ATIORTOR&'PO&T ( "um ( "aleAmt ! !
O$'& ( PA&TITIOD *? 6onth ! ProType&atio
<rom All"ales
Oragle 10 g Material

123
,I'&' ?ear \ 0>>1 AD- 6onth Q \ 1
)&O@P *? 6onth( ItemTypeI-
O&-'& *? 6onth( ItemTypeI- K
LAG ( ) a*3 LEAD F5*ct"%*s :
Are used to get a value in a ro%( %here that ro% is a certain number of ro%s a%ay from the
current ro%.
FIRST a*3 LAST F5*ct"%*s :
Are used to get the <I&"T and LA"T values in an ordered group( the functions can be used in
combination %ith all group functions.
SI E "elect 6onth (
"um ( "aleAmt ! 6onthAmt (
Avg ( "um ( "aleAmt ! ( / !
O$'& ( O&-'& *? 6onth ! Prev6thAmt (
L'A- ( "um ( "aleAmt ! ( / !
OVER ( ORDER BY Month !e"tMth#$t
<rom All"ales
,I'&' ?ear \ 0>>1 AD- 6onth Q \ 1
)&O@P *? 6onth
O&-'& *? 6onth K
SI E "elect 6in ( 6onth !
;''P ( -'D"'R&AD; <I&"T O&-'& *? "um ( "aleAmt ! !
Lo%est"ale6onth
<rom All"ales
,here ?ear \ 0>>1
)&O@P *? 6onth
O&-'& *? 6onth K
UPDATING THE DATA IN A TA'LE :
The @P-AT' statement is used to change the eisting values in a table or in the base table of
vie%.
It can be used to @P-AT' the master table of 6ateriali2ed $ie%.
PRERE,UISITES :
The Table must be in the o%n "chema.
@P-AT' ob#ect privilege should be available.
S&*ta6 :
UPDATE C TabeQNa$e E
SET C Spec"#"cat"%* E
WHERE Ca5se H
E6a$pes :
SI E @P-AT' '6P
Oragle 10 g Material

124
"'T Comm \ D@LL
,I'&' =ob \ LCL'&;S K
SI E @P-AT' 'mp
"'T ( =ob( -eptno ! \ ( "elect =ob( -eptno
<rom 'mp
,here 'mpno \ 7399 !
,here 'mpno \ 7598 K
SI E @P-AT' 'mployee
"'T -eptno \ ( "elect -eptno <rom 'mp
,here 'mpno \ 7788 !
,here =ob \ ( "elect =ob <rom 'mp
,here 'mpno \ 7788 K
SI E @P-AT' 'mp
"'T =ob \ L6ADA)'&S ( "al \ 04>> (
-eptno \ />
,here 'name \ LALL'DS K
SI E @P-AT' 'mp
"'T =ob \ LADAL?"TS (
"al \ "al : />>>
,here 'name \ L*LA;'S K
SI E @P-AT' 'mp '/
"'T -eptno \ ( "elect -eptno <rom -ept
,here Loc \ L-ALLA"S ! (
( "al( Comm ! \ ( "elect /./ H Avg ( "al ! (
/.4 H Avg ( Comm !
<rom 'mp '0
,here '/.-eptno \ '0.-eptno !
,here -eptno ID
( "elect -etpno <rom -ept
,here Loc \ LD', ?O&;S O& Loc \ L*O"TODS ! K
SI E @P-AT' 'mp
"'T "al \ "al H /./>
,here -eptno \ ( "elect -eptno <rom -ept
,here Loc \ LCIICA)OS ! K
APPL!ING DEFAULT /ALUES :
It is used to @P-AT' a value in a Column %ith -'<A@LT value set in the Constraints.
SI E @P-AT' 'mp
%E& %al ' DE(#)*&
,here 'name \ L"6ITIS K
Oragle 10 g Material

125
"Fl P @P-AT' 'mp
"'T Iiredate \ -'<A@LT
,here 'name \ LTA?LO&S K
DEFAULT ?e& B%r3 "s I*tr%35ce3 #r%$ Orace >" %*Bar3s:
RETURNING CLAUSE :
The returning clause is introduced from />g.
It is used to &eturn a value from a A))&')AT' <unction.
The Clause can be specified for tables and 6ateriali2ed $ie%s and for $ie%s %ith a single base
table.
Restr"ct"%*s :
'ach epression must be a simple epression or a single set Aggregate function.
It cannot be specified for multi table insert.
It cannot be used %ith parallel -6L or %ith &emote ob#ects.
It cannot be used to &etrieve LOD) Types.
It cannot be specified upon a $I', upon %hich a ID"T'A- O< Trigger is defined.
E6a$pe 1 :

SI E $A&IA*L' "umsal Dumber
SI E @P-AT' 'mp "et "al \ "al H /./
,here -eptno \ />
&'T@&DID) "um ( "al ! IDTO B"um"al K
SI E P&IDT "um"al K
E6a$pe 8 :
SI E $A&IA*L' "alary D@6*'& K
SI E $A&IA*L' -eptDum D@6*'& K
SI E $A&IA*L' Dame $A&CIA&0 ( /> ! K
SI E @P-AT' 'mp
"'T =ob \ L"AL'" 6ADS (
"al \ "al : />>> (
-eptno \ 1>
,here 'name \ L=OD'" L
&'T@&DID) "al H >.04 (
-eptno( 'name IDTO
B"alary( B-eptDum ( BDame K
MERGE :
The 6'&)' "tatement is used to "'L'CT ro%s from one or more sources for @P-AT' or
ID"'&T into one or more tables.
The 6'&)' "tatement is convenient to Combine multiple operations and avoid multiple
ID"'&T( @P-AT'( -'L'T'.
Oragle 10 g Material

126
6'&)' is a deterministic statement( using %hich the same ro% of the target table multiple times
in the same 6'&)' statement.
E6a$pe :
SI E C&'AT' table *onus
(
'mpno D@6*'& (
*onus D@6*'& -'<A@LT />> ! K
SI E ID"'&T IDTO *onus ( 'mpno !
( "'L'CT '.'mpno <rom 'mp '
,here =ob \ L"AL'" 6ADS ! K
SI E 6'&)' IDTO *onus *
@"ID) ( "'L'CT 'mpno( "al( -eptno <rom 'mp
,here -eptno \ 1> ! "
OD ( *.'mpno \ ".'mpno !
,I'D 6ATCI'- TI'D
@P-AT' "'T *.*onus \ *.*onus : "."al H >./
-'L'T' ,I'&' ( "."al P 3>>> !
,I'D DOT 6ATCI'- TI'D
ID"'&T ( *.'mpno( -.*onus !
$AL@'" ( ".'mpno( "."al H >./ !
,I'&' ( "."al Q \ 3>>> ! K
SI E C&'AT' Table 'amTimeTable
(
'amDame $A&CIA&0( 1> ! (
'amTime $A&CIA&0 ( 5 ! (
Constraint 'amDameP; P&I6A&? ;'? ( 'amDame ! ! K
SI E ID"'& IDTO 'amTimeTable
( LPI?"ICAL "CI'DC'"L ( L9B>> A6S ! K
SI E 6'&)' IDTO 'amTimeTable '/
@"ID) 'amTimeTable '0 OD
( '0.'amDame \ '/.'amDame AD-
'/.'amDame \ LPI?"ICAL "CI'DC'"S !
,I'D 6ATCI'- TI'D
@P-AT' "'T '/.'amTime \ S/>B1> A6S
,I'D DOT 6ATCI'- TI'D
ID"'&T ( '/.'amDame( '/.'amTime !
$AL@'" ( LPI?"ICAL "CI'DC'"S ( S/>B1> A6S ! K
SI E 6'&)' IDTO 'amTimeTable '/
@"ID) 'amTimeTable '0 OD
( '0.'amDame \ '/.'amDame AD-
'/.'amDame \ LCI'6ICAL "CI'DC'"S !
,I'D 6ATCI'- TI'D
@P-AT' "'T '/.'amTime \ S/0B1> P6S
,I'D DOT 6ATCI'- TI'D
ID"'&T ( '/.'amDame( '/.'amTime !
$AL@'" ( LCI'6ICAL "CI'DC'"S ( S/0B1> P6S ! K
DELETE STATEMENT :
Oragle 10 g Material

127
It is used to remove ro%s from
o An @n Partitioned or Partitioned Table.
o The @n Partitioned or Partitioned *ase Table of a $ie%.
o The @n Partitioned or Partitioned container table of ,ritable 6ateriali2ed $ie%.
PrereI5"s"tes :
To -'L'T' ro%s from a table( the table must be in the users shema.
To -'L'T' ro%s from a 6ateriali2ed $ie%( -'L'T' Ob#ect privilege is a must.
-'L'T' any Table system privilege allo%s to -'L'T' ro%s from any table or table partition or
form the *ase Table of any $ie%.
S&*ta6 :
DELETE J FROM K C TabeQNa$e E
J WHERE C%*3"t"%* K H
E6a$pe :
SI E -'L'T' <rom 'mp
,here 'name \ L"6ITIS K
SI E -'L'T' <rom 'mp
,here Iiredate P ToR-ate ( L>/G>/G/997S ( L--G66G????S ! K
SI E -'L'T' <rom "al)rade K
SI E -'L'T' <rom 'mp
,here -eptno \ ( "elect -eptno <rom -ept
,here -name \ L"AL'"S ! K
SI E -'L'T' <rom ( "elect H <rom 'mp !
,here =ob \ L"AL'"6ADS AD- Comm Q 1>> K
USING RETURNING CLAUSE :
SI E $A&IA*L' "alary D@6*'& K
SI E -'L'T' <rom 'mp
,here =ob \ L"AL'"6ADS
AD-
Iiredate Q "ysdate
&'T@&DID) "al IDTO B"alary K
SI E P&IDT B"alary K
TRANSACTION CONTROL :
Oracle "erver ensures -ata Consistency based upon transactions.
Transactions consist of -6L statements that ma+e up one consistent change to the data.
TRANSACTION START a*3 END Cases :
Oragle 10 g Material

128
A Transaction begins %hen the first eecutable "EL statement is encountered.
The Transaction terminates %hen the follo%ing specifications OCC@&.
o A COMMIT %r ROLL'AC< "s "ss5e3:
o A DDL State$e*t "ss5e3:
o A DML State$e*t "ss5e3:
o T@e USER e6"sts t@e S,L - P5s
o Fa"5re %# Mac@"*e %r S&ste$ cras@es:
A --L "tatement or a -CL statement is automatically committed and hence implicitly ends a
transaction.
E1PLICIT TRANSACTION CONTROL STATEMENTS :
The logic of Transaction can be controlled by using
o COMMIT :
It ends the current transaction by ma+ing all pending data changes permanent.
o SA/EPOINT Na$e :
It mar+s a "avePoint %ithin the Current Transaction.
o ROLL'AC< J TO SA/EPOINT NAME K :
It ends the Current Transaction by discarding all pending data changes.
STATE OF DATA 'EFORE COMMIT OR ROLL'AC< :
'very data change made during the transaction is temporary until the transaction is committed.
-ata 6anipulation operations primarily affect state of the data can be recovered.
The current user can revie% the results of the data manipulation operation by Fuerying the tables.
Other @"'&" cannot vie% the results of the data manipulation operations made by the current
user.
The O&ACL' "'&$'& institutes read consistency to ensure that each @"'& sees data as it
eisted at the last CO66IT.
STATE OF THE DATA AFTER COMMIT IS ISSUED :
-ata changes are %ritten to the database
The previous state of the data is permanently lost
All users can vie% the results of the transaction.
The LOC;" on the affected ro%s are released
All "A$'POIDT" are erased.
SI E COMMIT H
ALTERTING THE TA'LE DEFINITION :
The ALT'& TA*L' statement is used to change the Tables "tructural -efinition.
The ALT'& TA*L' statement is used to
Oragle 10 g Material

129
o ADD a *eB C%5$*
o MODIF! a* E6"st"*g C%5$*
o DEFINE a DEFAULT =a5e #%r a *eB C%5$*:
S&*ta6 :
ALTER TA'LE C TabeQ*a$e E
ADD ( C%5$*Na$e DataT&pe J DEFAULT E6pr K
J F C%5$*Na$e DataT&pe K G ) H
ALTER TA'LE C TabeNa$e E
MODIF! ( C%5$*Na$e Datat&pe
J DEFAULT E6pr K
J F C%5$* Datat&pe K G ) H
STATE OF THE DATA AFTER ROLL'AC< :
&OLL*AC; "tatement is used to discard all pending changes.
The -ate changes are undone.
The previous state of the data is returned
The LOC;" on the affected ro%s are released.
SI E ROLL'AC< H
ROLLING 'AC< CHANGES TO A SA/EPOINT :
"A$'POIDT is used to create a mar+er in the Current transaction.
@sing "A$'POIDT the transaction can be discarded up to the mar+er by using the
o ROLL'AC< TO C SA/EPOINT Na$e E H
If a second "A$'POIDT is created %ith the same name as an earlier "A$'POIDT( the 'arlier
"A$'POIDT is -eleted.
ADDING A COLUMN TO A TA'LE :
The A-- Clause is used to add Columns.
SI E ALTER TA'LE Dept90
ADD ( .%b /ARCHAR8 ( > ) ) H
G5"3e"*es #%r ADDING C%5$* :
A column can be A--'- or 6O-I<I'- but cannot be dropped from a table.
,e cannot specify the location %here the column can appear( it by default becomes the last
column.
If the table contains records( before the column is added( the ne% column contains D@LLSs.
MODIF!ING A COLUMN :
Oragle 10 g Material

130
A column -atatype( si2e and -efault value can be changed.
A change to the -efault value affects only subseFuent insertions to the table.
G5"3e"*es t% MODIF! a C%5$* :
,e can Increase the %idth or precision of a Dumeric Column
,e can -ecrease the %idth of a column if the column contains only D@LL values and if the table
has no ro%s.
,e can change the datatype if the column contains D@LLSs
,e can convert a CIA& column to the $A&CIA&0 datatype or convert a $A&CIA&0 column to
the CIA& datatype if the column contains D@LL values or if the "i2e is not changes.
A change to the default value of a column affects only subseFuent insertions to the table.
DROPPING A COLUMN :
A column can be dropped from a table by using the ALT'& TA*L' statement.
The -&OP Column Clause is used for this purpose and the feature is enabled from
Oralce 8i On%ards.
G5"3e"*es t% DROP a C%5$* :
The column may or may not contain data
Only one column can be dropped at a time.
The table must have atleast one column remaining in it after it is altered.
Once a column is dropped it cannot be recovered.
SI E ALTER TA'LE Dept90
DROP C%5$* .%b H
SET UNUSED OPTION :
The "'T @D@"'- OPTIOD mar+s one or more columns as @nused such that they can be
dropped %hen the damage on system &esources is less ( 8 i ! .
The &esponse time is faster than the -&OP Clause.
@nused columns are treated as if the %ere dropped( even trough their column data remains in the
tables ro%s.
After a column has been mar+ed unused( %e cannot have access to that column.
The names and types of columns mar+ed unused %ill not be displayed during a -'"C&I*'.
,e can add to a table a De% Column %ith the same name as an unused column.
SI E ALT'& TA*L' -ept1>
"'T @D@"'- ( 'name ! K
DROP UNUSED COLUMNS OPTION :
This option removes from the table all columns currently mar+ed as unused.
The option is used %hen %e %ant to reclaim the etra dis+ space from unused columns in the
table.
If the table does not contain unused columns the statement returns %ith no errors.
Oragle 10 g Material

131
Sql > #*&ER &#B*E De+t30
-&OP @D@"'- COL@6D" K
DROPPING A TA'LE :
It removes the definition of the Oracle Table.
The Command not only drops the table but the entire -atabase is lost along %ith the associated
Indees.
S&*ta6 :
DROP TA'LE C TabeQNa$e E H
E6a$pe :
-&OP TA*L' -ept1> K
GUIDELINES TO DROP TA'LE :
The data is totally deleted from the table
Any vie%s and synonyms %ill remain that are invalid
Any pending transactions are committed.
Only the create of the table is a user %ith -&OP any table privilege can remove a table
The drop table statement once eecuted is irreversible ..
CHANGING THE NAME OF AN O'.ECT :
The rename command can be used to change the name of a
Table
$ie%
"eFuence
"ynonym
To rename the ob#ect %e must be the o%ner of the ob#ect
S&*ta6 :
RENAME Q old name P TO Q ne% name P K
SI E Re*a$e -ept TO -epartment.
TRUNCATING A TA'LE :
It is used it remove all ro%s from a table and to release the storage space used by the specific
table.
The truncate table %ill not facilitate for rollbac+
S&*ta6 :
TRUNCATE TA'LE Q Table Dame P K
SI E TRUNCATE Table -epartment K
Oragle 10 g Material

132
%e must be the o%ner of the table for truncating
APPL!ING COMMENTS UPON A TA'LE :
The comments command is used to add comments to a table or a column or vie% etc.
'ach comment can be upto 0>>> bytes
The data dictionary is %hich comments are stored are.
ALLRCOLRCO66'DT"
@"'&RCOLRCO66'DT"
ALLRTA*RCO66'DT"
@"'&RTA*RCO66'DT"
S&*ta6:
COMMENT ON Table Q Table Dame P C column Q Tablename. Column P
is L Tet S K
SI E COMMENT ON Table 'mp is L the table storing employee information S K
DROPING A COMMENT :
A comment is dropped from the database by setting it to an empty string.
SI E COMMENT ON Table 'mp is L L K
AD/ANCED TA'LE CREATION STRATEGIES :
Creating a table from an eisting table
Oracle allo%s the creation of a ne% table onRtheRfly( depending on a select statement on an
already eisting table.
S&*ta6 :
Create Table Q Table Dame P
As
Seect columns
Fr%$ TableRDame K
T ,here Condition W K
The create table .As select.command %ill not %or+ if one of the selected columns use long
data type.
,hen the ne% table is described it reveals that it has YIDI'&IT'-Z the column definition from
the eisting table.
@sing this style %e can include all columns using Asteris+ is a subset of columns from table.
The ne% table can contain Yinvented columnsZ %hich are the product of function of the
combination of other columns
The column definition %ill ad#ust to the si2e necessary to contain the data is the invented
columns.
SI E Create Table "ampdept as "elect H from deptK
SI E Create Table "ampdept/(-eptid ( -eptname ( Place! As "elect H <rom -eptK
Oragle 10 g Material

133
SI E Create Table "ampdept0 as "elect -eptno (-name <rom -eptK
Creat"*g a tabe B"t@ c%5$* 3e#"*"t"%*s a*3 B"t@%5t 3ata :
SI E Create Table "ampdept1 As "elect H <rom -ept ,here / \ 0 K
Creat"*g a tabe B"t@%5t Ge*erat"*g re3% %g E*tr"es :
&edo log entries are chronological records of database actions used during database recoveries
The &'-O log entries generating can be avoided by using the DOLO))ID) +ey%ord
*y circumventing the performance of the create table command %ill improve as less %or+ is
being done
As the ne% table creation is not being %ritten to the redo log files (the table %ill not be reGcreate
follo%ing a database failure
The redo log files are used to recover the database
Sql> Create &a,le %a$+De+t !O*O--.!- #/ %ele0t 1 (ro$ De+t 2
Creat"*g I*3e6DOrga*"Ye3 Tabe :
An inde organi2ed table +eeps its data stored according to the primary hey column values for
the table
An inde organi2ed table stores its data as if the entire table %as stored in an inde
To create an inde organi2ed table the organi2ation inde clause of the create table is used
SI ECreate Table "ample ("ampid Dumber(3!(
Sampname $archar0(0>!(
"ampdate -AT'(
Constraint "ampid"ampnameP;
Primary ;ey("ampid( "ampname ! !organi2ation inde K
To create a table as an inde organi2ed table %e must create a primary +ey constraint on it
To minimi2e the amount of active management of the inde table only of the tableSs data is very
static
An inde organi2ed table is most effective %hen the primary +ey constitutes a large part of the
tables columns
W%r?"*g W"t@ Part"t"%*e3 Tabes :
-ividing the ro%s of a single table into multiple parts is called partitioning of a table.
The table that is partitioned is called partitioned table and the parts are called partitions
The partitioning is useful for very large tables
T@e I$p%rta*t G%as 'e@"*3 Part"t"%*"*g AreG
The performance of Fueries against the tables can improve performance
The management of the table becomes easier
As the partitioned table date is stored in multiple parts ( it is easier to load and delete data in
partitions than in the large table
The bac+up and recovery operating can perform better
Oragle 10 g Material

134
SI ECreate Table "ampletable("ampleid Dumber(3! Primary ;ey (
"ampname $archar0(0>!(
"ampdate -AT'(
"ampdesc LOD) ! K
Create Part"t"%* Tabe :
SI E Create Table "ampletablepart("ampid Dumber(3! Primary ;ey (
"ampname $archar0(0>! (
"ampdate -ate (
"ampdesc $archar0( 3>>> ! !
PA&TITIOD *y &AD)'( "ampid !
(PA&TITIOD "ampidpart/
$AL@'" L'"" TIAD(4>>>! (
PA&TITIOD "ampidpart0
$AL@'" L'"" TIAD(/>>>! (
PA&TITIOD "ampidpart1
$AL@'" L'"" TIAD ( 6avalue ! ! K
The maimum value need not be specified fr the last partition( the mavalue +ey%ord is
specified.
The mavalue specifies oracle to use the partition to store any data that could not be stored In
the easier partitions
,e can create multiple partitions each %ith its F%n upper value defined
The minimum value for the range is implicitly determined by oracle from the definition of the
preceding partition
HASH PARTITIONS UPON A TA'LE :
A hash partition upon a table determines the physical placement of data
The physical placement of data is determined by performed a hash function on the values of the
partition +ey
In hash partition consecutive values of the partition +ey are not generally stored in the same
partition
Iash partitioning distributes a set of records over a greater set of partitions( decreasing the
li+elihood for iCo contention
To create a hash partition %e use the partition by hash clause
<ormat choice
"pecify the number of partitions and the table space to use
specify the named partitions
SI E Create Table 'mptablehash('mpno Dumber(5!
Constraint 'mpnop+ Primary ;ey(
'name $archar0(1>! (
=ob $achar0(1>! (
-eptno Dumber(0! (
"al Dumber(8(0! (
Constraint -eptnof+RIash
<oreign ;ey( -eptno !
&eferences -ept( deptno ! !
Oragle 10 g Material

135
PA&TITIOD *? IA"I( -eptno !
PA&TITIOD" />K
SI E Create Table 'mptablehash ( 'mpno Dumber(5!
Constraint 'mpnop+ Primary +ey(........!
PA&TITIOD *? IA"I(-eptno!
PA&TITIOD 0 "T&O&' ID
(-eptnopart/ts ( -eptnopart0ts ! K
WOR<ING WITH LIST PARTITIONING :
In list partitioning %e specify oracle all the possible values and designated the partition into %hich
the corresponding ro%s should be inserted
SIECreate Table 'mpsamplelist
('mp Dumber(3!
Constraint 'mpnop+ Primary ;ey(
'name $archar0(0>! (
"al Dumber(8(1! (
Iiredate -ate (
-eptno Dumber(0! (
=ob $archar0(/4! (
Constraint -eptnof+ <oreign ;ey ( -eptno !
&eferences -ept( -eptno ! !
PA&TITIOD =part/ $AL@'" (LP&'"I-'DTS(SADALI"TS! (
PA&TITIOD =part0 $alues(L6ADA)'&S(S"AL'"6ADS(SCL'C;S! ! K
Generatin !"# $artition! %
sub partitions are partitions of partition
sub partitions can be used to combine the t%o types of partitions
/. &AD)' PA&TITIOD"
0. IA"I PA&TITIOD"
In very large tables( the Composite Partition strategy is an effective %ay of separating the data
into manageable and tunable divisions
SI E Create Table 'mpsamplesubpart ( 'mpno Dumber(5! Primary ;ey (
'name $archar0(1>! (
"al number(8(0! (
-eptno Dumber(0! (
=ob $archar0(1>! (
Constraint -eptnof+ <oreign ;ey( -eptno !
&eferences -ept( -eptno ! !
PA&TITIOD *? &AD)' ('name!
"@*PA&TITIOD *? IA"I(=ob!
"@*PA&TITIOD" 4
(PA&TITIOD Damep/ $AL@'" L'"" TIAD(S6S!
PA&TITIOD Damep0 $AL@'" L'"" TIAD ( 6avalue ! K
SPLITTING TA'LE PARTITIONS :
SIEAlter Table "ampletablepart "PLIT PA&TITIOD "ampidpart1 AT (0>>>!
IDTO (PA&TITIOD "ampidpart1 (
PA&TITIOD "A6PI-PA&T3 ! K
Oragle 10 g Material

136
SI E Alter Table 'mpsamplelist "PLIT PA&TITIOD =part0 $alues(L"AL'"6ADS! C
IDTO(PA&TITIOD =salesman( PA&TITIOD "salesman ! K
Merg"*g tabe PARTITIONS :
SI E Alter Table 'mpsamplelist 6'&)' PA&TITIOD" =part/(
=salesman IDTO PA&TITIOD =part/ K
SI E Alter Table "ampletablepart 6'&)' PA&TITIOD" "ampidpart0 (
"amplepart1 IDTO PA&TITIOD "ampidpart1 K
Dr%pp"*g a tabe PARTITION :
SI E Alter Table "ampletablepart -&OP PA&TITIOD "ampleidpart1 K
SI E Alter Table 'mpsamplelist -&OP PA&TITIOD =part/ K
Creat"*g I*3e6es 5p%* PARTITIONS :
Once a partitioned table is created (%e have to create an inde upon that table
The inde may be partitioned according to the same range of values as that %ere used to
partition the table
The indeed partitions can be palced into specific table spaces
SI E Create Inde 'mpsamplelistin OD 'mpsamplelist ( =ob ! LOCAL
( PA&TITIOD =part/(=part0! K

The local +ey%ord tells oracle to create a separate inde for each partition on the table
The global +ey%ord tells oracle to create a nonpartitioned inde
Local indees are easier to manage than global indees
)lobal indees can perform uniFueness chec+s faster than local indees
O'.ECT TA'LES :
ID an O*='CT TA*L' each ro% is treated as an O*='CT
'ach ro% in an ob#ect table has an OI-
The OI- is assigned by oracle %hen the ro% is created.
The ro%s of an ob#ect table can be referenced by other ob#ects %ith in the database
An ob#ect table is created using the create table command
CREATING AN O'.ECT :
"ynta B
C&'AT' O& &'PLAC' T?P' QT?P' DA6'P A" O*='CT
( Column Dame/ -ata Type("i2e!(.............Column Dame n
-ata Type( "i2e ! ! K
SI E C&'AT' O& &'PLAC' T?P' "tudent A" O*='CT( "tudid Dumber(5! (
"name $archar0(0>!(
Oragle 10 g Material

137
-ob -ate (
-oa -ate (
<ees Dumber(7(0! ! K
CREATING AN O'.ECT TA'LE :
"ynta B
Create Table TableRDame of typename K
SI E Create Table 6castudent Of "tudent K
The above statement creates the table mcastudent as an abstract data type
'ach ro% in the ob#ect table has an OI- value
The ro%s in ob#ect table are referenced as O*='CT"
INSERTING ROWS INTO O'.ECT TA'LES :
To insert a record into an ob#ect table %e may use the constructor method of the actual datatype
or directly implement the insert ststement
The normal insert or &')ATIODAL ID"'&T is possible only %hen the table doesnot contain any
nested datatypes
INSERT UNSING CONSTRUCTOR METHOD :
SI E Insert Into 6castudent $alues (9/013(S"atishS(S>4G"epG99S("ysdate(/1>> ! K
INSERT USING WITHOUT CONSTRUCTOR METHOD :
SI E Insert Into 6castudent $alues(/013(S;umarS(S>7GOctG98S("ysdate(/4>>> ! K
INSERTING RECORDS THROUGH COLUMN REFERENCES :
SI E Insert Into 6castudents ( "tudid( "tudname ! $alues ( /013( S;rishnaS ! K
UPDATING DATA FROM O'.ECT TA'LES :
SI E @pdate 6castudent "et "tudname \ L"riramS ,here "tudid \ /013 K
DELETING DATA FROM O'.ECT TA'LES :
SI E -elete <rom 6castudent ,here "tudid \ /013 K
SI E -elete <rom 6castudent K
SELECTING DATA FROM O'.ECT TA'LES :
the abstract data types column can be referred as a part of the tableSs columns
SI E "elect H <rom 6castudent K
Oragle 10 g Material

138
SI E "elect "tudid ( "tudname <rom 6castudent K
SI E "elect "tudid ( "tudname <rom 6castudent ,here "tudid\/013 K
THE REF FUNCTION :
The ref function allo%s to reference eisting ro% ob#ects
The OI- assigned to each ro% can be seen by using the &'< function
SI E "'L'CT &'<(A! from 6castudent A ,here "tudname \ S"ATI"IS K
The &'<(A! value %ill be different under different system and may be %rapped onto multiple
lines
The &'< function ta+es as its input the alias given to the ob#ect table
The &'< functions ta+es can only reference ro% ob#ects (hence %e cannot use &'< for
referencing column ob#ects
The column ob#ects can be of
/. Abstract data types
0. LO*Ss
1. collections
The &'< function by itself does not give any useful information
DEREF FUNCTION :
The -'&'< function ta+es a reference value i.e( the OI- generated for a reference and return
the value of the ro% ob#ect
SI E Create Table "tudentincharges ( Inchargename $archar0(1>!(
"tudentincharcge &'< "tudent ! K
The "tudentIncharge column references the data that is stored else%here
The &'< function points the "tundentInchage column to a ro% ob#ect of the student data type.
As mcastudent is an ob#ect table of the student data types the studentIncharge column can point
to the ro% ob#ect %ithin the mcastudent ob#ect table
The studentIncharge table can be described as an ordinary table .
SI E -'"C "tudentIncharge K
"eeing full details of the reference
SI E "'T -'"C&I*' -'PTI 0 K
SI E -'"C "tudentIncharge K
INSERTING RECORDS INTO REFERENCE TA'LES :
To insert records into reference tables %e have to use the &'< function
SI E ID"'&T IDTO "tudentIncharges "'L'CT L"@*&A6AD?A6 "I&6AS &'<(A!
Oragle 10 g Material

139
<rom 6castudents A %here Dame\S"ATI"I ;@6A&S K
IN THE A'O/E CASE :
/. The mcastudents table is Fueried first
0. The &'< function return the OI- for the ro% ob#ect selected
1. TI' selected OI- is stored in the studentIncharge table as a pointer to thet ro% ob#ect in
the mcastudents ob#ect table
3. The studentIncharges actually contains the name of the studentIncharge and a
reference to a ro% ob#ect in the mcastudents table
4. The &'<'&'DC' OI- can be seem by Fuerying upon the studentIncharge table
SI E "'L'CT H <&O6 studentIncharge K
The reference value cannot be seem until %e use the -'&'< function
SI E "'L'CT -'&'<(AP"tudentIncharge! <&O6 studentIncharge A
,I'&' InchargeDame\ S"@*&A6AD?A6 "IA&6AS K
The parameter for the -'&'< function is the column Dame of the &'< column but not the table
Dame.
POINTS TO NOTE :
The Fuery uses a reference to a ro% ob#ect to travel from one table to the second
A #oin is performing in the bac+ground %ithout specifying the #oin criteria
The ob#ect table it self is not mentioned in the Fuery
The name of the ob#ect table need not be +no%n to -'&'< the values
The entire referenced ob#ect is returned not #ust part of the ro%
IMPLEMENTING /ALUE FUNCTION :
SI E "'L'CT $AL@' (A! <&O6 6castudents A ,I'&' "DA6 \ S"ATI"IS K
/ALUE FUNCTION B
The $AL@' function is useful %hen debugging &'<'&'DC'"
The $AL@' function allo%s to Fuery the formatted values directly from the ob#ect table
,e can select the values from ob#ect table %ithout using the -'&'< Fuery upon the
studentIncharges ( studentIncharge column
IN/ALID REFERENCES :
,e can -'L'T' the ob#ect to %hich a &'<'&'DC' points
,e can -B'T' a ro% from mcastudents ob#ect table to %hich studentIncharge record ponts
SI E -'L'T' <&O6 6castudents ,I'&' name\Ssatish +umarS K
Oragle 10 g Material

140
DANGLING REF B It is a record %hich has an OI- pointing to a record in an ob#ect table for
%hich the reference record is not eisting in the original ob#ect table
As oracle generates a OI- for the ro% ob#ect %hich can be referenced by any other ro% from
other ob#ect table %hen the ro% ob#ect is -'L'T'- the OI- is lost and the oracle doesnSt reuse
the OI- numbers
Ience if a ne% record for L"ATI"I ;@6A&S is inserted this is given a ane OI- value (but the
studentIncharge record still points to the old value
In a relational system the #oin bet%een t%o tables in dependent only on the current data
In an OOP system the #oin is bet%een ob#ects (hence the fact that t%o ob#ects having the same
data does not mean they are same
O'.ECT /IEWSWITH REFS :
O*='CT vie%s are very important to super impose oops structures on an eiting relations table
,e can create abstract data types and use them %ithin the ob#ect vie% of an eisting table
O*='CT vie% acts as a bridge bet%een the eisting relational application to ob#ect relational
applications
CREATING USER DEFINED DATA T!PES AND INTEGRATING THEM TO RELATIONAL TA'LES :
RELATIONAL STAGE :
SI E Create Table "tudents( "tudid Dumber(5! Constraint "tudidp+ Primary ;ey (
"name $archar0(1>! (
"treet $archar0(3>! (
Cityname $archar0(04! (
"tatename $archar0(3>! (
Pincode Dumber(5! ! K
CREATING A'STRACT DATA T!PES :
SI E Create Or &aplace Type 6yaddress As Ob#ect ( "treet $archar0(3>! (
Cityname $archar0(04! (
"tatename $archar0(3>! (
Pincode number(5! ! K
SI E Create Or &eplace Type 6ystudent As Ob#ect("anme $archar0(1>!
"address 6yaddress ! K
As student table %as created %ithout using the 6yaddress and 6ystudent data types it has to be
accessed via O*='CT $I',".
The above principle of accessing data is called as O*='CT *A"'- ACC'""
An O*='CT $I', can be specified upon the abstract data types to apply onto the relational
table
CREATING AN O'.ECT /IEW :
SI E Create Or &eplace $ie% "tudentov ("tudid ("tuddef ! As
"elect "tudid ( 6ystudent ("name (
Oragle 10 g Material

141
6yaddress ("treet ( Cityname ( "tatename( Pincode ! !
<&O6 students K
%e can access the students table directly as a relational table or via the COD"T&@CTO&
methods for the abstract data types
O'.ECT /IEWS WITH REFERENCES :
If the students table is related to another table (then (ob#ect vie%s can be create as a references
bet%een the table
*y using the above concept O&ACL' uses the eisting Primary ;'?[<oreign ;ey relationships
to simulated OI-Ss for use by &'<Ss bet%een the table.
*y Implementing the above concept %e can access the table either as &'LATIODAL TA*L' O&
A" O*='CT"
,hen the table are treated as ob#ects %e can use the &'<Ss to automatically performs #oins upon
the tables using the -'&'< function
GENERATING OIDTS :
,e Can @se An Ob#ect vie% to assign OI-Ss to the records in students table.
OI-Ss are assigned to records inan O*='CT TA*L' and an O*='CT TA*L' in turn is based on
an A*"T&ACT -ATAT?P'"
As first step create an A*"T&ACT -ATA T?P' that has the same structure as the student
SI E Create Or &eplace Type "tudent Type As Ob#ect ( "tudid Dumber(5! (
"name $A&CIA&0(1>! ("treet $archar0(3>! (
Cityname $archar0(04!( "tatename $archar0(3>! (
Pincode Dumber( 5 ! ! K
As the net step create a vie% based upon the studenttype by assigning OI- values to the
records in stundent table
CREATE A REFERENCE TA'LE FOR STUDENTS :
SI E Create Table "tudent*oo+s( Libtranno Dumber(5!( "tudid Dumber(5!(
*oo+tittle $archar0(4>! ( Lendingdate -ate (
Constraint LibtreamDoP; Primary ;ey
(LibTeamDO( "tudid! (
Constraint "tudent*oo+s<; <oreign ;ey
("tudid! &eferences "tudents("tudid ! ! K
SI E ID"'&T IDTO "tudents $AL@'" ( />>>(S"ATI"I ;@6A&S(
S"II$ *A)IS ( SI?-'&A*A-S( SAD-I&A P&A-'"IS ( 4>5>>/ ! K
SI E ID"'&T IDTO "tudent*oo+s $AL@'" (/>>>( 0>>>( STI'&6O -?DA6IC"S( "?"-AT' ! K
"Fl P "elect H <rom Tab K
T*a$e TabT&pe C5stere3
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
Oragle 10 g Material

142
A-6ID TA*L'
A-6IDTA*L' TA*L'
A&TI"T TA*L'
I6A)'TA*L' TA*L'
TI'6' TA*L'
@PLOA- TA*L'
@PLOA-" TA*L'
@"'&" TA*L'
"Fl P -esc Admin K
Na$e N5Z T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
A-6ID $A&CIA&0(0>!
PA"",O&- $A&CIA&0(1>!
"Fl P -esc Artist K
Na$e N5Z T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
A&TI"TI- $A&CIA&0(0>!
A&TI"TDA6' $A&CIA&0(04!
PA"",O&- $A&CIA&0(/0!
IDTA&'A $A&CIA&0(/4!
'AP'&I'DC' $A&CIA&0(0!
CLA""DA6' $A&CIA&0(/0!
II"TO&? $A&CIA&0(044!
CODT'DT $A&CIA&0(044!
"Fl P -esc Theme K
Na$e N5Z T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
TI'6'I- D@6*'&(4!
TI'6'DA6' $A&CIA&0(/4!
CAT')O&? $A&CIA&0(/>!
TI'6'DOT' $A&CIA&0(044!
"Fl P -esc @pload K
Na$e N5Z T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
I6A)'I- D@6*'&(4!
<IL'DA6' $A&CIA&0( 1> !
A&TI"TI- $A&CIA&0( 0> !
I6A)'DA6' $A&CIA&0( 0> !
LOCATIOD D@6*'&( 1 !
Oragle 10 g Material

143
"Fl P -esc @sers K
Na$e N5Z T&pe
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
@"'&DA6' $A&CIA&0( 0> !
PA"",O&- $A&CIA&0( /0 !
DA6' $A&CIA&0( 04 !
PIOD' D@6*'&( /> !
'6AIL $A&CIA&0( 04 !
OCC@PATIOD $A&CIA&0( /4 !
A--&/ $A&CIA&0( 0> !
A--&0 $A&CIA&0( 0> !
A--&1 $A&CIA&0( 0> !
A--&3 $A&CIA&0( 0> !
"Fl P "elect H <rom Admintable K
USERID PWD
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
;alyan ;alyan
SI E C&'AT'O& &'PLAC' $I', "T@-'DTO$ O< "tudentType
,ITI O*='CT I-'DTI<I'& ("tudI-!A" "'L'CT "tudI-
"name( "treet( Cityname( "tatename( Pincode <rom "tudent K
The first part of the statement tells the database to C&'AT' a vie% based on the structure
defined in student Type
The net part of the C&'AT' $I', tells the database ho% to construct OI- values for the ro%s
in students
,ith the above step the ro%s of students are no% accessible as ro% O*='CT" via the
studentO$ $ie%
The PI- values generated for the studentO$ ro%s are called as P;OI-Ss as they are based upon
students primary +ey values
The relational tables can accessed as ro% O*='CT" if O*='CT $ie%s are created upon them
GENERATING REFERENCES :
Actually the ro%s of student*oo+s reference ro%s in students
Oragle 10 g Material

144
As per relational concept the relationship is determined by the foreign +ey pointing from the
"T@-'DT*OO;"."tudI- column to the "T@-'DTP"tudI- column
<rom the studentov O*='CT $ie% that has been created( and the ro%s in students table can be
accessed via OI-Ss %e have to C&'AT' &'<'&'DC' $AL@'" in student*oo+s that reference
students
Once the &efSs are create %e can use -'&'< function to Access the students data from student
*oo+s
SI E C&'AT' $I', "tudent*oo+sov As "'L'CT 6A;'R&'<( "tudentOv ( "tudI- !
studI-( LibTranDo( *oo+Tittle( Lending-ate <rom "tudent*oo+s K
MA<EQREF FUNCTION :
It creates &'<'&'DC'" %hich are called P;&'<Ss from an eisting vie% to another vie%
P;&'<Ss are named so as they are based on P&I6A&? ;'?"
The function ta+es as an arguments(s! as the name of the O*='CT $I', being &'<'&'DC'-
and the name of the column or columns that form the <O&'I)D ;'? in the Local Table
"ince 6A;'R&'< creates a vie% the result of an operation must be given a COL@6D ALIA"
,UER!ING THROUGH O'.ECT /IEWS :
,e use the -'&'< function to "'L'CT the value of the referenced data
The concept is almost all same as that of O*='CT $I',"
SI E "'LCT -'&'<("*."tudI-! <&O6 "tudent*oo+O$"* ,here
*oo+Tittle\STI'&6O -?DA6IC"S K
STEPS PERFORMED :
The Euery finds the record in the "tudent*oo+s table for %hich the Lending-ate is the Current
"ystem -ate .
Ta+ing the studid value from that record evaluates its reference
The evaluated reference studid is pointed to the P;OI- value in the studentOv O*='CT $I',
using 6A;'R&'<.
The "tudentO$ O*='CT $ie% returned the record %hose P;OI- matched the &'<'&'DC'-
value
The -'&'< function then gets activated returning the value of the referenced ro%
The Fuery return ro%s from students even through the @"'& actually Fueried on "tudent*oo+s
THINGS TO NOTE :
Ob#ect vie%s of column ob#ects enable to %or+ %ith table as if they %ere both relational tables
and ob#ect relational table
,hen O*='CT vie%s are etended to ro% O*='CT they enable to generated OI- values based
on established <O&'I)D ;?[ P&I6A&? ;'? relationship
O*='CT $ie%s allo% us to continue to use the eisting constraints and standard
ID"'&T(-'L'T' @P-AT' and "'L'CT statements
They help in using OOP features such as &'<'&'DC'" against the Ob#ect TA*L'"
They provide an Technological *ridge for migrating to an oops db architecture
Oracle performs =OID" that resolve the references defined in the database
,hen referencesXd data is retrieved it beings the entire ro% ob#ect that %as referenced
Oragle 10 g Material

145
To reference the data %e need to establish POI-Ss in the table that is LP&I6A&I ;'?S table
relationship
@se 6A;'R&'< to generate references in the table that is L<O&'I)D ;'?S table relationship
Once the above specifications are completed %e can %or+ %ith the data as if it %ere stored in
O*='CT TA*L'".
Oragle 10 g Material

146