You are on page 1of 87

ORACLE

http://dev.fyicenter.com/faq/oracle/
What Is Oracle?
Oracle is a company. Oracle is also a database server, which manages data in a very structured way. It allows users to store
and retrieve related data in a multiuser environment so that many users can concurrently access the same data. All this is
accomplished while delivering high performance. A database server also prevents unauthorized access and provides
efficient solutions for failure recovery.
What Is an Oracle Database?
An Oracle database is a collection of data treated as a big unit in the database server.
What Is an Oracle Instance?
very running Oracle database is associated with an Oracle instance. When a database is started on a database server
!regardless of the type of computer", Oracle allocates a memory area called the #ystem $lobal Area !#$A" and starts one
or more Oracle processes. %his combination of the #$A and the Oracle processes is called an Oracle instance. %he
memory and processes of an instance manage the associated database&s data efficiently and serve the one or multiple
users of the database.
What Is a 'arameter (ile?
A parameter file is a file that contains a list of initialization parameters and a value for each parameter. )ou specify
initialization parameters in a parameter file that reflect your particular installation. Oracle supports the following two types of
parameter files*
• #erver 'arameter (iles + ,inary version. 'ersistent.
• Initialization 'arameter (iles + %e-t version. .ot persistent.
What Is a #erver 'arameter (ile?
A server parameter file is a binary file that acts as a repository for initialization parameters. %he server parameter file can reside
on the machine where the Oracle database server e-ecutes. Initialization parameters stored in a server parameter file are
persistent, in that any changes made to the parameters while an instance is running can persist across instance shutdown and
startup.
What Is a Initialization 'arameter (ile?
An initialization parameter file is a te-t file that contains a list of initialization parameters. %he file should be written in the client&s
default character set. #ample initialization parameter files are provided on the Oracle distribution medium for each operating
system. A sample file is sufficient for initial use, but you will probably want to modify the file to tune the database for best
performance. Any changes will ta/e effect after you completely shut down and restart the instance.
What is #ystem $lobal Area !#$A"?
%he #ystem $lobal Area !#$A" is a memory area that contains data shared between all database users such as buffer
cache and a shared pool of #01 statements. %he #$A is allocated in memory when an Oracle database instance is started,
and any change in the value will ta/e effect at the ne-t startup.
What is 'rogram $lobal Area !'$A"?
A 'rogram $lobal Area !'$A" is a memory buffer that is allocated for each individual database session and it contains
session specific information such as #01 statement data or buffers used for sorting. %he value specifies the total memory
allocated by all sessions, and changes will ta/e effect as new sessions are started.
What Is a 2ser Account?
A user account is identified by a user name and defines the user&s attributes, including the following*
• 'assword for database authentication
• 'rivileges and roles
• Default tablespace for database ob3ects
• Default temporary tablespace for 4uery processing wor/ space
What Is the 5elation of a 2ser Account and a #chema?
2ser accounts and schemas have a one+to+one relation. When you create a user, you are also implicitly creating a schema
for that user. A schema is a logical container for the database ob3ects !such as tables, views, triggers, and so on" that the
user creates. %he schema name is the same as the user name, and can be used to unambiguously refer to ob3ects owned
by the user.
What Is a 2ser 5ole?
A user role is a group of privileges. 'rivileges are assigned to users through user roles. )ou create new roles, grant
privileges to the roles, and then grant roles to users.
What is a Database #chema?
A schema is a collection of logical structures of data, or schema ob3ects. A schema is owned by a database user and has
the same name as that user. ach user owns a single schema. #chema ob3ects can be created and manipulated with #01
and include* tables, views, and other types of data ob3ects.
What Is a Database %able?
A database table is a basic unit of data logical storage in an Oracle database. Data is stored in rows and columns. )ou
define a table with a table name, such as employees, and a set of columns. )ou give each column a column name, such as
employee6id, last6name, and 3ob6id7 a datatype, such as 8A59:A5;, DA%, or .2<,57 and a width. %he width can be
predetermined by the datatype, as in DA%. If columns are of the .2<,5 datatype, define precision and scale instead of
width. A row is a collection of column information corresponding to a single record.
What Is a %able Inde-?
Inde- is an optional structure associated with a table that allow #01 statements to e-ecute more 4uic/ly against a table. =ust
as the inde- in this manual helps you locate information faster than if there were no inde-, an Oracle Database inde-
provides a faster access path to table data. )ou can use inde-es without rewriting any 4ueries. )our results are the same,
but you see them more 4uic/ly.
What Is an Oracle %ablespace?
An Oracle tablespace is a big unit of logical storage in an Oracle database. It is managed and used by the Oracle server to
store structures data ob3ects, li/e tables and inde-es.
ach tablespace in an Oracle database consists of one or more files called datafiles, which are physical structures that
conform to the operating system in which Oracle is running.
1
What Is an Oracle Data (ile?
An Oracle data file is a big unit of physical storage in the O# file system. One or many Oracle data files are organized
together to provide physical storage to a single Oracle tablespace.
What Is a #tatic Data Dictionary?
Data dictionary tables are not directly accessible, but you can access information in them through data dictionary views. %o
list the data dictionary views available to you, 4uery the view DI9%IO.A5). <any data dictionary tables have three
corresponding views*
• An A116 view displays all the information accessible to the current user, including information from the current user&s
schema as well as information from ob3ects in other schemas, if the current user has access to those ob3ects by way of
grants of privileges or roles.
• A D,A6 view displays all relevant information in the entire database. D,A6 views are intended only for administrators.
%hey can be accessed only by users with the #19% A.) %A,1 privilege. %his privilege is assigned to the D,A role
when the system is initially installed.
• A 2#56 view displays all the information from the schema of the current user. .o special privileges are re4uired to
4uery these views.
What Is a Dynamic 'erformance 8iew?
Oracle contains a set of underlying views that are maintained by the database server and accessible to the database
administrator user #)#. %hese views are called dynamic performance views because they are continuously updated while a
database is open and in use, and their contents relate primarily to performance. Although these views appear to be regular
database tables, they are not. %hese views provide data on internal dis/ structures and memory structures. )ou can select
from these views, but you can never update or alter them.
What Is a 5ecycle ,in?
5ecycle bin is a logical storage to hold the tables that have been dropped from the database, in case it was dropped in error.
%ables in recycle bin can be recovered bac/ into database by the (lashbac/ Drop action. Oracle database recycle save the
same purpose as the recycle bin on your Windows des/top.
5ecycle bin can be turned on or off in the recyclebin>on?off in your parametere file.
What Is #01@'lus?
#01@'lus is an interactive and batch 4uery tool that is installed with every Oracle Database #erver or 9lient installation. It
has a command+line user interface, a Windows $raphical 2ser Interface !$2I" and the i#01@'lus web+based user interface.
What Is %ransport .etwor/ #ubstrate !%.#"?
%.#, %ransport .etwor/ #ubstrate, is a foundation technology, built into the Oracle .et foundation layer that wor/s with any
standard networ/ transport protocol.
What Is Open Database 9ommunication !OD,9"?
OD,9, Open Database 9ommunication, a standard A'I !application program interface" developed by <icrosoft for Windows
applications to communicate with database management systems.
Oracle offers OD,9 drivers to allow Windows applications to connect Oracle server through OD,9.
What Is #01@'lus?
#01@'lus is an interactive and batch 4uery tool that is installed with every Oracle Database #erver or 9lient installation. It
has a command+line user interface, a Windows $raphical 2ser Interface !$2I" and the i#01@'lus web+based user interface.
#01@'lus has its own commands and environment, and it provides access to the Oracle Database. It enables you to enter
and e-ecute #01, '1?#01, #01@'lus and operating system commands to perform the following*
• (ormat, perform calculations on, store, and print from 4uery results
• -amine table and ob3ect definitions
• Develop and run batch scripts
• 'erform database administration
)ou can use #01@'lus to generate reports interactively, to generate reports as batch processes, and to output the results to
te-t file, to screen, or to :%<1 file for browsing on the Internet. )ou can generate reports dynamically using the :%<1
output facility of #01@'lus, or using the dynamic reporting capability of i#01@'lus to run a script from a web page.
:ow %o #tart the 9ommand+1ine #01@'lus?
If you Oracle server or client installed on your windows system, you can start the command+line #01@'lus in two ways*
A. 9lic/ #tart B All 'rograms B Oracle ... B #tart #01 9ommand 1ine. %he #01@'lus command window will show up with a
message li/e this*
SQL*Plus: Release 10.2.0.1.0 - Production on Tue ...
Copyright c! 1"#2$ 200%$ &racle. 'll rights reser(ed.
SQL)
;. 9lic/ #tart B 5un..., enter CcmdC and clic/ OD. A Windows command window will show up. )ou can then use Windows
commands to start the command+line #01@'lus as shown in the tutorial e-ercise below*
)cd c:*oracle+e*app*oracle*product*10.2.0*ser(er*
).*,in*s-lplus .nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Tue ...
Copyright c! 1"#2$ 200%$ &racle. 'll rights reser(ed.
SQL)
:ow %o $et :elp at the #01 'rompt?
Once #01@'lus is started, you will get a #01 prompt li/e this* #01B. %his where you can enter commands for #01@'lus to
run.
%o get help information at the #01 prompt, you can use the :1' command as shown in the following tutorial e-ample*
SQL) /0LP 12304
0nter /elp 5topic6 7or help.
8 C&P9 P':S0 S/:T3&;2
88 30<120 PR12T SP&&L
2
. 30L PR&=PT SQLPL:S
'CC0PT 30SCR1>0 Q:1T ST'RT
'PP023 31SC&220CT R0C&?0R ST'RT:P
'RC/1?0 L&@ 031T R0='RA ST&R0
'TTR1>:T0 040C:T0 R0P<&&T0R T1=12@
>R0'A 041T R0P/0'30R TT1TL0
...
C&=P:T0 L1ST S0T 4Q:0R9
C&220CT P'SS;&R3 S/&;

SQL) /0LP C&220CT
C&220CT
-------
Connects a gi(en usernaBe to the &racle 3ata,ase. ;hen you
run a C&220CT coBBand$ the site pro7ile$ glogin.s-l$ and
the user pro7ile$ login.s-l$ are processed in that order.
C&220CT does not reproBpt 7or usernaBe or passCord i7 the
initial connection does not succeed.
C&2250CT6 5DlogonE.Epro+yF 5'S DS9S&P0RES9S3>'F66
Chere logon has the 7olloCing synta+:
usernaBe5.passCord658connectGidenti7ier6
What Information Is .eeded to 9onnect #01@'lus an Oracle #erver?
If you want to connect your #01@'lus session to an Oracle server, you need to /now the following information about this
server*
• %he networ/ hostname, or I' address, of the Oracle server.
• %he networ/ port number where the Oracle server is listening for incoming connections.
• %he name of the target database instance managed by the Oracle server.
• %he name of your user account predefined on in the target database instance.
• %he password of your user account predefined on in the target database instance.
What Is a 9onnect Identifier?
A Cconnect identifierC is an identification string of a single set of connection information to a specific target database instance
on a specific Oracle server.
9onnect identifiers are defined and stored in a file called tnsnames.ora located in EO5A916:O<?networ/?admin?
directory. :ere is one e-ample of a Cconnect identifierC definition*
<91G40 H
30SCR1PT1&2 H
'33R0SS H
PR&T&C&L H TCP!
/&ST H d,a.7yicenter.coB!
P&RT H 1%21!
!
C&220CTG3'T' H
S0R?0R H 3031C'T03!
S0R?1C0G2'=0 H 40!
!
!
%he above Cconnect identifierC defines C%.#6FC with the following connection information*
• %he networ/ hostname* dba.fyicenter.com.
• %he networ/ port number* AG;A.
• %he name of the target database instance* F.
:ow %o 9onnect a #01@'lus #ession to an Oracle #erver?
In order to connect a #01@'lus session to an Oracle server, you need to*
A. Obtain the connection information from the Oracle server D,A.
;. Define a new Cconnect identifierC called C()I6FC in your tnsnames.org file with the given connection information.
H. 5un the 9O..9% command in #01@'lus as shown in the tutorial e-ercise below*
)cd c:*oracle+e*app*oracle*product*10.2.0*ser(er*
).*,in*s-lplus .nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Tue ...
Copyright c! 1"#2$ 200%$ &racle. 'll rights reser(ed.
SQL) C&220CT 7yi.retneciy78<91G40I
Connected.
SQL) S0L0CT S9S3'T0 <R&= 3:'LI
S9S3'T0
---------
0%-='R-0J
What :appens If )ou 2se a Wrong 9onnect Identifier?
Of course, you will get an error, if you use a wrong connect identifier. :ere is an e-ample of how #01@'lus react to a wrong
connect identifier*
SQL) C&220CT 7yi.retneciy78;R&2@I
3
0RR&R:
&R'-121%K: T2S:could not resol(e the connect identi7ier
speci7ied
;arning: 9ou are no longer connected to &R'CL0.
What you need to do in this case*
• 9hec/ the 9O..9% command to ma/e sure that the connect identifier is entered correctly.
• 9hec/ the tnsnames.ora file to ma/e sure that the connect identifier is defined correctly.
• 9hec/ the tnsnames.ora file to ma/e sure that there is no multiple definitions of the same connect identifier.
• 9hec/ your files system to see if you have multiple copies of tnsnames.ora in different Oracle home directories,
because you installed multiple versions of Oracle. If you do have multiple copies, ma/e sure your #01@'lus session is
pic/ing up the correct copy of tnsnames.ora.
What %o Do If D,A 1ost the #)#%< 'assword?
If the D,A lost the password of the #)#%< user account, he?she can go to the Oracle server machine, and run #01@'lus
on server locally with the operating system authentication method to gain access to the database. %he tutorial e-ercise
below shows you how*
TerBinal ser(er to the &racle ser(er Bachine!
Start SQL*Plus!
SQL)C&220CT . 'S S9S3>'
Connected.
SQL) 'LT0R :S0R S9ST0= 1302T1<103 >9 ssapGiy7I
:ser altered.
.otice that the !?" in the 9O..9% command tells #01@'lus to use the current user on local operating system as the
connection authentication method.
What %ypes of 9ommands 9an ,e -ecuted in #01@'lus?
%here are I types of commands you can run at the #01@'lus command line prompt*
A. #01 commands + #tandard #01 statements to be e-ecuted on target database on the Oracle server. (or e-ample*
C#19% @ (5O< fyi6fa47C is a #01 command.
;. '1?#01 commands + '1?#01 statements to be e-ecuted by the Oracle server. (or e-ample* CF92%
D,<#6O2%'2%.'2%61I.!&Welcome to dba.fyicenter.com&"C runs a '1?#01 command.
#01@'lus commands + 9ommands to be e-ecuted by the local #01@'lus program itself. (or e-ample* C#% .211 &.211&C is
a #01@'lus command.
O# commands + 9ommands to be e-ecuted by the local operating system. (or e-ample* C:O#% dirC runs an operating
system command on the local machine.
:ow %o 5un #01 9ommands in #01@'lus?
If you want to run a #01 command in #01@'lus, you need to enter the #01 command in one or more lines and terminated
with !7". %he tutorial e-ercise below shows a good e-ample*
SQL) S0L0CT L;elcoBeML <R&= 3:'LI
L;0LC&=0
--------
;elcoBeM
SQL) S0L0CT L;elcoBe to <91center.coB tutorialsML
2 <R&= 3:'L
N I
L;0LC&=0T&<91C02T0R.C&=T:T&R1'LSML
-----------------------------------
;elcoBe to <91center.coB tutorialsM
:ow %o 5un '1?#01 #tatements in #01@'lus?
If you want to run a single '1?#01 statement in #01@'lus, you need to use the F92% command as shown in the
following tutorial e-ample*
SQL) S0T S0R?0R&:TP:T &2
SQL) 040C:T0 3>=SG&:TP:T.P:TGL120L;elcoBe to <91centerML!
;elcoBe to <91centerM
PL.SQL procedure success7ully coBpleted.
:ow %o 9hange #01@'lus #ystem #ettings?
#01@'lus environment is controlled a big list of #01@'lus system settings. )ou can change them by using the #%
command as shown in the following list*
• #% A2%O9O<<I% O(( + %urns off the auto+commit feature.
• #% (D,A9D O(( + #tops displaying the C;J rows selected.C message at the end of the 4uery output.
• #% :ADI.$ O(( + #tops displaying the header line of the 4uery output.
• #% 1I.#IK ;GL + #ets the number of characters per line when displaying the 4uery output.
• #% .W'A$ ; + #ets ; blan/ lines to be displayed on each page of the 4uery output.
• #% .W'A$ .O. + #ets for no blan/ lines to be displayed on each page of the 4uery output.
• #% .211 &null& + As/s #01@'lus to display &null& for columns that have null values in the 4uery output.
• #% 'A$#IK LM + #ets the number of lines per page when displaying the 4uery output.
• #% %I<I.$ O. + As/s #01@'lus to display the command e-ecution timing data.
• #% W5A' O(( + %urns off the wrapping feature when displaying 4uery output.
4
:ow %o 1oo/ at the 9urrent #01@'lus #ystem #ettings?
If you want to see the current values of #01@'lus system settings, you can use the #:OW command as shown in the
following tutorial e-ercise*
SQL) S/&; ':T&C&==1T
autocoBBit &<<
SQL) S/&; /0'312@
heading &2
SQL) S/&; L120S1O0
linesiPe #0
SQL) S/&; P'@0S1O0
pagesiPe 1K
SQL) S/&; <003>'CA
<003>'CA &2 7or J or Bore roCs
SQL) S/&; T1=12@
tiBing &<<
SQL) S/&; 2:LL
null QQ
SQL) S/&; 'LL
appin7o is &<< and set to QSQL*PlusQ
arraysiPe 1%
autocoBBit &<<
autoprint &<<
autoreco(ery &<<
autotrace &<<
,locRterBinator Q.Q he+ 2e!
cBdsep &<<
colsep Q Q
coBpati,ility (ersion 2'T1?0
concat Q.Q he+ 2e!
copycoBBit 0
C&P9T9P0C/0CA is &2
de7ine QSQ he+ 2J!
descri,e 30PT/ 1 L1202:= &<< 12302T &2
echo &<<
...
What Are #01@'lus nvironment 8ariables?
,ehaviors of #01@'lus are also controlled a some environment variables predefined on the local operating system. :ere are
some commonly used #01@'lus environment variables*
• O5A916:O< + %he home directory where your Oracle client application is installed.
• 'A%: + A list of directories where #01@'lus will search for e-ecutable or D11 files. 'A%: should include
EO5A916:O<Nbin.
• #01'12# + %he directory where localization messages are stored. #01'12# should be set to
EO5A916:O<Ns4lplusNmesg
• %.#6AD<I. + %he directory where the connect identifier file, tnsnames.ora is located. %.#6AD<I. should be set to
EO5A916:O<?networ/?admin.
:ow %o $enerate 0uery Output in :%<1 (ormat?
If you want your 4uery output to be generated in :%<1 format, you can use the C#% <A5D2' :%<1 O.C to turn on the
:%<1 feature. %he following tutorial e-ercise gives you a good e-ample*
SQL) connect /R.retneciy7
SQL) S0T ='RA:P /T=L &2
SQLSgtI S0L0CT <1RSTG2'=0$ L'STG2'=0$ /1R0G3'T0
T,r)
2 <R&= 0=PL&900S ;/0R0 <1RSTG2'=0 L1A0 LUohVLI
T,r)
Tp)
Tta,le ,orderHL1L CidthHL"0VL alignHLcenterL suBBaryHLScript outputL)
Ttr)
Tth scopeHQcolQ)
<1RSTG2'=0
T.th)
Tth scopeHQcolQ)
L'STG2'=0
T.th)
Tth scopeHQcolQ)
/1R0G3'T0
T.th)
T.tr)
Ttr)
Ttd)
5
Uohn
T.td)
Ttd)
Seo
T.td)
Ttd)
12-<0>-"#
T.td)
T.tr)
Ttr)
Ttd)
Uohn
T.td)
Ttd)
Russell
T.td)
Ttd)
01-&CT-"J
T.td)
T.tr)
T.ta,le)
Tp)
What Is Output #pooling in #01@'lus?
%he output spooling a nice feature of the command+line #01@'lus tool. If the spooling feature is turned on, #01@'lus will
send a carbon copy of the everything on your screen to a specified local file.
Output spooling is used mostly for 4uic/ dump of data to local files. :ere are the commands to turn on and off output
spooling in #01@'lus*
• #'OO1 file.ame + %urning on output spooling with the specified file.
• #'OO1 O(( + %urning off output spooling and close the spool file.
:ow %o #ave 0uery Output to a 1ocal (ile?
.ormally, when you run a #19% statement in #01@'lus, the output will be displayed on your screen. If you want the
output to be saved to local file, you can use the C#'OO1 file.ameC command to specify a local file and start the spooling
feature. When you are done with your #19% statement, you need to close the spool file with the C#'OO1 O((C
command. %he following tutorial e-ercise gives you a good e-ample*
SQL) connect /R.retneciy7
SQL) S0T /0'312@ &<<
SQL) S0T <003>'CA &<<
SQL) S0T L120S1O0 1000
SQL) SP&&L *teBp*eBployees.lst
SQL) S0L0CT * <R&= 0=PL&900SI
......
SQL) SP&&L &<<
)ou should get all records in employees.lst with fi-ed length fields.
What Is Input ,uffer in #01@'lus?
Input buffer is a nice feature of the command+line #01@'lus tool. It allows you to revise a multiple+line command and re+run
it with a couple of simple commands. ,y default, input buffer is always turned on in #01@'lus. %he last #01 statement is
always stored in the buffer. All you need is to remember to following commonly used commands*
• 1I#% + Displays the #01 statement !the last e-ecuted #01 statement" in the buffer.
• 52. + 5uns the #01 statement in the buffer again. C7C is a 4uic/ command e4uivalent to 52..
• 91A5 ,2((5 + 5emoves the #01 statement in the buffer.
• I.'2% line + Adds a new line into the buffer.
• A''.D te-t + Appends more te-t to the last line in the buffer.
• D1 + Deletes one line from the buffer.
• 9:A.$ ?old?new + 5eplaces &old& te-t with &new& te-t in the buffer.
:ow %o 5evise and 5e+5un the 1ast #01 9ommand?
If e-ecuted a long #01 statement, found a mista/e in the statement, and you don&t want enter that long statement again, you
can use the input buffer commands to the correct last statement and re+run it. %he following tutorial e-ercise gives you a
good e-ample*
SQL) connect /R.retneciy7
SQL) S0L0CT <1RSTG2'=0$ L'STG2'=0$ /1R0G3'T0
2 <R&= 0=PL&900 ;/0R0 <1RSTG2'=0 L1A0 LUohVLI
<R&= 0=PL&900 ;/0R0 <1RSTG2'=0 L1A0 LUohVL
*
0RR&R at line 2:
&R'-00"K2: ta,le or (ieC does not e+ist
SQL) L1ST
1 S0L0CT <1RSTG2'=0$ L'STG2'=0$ /1R0G3'T0
2* <R&= 0=PL&900S ;/0R0 <1RSTG2'=0 L1A0 LUohVL
SQL) C/'2@0 .0=PL&900.0=PL&900S.
2* <R&= 0=PL&900S ;/0R0 <1RSTG2'=0 L1A0 LUohVL
6
SQL) R:2
Query output!
SQL) 12P:T &R30R >9 <1R0G3'T0
SQL) L1ST
1 S0L0CT <1RSTG2'=0$ L'STG2'=0$ /1R0G3'T0
2 <R&= 0=PL&900 ;/0R0 <1RSTG2'=0 L1A0 LUohVL
N* &R30R >9 /1R0G3'T0
SQL) R:2
Query output!
SQL) CL0'R >:<<0R
,u77er cleared
SQL) L1ST
SP2-022N: 2o lines in SQL ,u77er.
:ow 5un #01@'lus 9ommands %hat Are #tored in a 1ocal (ile?
If you have a group of commands that you need to run them repeatedly every day, you can save those commands in a file
!called #01 script file", and using the COfile.ameC command to run them in #01@'lus. If you want to try this, create a file
called NtempNinput.s4l with*
S0L0CT L;elcoBe toL <R&= 3:'LI
S0L0CT L<91center.coBML <R&= 3:'LI
%hen run the COC command in #01@'lus as*
SQL) connect /R.retneciy7
SQL) 8*teBp*input.s-l
L;0LC&=0T&
----------
;elcoBe to
L<91C02T0R.C&=
--------------
<91center.coBM
:ow %o 2se #01@'lus ,uilt+in %imers?
If you don&t have a stopwatch?timer and want to measure elapsed periods of time, you can #01@'lus ,uilt+in %imers with the
following commands*
• %I<I.$ + Displays number of timers.
• %I<I.$ #%A5% PnameQ + #tarts a new timer with or without a name.
• %I<I.$ #:OW PnameQ + #hows the current time of the named or not+named timer.
• %I<I.$ #%O' PnameQ + #tops the named or not+named timer.
%he following tutorial e-ercise shows you a good e-ample of using #01@'lus built+in timers*
SQL) T1=12@ ST'RT tiBerG1
soBe seconds later!
SQL) T1=12@ ST'RT tiBerG2
soBe seconds later!
SQL) T1=12@ ST'RT tiBerGN
soBe seconds later!
SQL) T1=12@ S/&; tiBerG1
tiBing 7or: tiBerG2
0lapsed: 00:00:1".KN
soBe seconds later!
SQL) T1=12@ ST&P tiBerG2
tiBing 7or: tiBerG2
0lapsed: 00:00:NJ.N2
SQL) T1=12@
2 tiBing eleBents in use
What Is Oracle #erver Autotrace?
Autotrace is Oracle server feature that generates two statement e-ecution reports very useful for performance tuning*
• #tatement e-ecution path + #hows you the e-ecution loop logic of a D<1 statement.
• #tatement e-ecution statistics + #hows you various e-ecution statistics of a D<1 statement.
%o turn on the autotrace feature, the Oracle server D,A need to*
• 9reate a special table called '1A.6%A,1.
• 9reate a special security role called '12#%5A9.
• $rant '12#%5A9 role your user account.
:ow %o #et 2p Autotrace for a 2ser Account?
If an Oracle user wants to use the autotrace feature, you can use the tutorial as an e-ample to create the re4uired table
'1A.6%A,1, the re4uired security role '12#%5A9, and grant the role to that user*
7
SQL) C&220CT /R.retneciy7
SQL) 8*oracle+e*app*oracle*product*10.2.0*ser(er
*R3>=S*'3=12*:TL4PL'2.SQL
Ta,le /R.PL'2GT'>L0! created.
SQL) C&220CT . 'S S9S3>'
SQL) 8C:*oracle+e*app*oracle*product*10.2.0*ser(er
*SQLPL:S*'3=12*PL:STRC0.SQL
SQL) drop role plustraceI
Role PL:STR'C0! dropped.

SQL) create role plustraceI
Role PL:STR'C0! created.

SQL) grant plustrace to d,a Cith adBin optionI
@rant succeeded.
SQL) @R'2T PL:STR'C0 T& /RI
@rant succeeded.
5emember that '1A.6%A,1 table must be created under the user schema :5.
:ow %o $et -ecution 'ath 5eports on 0uery #tatements?
If your user account has autotrace configured by the D,A, you can use the C#% A2%O%5A9 O. F'1AI.C command to
turn on e-ecution path reports on 4uery statements. %he tutorial e-ercise bellow shows you a good e-ample*
SQL) C&220CT /R.retneciy7
SQL) S0T ':T&TR'C0 &2 04PL'12
SQL) S0L0CT 0.L'STG2'=0$ 0.S'L'R9$ U.U&>GT1TL0
2 <R&= 0=PL&900S 0$ U&>S U
N ;/0R0 0.U&>G13HU.U&>G13 '23 0.S'L'R9)12000I
L'STG2'=0 S'L'R9 U&>GT1TL0
----------------- ---------- -----------------------------
Aing 2K000 President
Aochhar 1W000 'dBinistration ?ice President
3e /aan 1W000 'dBinistration ?ice President
Russell 1K000 Sales =anager
Partners 1N%00 Sales =anager
/artstein 1N000 =arReting =anager
J roCs selected.
0+ecution Plan
-----------------------------------------------------------
Plan hash (alue: N#%1#""N"W
-----------------------------------------------------------
1dE&peration E2aBe ERoCsE>ytesE CostETiBe
E E E E E.VCP:E
-----------------------------------------------------------
0ES0L0CT ST'T0=02T E E %"E 2#N2E K.0E00:00:01
1E 20ST03 L&&PS E E %"E 2#N2E K.0E00:00:01
*2E T'>L0 'CC0SS <:LL E0=PL&900SE %"E 12N"E N.0E00:00:01
NE T'>L0 'CC0SS EU&>S E 1E 2WE 1.0E00:00:01
E >9 12304 R&;13 E E E E E
*KE 12304 :21Q:0 SC'2EU&>G13GPAE 1E E 0.0E00:00:01
-----------------------------------------------------------
Predicate 1n7orBation identi7ied ,y operation id!:
---------------------------------------------------
2 - 7ilterQ0Q.QS'L'R9Q)12000!
K - accessQ0Q.QU&>G13QHQUQ.QU&>G13Q!
:ow %o $et -ecution #tatistics 5eports on 0uery #tatements?
If your user account has autotrace configured by the D,A, you can use the C#% A2%O%5A9 O. #%A%I#%I9#C command
to turn on e-ecution statistics reports on 4uery statements. %he tutorial e-ercise bellow shows you a good e-ample*
SQL) C&220CT /R.retneciy7
SQL) S0T ':T&TR'C0 &2 ST'T1ST1CS
SQL) S0L0CT 0.L'STG2'=0$ 0.S'L'R9$ U.U&>GT1TL0
2 <R&= 0=PL&900S 0$ U&>S U
N ;/0R0 0.U&>G13HU.U&>G13 '23 0.S'L'R9)12000I
L'STG2'=0 S'L'R9 U&>GT1TL0
----------------- ---------- -----------------------------
Aing 2K000 President
8
Aochhar 1W000 'dBinistration ?ice President
3e /aan 1W000 'dBinistration ?ice President
Russell 1K000 Sales =anager
Partners 1N%00 Sales =anager
/artstein 1N000 =arReting =anager
J roCs selected.
Statistics
----------------------------------------------------------
0 recursi(e calls
0 d, ,locR gets
1J consistent gets
0 physical reads
0 redo siPe
W20 ,ytes sent (ia SQL*2et to client
N#K ,ytes recei(ed (ia SQL*2et 7roB client
2 SQL*2et roundtrips to.7roB client
0 sorts BeBory!
0 sorts disR!
J roCs processed
:ow <uch <emory )our AMg F #erver Is 2sing?
)our AMg F #erver is using about ARM<, of memory even there is no users on the server. %he server memory usage is
displayed on your server home page, if you log in as #)#%<.
:ow %o #tart )our AMg F #erver from 9ommand 1ine?
)ou can start your AMg F server from command line by*
• Open a command line window.
• 9hange directory to Noracle-eNappNoracleNproductNAM.;.MNserverN,I.N.
• 5un #tartD,.bat.
%he batch file #tartD,.bat contains*
net start &racle40T2SListener
net start &racleSer(ice40
8oradiB -startup -sid 40 -starttype inst ) nul 2)S1
:ow %o #hutdown )our AMg F #erver from 9ommand 1ine?
)ou can shutdown your AMg F server from command line by*
• Open a command line window.
• 9hange directory to Noracle-eNappNoracleNproductNAM.;.MNserverN,I.N.
• 5un #topD,.bat.
%he batch file #topD,.bat contains*
net stop &racleSer(ice40
:ow %o 2nloc/ the #ample 2ser Account?
)our AMg F server comes with a sample database user account called :5. ,ut this account is loc/ed. )ou must unloc/ it
before you can use it*
• 1og into the server home page as #)#%<.
• 9lic/ the Administration icon, and then clic/ Database 2sers.
• 9lic/ the :5 schema icon to display the user information for :5.
• nter a new password !hr" for :5, and change the status to 2nloc/ed.
• 9lic/ Alter 2ser to save the changes.
.ow user account :5 is ready to use.
:ow %o 9hange #ystem $lobal Area !#$A"?
)our AMg F server has a default setting for #ystem $lobal Area !#$A" of AIM<,. %he #$A size can be changed to a new
value depending on how many concurrent sessions connecting to your server. If you are running this server 3ust for yourself
to improve your D,A s/ill, you should change the #$A size to H;<, by*
• 1og into the server home page as #)#%<.
• $o to Administration, then <emory.
• 9lic/ 9onfigure #$A.
• nter the new memory size* H;
• 9lic/ Apply 9hanges to save the changes.
• 5e+start your server.
:ow %o 9hange 'rogram $lobal Area !'$A"?
)our AMg F server has a default setting for 'rogram $lobal Area !'$A" of IM<,. %he '$A size can be changed to a new
value depending on how much data a single session should be allocated. If you thin/ your session will be short with a small
amount of data, you should change the '$A size to AL<, by*
• 1og into the server home page as #)#%<.
• $o to Administration, then <emory.
• 9lic/ 9onfigure '$A.
• nter the new memory size* AL
• 9lic/ Apply 9hanges to save the changes.
• 5e+start your server.
9
What :appens If )ou #et the #$A %oo 1ow?
1et&s you made a mista/e and changed to #$A to AL<, from the #)#%< admin home page. When you run the batch file
#tartD,.bat, it will return a message saying server stated. :owever, if you try to connect to your server home page*
http*??localhost*RMRM?ape-?, you will get no response. Why? )our server is running, but the default instance F was not
started.
If you go the 9ontrol 'anel and #ervices, you will see service Oracle#erviceF is listed not in the running status.
What %o Do If the #tart,D.bat (ailed to #tart the F Instance?
If #tart,D.bat failed to start the F instance, you need to try to start the instance with other approaches to get detail error
messages on why the instance can not be started.
One good approach to start the default instance is to use #01@'lus. :ere is how to use #01@'lus to start the default
instance in a command window*
)cd &racle40 hoBe directory!
).*,in*startd,
).*,in*s-lplus
0nter user-naBe: S9ST0=
0nter passCord: 7yicenter
0RR&R:
&R'-010NK: &R'CL0 not a(aila,le
&R'-2W101: shared BeBory realB does not e+ist
%he first CcdC is to move the current directory the AMg F home directory. %he second command C.NbinNstartdbC is to ma/e
sure the %.# listener is running. %he third command C.NbinNs4lplusC launches #01@'lus. %he error message CO5A+;JAMAC
tells you that there is a memory problem with the default instance.
#o you can not use the normal login process to the server without a good instance. #ee other tips on how to log into a server
without any instance.
:ow %o 1ogin to the #erver without an Instance?
If your default instance is in trouble, and you can not use the normal login process to reach the server, you can use a special
login to log into the server without any instance. :ere is how to use #01@'lus to log in as as a system ,DA*
)cd &racle40 hoBe directory!
).*,in*startd,
).*,in*s-lplus
0nter user-naBe: S9ST0=.7yicenter 'S S9S3>'
Connected to an idle instance
SQL) shoC instance
instance QlocalQ
%he tric/ is to put user name, password and login options in a single string as the user name. CA# #)#D,AC tells the server
to not start any instance, and connect the session the idle instance.
1og in as #)#D,A is very useful for performing D,A tas/s.
:ow %o 2se CstartupC 9ommand to #tart Default Instance?
If you logged in to the server as a #)#D,A, you start the default instance with the CstartupC command. :ere is how to start
the default instance in #01@'lus in #)#D,A mode*
).*,in*s-lplus
0nter user-naBe: S9ST0=.7yicenter 'S S9S3>'
Connected to an idle instance
SQL) shoC instance
instance QlocalQ
SQL) startup
&R'-00#21: Speci7ied (alue o7 sgaGtarget 1J= is too sBall$
needs to ,e at least 20=
.ow the server is telling you more details about the memory problem on your default instance* your #$A setting of AL<, is
too small. It must be increased to at least ;M<,.
Where Are the #ettings #tored for ach Instance?
#ettings for each instance are stored in a file called #erver 'arameter (ile !#'(ile". Oracle supports two types of parameter
files, %e-t type, and ,inary type. parameter files should be located in EO5A916:O<Ndatabase directory. A parameter file
should be named li/e CinitE#ID.oraC, where E#ID is the instance name.
:ow %o 9hec/ the #erver 8ersion?
Oracle server sersion information is stored in a table called* '5OD29%69O<'O..%685#IO.. )ou can use a simple
#19% statement to view the version information li/e this*
).*,in*s-lplus
0nter user-naBe: S9ST0=.7yicenter 'S S9S3>'
Connected to an idle instance
SQL) C&L PR&3:CT <&R='T 'N%
SQL) C&L ?0RS1&2 <&R='T '1%
SQL) C&L ST'T:S <&R='T '1%
SQL) S0L0CT * <R&= PR&3:CTGC&=P&202TG?0RS1&2I
PR&3:CT ?0RS1&2 ST'T:S
----------------------------------- ----------- ----------
2LSRTL 10.2.0.1.0 Production
&racle 3ata,ase 10g 0+press 0dition 10.2.0.1.0 Product
PL.SQL 10.2.0.1.0 Production
T2S 7or N2-,it ;indoCs: 10.2.0.1.0 Production
What Are DD1 #tatements?
DD1 !Data Definition 1anguage" statements are statements to create and manage data ob3ects in the database. %he are H
primary DD1 statements*
10
• 95A% + 9reating a new database ob3ect.
• A1%5 + Altering the definition of an e-isting data ob3ect.
• D5O' + Dropping an e-isting data ob3ect.
:ow %o 9reate a .ew %able?
If you want to create a new table in your own schema, you can log into the server with your account, and use the 95A%
%A,1 statement. %he following script shows you how to create a table*
).*,in*s-lplus .nolog
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 tip id 2:=>0R%! PR1='R9 A09$
2 su,Xect ?'RC/'R#0! 2&T 2:LL$
N description ?'RC/'R2%J! 2&T 2:LL$
K createGdate 3'T0 30<':LT sysdate!!I
Ta,le created.
%his scripts creates a testing table called CtipC with I columns in the schema associated with the log in account C:5C.
:ow %o 9reate a .ew %able by #electing 5ows from Another %able?
1et&s say you have a table with many data rows, now you want to create a bac/up copy of this table of all rows or a subset of
them, you can use the 95A% %A,1...A# #19% statement to do this. :ere is an e-ample script*
).*,in*s-lplus .nolog
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ salary
2 <R&= eBpGdeptG10I
<1RSTG2'=0 L'STG2'=0 S'L'R9
-------------------- ------------------------- ----------
Uenni7er ;halen KK00
As you can see, this #01 scripts created a table called Cemp6dept6AMC using the same column definitions as the
CemployeesC table and copied data rows of one department.
%his is really a 4uic/ and easy way to create a table.
:ow %o Add a .ew 9olumn to an -isting %able?
If you have an e-isting table with e-isting data rows, and want to add a new column to that table, you can use the A1%5
%A,1 ... ADD statement to do this. :ere is an e-ample script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG110
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH110I
Ta,le created.
SQL) 'LT0R T'>L0 eBpGdeptG110 '33 (acation 2:=>0R!I
Ta,le altered.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ (acation
2 <R&= eBpGdeptG110I
<1RSTG2'=0 L'STG2'=0 ?'C'T1&2
-------------------- ------------------------- ----------
Shelley /iggins
;illiaB @ietP
%his #01 script added a new column called CvacationC to the Cemp6dept6AAMC table. .211 values were added to this column
on all e-isting data rows.
:ow %o Delete a 9olumn in an -isting %able?
If you have an e-isting column in a table and you need that column any more, you can delete it with A1%5 %A,1 ...
D5O' 9O12<. statement. :ere is an e-ample #01 script*
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) S0L0CT lastGnaBe <R&= eBpGdeptG"0I
L'STG2'=0
-------------------------
Aing
Aochhar
3e /aan
SQL) 'LT0R T'>L0 eBpGdeptG"0 3R&P C&L:=2 lastGnaBeI
Ta,le altered.
SQL) S0L0CT lastGnaBe <R&= eBpGdeptG"0I
0RR&R at line 1:
11
&R'-00"0K: QL'STG2'=0Q: in(alid identi7ier
As you can see the column Clast6nameC is gone.
:ow %o Drop an -isting %able?
If you want to delete an e-isting table and its data rows, you can use the D5O' %A,1 statement as shown in this script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) 3R&P T'>L0 eBpGdeptG10I
Ta,le dropped.
,e careful, when you use the D5O' %A,1 statement. All data rows are gone too.
:ow %o 9reate a %able Inde-?
If you have a table with a lots of rows, and you /now that one of the columns will be used often a search criteria, you can
add an inde- for that column to in improve the search performance. %o add an inde-, you can use the 95A% I.DF
statement as shown in the following script*
CR0'T0 T'>L0 tip id 2:=>0R%! PR1='R9 A09$
su,Xect ?'RC/'R#0! 2&T 2:LL$
description ?'RC/'R2%J! 2&T 2:LL$
createGdate 3'T0 30<':LT sysdate!!I
Ta,le created.

CR0'T0 12304 tipGsu,Xect &2 tipsu,Xect!I
1nde+ created.
:ow %o 5ename an Inde-?
1et&s say you have an e-isting inde-, and you don&t li/e its name anymore for some reason, you can rename it with the
A1%5 I.DF ... 5.A< %O statement. :ere is an e-ample script on how to rename an inde-*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.

S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K1%N ST:302T :21Q:0
S9SGC00K1%K ST:302T :21Q:0
'LT0R 12304 S9SGC00K1%N R02'=0 T& studentGpRI
StateBent processed.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
ST:302TGPA ST:302T :21Q:0
S9SGC00K1%K ST:302T :21Q:0
:ow %o Drop an Inde-?
If you don&t need an e-isting inde- any more, you should delete it with the D5O' I.DF statement. :ere is an e-ample #01
script*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.
CR0'T0 12304 studentG,irthGdate &2 student,irthGdate!I
1nde+ created.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K12" ST:302T :21Q:0
S9SGC00K1N0 ST:302T :21Q:0
ST:302TG>1RT/G3'T0 ST:302T 2&2:21Q:0
3R&P 12304 ST:302TG>1RT/G3'T0I
1nde+ dropped.
:ow %o 9reate a .ew 8iew?
)ou can create a new view based on one or more e-isting tables by using the 95A% 8IW statement as shown in the
following script*
CR0'T0 ?10; eBployeeGdepartBent 'S
12
S0L0CT e.eBployeeGid$ e.7irstGnaBe$ e.lastGnaBe$
e.eBail$ e.BanagerGid$ d.departBentGnaBe
<R&= eBployees e$ departBents d
;/0R0 e.departBentGid H d.departBentGidI
?ieC created.
S0L0CT 7irstGnaBe$ lastGnaBe$ departBentGnaBe
<R&= eBployeeGdepartBent ;/0R0 BanagerGid H 101I
<1RSTG2'=0 L'STG2'=0 30P'RT=02TG2'=0
-------------------- ------------------- ----------------
2ancy @reen,erg <inance
Uenni7er ;halen 'dBinistration
Susan =a(ris /uBan Resources
/erBann >aer Pu,lic Relations
Shelley /iggins 'ccounting
:ow %o Drop an -isting 8iew?
If you have an e-isting view, and you don&t want it anymore, you can delete it by using the D5O' 8IW statement as shown
in the following script*
3R&P ?10; eBployeeGdepartBentI
?ieC dropped.
What Are D<1 #tatements?
D<1 !Data <anipulation 1anguage" statements are statements to change data values in database tables. %he are H primary
D<1 statements*
• I.#5% + Inserting new rows into database tables.
• 2'DA% + 2pdating e-isting rows in database tables .
• D1% + Deleting e-isting rows from database tables.
:ow %o 9reate a %esting %able?
If you want to practice D<1 statements, you should create a testing table as shown in the script below*
CR0'T0 T'>L0 7yiGlinRs id 2:=>0RK! PR1='R9 A09$
url ?'RC/'R2#0! 2&T 2:LL$
notes ?'RC/'R2102K!$
counts 2:=>0R$
created 3'T0 30<':LT sysdate!!I
Ta,le created.
)ou should /eep this table for to practice other tutorial e-ercises presented in this collection.
:ow %o #et 2p #01@'lus Output (ormat?
If you want to practice #01 statements with #01@'lus, you need to set up your #01@'lus output formatting parameter
properly. %he following #01@'lus commands shows you some e-amples*
C&L:=2 id <&R='T """"I
C&L:=2 url <&R='T '2KI
C&L:=2 notes <&R='T '12I
C&L:=2 counts <&R='T """"""I
S0T 2:LL L2:LLLI
:ow %o Insert a .ew 5ow into a %able?
%o insert a new row into a table, you should use the I.#5% I.%O statement with values specified for all columns as shown
in the following e-ample*
12S0RT 12T& 7yiGlinRs ?'L:0S 101$
Lhttp:..de(.7yicenter.coBL$
2:LL$
0$
LN0-'pr-200JL!I
1 roC created.
S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
----- ------------------------ -------- ------- ---------
101 http:..de(.7yicenter.coB 2:LL 0 N0-'pr-0J
:ow %o #pecify Default 8alues in I.#5% #tatement?
If a column is defined with a default value in a table, you can use the /ey word D(A21% in the I.#5% statement to ta/e
the default value for that column. %he following tutorial e-ercise gives a good e-ample*
12S0RT 12T& 7yiGlinRs ?'L:0S 102$
Lhttp:..d,a.7yicenter.coBL$
2:LL$
0$
30<':LT!I
1 roC created.
S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
----- ------------------------ -------- ------- ---------
101 http:..de(.7yicenter.coB 2:LL 0 N0-'pr-0J
102 http:..d,a.7yicenter.coB 2:LL 0 0W-='9-0J
:ow %o Omit 9olumns with Default 8alues in I.#5% #tatement?
If you don&t want to specify values for columns that have default values, or you want to specify values to columns in an order
different than how they are defined, you can provide a column list in the I.#5% statement. If a column is omitted in the
column, Oracle applies H rules*
• If default value is defined for the column, that default value will be used.
13
• If no default value is defined for the column and .211 is allowed, .211 will be used.
• If no default value is defined for the column and .211 is not allowed, error will be returned.
%he following tutorial e-ercise gives a good e-ample*
12S0RT 12T& 7yiGlinRs url$ id!
?'L:0S Lhttp:..s-a.7yicenter.coBL$ 10N!I
1 roC created.
S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
----- ------------------------ -------- ------- ---------
101 http:..de(.7yicenter.coB 2:LL 0 N0-'pr-0J
102 http:..d,a.7yicenter.coB 2:LL 0 0W-='9-0J
10N http:..s-a.7yicenter.coB 2:LL 2:LL 0W-='9-0J
:ow %o Insert <ultiple 5ows with One I.#5% #tatement?
If you want to insert multiple rows with a single I.#5% statement, you can use a sub4uery instead of the 8A12# clause.
5ows returned from the sub4uery will be inserted the target table.
%he following tutorial e-ercise gives a good e-ample*
12S0RT 12T& 7yiGlinRs
S0L0CT departBentGid$ departBentGnaBeEEL.coBL$ 2:LL$ 2:LL$
S9S3'T0 <R&= departBents ;/0R0 departBentGid )H 2%0I
N roC created.
S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
----- ------------------------ -------- ------- ---------
101 http:..de(.7yicenter.coB 2:LL 0 N0-'pr-0J
102 http:..d,a.7yicenter.coB 2:LL 0 0W-='9-0J
10N http:..s-a.7yicenter.coB 2:LL 2:LL 0W-='9-0J
2%0 Retail Sales.coB 2:LL 2:LL 0W-='9-0J
2J0 Recruiting.coB 2:LL 2:LL 0W-='9-0J
2W0 Payroll.coB 2:LL 2:LL 0W-='9-0J
:ow %o 2pdate 8alues in a %able?
If you want to update some values in one row or multiple rows in a table, you can use the 2'DA% statement. %he script
below shows a good e-ample*
:P3'T0 7yiGlinRs S0T counts H """$ notes H L@ood site.L
;/0R0 id H 101I
1 roC updated.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id H 101I
13 :RL 2&T0S C&:2TS CR0'T03
---- ------------------------ ---------- ------ ---------
101 http:..de(.7yicenter.coB @ood site. """ 0W-='9-0J
:ow %o 2pdate 8alues on <ultiple 5ows?
If the W:5 clause in an 2'DA% matches multiple rows, the #% clause will be applied to all matched rows. %his rule
allows you to update values on multiple rows in a single 2'DA% statement. :ere is a good e-ample*
:P3'T0 7yiGlinRs S0T counts H "$ notes H L;rong :RLL
;/0R0 id )H 2%0I
N roCs updated.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id )H 2%0I
13 :RL 2&T0S C&:2TS CR0'T03
----- -------------------- ------------ ------- ---------
2%0 Retail Sales.coB ;rong :RL " 0W-='9-0J
2J0 Recruiting.coB ;rong :RL " 0W-='9-0J
2W0 Payroll.coB ;rong :RL " 0W-='9-0J
%his statement updated H rows with the same new values on all H rows.
:ow %o 2se -isting 8alues in 2'DA% #tatements?
If a row matches the W:5 clause in a 2'DA% statement, e-isting values in this row can be used in e-pressions to
provide new values in the #% clause. -isting values are represented by columns in the e-pressions. %he tutorial e-ercise
below shows a good e-ample*
:P3'T0 7yiGlinRs S0T id H 1000 Y id$ counts H id*2
;/0R0 id )H 2%0I
N roCs updated.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id )H 2%0I
13 :RL 2&T0S C&:2TS CR0'T03
----- -------------------- ------------ ------- ---------
12%0 Retail Sales.coB ;rong :RL %00 0W-='9-0J
12J0 Recruiting.coB ;rong :RL %20 0W-='9-0J
12W0 Payroll.coB ;rong :RL %K0 0W-='9-0J
%his statement increased values in the id column by AMMM.
:ow %o 2se 8alues from Other %ables in 2'DA% #tatements?
If you want to update values in one with values from another table, you can use a sub4uery in the #% clause. %he sub4uery
should return only one row for each row in the update table that matches the W:5 clause. %he tutorial e-ercise below
shows a good e-ample*
:P3'T0 7yiGlinRs S0T notes$ created! H
S0L0CT lastGnaBe$ hireGdate <R&= eBployees
;/0R0 eBployeeGid H id!
14
;/0R0 id T 110I
N roCs updated.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id T 110I
13 :RL 2&T0S C&:2TS CR0'T03
---- ------------------------ --------- ------- ---------
101 http:..de(.7yicenter.coB Aochhar """ 21-S0P-#"
102 http:..d,a.7yicenter.coB 3e /aan 0 1N-U'2-"N
10N http:..s-a.7yicenter.coB /unold 2:LL 0N-U'2-"0
%his statement updated H rows with values from the employees table.
What :appens If the 2'DA% #ub4uery 5eturns <ultiple 5ows?
If a sub4uery is used in a 2'DA% statement, it must return e-actly one row for each row in the update table that matches
the W:5 clause. If it returns multiple rows, Oracle server will give you an error message. %o test this out, you can try the
following tutorial e-ercise*
:P3'T0 7yiGlinRs S0T notes$ created! H
S0L0CT lastGnaBe$ hireGdate <R&= eBployees
;/0R0 eBployeeGid T id!
;/0R0 id T 110I
0RR&R at line 1:
&R'-01K2W: single-roC su,-uery returns Bore than one roC
%he problem is the criteria in the sub4uery* Cemployee6id S idC
:ow %o Delete an -isting 5ow from a %able?
If you want to delete an e-isting row from a table, you can use the D1% statement with a W:5 clause to identify that
row. :ere is good sample of D1% statements*
12S0RT 12T& 7yiGlinRs url$ id!
?'L:0S Lhttp:..CCC.Byspace.coBL$ N01!I
1 roC created.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id H N01I
13 :RL 2&T0S C&:2TS CR0'T03
----- ------------------------ -------- ------- ---------
N01 http:..CCC.Byspace.coB 2:LL 2:LL 0W-='9-0J
30L0T0 <R&= 7yiGlinRs ;/0R0 id H N01I
1 roC deleted.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id H N01I
no roCs selected
:ow %o Delete <ultiple 5ows from a %able?
)ou can delete multiple rows from a table in the same way as deleting a single row, e-cept that the W:5 clause will
match multiple rows. %he tutorial e-ercise below deletes H rows from the fyi6lin/s table*
S0L0CT * <R&= 7yiGlinRs ;/0R0 id )H 2%0I
13 :RL 2&T0S C&:2TS CR0'T03
----- --------------------- ----------- ------- ---------
12%0 Retail Sales.coB ;rong :RL %00 0W-='9-0J
12J0 Recruiting.coB ;rong :RL %20 0W-='9-0J
12W0 Payroll.coB ;rong :RL %K0 0W-='9-0J
30L0T0 <R&= 7yiGlinRs ;/0R0 id )H 2%0I
N roC deleted.
S0L0CT * <R&= 7yiGlinRs ;/0R0 id )H 2%0I
no roCs selected
:ow %o Delete All 5ows a %able?
If you want to delete all rows from a table, you have two options*
• 2se the D1% statement with no W:5 clause.
• 2se the %52.9A% %A,1 statement.
%he %52.9A% statement is more efficient the D1% statement. %he tutorial e-ercise shows you a good e-ample of
%52.9A% statement*
S0L0CT C&:2T*! <R&= 7yiGlinRsI
C&:2T*!
----------
N
TR:2C'T0 T'>L0 7yiGlinRsI
Ta,le truncated.
S0L0CT C&:2T*! <R&= 7yiGlinRsI
C&:2T*!
----------
0
What Is a #19% 0uery #tatement?
%he #19% statement is also called the 4uery statement. It is the most fre4uently used #01 statement in any database
application. A #19% statement allows you to retrieve data from one or more tables, or views, with different selection
criteria, grouping criteria and sorting orders.
:ow %o #elect All 9olumns of All 5ows from a %able?
%he simplest 4uery statement is the one that selects all columns of all rows from a table* C#19% @ (5O< table6name7C.
%he !@" in the #19% clause tells the 4uery to return all columns. %he tutorial e-ercise below gives you a good e-ample*
15
SQL) S0L0CT * <R&= departBentsI
30P'RT=02TG13 30P'RT=02TG2'=0 ='2'@0RG13 L&C'T1&2G13
------------- -------------------- ---------- -----------
10 'dBinistration 200 1W00
20 =arReting 201 1#00
N0 Purchasing 11K 1W00
K0 /uBan Resources 20N 2K00
%0 Shipping 121 1%00
J0 1T 10N 1K00
W0 Pu,lic Relations 20K 2W00
#0 Sales 1K% 2%00
"0 0+ecuti(e 100 1W00
......
:ow %o #elect #ome 9olumns from a %able?
If you want e-plicitly tell the 4uery to some columns, you can specify the column names in #19% clause. %he following
select statement returns only two columns from the table CdepartmentsC*
SQL) S0L0CT locationGid$ departBentGnaBe <R&= 30P'RT=02TSI
L&C'T1&2G13 30P'RT=02TG2'=0
----------- ------------------------------
1W00 'dBinistration
1#00 =arReting
1W00 Purchasing
2K00 /uBan Resources
1%00 Shipping
1K00 1T
2W00 Pu,lic Relations
2%00 Sales
1W00 0+ecuti(e
......
:ow %o #elect #ome 5ows from a %able?
If you don&t want select all rows from a table, you can specify a W:5 clause to tell the 4uery to return only the rows that
meets the condition defined in the W:5 clause. %he following select statement only returns rows that has department
name starts with the letter C9C*
SQL) S0L0CT * <R&= departBents
2 ;/0R0 departBentGnaBe L1A0 LCVLI
30P'RT=02TG13 30P'RT=02TG2'=0 ='2'@0RG13 L&C'T1&2G13
------------- -------------------- ---------- -----------
1N0 Corporate Ta+ 1W00
1K0 Control 'nd Credit 1W00
1#0 Construction 1W00
1"0 Contracting 1W00
......
:ow %o #ort the 0uery Output?
If you want the returning rows to be sorted, you can specify a sorting e-pression in the O5D5 ,) clause. %he following
select statement returns rows sorted by the values in the Cmanager6idC column*
SQL) S0L0CT * <R&= departBents &R30R >9 BanagerGidI
30P'RT=02TG13 30P'RT=02TG2'=0 ='2'@0RG13 L&C'T1&2G13
------------- -------------------- ---------- -----------
"0 0+ecuti(e 100 1W00
J0 1T 10N 1K00
100 <inance 10# 1W00
N0 Purchasing 11K 1W00
%0 Shipping 121 1%00
#0 Sales 1K% 2%00
10 'dBinistration 200 1W00
20 =arReting 201 1#00
......
9an the 0uery Output ,e #orted by <ultiple 9olumns?
)ou can specifying multiple columns in the O5D5 ,) clause as shown in the following e-ample statement, which returns
employees& salaries sorted by department and salary value*
SQL) S0L0CT departBentGid$ 7irstGnaBe$ lastGnaBe$ salary
<R&= eBployees &R30R >9 departBentGid$ salaryI
30P'RT=02TG13 <1RSTG2'=0 L'STG2'=0 S'L'R9
------------- --------------- --------------- ----------
10 Uenni7er ;halen KK00
20 Pat <ay J000
20 =ichael /artstein 1N000
N0 Aaren ColBenares 2%00
N0 @uy /iBuro 2J00
N0 Sigal To,ias 2#00
N0 Shelli >aida 2"00
N0 'le+ander Ahoo N100
N0 3en Raphaely 11000
K0 Susan =a(ris J%00
%0 TU &lson 2100
......
:ow %o #ort Output in Descending Order?
If you want to sort a column in descending order, you can specify the D#9 /eyword in the O5D5 ,) clause. %he
following #19% statement first sorts the department in descending order, then sorts the salary in ascending order*
16
SQL) S0L0CT departBentGid$ 7irstGnaBe$ lastGnaBe$ salary
<R&= eBployees &R30R >9 departBentGid 30SC$ salaryI
30P'RT=02TG13 <1RSTG2'=0 L'STG2'=0 S'L'R9
------------- --------------- --------------- ----------
AiB,erely @rant W000
110 ;illiaB @ietP #N00
110 Shelley /iggins 12000
100 Luis Popp J"00
100 1sBael Sciarra WW00
100 Uose =anuel :rBan W#00
100 Uohn Chen #200
100 3aniel <a(iet "000
......
:ow %o 2se #19% #tatement to 9ount the .umber of 5ows?
If you want to count the number of rows, you can use the 9O2.%!@" function in the #19% clause. %he following select
statement returns the number of rows in the CdepartmentC table*
SQL) S0L0CT C&:2T*! <R&= departBentsI
C&:2T*!
----------
2W
#o there are ;J rows in the CdepartmentsC table.
9an #19% #tatements ,e 2sed on 8iews?
#elect !4uery" statements can used on views in the same way as tables. %he following tutorial e-ercise helps you creating a
view and running a 4uery statement on the view*
SQL) CR0'T0 ?10; BanagedGdept 'S
S0L0CT * <R&= departBents ;/0R0 BanagerGid 1S 2&T 2:LLI
?ieC created.
SQL) S0L0CT * <R&= BanagedGdept ;/0R0 locationGid H 1W00I
30P'RT=02TG13 30P'RT=02TG2'=0 ='2'@0RG13 L&C'T1&2G13
------------- -------------------- ---------- -----------
10 'dBinistration 200 1W00
N0 Purchasing 11K 1W00
"0 0+ecuti(e 100 1W00
100 <inance 10# 1W00
110 'ccounting 20% 1W00
:ow %o (ilter Out Duplications in the 5eturning 5ows?
If there are duplications in the returning rows, and you want to remove the duplications, you can use the /eyword DI#%I.9%
or 2.I02 in the #19% clause. %he tutorial e-ercise below shows you that DI#%I.9% wor/s on selected columns only*
SQL) CR0'T0 T'>L0 7yiGteaB 'S
S0L0CT 7irstGnaBe$ lastGnaBe <R&= eBployees
;/0R0 7irstGnaBe H LUohnLI
Ta,le created.
SQL) 12S0RT 12T& 7yiGteaB ?'L:0S LUohnL$ LChenL!I
SQL) 12S0RT 12T& 7yiGteaB ?'L:0S LUaBesL$ LChenL!I
SQL) 12S0RT 12T& 7yiGteaB ?'L:0S LPeterL$ LChenL!I
SQL) 12S0RT 12T& 7yiGteaB ?'L:0S LUohnL$ LChenL!I
SQL) S0L0CT * <R&= 7yiGteaBI
<1RSTG2'=0 L'STG2'=0
-------------------- -------------------------
Uohn Chen
Uohn Russell
Uohn Seo
Uohn Chen
UaBes Chen
Peter Chen
Uohn Chen
SQL) S0L0CT 31ST12CT * <R&= 7yiGteaBI
<1RSTG2'=0 L'STG2'=0
-------------------- -------------------------
Peter Chen
Uohn Chen
UaBes Chen
Uohn Seo
Uohn Russell
SQL) S0L0CT 31ST12CT lastGnaBe <R&= 7yiGteaBI
L'STG2'=0
-------------------------
Chen
Russell
Seo
What Are $roup (unctions?
$roup functions are functions applied to a group of rows. -amples of group functions are*
• 9O2.%!@" + 5eturns the number of rows in the group.
17
• <I.!e-p" + 5eturns the minimum value of the e-pression evaluated on each row of the group.
• <AF!e-p" + 5eturns the ma-imum value of the e-pression evaluated on each row of the group.
• A8$!e-p" + 5eturns the average value of the e-pression evaluated on each row of the group.
:ow %o 2se $roup (unctions in the #19% 9lause?
If group functions are used in the #19% clause, they will be used on the rows that meet the 4uery selection criteria, the
output of group functions will be returned as output of the 4uery. %he following select statement returns I values calculate by
I group functions on all rows of the CdepartmentsC table*
SQL) S0L0CT C&:2T*!$ =12departBentGid!$
2 ='4departBentGid! <R&= departBentsI
C&:2T*! =1230P'RT=02TG13! ='430P'RT=02TG13!
---------- ------------------ ------------------
2W 10 2W0
9an $roup (unctions ,e <i-ed with .on+group #election (ields?
If a group function is used in the #19% clause, all other selection fields must be group level fields. .on+group fields can
not be mi-ed with group fields in the #19% clause. %he script below gives you an e-ample of invalid #19% statements
with group and non+gorup selection fields*
SQL) S0L0CT C&:2T*!$ departBentGid <R&= departBentsI
&R'-00"NW: not a single-group group 7unction
In this e-ample, 9O2.%!@" is a group field and department6id is a non+group field.
:ow %o Divide 0uery Output into $roups?
)ou can divide 4uery output into multiple groups with the $5O2' ,) clause. It allows you specify a column as the grouping
criteria, so that rows with the same value in the column will be considered as a single group. When the $5O2' ,) clause is
specified, the select statement can only be used to return group level information. %he following script gives you a good
$5O2' ,) e-ample*
SQL) S0L0CT departBentGid$ =12salary!$ ='4salary!$
2 '?@salary! <R&= eBployees @R&:P >9 departBentGidI
30P'RT=02TG13 =12S'L'R9! ='4S'L'R9! '?@S'L'R9!
------------- ----------- ----------- -----------
100 J"00 12000 #J00
N0 2%00 11000 K1%0
W000 W000 W000
"0 1W000 2K000 1"NNN.NNNN
20 J000 1N000 "%00
W0 10000 10000 10000
110 #N00 12000 101%0
%0 2100 #200 NKW%.%%%%J
......
:ow %o Apply (iltering 9riteria at $roup 1evel?
If you want to return only specific groups from the 4uery, you can apply filtering criteria at the group level by using the
:A8I.$ clause inside the $5O2' ,) clause. %he following script gives you a good :A8I.$ e-ample*
SQL) S0L0CT departBentGid$ =12salary!$ ='4salary!$
2 '?@salary! <R&= eBployees @R&:P >9 departBentGid
N /'?12@ '?@salary! T %000I
30P'RT=02TG13 =12S'L'R9! ='4S'L'R9! '?@S'L'R9!
------------- ----------- ----------- -----------
N0 2%00 11000 K1%0
%0 2100 #200 NKW%.%%%%J
10 KK00 KK00 KK00
:ow %o 9ount Duplicated 8alues in a 9olumn?
If you have a column with duplicated values, and you want to /now what are those duplicated values are and how many
duplicates are there for each of those values, you can use the $5O2' ,) ... :A8I.$ clause as shown in the following
e-ample. It returns how many duplicated first names in the employees table*
SQL) S0L0CT 7irstGnaBe$ C&:2T*! <R&= eBployees
@R&:P >9 7irstGnaBe /'?12@ C&:2T*! ) 1I
<1RSTG2'=0 C&:2T*!
-------------------- ----------
Peter N
=ichael 2
Ste(en 2
Uohn N
Uulia 2
;illiaB 2
Aaren 2
Ae(in 2
......
9an <ultiple 9olumns ,e 2sed in $5O2' ,)?
)ou can use multiple columns in the $5O2' ,) clause as shown in the following e-ample. It returns how many employees
are having the same salary in each department*
SQL) S0L0CT departBentGid$ salary$ count*!
2 <R&= eBployees @R&:P >9 departBentGid$
N salary /'?12@ count*! ) 1I
30P'RT=02TG13 S'L'R9 C&:2T*!
------------- ---------- ----------
"0 1W000 2
%0 N200 K
%0 2200 2
%0 NJ00 2
#0 10%00 2
18
#0 "000 2
%0 2W00 2
......
9an $roup (unctions ,e 2sed in the O5D5 ,) 9lause?
If the 4uery output is aggregated as groups, you can sort the groups by using group functions in the O5D5 ,) clause. %he
following statement returns how many employees are having the same salary in each department. %he group output is
sorted by the count in each group in descending order*
SQL) S0L0CT departBentGid$ salary$ count*!
2 <R&= eBployees @R&:P >9 departBentGid$
N salary /'?12@ count*! ) 1
&R30R >9 C&:2T*! 30SCI
30P'RT=02TG13 S'L'R9 C&:2T*!
------------- ---------- ----------
%0 2%00 %
%0 N200 K
%0 2#00 N
#0 10000 N
#0 "%00 N
%0 N100 N
%0 2J00 N
.....
:ow %o =oin %wo %ables in a #ingle 0uery?
%wo tables can be 3oined together in a 4uery in I ways*
• Inner =oin* 5eturns only rows from both tables that satisfy the 3oin condition.
• 1eft Outer =oin* 5eturns rows from both tables that satisfy the 3oin condition, and the rest of rows from the first !left"
table.
• 5ight Outer =oin* 5eturns rows from both tables that satisfy the 3oin condition, and the rest of rows from the second
!right" table.
• (ull Outer =oin* 5eturns rows from both tables that satisfy the 3oin condition, the rest of rows from the first !left" table,
and the rest of rows from the second !right" table.
:ow %o Write a 0uery with an Inner =oin?
If you want to 4uery from two tables with an inner 3oin, you can use the I..5 =OI. ... O. clause in the (5O< clause. %he
following 4uery returns output with an inner 3oin from two tables* employees and departments. %he 3oin condition is that the
department ID in the employees table e4uals to the department ID in the departments table*
SQL) S0L0CT eBployees.7irstGnaBe$ eBployees.lastGnaBe$
2 departBents.departBentGnaBe
N <R&= eBployees 1220R U&12 departBents
K &2 eBployees.departBentGidHdepartBents.departBentGidI
<1RSTG2'=0 L'STG2'=0 30P'RT=02TG2'=0
-------------------- -------------------- ---------------
Ste(en Aing 0+ecuti(e
2eena Aochhar 0+ecuti(e
Le+ 3e /aan 0+ecuti(e
'le+ander /unold 1T
>ruce 0rnst 1T
3a(id 'ustin 1T
?alli Pata,alla 1T
......
.ote that when multiple tables are used in a 4uery, column names need to be prefi-ed with table names in case the same
column name is used in both tables.
:ow %o Define and 2se %able Alias .ames?
When column names need to be prefi-ed with table names, you can define table alias name and use them to prefi- column
names as shown in the following select statement*
SQL) S0L0CT e.7irstGnaBe$ e.lastGnaBe$ d.departBentGnaBe
<R&= eBployees e 1220R U&12 departBents d
&2 e.departBentGidHd.departBentGidI
<1RSTG2'=0 L'STG2'=0 30P'RT=02TG2'=0
-------------------- -------------------- ---------------
Ste(en Aing 0+ecuti(e
2eena Aochhar 0+ecuti(e
Le+ 3e /aan 0+ecuti(e
'le+ander /unold 1T
>ruce 0rnst 1T
3a(id 'ustin 1T
?alli Pata,alla 1T
......
:ow %o Write a 0uery with a 1eft Outer =oin?
If you want to 4uery from two tables with a left outer 3oin, you can use the 1(% O2%5 =OI. ... O. clause in the (5O<
clause. %he following 4uery returns output with a left outer 3oin from two tables* departments and employees. %he 3oin
condition is that the manager ID in the departments table e4uals to the employee ID in the employees table*
SQL) set 2:LL L2:LLL
SQL) S0L0CT d.departBentGnaBe$ e.7irstGnaBe$ e.lastGnaBe
2 <R&= departBents d L0<T &:T0R U&12 eBployees e
N &2 d.BanagerGid H e.eBployeeGidI
30P'RT=02TG2'=0 <1RSTG2'=0 L'STG2'=0
-------------------- -------------------- --------------
'dBinistration Uenni7er ;halen
19
=arReting =ichael /artstein
Purchasing 3en Raphaely
/uBan Resources Susan =a(ris
Shipping 'daB <ripp
1T 'le+ander /unold
......
Treasury 2:LL 2:LL
Corporate Ta+ 2:LL 2:LL
Control 'nd Credit 2:LL 2:LL
Shareholder Ser(ices 2:LL 2:LL
>ene7its 2:LL 2:LL
=anu7acturing 2:LL 2:LL
Construction 2:LL 2:LL
......
.ote that a left outer 3oin may return e-tra rows from the first !left" table that do not satisfy the 3oin condition. In those e-tra
rows, columns from the second !right" table will be given null values.
%he e-tra rows returned from the left outer 3oin in this e-ample represents departments that have no manager IDs.
:ow %o Write a 0uery with a 5ight Outer =oin?
If you want to 4uery from two tables with a right outer 3oin, you can use the 5I$:% O2%5 =OI. ... O. clause in the (5O<
clause. %he following 4uery returns output with a right outer 3oin from two tables* departments and employees. %he 3oin
condition is that the manager ID in the departments table e4uals to the employee ID in the employees table*
SQL) set 2:LL L2:LLL
SQL) S0L0CT d.departBentGnaBe$ e.7irstGnaBe$ e.lastGnaBe
2 <R&= departBents d R1@/T &:T0R U&12 eBployees e
N &2 d.BanagerGid H e.eBployeeGidI
30P'RT=02TG2'=0 <1RSTG2'=0 L'STG2'=0
-------------------- -------------------- ---------------
'dBinistration Uenni7er ;halen
=arReting =ichael /artstein
Purchasing 3en Raphaely
/uBan Resources Susan =a(ris
Shipping 'daB <ripp
1T 'le+ander /unold
......
2:LL Clara ?ishney
2:LL Uason =allin
2:LL /aPel PhiltanRer
2:LL 2anette CaB,rault
2:LL 'lana ;alsh
2:LL Aaren Partners
2:LL >ruce 0rnst
......
.ote that a right outer 3oin may return e-tra rows from the second !right" table that do not satisfy the 3oin condition. In those
e-tra rows, columns from the first !left" table will be given null values.
%he e-tra rows returned from the right outer 3oin in this e-ample represents employees that are not assigned as managers in
the departments table.
:ow %o Write a 0uery with a (ull Outer =oin?
If you want to 4uery from two tables with a full outer 3oin, you can use the (211 O2%5 =OI. ... O. clause in the (5O<
clause. %he following 4uery returns output with a full outer 3oin from two tables* departments and employees. %he 3oin
condition is that the manager ID in the departments table e4uals to the employee ID in the employees table*
SQL) set 2:LL L2:LLL
SQL) S0L0CT d.departBentGnaBe$ e.7irstGnaBe$ e.lastGnaBe
2 <R&= departBents d <:LL &:T0R U&12 eBployees e
N &2 d.BanagerGid H e.eBployeeGidI
30P'RT=02TG2'=0 <1RSTG2'=0 L'STG2'=0
-------------------- -------------------- --------------
'dBinistration Uenni7er ;halen
=arReting =ichael /artstein
Purchasing 3en Raphaely
/uBan Resources Susan =a(ris
Shipping 'daB <ripp
1T 'le+ander /unold
......
Treasury 2:LL 2:LL
Corporate Ta+ 2:LL 2:LL
Control 'nd Credit 2:LL 2:LL
Shareholder Ser(ices 2:LL 2:LL
>ene7its 2:LL 2:LL
=anu7acturing 2:LL 2:LL
Construction 2:LL 2:LL
......
2:LL Clara ?ishney
2:LL Uason =allin
2:LL /aPel PhiltanRer
2:LL 2anette CaB,rault
2:LL 'lana ;alsh
2:LL Aaren Partners
2:LL >ruce 0rnst
......
20
.ote that a right outer 3oin may return two sets of e-tra rows* one set from the first !left" table that do not satisfy the 3oin
condition, and the other set from the second !right" table that do not satisfy the 3oin condition.
:ow %o Write an Inner =oin with the W:5 9lause?
If you don&t want to use the I..5 =OI. ... O. clause to write an inner 3oin, you can put the 3oin condition in the W:5
clause as shown in the following 4uery e-ample*
SQL) S0L0CT d.departBentGnaBe$ e.7irstGnaBe$ e.lastGnaBe
2 <R&= departBents d$ eBployees e
N ;/0R0 d.BanagerGid H e.eBployeeGidI
30P'RT=02TG2'=0 <1RSTG2'=0 L'STG2'=0
-------------------- -------------------- --------------
'dBinistration Uenni7er ;halen
=arReting =ichael /artstein
Purchasing 3en Raphaely
/uBan Resources Susan =a(ris
Shipping 'daB <ripp
1T 'le+ander /unold
......
:ow %o Write a 1eft Outer =oin with the W:5 9lause?
If you don&t want to use the 1(% O2%5 =OI. ... O. clause to write a left outer 3oin, you can use a special criteria in the
W:5 clause as Cleft6table.column > right6table.column!T"C. %he select statement below is an e-ample of a left outer 3oin
written with the W:5 clause*
SQL) set 2:LL L2:LLL
SQL) S0L0CT d.departBentGnaBe$ e.7irstGnaBe$ e.lastGnaBe
2 <R&= departBents d$ eBployees e
N ;/0R0 d.BanagerGid H e.eBployeeGidY!I
30P'RT=02TG2'=0 <1RSTG2'=0 L'STG2'=0
-------------------- -------------------- --------------
'dBinistration Uenni7er ;halen
=arReting =ichael /artstein
Purchasing 3en Raphaely
/uBan Resources Susan =a(ris
Shipping 'daB <ripp
1T 'le+ander /unold
......
Treasury 2:LL 2:LL
Corporate Ta+ 2:LL 2:LL
Control 'nd Credit 2:LL 2:LL
Shareholder Ser(ices 2:LL 2:LL
>ene7its 2:LL 2:LL
=anu7acturing 2:LL 2:LL
......
.ote that a left outer 3oin may return e-tra rows from the first !left" table that do not satisfy the 3oin condition. In those e-tra
rows, columns from the second !right" table will be given null values.
%he e-tra rows returned from the left outer 3oin in this e-ample represents departments that have no manager IDs.
:ow %o .ame 0uery Output 9olumns?
ach column in the 4uery output has a default name. If you don&t li/e the default name, you can specify a new name for any
column in the 4uery output by using the A# clause. %he following statement shows you a good e-ample*
SQL) S0L0CT departBentGid 'S 13$ =12salary! 'S LoC$
2 ='4salary! 'S /igh$ '?@salary! 'S '(erage
N <R&= eBployees @R&:P >9 departBentGid
K /'?12@ '?@salary! T %000I
13 L&; /1@/ '?0R'@0
---------- ---------- ---------- ----------
N0 2%00 11000 K1%0
%0 2100 #200 NKW%.%%%%J
10 KK00 KK00 KK00
What Is a #ub4uery?
A sub4uery is a #19% statement used as part of the selection criteria of the main #19% statement. %he sub4uery
specified in the W:5 clause will be evaluated repeated on each row of the selection base table. %he output of the
sub4uery will be used in the final evaluation of the criteria. 2sually, sub4ueries are used in the following boolean operations*
• Ce-pression I. !sub4uery"C
• Ce-pression .O% I. !sub4uery"C
• CFI#%# !sub4uery"C
• C.O% FI#%# !sub4uery"C
:ow %o 2se #ub4ueries with the I. Operator?
A sub4uery can be used with the I. operator as Ce-pression I. !sub4uery"C. %he sub4uery should return a single column
with one or more rows to form a list of values to be used by the I. operation. %he following tutorial e-ercise shows you how
to use a sub4uery with the I. operator*
SQL) S0L0CT 7irstGnaBe$ lastGnaBe <R&= eBployees
2 ;/0R0 departBentGid 12
N S0L0CT departBentGid <R&= departBents
K ;/0R0 locationGid H 1W00
% !I
<1RSTG2'=0 L'STG2'=0
21
-------------------- -------------------------
Ste(en Aing
2eena Aochhar
Le+ 3e /aan
2ancy @reen,erg
3aniel <a(iet
Uohn Chen
1sBael Sciarra
......
:ow %o 2se #ub4ueries with the FI#%# Operator?
A sub4uery can be used with the FI#%# operator as CFI#%# !sub4uery"C, which returns true if the sub4uery returns one
or more rows. %he following statement is a good e-ample of CFI#%# !sub4uery"C. It returns rows from employees table that
there are rows e-isting in the departments table lin/ed to the employees table with location6id > AJMM.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe <R&= eBployees e
2 ;/0R0 041STS
N S0L0CT * <R&= departBents d
K ;/0R0 e.departBentGid H d.departBentGid
% '23 d.locationGid H 1W00
J !I
<1RSTG2'=0 L'STG2'=0
-------------------- -------------------------
Ste(en Aing
2eena Aochhar
Le+ 3e /aan
2ancy @reen,erg
3aniel <a(iet
Uohn Chen
1sBael Sciarra
......
:ow %o 2se #ub4ueries in the (5O< clause?
If you have a 4uery returning many rows of data, and you want to perform another 4uery on those rows, you can put the first
4uery as a sub4uery in the (5O< clause of the second 4uery. %he following statement shows you how to use a sub4uery as
base table for the main 4uery*
SQL) S0L0CT * <R&=
2 S0L0CT 7irstGnaBe$ lastGnaBe$ departBentGnaBe
N <R&= eBployees e$ departBents d
K ;/0R0 e.departBentGid H d.departBentGid
% ! ;/0R0 departBentGnaBe L1A0 LSVL &R30R >9 lastGnaBeI
<1RSTG2'=0 L'STG2'=0 30P'RT=02TG2'=0
----------------- ---------------------- ---------------
0llen ',el Sales
Sundar 'nde Sales
=oPhe 'tRinson Shipping
'Bit >anda Sales
0liPa,eth >ates Sales
Sarah >ell Shipping
......
:ow %o 9ount $roups 5eturned with the $5O2' ,) 9lause?
If you use the 9O2.%!@" function on groups returned with the $5O2' ,) clause, it will count the number of rows within
each group, not the number of groups. If you want to count the number of groups, you can put the $5O2' ,) 4uery into a
sub4uery and apply the 9O2.%!@" function on the main 4uery as shown in the following tutorial e-ercise*
SQL) S0L0CT 7irstGnaBe$ C&:2T*! <R&= eBployees
@R&:P >9 7irstGnaBe /'?12@ C&:2T*! ) 1I
<1RSTG2'=0 C&:2T*!
-------------------- ----------
Peter N
=ichael 2
Ste(en 2
Uohn N
Uulia 2
;illiaB 2
Aaren 2
Ae(in 2
......
SQL) S0L0CT C&:2T*! <R&=
S0L0CT 7irstGnaBe$ C&:2T*! <R&= eBployees
@R&:P >9 7irstGnaBe /'?12@ C&:2T*! ) 1
!I
C&:2T*!
----------
1N
:ow %o 5eturn %op G 5ows?
If you want the 4uery to return only the first G rows, you can use the pseudo column called 5OW.2< in the W:5 clause.
5OW.2< contains the row number of each returning row from the 4uery. %he following statement returns the first G rows
from the employees table*
SQL) S0L0CT eBployeeGid$ 7irstGnaBe$ lastGnaBe
<R&= eBployees ;/0R0 R&;2:= TH %I
0=PL&900G13 <1RSTG2'=0 L'STG2'=0
22
----------- -------------------- -------------
100 Ste(en Aing
101 2eena Aochhar
102 Le+ 3e /aan
10N 'le+ander /unold
10K >ruce 0rnst
What Is a %ransaction?
A transaction is a logical unit of wor/ re4uested by a user to be applied to the database ob3ects. Oracle server introduces the
transaction concept to allow users to group one or more #01 statements into a single transaction, so that the effects of all
the #01 statements in a transaction can be either all committed !applied to the database" or all rolled bac/ !undone from the
database".
:ow %o #tart a .ew %ransaction?
%here is no #01 statement to e-plicitly start a new transaction. Oracle server implicitly starts a new transaction with the
following two conditions*
• %he first e-ecutable statement of a new user session will automatically start a new transaction.
• %he first e-ecutable statement after a previous transaction has been ended will automatically start a new transaction.
:ow %o nd the 9urrent %ransaction?
%here are several ways the current transaction can be ended*
• 5unning the 9O<<I% statement will e-plicitly end the current transaction.
• 5unning the 5O11,A9D statement will e-plicitly end the current transaction.
• 5unning any DD1 statement will implicitly end the current transaction.
• Disconnecting a user session will implicitly end the current transaction.
• Dilling a user session will implicitly end the current transaction.
:ow %o 9reate a %esting %able?
If you want to practice D<1 statements, you should create a testing table as shown in the script below*
)cd &racle40 hoBe directory!
).*,in*s-lplus .nolog
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 7yiGlinRs id 2:=>0RK! PR1='R9 A09$
url ?'RC/'R21J! 2&T 2:LL$
notes ?'RC/'R21J!$
counts 2:=>0RK!$
created 3'T0 30<':LT sysdate!!I
Ta,le created.
)ou should /eep this table for to practice other tutorial e-ercises presented in this collection.
:ow %o 9ommit the 9urrent %ransaction?
If you have used some D<1 statements updated some data ob3ects, and you want to have the updates to be permanently
recorded in the database, you can use the 9O<<I% statement. It will ma/e all the database changes made in the current
transaction become permanent and end the current transaction. %he following tutorial e-ercise shows you how to use
9O<<I% statements*
SQL) connect /R.7yicenter
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S L7yicenter.coBL$ 101!I
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S Lcenter7yi.coBL$ 110!I
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 7yicenter.coB 0W-='9-0J
110 center7yi.coB 0W-='9-0J
SQL) C&==1TI
CoBBit coBplete.
:ow %o 5ollbac/ the 9urrent %ransaction?
If you have used some D<1 statements updated some data ob3ects, you find a problem with those updates, and you don&t
want those updates to be permanently recorded in the database, you can use the 5O11,A9D statement. It will remove all
the database changes made in the current transaction and end the current transaction. %he following tutorial e-ercise shows
you how to use 5O11,A9D statements*
SQL) connect /R.7yicenter
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S Lgoogle.coBL$ 102!I
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
N ?'L:0S LByspace.coBL$ 10N!I
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 7yicenter.coB 0W-='9-0J
23
110 center7yi.coB 0W-='9-0J
102 google.coB 0W-='9-0J
10N Byspace.coB 0W-='9-0J
SQL) R&LL>'CAI
Roll,acR coBplete.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 7yicenter.coB 0W-='9-0J
110 center7yi.coB 0W-='9-0J
As you can see, the two new records inserted into the table were removed by the 5O11,A9D statement.
What :appens to the 9urrent %ransaction If a DD1 #tatement Is -ecuted?
If a DD1 statement is e-ecuted, the current transaction will be committed and ended. All the database changes made in the
current transaction will become permanent. %his is called an implicit commit by a DD1 statement. %he following tutorial
e-ercise shows you that the 95A% %A,1 statement forced the current transaction to be committed and ended. %he
subse4uent 5O11,A9D statement has no effects on the closed transaction.
SQL) connect /R.7yicenter
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S Loracle.coBL$ 112!I

SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S Ls-l.coBL$ 11N!I
SQL) CR0'T0 T'>L0 7yiGteBp 'S S0L0CT * <R&= 7yiGlinRs!I
Ta,le created.
SQL) R&LL>'CAI
Roll,acR coBplete.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 7yicenter.coB 0W-='9-0J
110 center7yi.coB 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
What :appens to the 9urrent %ransaction If the #ession Is nded?
If a session is ended, the current transaction in that session will be committed and ended. All the database changes made in
the current transaction will become permanent. %his is called an implicit commit when session is ended. %he following
tutorial e-ercise shows you that the CdisconnectC command forces the current transaction to be committed and ended. When
the session is reconnected, you can see the changes made by the 2'DA% statements.
SQL) connect /R.7yicenter
SQL) :P3'T0 7yiGlinRs S0T url H L<91C02T0R.C&=L
2 ;/0R0 id H 101I

SQL) :P3'T0 7yiGlinRs S0T url H LC02T0R<91.C&=L
2 ;/0R0 id H 110I
SQL) disconnect
SQL) connect /R.7yicenter
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
What :appens to the 9urrent %ransaction If the #ession Is Dilled?
If a session is /illed by the D,A, the current transaction in that session will be rolled bac/ and ended. All the database
changes made in the current transaction will be removed. %his is called an implicit rollbac/ when session is /illed. %he
following tutorial e-ercise shows you that the D,A DI11 ###IO. command forces the current transaction to be rolled bac/
with all the changes uncommitted.
SQL) connect /R.7yicenter
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
SQL) 30L0T0 <R&= 7yiGlinRs Chere id H 112I
1 roC deleted.
24
SQL) 30L0T0 <R&= 7yiGlinRs Chere id H 11NI
1 roC deleted.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
Deep the C:5C #01@'lus window as is, and open another window to run another instance of #01@'lus.
)cd &racle40 hoBe directory!
).*,in*s-lplus .nolog
SQL) connect S9ST0=.passCord
Connected.
SQL) S0L0CT sid$ serialZ$ usernaBe$ status$ type
2 <R&= ?[S0SS1&2 ;/0R0 usernaBe H L/RLI
S13 S0R1'LZ :S0R2'=0 ST'T:S T9P0
---------- ---------- ------------------ -------- -----
N" 1K1 /R 12'CT1?0 :S0R
SQL) 'LT0R S9ST0= A1LL S0SS1&2 LN"$1K1LI
SysteB altered.
$o bac/ to the C:5C #01@'lus window.
SQL) S0L0CT * <R&= 7yiGlinRsI
&R'-0002#: your session has ,een Rilled
SQL) connect /R.7yicenter
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
As you can see, two records were rolled bac/ as the session got /illed by another session.
:ow Does Oracle :andle 5ead 9onsistency?
Oracle supports two options for you on how to maintain read consistency*
• 5AD W5I% !the default option", also called statement+level read consistency.
• 5AD O.1), also called transaction+level read consistency.
What Is a 5AD W5I% %ransaction?
A 5AD W5I% transaction is a transaction in which the read consistency is set at the statement level. In a 5AD W5I%
transaction, a logical snapshot of the database is created at the beginning of the e-ecution of each statement and released
at the end of the e-ecution. %his guaranties that all reads within a single statement get consistent data from the database.
(or e-ample, if you have a 4uery statement that ta/es AM minutes to be e-ecuted, a snapshot of the database will be
created for this statement for AM minutes. If a sub4uery is used in this statement, it will get the consistent data no matter
when it gets e-ecuted within this AM minutes. In another word, data changes made during this AM minutes by other users will
not impact the e-ecution of this 4uery statement.
,y default, all transactions are started as 5AD W5I% transactions.
What Is a 5AD O.1) %ransaction?
A 5AD O.1) transaction is a transaction in which the read consistency is set at the transaction level. In a 5AD O.1)
transaction, a logical snapshot of the database is created at the beginning of the transaction and released at the end of the
transaction. %his guaranties that all reads in all statements within this transaction get consistent data from the database.
(or e-ample, if you have a transaction with many statements that ta/es AM hours to be e-ecuted, a snapshot of the
database will be created for this transaction for AM hours. If a 4uery statement is e-ecuted at the beginning of the transaction
and at the end of the transaction, it will return the same result guarantied. In another word, data changes made during this
AM hours by other users will not impact the e-ecution of statements within this transaction.
:ow %o #et a %ransaction %o ,e 5AD O.1)?
If you want a transaction to be set as 5AD O.1), you need to the transaction with the #% %5A.#A9%IO. 5AD O.1)
statement. .ote that a D<1 statement will start the transaction automatically. #o you have to issue the #% %5A.#A9%IO.
statement before any D<1 statements. %he tutorial e-ercise below shows you a good e-ample of 5AD O.1) transaction*
SQL) connect /R.7yicenter
SQL) S0T TR'2S'CT1&2 R0'3 &2L9I
Transaction set.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
Deep the C:5C #01@'lus window as is, and open another window to run another instance of #01@'lus.
)cd &racle40 hoBe directory!
).*,in*s-lplus .nolog
25
SQL) connect S9ST0=.passCord
Connected.
SQL) 30L0T0 <R&= hr.7yiGlinRs Chere id H 112I
1 roC deleted.
SQL) 30L0T0 <R&= hr.7yiGlinRs Chere id H 11NI
1 roC deleted.
SQL) C&==1TI
CoBBit coBplete.
$o bac/ to the C:5C #01@'lus window.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
112 oracle.coB 0W-='9-0J
11N s-l.coB 0W-='9-0J
SQL) C&==1TI
CoBBit coBplete.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
As you can see that two records were deleted from another session after the :5 session started the 5AD O.1)
transaction. %he deleted records was not impacting any 4uery statements until the transaction was ended with the 9O<<I%
statement.
What Are the 5estrictions in a 5AD O.1) %ransaction?
%here are lots of restrictions in a 5AD O.1) transaction*
• )ou can not switch to 5AD W5I% mode.
• )ou can not run any I.#5%, 2'DA%, D1% statements.
• )ou can run #19% 4uery statements.
%he tutorial e-ercise below shows you some of the restrictions*
SQL) connect /R.7yicenter
SQL) S0T TR'2S'CT1&2 R0'3 &2L9I
Transaction set.
SQL) S0T TR'2S'CT1&2 R0'3 ;R1T0I
&R'-01K%N: S0T TR'2S'CT1&2 Bust ,e 7irst stateBent o7
transaction
SQL) 12S0RT 12T& 7yiGlinRs url$ id!
2 ?'L:0S Ls-l.coBL$ 11N!I
&R'-01K%J: Bay not per7orB insert.delete.update operation
inside a R0'3 &2L9 transaction
SQL) 30L0T0 <R&= 7yiGlinRs Chere id H 110I
&R'-01K%J: Bay not per7orB insert.delete.update operation
inside a R0'3 &2L9 transaction
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
------- ---------------- ---------- ---------- ---------
101 <91C02T0R.C&= 0W-='9-0J
110 C02T0R<91.C&= 0W-='9-0J
What Are the $eneral 5ules on Data 9onsistency?
• All #01 statements always wor/ with a snapshot of the database to provide data consistency.
• (or 5AD W5I% transactions, the snapshot is ta/en when each statement starts.
• (or 5AD O.1) transactions, the snapshot is ta/en when the transaction starts.
• %he snapshot never include uncommitted changes from other transactions.
• %he snapshot always include uncommitted changes from its own transaction.
What Are %ransaction Isolation 1evels #upported by Oracle?
Oracle supports two transaction isolation levels*
• 5AD 9O<<I%%D !the default option". If the transaction contains D<1 that re4uires row loc/s held by another
transaction, then the D<1 statement waits until the row loc/s are released.
• #5IA1IKA,1. If a serializable transaction contains data manipulation language !D<1" that attempts to update any
resource that may have been updated in a transaction uncommitted at the start of the serializable transaction, then the
D<1 statement fails.
What Is a Data 1oc/?
26
A data loc/ is logical flag the Oracle server is placed on data ob3ects to give an e-clusive right to a transaction. #tatements
in other transactions needs to respect data loc/s based on certain rules. 5ules on data loc/s are*
• #19% 4uery statements do not create any data loc/s.
• I.#5%, 2'DA%, and D1% statements create data loc/s on the affected rows.
• Data loc/s are released when the owner transaction ends.
:ow Data 1oc/s Are 5espected?
:ere are the rules on how data loc/s are respected*
• All statements ignore data loc/s owned its own transaction.
• #19% 4uery statements ignores data loc/s owned by any transactions.
• I.#5%, 2'DA%, and D1% statements in a 5AD 9O<<I%%D transaction will wait for data loc/s on their
targeted rows by other transactions to be released.
• I.#5%, 2'DA%, and D1% statements in a #5IA1IKA,1 transaction will fail if their targeted rows has data
loc/s owned by other transactions.
:ow %o -periment a Data 1oc/?
If you want to have some e-perience with data loc/s, you can create two windows runing two #01@'lus sessions. In session
A, you can run a 2'DA% statements to create a data loc/. ,efore committing session ;, switch to session ;, and run a
2'DA% statements on the same row as session A. %he 2'DA% statement will be put into wait status because of the data
loc/. (ollow the tutorial e-ercise below to e-perience yourself*
session 1!
SQL) connect /R.7yicenter
SQL) S0T TR'2S'CT1&2
1S&L'T1&2 L0?0L
R0'3 C&==1TT03I
Transaction set.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S
--- ---------------- --------
101 <91C02T0R.C&=
110 C02T0R<91.C&=
SQL) :P3'T0 7yiGlinRs
S0T urlHL7yicenter.coBL
;/0R0 idH101I
1 roC updated.
locR created on roC idH101!
session 2!
SQL) connect /R.7yicenter

SQL) S0T TR'2S'CT1&2
1S&L'T1&2 L0?0L
R0'3 C&==1TT03I
Transaction set.
SQL) :P3'T0 7yiGlinRs S0T
notesHL<'Q ResourceL
;/0R0 idH101I
Cait on locR at idH101!
SQL) C&==1TI
locR on roC idH101 released!
ready to run :P3'T0!
1 roC updated.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S
--- ---------------- --------
101 7yicenter.coB
110 C02T0R<91.C&=
SQL) C&==1TI
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S
--- ---------------- ------------
101 7yicenter.coB <'Q Resource
110 C02T0R<91.C&=
:ow %o 8iew -isting 1oc/s on the Database?
As can see from the pervious tutorial e-ercise, performance of the second session is greatly affected by the data loc/
created on the database. %o maintain a good performance level for all sessions, you need to monitor the number of data
loc/s on the database, and how long do they last.
Oracle maintains current e-isting data loc/s in a Dynamic 'erformance 8iew called 8E1O9D with columns li/e*
• #ID + #ession ID to identify the session that owns this loc/.
27
• %)' + %he type of the loc/, li/e %< !D<1 en4ueue" and %F !%ransaction en4ueue".
• 1<OD + %he loc/ mode in which the session holds the loc/.
• 502#% + %he loc/ mode in which the session re4uests the loc/.
• 9%I< + %he time since current loc/ mode was granted
• ,1O9D + A value of either M or A, depending on whether or not the loc/ in 4uestion is the bloc/er.
%he following tutorial e-ercise shows you how to view e-isting loc/s on the database*
session 1!
SQL) connect /R.7yicenter
SQL) :P3'T0 7yiGlinRs
S0T urlHLcenter7yi.coBL
;/0R0 idH110I
1 roC updated.
session 2!
SQL) connect /R.7yicenter
SQL) 12S0RT 12T& 7yiGlinRs
url$ id! ?'L:0S
Loracle.coBL$ 112!I
1 roC created.
SQL) :P3'T0 7yiGlinRs
S0T notesHL<91 ResourceL
;/0R0 idH110I
Cait on locR at idH110!
.ow /eep those two sessions as is. )ou need to open a third window to connect to the database as #)#%< to view all
current loc/s*
session N!
SQL) connect S9ST0=.passCord
SQL) select sid$ usernaBe 7roB ([session
2 Chere usernaBeHL/RLI
S13 :S0R2'=0
---------- ------------------------------
2N /R
N" /R
SQL) S0L0CT sid$ type$ lBode$ re-uest$ ctiBe$ ,locR
<R&= ?[L&CA ;/0R0 sid in 2N$ N"! &R30R >9 ctiBe 30SCI
S13 T9 L=&30 R0Q:0ST CT1=0 >L&CA
---- -- ---------- ---------- ---------- ----------
1 N" T4 J 0 #K 1
2 N" T= N 0 #K 0
N 2N T= N 0 2W 0
K 2N T4 J 0 2W 0
% 2N T4 0 J 1# 0
)ou should read the output as*
• 1ine UA and U; represent the loc/ resulted from the 2'DA% statement in session UA on row id>AAM.
• 1ine UH and UI represent the loc/ resulted from the I.#5% statement in session U; on row id>AA;.
• 1ine UG represents a re4uest of loc/ resulted from the 2'DA% statement in session U; on row id>AAM, which is
bloc/ed by the loc/ from line UA and U;.
What Is a Dead 1oc/?
A dead loc/ is phenomenon happens between two transactions with each of them holding a loc/ that bloc/s the other
transaction as shown in the following diagram*
transaction 1! transaction 2!
update roC 4 to create locR 1
update roC 9 to create locR 2
update roC 4
,locRed ,y locR 1!
update roC 9
,locRed ,y locR 2!
dead locR created!
:ow Oracle :andles Dead 1oc/s?
Oracle server automatically detects dead loc/s. When a dead loc/ is detected, Oracle server will select a victim transaction,
and fail its statement that is bloc/ed in the dead loc/ to brea/ the dead loc/. %he tutorial e-ercise below shows you an
e-ample of statements failed by Oracle server because of dead loc/s*
session 1!
SQL) connect /R.7yicenter
SQL) :P3'T0 7yiGlinRs
S0T notesHLSession 1L
;/0R0 idH101I
1 roC updated.
session 2!
SQL) connect /R.7yicenter
28
SQL) :P3'T0 7yiGlinRs
S0T notesHLSession 2L
;/0R0 idH110I
1 roC updated.

SQL) :P3'T0 7yiGlinRs
S0T notesHLSession 2L
;/0R0 idH101I
,locRed ,y locR idH101!
SQL) :P3'T0 7yiGlinRs
S0T notesHLSession 1L
;/0R0 idH110I
,locRed ,y locR on roC idH110!
&R'-000J0: deadlocR
detected Chile Caiting
7or resource
stateBent 7ailed!
What Is a 2ser Account?
A user account is identified by a user name and defines the user&s attributes, including the following*
• 'assword for database authentication
• 'rivileges and roles
• Default tablespace for database ob3ects
• Default temporary tablespace for 4uery processing wor/ space
What Is the 5elation of a 2ser Account and a #chema?
2ser accounts and schemas have a one+to+one relation. When you create a user, you are also implicitly creating a schema
for that user. A schema is a logical container for the database ob3ects !such as tables, views, triggers, and so on" that the
user creates. %he schema name is the same as the user name, and can be used to unambiguously refer to ob3ects owned
by the user.
What Is a 2ser 5ole?
A user role is a group of privileges. 'rivileges are assigned to users through user roles. )ou create new roles, grant
privileges to the roles, and then grant roles to users.
What Are the #ystem 'redefined 2ser 5oles?
Oracle AMg F comes with H predefined roles*
• 9O..9% + nables a user to connect to the database. $rant this role to any user or application that needs database
access.
• 5#O259 + nables a user to create certain types of schema ob3ects in his own schema. $rant this role only to
developers and to other users that must create schema ob3ects. %his role grants a subset of the create ob3ect system
privileges.
• D,A + nables a user to perform most administrative functions, including creating users and granting privileges7
creating and granting roles7 creating and dropping schema ob3ects in other users& schemas7 and more. It grants all
system privileges, but does not include the privileges to start up or shut down the database. It is by default granted to
user #)#%<.
What Are Internal 2ser Account?
An internal user account is a system predefined user account. Oracle AMg F comes with a number of internal accounts*
• #)#%< + %his is the user account that you log in with to perform all administrative functions other than starting up and
shutting down the database. #)#%< is automatically created when you install the server. It&s password is the one you
specified during the installation process.
• #)# + %his is another user account automatically created when you install the server. It&s password is the one you
specified during the installation process. All base tables and views for the database data dictionary are stored in the
#)# schema. #o avoid log in as user #)# as much as possible to reduce the ris/ of damaging those important data
ob3ects. 2ser #)#%< is preferred for all administrative tas/s e-cept starting up and shutting down.
• Other internal user accounts + Other special user accounts are predefined for special purposes. (or e-ample, 9%F#)#
is a special user account used by the Oracle %e-t product.
:ow %o 9onnect to the #erver with 2ser Account* #)#?
#)# is a very special user account. It has been associated with the highest privilege call #)#D,A. .ormally, you should not
connect to the server with #)#. ,ut if you want to use it, you need to use a special connect command*
)cd &racle40 hoBe directory!
).*,in*s-lplus .nolog
SQL) connect S9S.7yicenter 'S S9S3>'
Connected.
SQL) -uit
.ote that the C?nologC option is used to start #01@'lus without login immediately. A special form of the CconnectC command
is used to include the user name, password, and the privilege in the same line.
)ou can not log in with #)# without #)#D,A privilege.
:ow %o 2se Windows 2ser to 9onnect to the #erver?
During the installation process, AMg F will create a special Windows user group called O5A6D,A, and put your Windows
user into this group. Any Windows users in this group can be connected to Oracle server with #)#D,A privilege without any
Oracle server user account. %his process is called connecting the server as #)#D,A with O# Authentication. :ere is how to
do this with a special form of the CconnectC command*
29
Log in Cith the saBe user you used to install 10g 40!
)cd &racle40 hoBe directory!
).*,in*startd,
).*,in*s-lplus .nolog
SQL) connect . 'S S9S3>'
Connected.
SQL) -uit
#o if CconnectC is used without user name and password, the current Windows user will be trusted if he?she is in the
O5A6D,A user group on the Windows system.
:ow %o 1ist All 2ser Accounts?
2ser accounts can be accessed through a system view called A1162#5#. A simple #19% statement can be used to get
a list of all user accounts. %ry the following script*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
Connected.
SQL) S0L0CT * <R&= 'LLG:S0RSI
:S0R2'=0 :S0RG13 CR0'T03
------------------------------ ---------- ---------
<L&;SG020100 N% 0W-<0>-0J
<L&;SG<1L0S NK 0W-<0>-0J
/R NN 0W-<0>-0J
=3S9S N2 0W-<0>-0J
'2&29=&:S 2# 0W-<0>-0J
43> 2W 0W-<0>-0J
CT4S9S 2% 0W-<0>-0J
3>S2=P 2N 0W-<0>-0J
TS=S9S 20 0W-<0>-0J
31P 1# 0W-<0>-0J
&:TL2 11 0W-<0>-0J
S9ST0= % 0W-<0>-0J
S9S 0 0W-<0>-0J
:ow %o 9reate a .ew 2ser Account?
If you want to create a new user account, you can log in as #)#%< and use the 95A% 2#5 command as shown in
the following e-ample*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
Connected.
SQL) CR0'T0 :S0R 30? 1302T1<103 >9 de(eloper 'CC&:2T :2L&CAI
:ser created.
.ote that 95A% is a #01 statement, so you need to terminate it with C7C. %his command creates a user called CD8C, with
a password of CdeveloperC. )ou can test this account by log in with D8 from #01@'lus.
:ow %o 9hange 2ser 'assword?
If you want to change a user&s password, you can log in as #)#%< and use the A1%5 2#5 command as shown in the
following e-ample*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
Connected.
SQL) 'LT0R :S0R 30? 1302T1<103 >9 ,eginnerI
:ser altered.
.ote that A1%5 is #01 statement, so you need to terminate it with C7C. %his command resets D8&s password to
CbeginnerC.
:ow %o Delete a 2ser Account?
If you want to delete a user account and its associated schema, you can log in as #)#%< and use the D5O' 2#5
command as shown in the following e-ample*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
Connected.
SQL) 3R&P :S0R 30? C'SC'30I
:ser dropped.
SQL) CR0'T0 :S0R 30? 1302T1<103 >9 de(eloper 'CC&:2T :2L&CAI
:ser created.
What 'rivilege Is .eeded for a 2ser to 9onnect to Oracle #erver?
Oracle deny connection to users who has no 95A% ###IO. privilege. %ry the following tutorial e-ercise, you will find
out how Oracle denies connection*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
30
SQL) CR0'T0 :S0R 30? 1302T1<103 >9 de(eloper 'CC&:2T :2L&CAI
:ser created.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
&R'-010K%: user 30? lacRs CR0'T0 S0SS1&2 pri(ilegeI
logon denied
Oracle error message is pretty clear.
:ow %o $rant 95A% ###IO. 'rivilege to a 2ser?
If you want give a user the 95A% ###IO. privilege, you can use the $5A.% command. %he following tutorial e-ercise
shows you how to grant D8 the privilege to connect to the server*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) @R'2T CR0'T0 S0SS1&2 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
Connected.
:ow %o 5evo/e 95A% ###IO. 'rivilege from a 2ser?
If you ta/e away the 95A% ###IO. privilege from a user, you can use the 58OD command as shown in the
following e-ample script*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) R0?&A0 CR0'T0 S0SS1&2 <R&= de(I
Re(oRe succeeded.
SQL) @R'2T CR0'T0 S0SS1&2 T& de(I
@rant succeeded.
%his script restored the 95A% ###IO. privilege to user CdevC, so you can continue other e-ample scripts below.
:ow %o 1oc/ and 2nloc/ a 2ser Account?
If you want to loc/ a user account for a short period of time, and unloc/ it later, you can use the A1%5 2#5 ... A99O2.%
command. %he following sample script shows how to use this command*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) 'LT0R :S0R de( 'CC&:2T L&CAI
:ser altered.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
&R'-2#000: the account is locRed
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) 'LT0R :S0R de( 'CC&:2T :2L&CAI
:ser altered.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
Connected.
What 'rivilege Is .eeded for a 2ser to 9reate %ables?
%o be able to create tables in a user&s own schema, the user needs to have the 95A% %A,1 privilege, or the 95A%
A.) %A,1 privilege, which is more powerful, and allows the user to create tables in other user&s schema. %he following
tutorial e-ercise gives you a good e-ample on 95A% %A,1 privilege*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 T'>L0 7yi id 2:=>0R!I
&R'-010N1: insu77icient pri(ileges
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T CR0'T0 T'>L0 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 T'>L0 7yi id 2:=>0R!I
&R'-01"%0: no pri(ileges on ta,lespace LS9ST0=L
%he above error message tells that user CdevC is not allowed to use the tablespace C#)#%<C. #ee the ne-t 4uestion for
answers.
:ow %o Assign a %ablespace to a 2sers?
31
When you create a new user, Oracle will assign the #)#%< tablespace to the user by default. If you want to change this,
you can assign a different table space to a user using the A1%5 2#5 command. %he following tutorial e-ercise changes
user dev&s default tablespace, and assigns I<, of space to dev*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) 'LT0R :S0R de( 30<':LT T'>L0SP'C0 :S0RSI
:ser altered.
SQL) 'LT0R :S0R de( Q:&T' K= &2 :S0RSI
:ser altered.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 T'>L0 7yi id 2:=>0R!I
Ta,le created.
SQL) 3R&P T'>L0 7yiI
Ta,le dropped.
SQL) CR0'T0 T'>L0 7yi id 2:=>0R!I
Ta,le created.
As you can see, CdevC can create and drop tables now. )ou can also let CdevC to create tables in any tablespace without any
restriction by granting him the 2.1I<I%D %A,1#'A9 system privilege.
What 'rivilege Is .eeded for a 2ser to 9reate 8iews?
%o be able to create views in a user&s own schema, the user needs to have the 95A% 8IW privilege, or the 95A%
A.) 8IW privilege, which is more powerful, and allows the user to create views in other user&s schema. %he following
tutorial e-ercise gives you a good e-ample on 95A% 8IW privilege*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 ?10; 7yiG(ieC 'S S0L0CT * <R&= 7yiI
&R'-010N1: insu77icient pri(ileges
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T CR0'T0 ?10; T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 ?10; 7yiG(ieC 'S S0L0CT * <R&= 7yiI
?ieC created.
SQL) 3R&P ?10; 7yiG(ieCI
?ieC dropped.
SQL) CR0'T0 ?10; 7yiG(ieC 'S S0L0CT * <R&= 7yiI
?ieC created.
As you can see, CdevC can create and drop views now.
What 'rivilege Is .eeded for a 2ser to 9reate Inde-es?
(or a user to create inde-es, he?she needs the same privilege as the creating tables. =ust ma/e sure he?she has the
95A% %A,1 privilege. %he following tutorial e-ercise gives you a good e-ample on creating view privilege*
).*,in*s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) @R'2T CR0'T0 T'>L0 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) CR0'T0 12304 7yiGinde+ &2 7yiid!I
1nde+ created.
SQL) 3R&P 12304 7yiGinde+I
1nde+ dropped.
SQL) CR0'T0 12304 7yiGinde+ &2 7yiid!I
1nde+ created.
%his e-ercise assumes that you followed previous e-ercises in the (A0 collection.
What 'rivilege Is .eeded for a 2ser to 0uery %ables in Another #chema?
(or a user to run 4ueries !#19% statements" on tables of someone else&s schema, he?she needs the #19% A.)
%A,1 privilege. %he following tutorial e-ercise gives you a good e-ample of granting CdevC to 4uery tables in ChrC schema*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
32
SQL) S0L0CT C&:2T*! <R&= hr.eBployeesI
&R'-010N1: insu77icient pri(ileges
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T S0L0CT '29 T'>L0 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) S0L0CT C&:2T*! <R&= hr.eBployeesI
C&:2T*!
----------
10W
As you can see, CdevC can 4uery tables in any schema now.
)ou also need to remember that table name must be prefi-ed with the schema name !same as owner user name".
What 'rivilege Is .eeded for a 2ser to Insert 5ows to %ables in Another #chema?
(or a user to insert rows into tables of someone else&s schema, he?she needs the I.#5% A.) %A,1 privilege. %he
following tutorial e-ercise gives you a good e-ample of granting CdevC to insert rows in ChrC schema*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) 12S0RT 12T& hr.Xo,s
?'L:0S L3?.<91L$ L3e( <91 ConsultantL$ WW00$ ##00!I
&R'-010N1: insu77icient pri(ileges
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T 12S0RT '29 T'>L0 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) 12S0RT 12T& hr.Xo,s
?'L:0S L3?.<91L$ L3e( <91 ConsultantL$ WW00$ ##00!I
1 roC created.
As you can see, CdevC can insert rows in any schema now. ,ut you should be careful when giving this privilege to a regular
developer.
What 'rivilege Is .eeded for a 2ser to Delete 5ows from %ables in Another #chema?
(or a user to delete rows from tables of someone else&s schema, he?she needs the D1% A.) %A,1 privilege. %he
following tutorial e-ercise gives you a good e-ample of granting CdevC to delete rows in ChrC schema*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) 30L0T0 <R&= hr.Xo,s ;/0R0 Xo,Gid H L3?.<91LI
&R'-010N1: insu77icient pri(ileges
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T 30L0T0 '29 T'>L0 T& de(I
@rant succeeded.
SQL) disconnect
SQL) C&220CT 30?.de(eloper
SQL) 30L0T0 <R&= hr.Xo,s ;/0R0 Xo,Gid H L3?.<91LI
1 roC deleted.
As you can see, CdevC can delete rows in any schema now. ,ut you should be careful when giving this privilege to a regular
developer.
:ow %o (ind Out What 'rivileges a 2ser 9urrently :as?
'rivileges granted to users are listed in two system views* D,A6#)#6'5I8#, and 2#56#)#6'5I8#. )ou can find out
what privileges a user currently has by running a 4uery on those views as shown in the tutorial e-ercise below*
).*,in*s-lplus .nolog
SQL) C&220CT 30?.de(eloper
SQL) S0L0CT usernaBe$ pri(ilege <R&= :S0RGS9SGPR1?SI
:S0R2'=0 PR1?1L0@0
------------------------------ ----------------------
30? S0L0CT '29 T'>L0
30? 12S0RT '29 T'>L0
30? CR0'T0 S0SS1&2
30? CR0'T0 ?10;
30? 30L0T0 '29 T'>L0
33
30? CR0'T0 '29 T'>L0
SQL) disconnect
SQL) connect S9ST0=.7yicenter
SQL) @R'2T 30L0T0 '29 T'>L0 T& de(I
@rant succeeded.
SQL) S0L0CT @R'2T00$ PR1?1L0@0 <R&= 3>'GS9SGPR1?S
;/0R0 @R'2T00 H L/RLI
@R'2T00 PR1?1L0@0
------------------------------ -----------------------
/R CR0'T0 ?10;
/R :2L1=1T03 T'>L0SP'C0
/R 30>:@ C&220CT S0SS1&2
/R CR0'T0 3'T'>'S0 L12A
/R CR0'T0 S0Q:02C0
/R CR0'T0 S0SS1&2
/R 30>:@ '29 PR&C03:R0
/R 'LT0R S0SS1&2
/R CR0'T0 S92&29=
1oo/s li/e ChrC has move privileges than CdevC.
What Is a Database %able?
A database table is a basic unit of data logical storage in an Oracle database. Data is stored in rows and columns. )ou
define a table with a table name, such as employees, and a set of columns. )ou give each column a column name, such as
employee6id, last6name, and 3ob6id7 a datatype, such as 8A59:A5;, DA%, or .2<,57 and a width. %he width can be
predetermined by the datatype, as in DA%. If columns are of the .2<,5 datatype, define precision and scale instead of
width. A row is a collection of column information corresponding to a single record.
:ow <any %ypes of %ables #upported by Oracle?
Oracle supports I types of tables based on how data is organized in storage*
• Ordinary !heap+organized" table + %his is the basic, general purpose type of table. Its data is stored as an unordered
collection !heap"
• 9lustered table + A clustered table is a table that is part of a cluster. A cluster is a group of tables that share the same
data bloc/s because they share common columns and are often used together.
• Inde-+organized table + 2nli/e an ordinary !heap+organized" table, data for an inde-+organized table is stored in a ,+
tree inde- structure in a primary /ey sorted manner. ,esides storing the primary /ey column values of an inde-+
organized table row, each inde- entry in the ,+tree stores the non/ey column values as well.
• 'artitioned table + 'artitioned tables allow your data to be bro/en down into smaller, more manageable pieces called
partitions, or even subpartitions. ach partition can be managed individually, and can operate independently of the
other partitions, thus providing a structure that can be better tuned for availability and performance.
:ow %o 9reate a .ew %able in )our #chema?
If you want to create a new table in your own schema, you can log into the server with your account, and use the 95A%
%A,1 statement. %he following script shows you how to create a table*
).*,in*s-lplus .nolog
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 tip id 2:=>0R%! PR1='R9 A09$
2 su,Xect ?'RC/'R#0! 2&T 2:LL$
N description ?'RC/'R2%J! 2&T 2:LL$
K createGdate 3'T0 30<':LT sysdate!!I
Ta,le created.
%his scripts creates a testing table called CtipC with I columns in the schema associated with the log in account C:5C.
:ow %o 9reate a .ew %able by #electing 5ows from Another %able?
1et&s say you have a table with many data rows, now you want to create a bac/up copy of this table of all rows or a subset of
them, you can use the 95A% %A,1...A# #19% statement to do this. :ere is an e-ample script*
).*,in*s-lplus .nolog
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ salary <R&= eBpGdeptG10I
<1RSTG2'=0 L'STG2'=0 S'L'R9
-------------------- ------------------------- ----------
Uenni7er ;halen KK00
As you can see, this #01 scripts created a table called Cemp6dept6AMC using the same column definitions as the
CemployeesC table and copied data rows of one department.
%his is really a 4uic/ and easy way to create a table.
:ow %o 5ename an -isting %able?
If you don&t li/e the name of an e-isting table, you change it by using the 95A% %A,1 ... 5.A< %O statement. :ere
is a sample script*
34
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) 'LT0R T'>L0 eBpGdeptG10 R02'=0 T& eBpGdeptGd,aI
Ta,le altered.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ salary <R&= eBpGdeptGd,aI
<1RSTG2'=0 L'STG2'=0 S'L'R9
-------------------- ------------------------- ----------
Uenni7er ;halen KK00
:ow %o Drop an -isting %able?
If you want to delete an e-isting table and its data rows, you can use the D5O' %A,1 statement as shown in this script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) 3R&P T'>L0 eBpGdeptG10I
Ta,le dropped.
,e careful, when you use the D5O' %A,1 statement. All data rows are gone too.
:ow %o Add a .ew 9olumn to an -isting %able?
If you have an e-isting table with e-isting data rows, and want to add a new column to that table, you can use the A1%5
%A,1 ... ADD statement to do this. :ere is an e-ample script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG110
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH110I
Ta,le created.
SQL) 'LT0R T'>L0 eBpGdeptG110 '33 (acation 2:=>0R!I
Ta,le altered.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ (acation
2 <R&= eBpGdeptG110I
<1RSTG2'=0 L'STG2'=0 ?'C'T1&2
-------------------- ------------------------- ----------
Shelley /iggins
;illiaB @ietP
%his #01 script added a new column called CvacationC to the Cemp6dept6AAMC table. .211 values were added to this column
on all e-isting data rows.
:ow %o Add a .ew 9olumn to an -isting %able with a Default 8alue?
If you want to add a new column to an e-isting table, and insert a default value in this column on all e-isting data rows, you
can use the A1%5 %A,1 ... ADD statement with the D(A21% clause. :ere is an e-ample script*
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) 'LT0R T'>L0 eBpGdeptG"0
2 '33 (acation 2:=>0R 30<':LT 10!I
Ta,le altered.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe$ (acation
2 <R&= eBpGdeptG"0I
<1RSTG2'=0 L'STG2'=0 ?'C'T1&2
-------------------- ------------------------- ----------
Ste(en Aing 10
2eena Aochhar 10
Le+ 3e /aan 10
As you can see, the CD(A21% AMC clause did inserted AM to all e-isting data rows.
:ow %o 5ename a 9olumn in an -isting %able?
1et&s say you have an e-isting with an e-isting column, but you don&t li/e the name of that column, can you rename that
column name? %he answer is yes. )ou can use the A1%5 %A,1 ... 5.A< 9O12<. statement to do this. #ee the
following #01 script*
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) S0L0CT 7irstGnaBe$ lastGnaBe <R&= eBpGdeptG"0I
<1RSTG2'=0 L'STG2'=0
-------------------- -------------------------
Ste(en Aing
2eena Aochhar
35
Le+ 3e /aan
SQL) 'LT0R T'>L0 eBpGdeptG"0 R02'=0 C&L:=2 7irstGnaBe
2 T& 7naBeI
Ta,le altered.
SQL) S0L0CT 7naBe$ lastGnaBe <R&= eBpGdeptG"0I
<2'=0 L'STG2'=0
-------------------- -------------------------
Ste(en Aing
2eena Aochhar
Le+ 3e /aan
As you can see the column Cfirst6nameC is nicely changed to CfnameC.
:ow %o Delete a 9olumn in an -isting %able?
If you have an e-isting column in a table and you need that column any more, you can delete it with A1%5 %A,1 ...
D5O' 9O12<. statement. :ere is an e-ample #01 script*
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) S0L0CT lastGnaBe <R&= eBpGdeptG"0I
L'STG2'=0
-------------------------
Aing
Aochhar
3e /aan
SQL) 'LT0R T'>L0 eBpGdeptG"0 3R&P C&L:=2 lastGnaBeI
Ta,le altered.
SQL) S0L0CT lastGnaBe <R&= eBpGdeptG"0I
0RR&R at line 1:
&R'-00"0K: QL'STG2'=0Q: in(alid identi7ier
As you can see the column Clast6nameC is gone.
:ow %o 8iew All 9olumns in an -isting %able?
If you have an e-isting table and want to /now how many columns are in the table and how they are defined, you can use
the system view 2#56%A,69O12<.# as shown in the following tutorial e-ercise*
SQL) C&L dataGtype <&R='T '12I
SQL) S0L0CT coluBnGnaBe$ dataGtype$ dataGlength
<R&= userGta,GcoluBns ;/0R0 ta,leGnaBe H L0=PL&900SLI
C&L:=2G2'=0 3'T'GT9P0 3'T'GL02@T/
------------------------------ ------------ -----------
0=PL&900G13 2:=>0R 22
<1RSTG2'=0 ?'RC/'R2 20
L'STG2'=0 ?'RC/'R2 2%
0='1L ?'RC/'R2 2%
P/&20G2:=>0R ?'RC/'R2 20
/1R0G3'T0 3'T0 W
U&>G13 ?'RC/'R2 10
S'L'R9 2:=>0R 22
C&==1SS1&2GPCT 2:=>0R 22
='2'@0RG13 2:=>0R 22
30P'RT=02TG13 2:=>0R 22
:ow %o 5ecover a Dropped %able?
If you accidentally dropped a table, can you recover it bac/? %he answer is yes, if you have the recycle bin feature turned
on. )ou can use the (1A#:,A9D %A,1 ... %O ,(O5 D5O' statement to recover a dropped table from the recycle bin
as shown in the following #01 script*
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) S0L0CT C&:2T*! <R&= eBpGdeptG"0I
C&:2T*!
----------
N
SQL) 3R&P T'>L0 eBpGdeptG"0I
Ta,le dropped.
SQL) <L'S/>'CA T'>L0 eBpGdeptG"0 T& >0<&R0 3R&P
2 R02'=0 T& eBpGdeptG,cRI
<lash,acR coBplete.
SQL) S0L0CT C&:2T*! <R&= eBpGdeptG,cRI
C&:2T*!
----------
N
%he (1A#:,A#D statement in this script recovered the dropped table Cemp6dept6VMC to new name Cemp6dept6bc/C. All the
data rows are recovered nicely.
36
What Is a 5ecycle ,in?
5ecycle bin is a logical storage to hold the tables that have been dropped from the database, in case it was dropped in error.
%ables in recycle bin can be recovered bac/ into database by the (lashbac/ Drop action. Oracle database recycle save the
same purpose as the recycle bin on your Windows des/top.
5ecycle bin can be turned on or off in the recyclebin>on?off in your parameter file.
:ow %o %urn On or Off 5ecycle ,in for the Instance?
)ou can turn on or off the recycle bin feature for an instance in the instance parameter file with Crecyclebin>on?offC. )ou can
also turn on or off the recycle bin feature on the running instance with a #01@'lus command, if you log in as #)#%<. #ee
the following e-ample*
SQL) connect S9ST0=.7yicenter
Connected.
SQL) S/&; P'R'=0T0RS R0C9CL0>12
2'=0 T9P0 ?'L:0
------------------------------------ ----------- -------
recycle,in string on
SQL) 'LT0R S9ST0= S0T R0C9CL0>12 H &<<I
SysteB altered.
SQL) S/&; P'R'=0T0RS R0C9CL0>12
2'=0 T9P0 ?'L:0
------------------------------------ ----------- -------
recycle,in string &<<
Warning* %urning off the recycle bin feature will give your users hard times on recovering dropped tables.
:ow %o 8iew the Dropped %ables in )our 5ecycle ,in?
)ou can loo/ what&s in your recycle bin through the predefined view called 59)91,I.. )ou can use the #19%
statement to list the dropped tables as shown in the following script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) S0L0CT C&:2T*! <R&= eBpGdeptG"0I
C&:2T*!
----------
N
SQL) 3R&P T'>L0 eBpGdeptG"0I
Ta,le dropped.
SQL) C&L originalGnaBe <&R='T '1K
SQL) S0L0CT o,XectGnaBe$ originalGnaBe$ droptiBe
2 <R&= recycle,inI
&>U0CTG2'=0 &R1@12'LG2'=0 3R&PT1=0
------------------------------ ------------- ---------------
>12[uaSS.heeQuys%N/g4Rh00QHH[0 0=PG30PTG10 0J-0K-01:1#:%W:
>12[gSt"%rW'TA@:Pu'L1/yKdCHH[0 0=PG30PTG10 0J-0K-01:1":%":
>12[,LuR,cgSQJBA1P2Q?R7Y7QHH[0 0=PG30PTG"0 0J-0K-01:20:KW:
As you can use the <'6D'%6AM was dropped twice. If the same table was dropped multiple times, you need to restore
by using the ob3ect name in the recycle bin with (1A#:,A9D statement.
.ote that 59)91,I. is 3ust an alias of 2#5659)91,I..
:ow %o mpty )our 5ecycle ,in?
If your recycle bin is full, or you 3ust want to clean your recycle bin to get rid of all the dropped tables, you can empty it by
using the '25$ statement in two formats*
• '25$ 59)91,I. + 5emoves all dropped tables from your recycle bin.
• '25$ %A,1 table6name + 5emoves the specified table from your recycle bin.
:ere is an e-ample script on how to use the '25$ statement*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 eBpGdeptG10
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH10I
Ta,le created.
SQL) 3R&P T'>L0 eBpGdeptG10I
Ta,le dropped.
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) 3R&P T'>L0 eBpGdeptG"0I
Ta,le dropped.
SQL) S0L0CT C&:2T*! <R&= recycle,inI
37
C&:2T*!
----------
%
SQL) P:R@0 T'>L0 eBpGdeptG"0I
Ta,le purged.
SQL) S0L0CT C&:2T*! <R&= recycle,inI
C&:2T*!
----------
K
SQL) P:R@0 R0C9CL0>12I
Recycle,in purged.
SQL) S0L0CT C&:2T*! <R&= recycle,inI
C&:2T*!
----------
0
:ow %o %urn On or Off 5ecycle ,in for the #ession?
If you want to control the recycle bin feature in your own session, you can use the A1%5 ###IO. statement to turn on or
off. :ere is an e-ample #01 script*
SQL) connect /R.7yicenter
Connected.
SQL) S0L0CT C&:2T*! <R&= recycle,inI
C&:2T*!
----------
0
SQL) 'LT0R S0SS1&2 S0T recycle,in H o77I
Session altered.
SQL) CR0'T0 T'>L0 eBpGdeptG"0
2 'S S0L0CT * <R&= eBployees ;/0R0 departBentGidH"0I
Ta,le created.
SQL) 3R&P T'>L0 eBpGdeptG"0I
Ta,le dropped.
SQL) S0L0CT C&:2T*! <R&= recycle,inI
C&:2T*!
----------
0
Warning* %urning off the recycle bin feature in your session will give yourself hard times on recovering dropped tables.
:ow %o 1ist All %ables in )our #chema?
If you log in with your Oracle account, and you want to get a list of all tables in your schema, you can get it through the
2#56%A,1# view with a #19% statement, as shown in the following #01 script*
SQL) connect /R.7yicenter
Connected.
SQL) S0L0CT ta,leGnaBe$ status$ nuBGroCs <R&= :S0RGT'>L0SI
T'>L0G2'=0 ST'T:S 2:=GR&;S
------------------------------ -------- ----------
R0@1&2S ?'L13 K
L&C'T1&2S ?'L13 2N
30P'RT=02TS ?'L13 2W
U&>S ?'L13 1"
0=PL&900S ?'L13 10W
U&>G/1ST&R9 ?'L13 10
C&:2TR10S ?'L13 2%
W roCs selected.
What Is a %able Inde-?
Inde- is an optional structure associated with a table that allow #01 statements to e-ecute more 4uic/ly against a table. =ust
as the inde- in this manual helps you locate information faster than if there were no inde-, an Oracle Database inde-
provides a faster access path to table data. )ou can use inde-es without rewriting any 4ueries. )our results are the same,
but you see them more 4uic/ly.
:ow %o 5un #01 #tatements through the Web Interface?
If you don&t li/e the command line interface offered by #01@'lus, you can use the Web interface to run #01 statements.
:ere is how*
• Open your Web browser to http*??localhost*RMRM?ape-?
• 1og in to the server with the predefined sample user account* :5?fyicenter
• 9lic/ the #01 icon
• 9lic/ the #01 9ommands icon
• nter any #01 statement li/e* C#19% 9O2.%!@" (5O< 2#56%A,1#7C, in the te-t area and clic/ 5un button
• )our Oracle will e-ecute the statement, and display the result in the result area.
38
:ow %o 9reate a %able Inde-?
If you have a table with a lots of rows, and you /now that one of the columns will be used often a search criteria, you can
add an inde- for that column to in improve the search performance. %o add an inde-, you can use the 95A% I.DF
statement as shown in the following script*
CR0'T0 T'>L0 tip id 2:=>0R%! PR1='R9 A09$
su,Xect ?'RC/'R#0! 2&T 2:LL$
description ?'RC/'R2%J! 2&T 2:LL$
createGdate 3'T0 30<':LT sysdate!!I
Ta,le created.

CR0'T0 12304 tipGsu,Xect &2 tipsu,Xect!I
1nde+ created.
:ow %o 1ist All Inde-es in )our #chema?
If you log in with your Oracle account, and you want to get a list of all inde-es in your schema, you can get it through the
2#56I.DF# view with a #19% statement, as shown in the following #01 script*
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H L0=PL&900SLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
0=PG0='1LG:A 0=PL&900S :21Q:0
0=PG0=PG13GPA 0=PL&900S :21Q:0
0=PG30P'RT=02TG14 0=PL&900S 2&2:21Q:0
0=PGU&>G14 0=PL&900S 2&2:21Q:0
0=PG='2'@0RG14 0=PL&900S 2&2:21Q:0
0=PG2'=0G14 0=PL&900S 2&2:21Q:0
As you can see, the pre+defined table <'1O)# has L inde-es defined in the default sample database.
What Is an Inde- Associated with a 9onstraint?
An inde- associated with a constraint because this constraint is re4uired to have an inde-. %here are two types of
constraints are re4uired to have inde-es* 2.I02 and '5I<A5) D). When you defines a 2.I02 or '5I<A5) D)
constraint in a table, Oracle will automatically create an inde- for that constraint. %he following script shows you an e-ample*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.

S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K12N ST:302T :21Q:0
S9SGC00K12K ST:302T :21Q:0
%he result confirms that Oracle automatically created two inde-es for you.
:ow %o 5ename an Inde-?
1et&s say you have an e-isting inde-, and you don&t li/e its name anymore for some reason, you can rename it with the
A1%5 I.DF ... 5.A< %O statement. :ere is an e-ample script on how to rename an inde-*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.

S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K1%N ST:302T :21Q:0
S9SGC00K1%K ST:302T :21Q:0
'LT0R 12304 S9SGC00K1%N R02'=0 T& studentGpRI
StateBent processed.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
ST:302TGPA ST:302T :21Q:0
S9SGC00K1%K ST:302T :21Q:0
:ow %o Drop an Inde-?
If you don&t need an e-isting inde- any more, you should delete it with the D5O' I.DF statement. :ere is an e-ample #01
script*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.
39
CR0'T0 12304 studentG,irthGdate &2 student,irthGdate!I
1nde+ created.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K12" ST:302T :21Q:0
S9SGC00K1N0 ST:302T :21Q:0
ST:302TG>1RT/G3'T0 ST:302T 2&2:21Q:0
3R&P 12304 ST:302TG>1RT/G3'T0I
1nde+ dropped.
9an )ou Drop an Inde- Associated with a 2ni4ue or 'rimary Dey 9onstraint?
)ou can not delete the inde- associated with a uni4ue or primary /ey constraint. If you try, you will get an error li/e this*
O5A+M;I;V* cannot drop inde- used for enforcement of uni4ue?primary /ey.
What :appens to Inde-es If )ou Drop a %able?
If you drop a table, what happens to its inde-es? %he answer is that if a table is dropped, all its inde-es will be dropped too.
%ry the following script to see yourself*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.
CR0'T0 12304 studentG,irthGdate &2 student,irthGdate!I
1nde+ created.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K1K1 ST:302T :21Q:0
S9SGC00K1K2 ST:302T :21Q:0
ST:302TG>1RT/G3'T0 ST:302T 2&2:21Q:0
3R&P T'>L0 ST:302TI
Ta,le dropped.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
no data 7ound
.o records in 2#56I.DF# view found for your name #%2D.%, after you dropped #%2D.% table.
:ow %o 5ecover a Dropped Inde-?
If you have the recycle bin feature turned on, dropped inde-es are stored in the recycle bin. ,ut it seems to be command to
restore a dropped inde- out of the recycle bin. (1A#:,A9D I.DF is not a valid statement. #ee the following script*
'LT0R S0SS1&2 S0T recycle,in H onI
StateBent processed.
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.
CR0'T0 12304 studentG,irthGdate &2 student,irthGdate!I
1nde+ created.
3R&P 12304 ST:302TG>1RT/G3'T0I
1nde+ dropped.
S0L0CT o,XectGnaBe$ originalGnaBe$ type$ droptiBe
<R&= recycle,inI
&>U0CTG2'=0 &R1@12'LG2'=0 T9P0 3R&PT1=0
----------------------- ------------------ ----- ----------
>12[1LlsXT+0RA-YCW'HH[0 ST:302TG>1RT/G3'T0 12304 200J-0K-01
<L'S/>'CA 12304 studentG,irthGdate T& >0<&R0 3R&PI
&R'-00"0%: Bissing ReyCord
What :appens to the Inde-es If a %able Is 5ecovered?
If you dropped a table, and recovered it bac/ from the recycle bin, what happens to its inde-es? Are all inde-es recovered
bac/ automatically? %he answer is that all inde-es will be recovered, if you recover a dropped table from the recycle bin.
:owever, the inde-es& names will not be the original names. Inde-es will be recovered with the system assigned names
when they were dropped into the cycle bin. %he following #01 script shows you this behavior*
'LT0R S0SS1&2 S0T recycle,in H onI
StateBent processed.
40
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.
CR0'T0 12304 studentG,irthGdate &2 student,irthGdate!I
1nde+ created.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K1K1 ST:302T :21Q:0
S9SGC00K1K2 ST:302T :21Q:0
ST:302TG>1RT/G3'T0 ST:302T 2&2:21Q:0
3R&P T'>L0 studentI
Ta,le dropped.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
no data 7ound
<L'S/>'CA T'>L0 student T& >0<&R0 3R&PI
<lash,acR coBplete.
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
------------------------------ ---------- ---------
>12[AKWSgYudQ(2t3:;%c;'1rQHH[0 ST:302T :21Q:0
>12[J;10gcW"Q2-LS2@p2/2Q1QHH[0 ST:302T :21Q:0
>12["/COerBRRtY"gon/S.RlsQHH[0 ST:302T 2&2:21Q:0
If you have trouble running the (1A#:,A9D statement on your Web interface, run it with #01@'lus.
:ow %o 5ebuild an Inde-?
If you want to rebuild an inde-, you can use the CA1%5 I.DF ... 5,2I1D statement as shown in the following #01 script*
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H L0=PL&900SLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
0=PG0='1LG:A 0=PL&900S :21Q:0
0=PG0=PG13GPA 0=PL&900S :21Q:0
0=PG30P'RT=02TG14 0=PL&900S 2&2:21Q:0
0=PGU&>G14 0=PL&900S 2&2:21Q:0
0=PG='2'@0RG14 0=PL&900S 2&2:21Q:0
0=PG2'=0G14 0=PL&900S 2&2:21Q:0
'LT0R 12304 0=PG2'=0G14 R0>:1L3I
StateBent processed.
:ow %o #ee the %able 9olumns 2sed in an Inde-?
)ou can a list of inde-es in your schema from the 2#56I.DF# view, but it will not give you the columns used in each
inde- in the 2#56I.DF# view. If you want to see the columns used in an inde-, you can use the
2#56I.D69O12<.# view. :ere is an e-ample script for you*
S0L0CT inde+GnaBe$ ta,leGnaBe$ coluBnGnaBe
<R&= :S0RG123GC&L:=2S ;/0R0 ta,leGnaBe H L0=PL&900SLI
12304G2'=0 T'>L0G2'=0 C&L:=2G2'=0
-------------------- ---------------- ----------------
0=PG0='1LG:A 0=PL&900S 0='1L
0=PG0=PG13GPA 0=PL&900S 0=PL&900G13
0=PG30P'RT=02TG14 0=PL&900S 30P'RT=02TG13
0=PGU&>G14 0=PL&900S U&>G13
0=PG='2'@0RG14 0=PL&900S ='2'@0RG13
0=PG2'=0G14 0=PL&900S L'STG2'=0
0=PG2'=0G14 0=PL&900S <1RSTG2'=0
:ow %o 9reate a #ingle Inde- for <ultiple 9olumns?
If you /now a group of multiple columns will be always used together as search criteria, you should create a single inde- for
that group of columns with the CO. table6name!colA, col;, ..."C clause. :ere is an e-ample of one inde- for two columns*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
Ta,le created.

CR0'T0 12304 studentGnaBes &2 student7irstGnaBe$lastGnaBe!I
1nde+ created.
41
S0L0CT inde+GnaBe$ ta,leGnaBe$ uni-ueness
<R&= :S0RG123040S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 :21Q:020S
----------------------- --------------------- ---------
S9SGC00K12N ST:302T :21Q:0
S9SGC00K12K ST:302T :21Q:0
ST:302TG2'=0S ST:302T 2&2:21Q:0
S0L0CT inde+GnaBe$ ta,leGnaBe$ coluBnGnaBe
<R&= :S0RG123GC&L:=2S ;/0R0 ta,leGnaBe H LST:302TLI
12304G2'=0 T'>L0G2'=0 C&L:=2G2'=0
-------------------- ---------------- ----------------
S9SGC00K1N% ST:302T 13
S9SGC00K1NJ ST:302T S&C1'LG2:=>0R
ST:302TG2'=0S ST:302T <1RSTG2'=0
ST:302TG2'=0S ST:302T L'STG2'=0
%he 2#56I.D69O12<.# view confirms that inde- #%2D.D6.A<# has two columns (I5#%6.A< and
1A#%6.A<.
What Is an Oracle %ablespace?
An Oracle tablespace is a big unit of logical storage in an Oracle database. It is managed and used by the Oracle server to
store structures data ob3ects, li/e tables and inde-es.
What Is an Oracle Data (ile?
An Oracle data file is a big unit of physical storage in the O# file system. One or many Oracle data files are organized
together to provide physical storage to a single Oracle tablespace.
:ow a %ablespace Is 5elated to Data (iles?
ach tablespace in an Oracle database consists of one or more files called datafiles, which are physical structures that
conform to the operating system in which Oracle is running.
:ow a Database Is 5elated to %ablespaces?
A database&s data is collectively stored in the datafiles that constitute each tablespace of the database. (or e-ample, the
simplest Oracle database would have one tablespace and one datafile. Another database can have three tablespaces, each
consisting of two datafiles !for a total of si- datafiles".
:ow %o 8iew the %ablespaces in the 9urrent Database?
If you want to get a list of all tablespaces used in the current database instance, you can use the D,A6%A,1#'A9# view
as shown in the following #01 script e-ample*
SQL) connect S9ST0=.7yicenter
Connected.
SQL) S0L0CT T'>L0SP'C0G2'=0$ ST'T:S$ C&2T02TS
2 <R&= :S0RGT'>L0SP'C0SI
T'>L0SP'C0G2'=0 ST'T:S C&2T02TS
------------------------------ --------- ---------
S9ST0= &2L120 P0R='202T
:23& &2L120 :23&
S9S':4 &2L120 P0R='202T
T0=P &2L120 T0=P&R'R9
:S0RS &2L120 P0R='202T
What Are the 'redefined %ablespaces in a Database?
When you create a new database, Oracle server will create I re4uired tablespaces for the new database*
• #)#%< %ablespace + very Oracle database contains a tablespace named #)#%<, which Oracle creates
automatically when the database is created. %he #)#%< tablespace is always online when the database is open.
• #)#A2F %ablespace + %he #)#A2F tablespace was installed as an au-iliary tablespace to the #)#%< tablespace
when you created your database. #ome database components that formerly created and used separate tablespaces
now occupy the #)#A2F tablespace.
• 2.DO %ablespace + 2.DO tablespaces are special tablespaces used solely for storing undo information. )ou cannot
create any other segment types !for e-ample, tables or inde-es" in undo tablespaces. ach database contains zero or
more undo tablespaces. In automatic undo management mode, each Oracle instance is assigned one !and only one"
undo tablespace. 2ndo data is managed within an undo tablespace using undo segments that are automatically
created and maintained by Oracle.
• %<' %ablespace + When the #)#%< tablespace is locally managed, you must define at least one default temporary
tablespace when creating a database. A locally managed #)#%< tablespace cannot be used for default temporary
storage.
:ow %o 8iew the Data (iles in the 9urrent Database?
If you want to get a list of all tablespaces used in the current database instance, you can use the D,A6%A,1#'A9# view
as shown in the following #01 script e-ample*
SQL) connect S9ST0=.7yicenter
Connected.
SQL) col ta,lespaceGnaBe 7orBat a1JI
SQL) col 7ileGnaBe 7orBat aNJI
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G2'=0$ >9T0S
2 <R&= 3>'G3'T'G<1L0SI
T'>L0SP'C0G2'=0 <1L0G2'=0 >9T0S
--------------- ------------------------------- ---------
:S0RS *&R'CL040*&R'3'T'*40*:S0RS.3>< 10K#%WJ00
S9S':4 *&R'CL040*&R'3'T'*40*S9S':4.3>< KJ1NWNKK0
:23& *&R'CL040*&R'3'T'*40*:23&.3>< "KNW1#K0
42
S9ST0= *&R'CL040*&R'3'T'*40*S9ST0=.3>< N%J%1%#K0
:ow %o 9reate a new Oracle Data (ile?
%here is no dedicated statement to create a data file. Data files are created as part of statements that manages other data
structures, li/e tablespace and database.
:ow %o 9reate a .ew %ablespace?
If you want a new dataspace, you can use the 95A% %A,1#'A9 ... DA%A(I1 statement as shown in the following
script*
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) S0L0CT T'>L0SP'C0G2'=0$ ST'T:S$ C&2T02TS
2 <R&= :S0RGT'>L0SP'C0SI
T'>L0SP'C0G2'=0 ST'T:S C&2T02TS
---------------- --------------- ---------
S9ST0= &2L120 P0R='202T
:23& &2L120 :23&
S9S':4 &2L120 P0R='202T
T0=P &2L120 T0=P&R'R9
:S0RS &2L120 P0R='202T
=9GSP'C0 &2L120 P0R='202T
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G2'=0$ >9T0S
2 <R&= 3>'G3'T'G<1L0SI
T'>L0SP'C0G2'=0 <1L0G2'=0 >9T0S
--------------- -------------------------------- ---------
:S0RS *&R'CL040*&R'3'T'*40*:S0RS.3>< 10K#%WJ00
S9S':4 *&R'CL040*&R'3'T'*40*S9S':4.3>< KJ1NWNKK0
:23& *&R'CL040*&R'3'T'*40*:23&.3>< "KNW1#K0
S9ST0= *&R'CL040*&R'3'T'*40*S9ST0=.3>< N%J%1%#K0
=9GSP'C0 *T0=P*=9GSP'C0.3>< 10K#%WJ0
#o one statement created two structures* a tablespace and a data file. If you chec/ your file system with Windows file
e-plorer, you will see the data file is located in the Ntemp directory of. %he data file size is about AM<,. Its contents should be
blan/ and full of N-MM at this time.
:ow %o 5ename a %ablespace?
)ou can easily rename a tablespace by using the A1%5 %A,1#'A9 ... 5.A< %O statement as shown in the
e-ample below*
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) 'LT0R T'>L0SP'C0 ByGspace R02'=0 T& yourGspaceI
Ta,lespace created.
SQL) S0L0CT T'>L0SP'C0G2'=0$ ST'T:S$ C&2T02TS
2 <R&= :S0RGT'>L0SP'C0SI
T'>L0SP'C0G2'=0 ST'T:S C&2T02TS
---------------- --------------- ---------
S9ST0= &2L120 P0R='202T
:23& &2L120 :23&
S9S':4 &2L120 P0R='202T
T0=P &2L120 T0=P&R'R9
:S0RS &2L120 P0R='202T
9&:RGSP'C0 &2L120 P0R='202T
:ow %o Drop a %ablespace?
If you have an e-isting tablespace and you don&t want it anymore. )ou can delete a tablespace by using the D5O'
%A,1#'A9 statement as shown in the e-ample below*
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) 3R&P T'>L0SP'C0 ByGspaceI
Ta,lespace dropped.
What :appens to the Data (iles If a %ablespace Is Dropped?
If a tablespace is dropped, what happens to its data files? ,y default, data files will remain in O# file system, even if the
tablespace they are mapped is dropped. Of course, you delete the data files using O# commands, if they are no longer
needed.
Another way of deleting data files is to use the I.912DI.$ clause in the D5O' %A,1#'A9 statement. :ere is a #01
sample script*
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) 3R&P T'>L0SP'C0 ByGspace 12CL:312@ C&2T02TS
2 '23 3'T'<1L0SI
Ta,lespace dropped.
With the I.912DI.$ 9O.%.%# A.D DA%A(I1# clause, all contents and mapped data files are also deleted.
:ow %o 9reate a %able in a #pecific %ablespace?
43
After you have created a new tablespace, you can give it to your users for them to create tables in the new tablespace. %o
create a table in a specific tablespace, you need to use the %A,1#'A9 clause in the 95A% %A,1 statement. :ere is
a sample script*
SQL) connect S9ST0=.7yicenter
Connected.
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0 ByGteaB T'>L0SP'C0 ByGspace
2 'S S0L0CT * <R&= eBployeesI
Ta,le created.
SQL) S0L0CT ta,leGnaBe$ ta,lespaceGnaBe$ nuBGroCs
2 <R&= :S0RGT'>L0S
N ;/0R0 ta,lespaceGnaBe in L:S0RSL$ L=9GSP'C0L!I
T'>L0G2'=0 T'>L0SP'C0G2'=0 2:=GR&;S
------------------------------ ---------------- ----------
=9GT0'= =9GSP'C0 -
0=PL&900S :S0RS 10W
...
:ow %o #ee (ree #pace of ach %ablespace?
One of the important D,A tas/s is to watch the storage usage of all the tablespaces to ma/e sure there are enough free
space in each tablespace for database applications to function properly. (ree space information can be monitored through
the 2#56(56#'A9 view. ach record in 2#56(56#'A9 represents an e-tent, a contiguous area of space, of
free space in a data file of a tablespace.
:ere is #01 script e-ample on how to see free space of a tablespace*
SQL) connect /R.7yicenter
Connected.
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G13$ >9T0S
2 <R&= :S0RG<R00GSP'C0
N ;/0R0 T'>L0SP'0G2'=0 12 L:S0RSL$ L=9GSP'C0L!I
T'>L0SP'C0G2'=0 <1L0G13 >9T0S
------------------------------ ---------- ----------
=9GSP'C0 % 10N%KJ##
:S0RS K 101"WK01J
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
:S0RS K J%%NJ
%his tells us that*
• <)6#'A9 has a single free e-tent of AM<,.
• 2#5# has one big free e-tent of AMM<,, and many small free e-tents of LID,.
:ow %o ,ring a %ablespace Offline?
If you want to stop users using a tablespace, you can bring it offline using the A1%5 %A,1#'A9 ... O((1I. statement
as shown in the following script*
SQL) connect /R.7yicenter
Connected.
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) 'LT0R T'>L0SP'C0 ByGspace &<<L120 2&R='LI
Ta,lespace altered.
After bringing a tablespace offline, you can bac/up or rename the data file safely.
:ow %o ,ring a %ablespace Online?
If you have brought a tablespace offline, now you want to ma/e it available to users again, you can use the A1%5
%A,1#'A9 ... O.1I. statement as shown in the following script*
SQL) connect /R.7yicenter
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
44
SQL) 'LT0R T'>L0SP'C0 ByGspace &<<L120 2&R='LI
Ta,lespace altered.
...
SQL) 'LT0R T'>L0SP'C0 ByGspace &2L120I
Ta,lespace altered.
:ow %o Add Another Datafile to a %ablespace?
If you created a tablespace with a data file a month ago, now RMW of the data file is used, you should add another data file to
the tablespace. %his can be done by using the A1%5 %A,1#'A9 ... ADD DA%A(I1 statement. #ee the following
sample script*
SQL) connect /R.7yicenter
SQL) CR0'T0 T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspace.d,7L S1O0 10=I
Ta,lespace created.
SQL) 'LT0R T'>L0SP'C0 ByGspace
2 3'T'<1L0 L.teBp.ByGspaceG2.d,7L S1O0 %=I
Ta,lespace altered.
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G2'=0$ >9T0S
2 <R&= 3>'G3'T'G<1L0SI
T'>L0SP'C0G2'=0 <1L0G2'=0 >9T0S
--------------- --------------------------------- ---------
:S0RS C:*&R'CL040*&R'3'T'*40*:S0RS.3>< 10K#%WJ00
S9S':4 C:*&R'CL040*&R'3'T'*40*S9S':4.3>< KJ1NWNKK0
:23& C:*&R'CL040*&R'3'T'*40*:23&.3>< "KNW1#K0
S9ST0= C:*&R'CL040*&R'3'T'*40*S9ST0=.3>< N%J%1%#K0
=9GSP'C0 C:*T0=P*=9GSP'C0.3>< 10K#%WJ0
=9GSP'C0 C:*T0=P*=9GSP'C0G2.3>< %2K2##0
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G13$ >9T0S
2 <R&= :S0RG<R00GSP'C0
N ;/0R0 T'>L0SP'0G2'=0 12 L=9GSP'C0L!I
T'>L0SP'C0G2'=0 <1L0G13 >9T0S
------------------------------ ---------- ----------
=9GSP'C0 J %1WWNKK
=9GSP'C0 % 10N%KJ##
%his script created one tablespace with two data files.
What :appens If )ou 1ost a Data (ile?
After you shuting down an Oracle database, you accidently deleted a data file from the operating system. If you try to start
the database again you will get error when Oracle tries to open the database after mounting the database. %he following
tutorial e-amples shows you what will happen if the data file c*NtempNmy6space.dbf is deleted. Oracle can still start the
database instance and mount the database. ,ut it will fail on openning the database as shown below in a #01@'lus session*
)s-lplus .nolog
SQL) connect S9ST0=.7yicenter 'S S9S3>'
SQL) ST'RT:P
&R'CL0 instance started.
Total SysteB @lo,al 'rea 100JJN2"J ,ytes
<i+ed SiPe 12#%"%J ,ytes
?aria,le SiPe %#W20KKK ,ytes
3ata,ase >u77ers NWWK#WNJ ,ytes
Redo >u77ers 2"0#1J0 ,ytes
3ata,ase Bounted.
&R'-011%W: cannot identi7y.locR data 7ile % - see 3>;R
trace 7ile
&R'-01110: data 7ile %: LC:*T0=P*=9GSP'C0.3><L
SQL) S/:T3&;2I
&R'-0110": data,ase not open
3ata,ase disBounted.
&R'CL0 instance shut doCn.
:ow 5emove Data (iles befor opening a Database?
1et&s say you have a corrupted data file or lost a data file. Oracle can mount the database. ,ut it will not open the database.
What you can do is to set the bad data file as offline befor opening the database. %he tutorial e-ercise shows you how to set
two data files offline and open the database without them*
)s-lplus .nolog
SQL) connect S9ST0=.7yicenter 'S S9S3>'
SQL) ST'RT:P =&:2TI
&R'CL0 instance started.
Total SysteB @lo,al 'rea 100JJN2"J ,ytes
45
<i+ed SiPe 12#%"%J ,ytes
?aria,le SiPe %#W20KKK ,ytes
3ata,ase >u77ers NWWK#WNJ ,ytes
Redo >u77ers 2"0#1J0 ,ytes
3ata,ase Bounted.
SQL) 'LT0R 3'T'>'S0 3'T'<1L0 L*teBp*ByGspace.d,7L
2 &<<L120 3R&PI
3ata,ase altered.
SQL) 'LT0R 3'T'>'S0 3'T'<1L0 L*teBp*ByGspaceG2.d,7L
2 &<<L120 3R&PI
3ata,ase altered.
SQL) 'LT0R 3'T'>'S0 &P02I
3ata,ase altered.
SQL) col 7ileGnaBe 7orBat aNJI
SQL) col ta,lespaceGnaBe 7orBat a1JI
SQL) S0L0CT T'>L0SP'C0G2'=0$ <1L0G2'=0$ >9T0S
2 <R&= 3>'G3'T'G<1L0SI
T'>L0SP'C0G2'=0 <1L0G2'=0 >9T0S
--------------- --------------------------------- ---------
:S0RS C:*&R'CL040*&R'3'T'*40*:S0RS.3>< 10K#%WJ00
S9S':4 C:*&R'CL040*&R'3'T'*40*S9S':4.3>< %0NN1JK#0
:23& C:*&R'CL040*&R'3'T'*40*:23&.3>< "KNW1#K0
S9ST0= C:*&R'CL040*&R'3'T'*40*S9ST0=.3>< NJW001J00
=9GSP'C0 C:*T0=P*=9GSP'C0.3><
=9GSP'C0 C:*T0=P*=9GSP'C0G2.3><
At this point, if you don&t care about the data in <)6#'A9, you can drop it now with the database opened.
:ow %o 9reate an Oracle Database?
%here are two ways to create a new database*
• 2se the Database 9onfiguration Assistant !D,9A" to create a database interactively.
• 2se the 95A% DA%A,A# statement to create a database manually.
:ow %o 9reate an Oracle Database <anually?
,ased on Oracle&s Administrator $uide, there are AA steps to create a database with the 95A% DA%A,A# statement*
• #tep A* Decide on )our Instance Identifier !#ID"
• #tep ;* stablish the Database Administrator Authentication <ethod
• #tep H* 9reate the Initialization 'arameter (ile
• #tep I* 9onnect to the Instance
• #tep G* 9reate a #erver 'arameter (ile !5ecommended"
• #tep L* #tart the Instance
• #tep J* Issue the 95A% DA%A,A# #tatement
• #tep R* 9reate Additional %ablespaces
• #tep V* 5un #cripts to ,uild Data Dictionary 8iews
• #tep AM* 5un #cripts to Install Additional Options !Optional"
• #tep AA* ,ac/ 2p the Database.
Other items in this (A0 collection will follow those steps to help you creating a new database manually from beginning to
end.
:ow %o #elect an Oracle #ystem ID !#ID"?
%his is #tep A. If you are planning to create a new database, you need to select an Oracle #ystem ID !#ID". %his ID will be
used to identify the new Oracle database and its Oracle instance. #ID must be uni4ue if you want to run multiple databases
on a single server.
1et&s set #ID for the new database to be* ()I.
:ow %o stablish Administrator Authentication to the #erver?
%his is #tep ;. %here are two ways to establish administrator authentication to a new database.
• 2se a password file.
• 2se operating system !O#" authentication.
2sing O# authentication is easier on Windows system. If you used your own Windows user account to install Oracle server,
it will put your Windows user account into a special Window&s user group called #)#D,A. %his Window&s user group will be
fully trusted by Oracle server with #)#D,A privilege.
%o continue with other steps, ma/e sure you logged into the Windows system with a user account in the #)#D,A group.
:ow %o 9reate an Initialization 'arameter (ile?
%his is #tep H. %o run an Oracle database as an Oracle instance, you need to create an initialization parameter file, which
contains a set of initialization parameters.
%he easiest way to create an initialization parameter file to copy from the sample file provided by Oracle. )ou can do this in
a command window as shown below*
)cd [&R'CL0G/&=0
)copy .*con7ig*scripts*init.ora .*data,ase*init<91Gini.ora
46
)edit .*data,ase*init<91Gini.ora
replace 40 ,y <91!
In this e-ample, only the #ID is changed from F to ()I. All other parameters are maintained as is.
:ow %o 9onnect the Oracle #erver as #)#D,A?
%his is #tep I. %he best way to connect to the Oracle server as an administrator is to use #01@'lus. )ou need to run
#01@'lus with ?nolog option and use the 9O..9% with blan/ user name, blan/ password and A# #)#D,A option. :ere is
a sample session*
)cd [&R'CL0G/&=0
).*,in*s-lplus .nolog
SQL) C&220CT . 'S S9S3>'
Connected.
:ow %o 9reate a #erver 'arameter (ile?
%his is #tep G. %he initialization parameter file is good to get an Oracle database instance started. ,ut it is not ideal run an
instance as production. )ou need to convert the initialization parameter file into a #erver 'arameter (ile !#'(ile" using the
95A% #'(I1 statement. %he script below shows you how do this*
SQL) CR0'T0 SP<1L0H[&R'CL0G/&=0.d,s.SP<1L0<91.ora
2 <R&= P<1L0H[&R'CL0G/&=0.data,ase.init<91Gini.oraI
<ile created.
.ote that EO5A916:O< should be replaced by the real path name where your Oracle server is intalled.
%he #'(ile should be located in the e-pected directory and named as #'(I1!E#ID".ora.
:ow %o #tart an Oracle Instance?
%his is #tep L. .ow you are ready to start the new Oracle Instance without any database. %his instance will be used to
create a database. #tarting an instance without database can be done by using #%A5%2' .O<O2.% statement as shown
below*
).*,in*s-lplus .nolog
SQL) C&220CT . 'S S9S3>'
Connected.
SQL) S/:T3&;2
&R'CL0 instance shut doCn.
SQL) ST'RT:P 2&=&:2T
&R'-00#21: Speci7ied (alue o7 sgaGtarget 1J= is too sBall$
needs to ,e at least 20=
%he #:2%DOW. command is need to bring the default instance F down.
%he #%A5%2' .O<O2.% command failed because it tried to start the default instance F, and there is a bad parameter in
the F instance #'(ile.
#ee the ne-t (A0 4uestion to find another way to start the new instance ()I.
:ow %o #tart a #pecific Oracle Instance?
A simple way to start a specific Oracle instance is to start the instance with the '(I1 option as shown in the following
e-ample*
).*,in*s-lplus .nolog
SQL) C&220CT . 'S S9S3>'
Connected.
SQL) ST'RT:P 2&=&:2T
P<1L0H[&R'CL0G/&=0.data,ase.init<91Gini.ora
&R'-02WW#: 2aBe gi(en 7or the log directory is in(alid
%he '(I1 option allows you to specify the initialization parameter file of a specific Oracle instance. ,ut the initialization
parameter file created in #tep H has some problem with the log directory.
:ow %o #tart Instance with a <inimal Initialization 'arameter (ile?
%he sample initialization parameter file provided by Oracle seems to be not wor/ing. ,ut we can try to start the new instance
with a minimal initialization parameter file !'(ile". (irst you can create another '(ile,
EO5A916:O<?database?init()I6ini6min.ora, as shown below*
d,GnaBeH<91
controlG7ilesHQ*oracle+e*oradata*<91*control.d,7Q!
undoGBanageBentH':T&
%hen start the ()I instance again*
SQL) C&220CT . 'S S9S3>'
Connected.
SQL) ST'RT:P 2&=&:2T
P<1L0H[&R'CL0G/&=0.data,ase.init<91GiniGBin.ora
&R'CL0 instance started.
Total SysteB @lo,al 'rea 11N2KJ20# ,ytes
<i+ed SiPe 12#J02# ,ytes
?aria,le SiPe %#W20NW2 ,ytes
3ata,ase >u77ers %0NN1JK# ,ytes
Redo >u77ers 2"0#1J0 ,ytes
:ow %o 5un 95A% DA%A,A# #tatement?
%his is #tep J. Oracle Administrator $uide provided a sample 95A% DA%A,A# statement. ,ut it is a long statement.
)ou can modify and same it in a file, EO5A916:O<?configscripts?create6database6fyi.s4l, and run the file within
#01@'lus. :ere is a copy of the modified 95A% DA%A,A# statement*
47
CR0'T0 3'T'>'S0 <91
:S0R S9S 1302T1<103 >9 7yicenter
:S0R S9ST0= 1302T1<103 >9 7yicenter
L&@<1L0 @R&:P 1 L.oracle+e.oradata.<91.redo01.logL! S1O0 10=$
@R&:P 2 L.oracle+e.oradata.<91.redo02.logL! S1O0 10=$
@R&:P N L.oracle+e.oradata.<91.redo0N.logL! S1O0 10=
='4L&@<1L0S %
='4L&@=0=>0RS %
='4L&@/1ST&R9 1
='43'T'<1L0S 100
='412ST'2C0S 1
C/'R'CT0R S0T :SW'SC11
2'T1&2'L C/'R'CT0R S0T 'L1J:T<1J
3'T'<1L0 L.oracle+e.oradata.<91.systeB01.d,7L S1O0 N2= R0:S0
04T02T ='2'@0=02T L&C'L
S9S':4 3'T'<1L0 L.oracle+e.oradata.<91.sysau+01.d,7L S1O0 N2= R0:S0
30<':LT T'>L0SP'C0 t,sG1
30<':LT T0=P&R'R9 T'>L0SP'C0 teBpts1
T0=P<1L0 L.oracle+e.oradata.<91.teBp01.d,7LS1O0 20= R0:S0
:23& T'>L0SP'C0 undot,s
3'T'<1L0 L.oracle+e.oradata.<91.undot,s01.d,7L
S1O0 20= R0:S0 ':T&04T023 &2 ='4S1O0 :2L1=1T03I
:ere is how to run this long 95A% DA%A,A# statement*
SQL) 8[&R'CL0G/&=0*con7ig*scripts*createGdata,aseG7yi.s-lI
CR0'T0 3'T'>'S0 <91
*
0RR&R at line 1:
&R'-01%01: CR0'T0 3'T'>'S0 7ailed
&R'-00200: control 7ile could not ,e created
&R'-00202: control 7ile: LC:*&R'CL040*&R'3'T'*<91*C&2TR&L.3><L
&R'-2W0K0: 7ile create error$ una,le to create 7ile
&S3-0K002: una,le to open 7ile
&.S-0rror: &S N! The systeB cannot 7ind the path speci7ied.
Don&t worry about the error messages. %hey are caused by the missing directory for data files in file system.
:ow %o Do 9lean 2p If 95A% DA%A,A# (ailed?
%o better organize data files, you should create a dedicated directory for each Oracle database. %his can be done by using
Windows file e-plorer to create the Noracle-eNoradataNfyiN directory. %ry the 95A% DA%A,A# statement again, when you
have the directory ready.
If your 95A% DA%A,A# statement failed half way again, you may have to clean up the partial result of the 95A%
DA%A,A# statement. :ere is a list of suggestions for you*
• 5un #:2%DOW. command to stop the partial started database instance.
• 5emove all files in ()I directory* Noracle-eNoradataNfyiN
• 5un #%A5%2' .O<O2.% '(I1 command to start the empty instance again to be ready for 95A% DA%A,A#
statement.
:ow %o 5un 95A% DA%A,A# #tatement Again?
After cleaning up the results of a previously failed 95A% DA%A,A# statement, you can run the 95A% DA%A,A#
statement again as shown below*
SQL) 8[&R'CL0G/&=0*con7ig*scripts*createGdata,aseG7yi.s-lI
CR0'T0 3'T'>'S0 <91
*
0RR&R at line 1:
&R'-010"2: &R'CL0 instance terBinated. 3isconnection 7orced
#omething went wrong again. %his time it might related the limitation that Oracle AMg F only supports one database
instance. With the default instance called CFC already defined, creating another database instance might be not allowed.
:ow %o 9reate Additional %ablespaces for an .ew Database?
%his is #tep R. 9reating additional tablespaces can be done by using the 95A% %A,1#'A9 statement as shown in the
following sample script*
SQL) CR0'T0 T'>L0SP'C0 users
2 3'T'<1L0 L.oracle+e.oradata.<91.users01.d,7L S1O0 10=I
SQL) CR0'T0 T'>L0SP'C0 ind+
2 3'T'<1L0 L.oracle+e.oradata.<91.ind+01.d,7L S1O0 10=I
:ow %o ,uild Data Dictionary 8iew an .ew Database?
%his is #tep V. %he Oracle Administrator $uide suggests to run two #01 scripts provided by Oracle as shown bellow*
SQL) 8.u01.oracle.rd,Bs.adBin.catalog.s-l
SQL) 8.u01.oracle.rd,Bs.adBin.catproc.s-l
What Is '1?#01?
'1?#01 is a modern, bloc/+structured programming language. It provides several features that ma/e developing powerful
database applications very convenient. (or e-ample, '1?#01 provides procedural constructs, such as loops and conditional
statements, that are not available in standard #01.
'1?#01 code runs on the server, so using '1?#01 lets you centralize significant parts of your database applications for
increased maintainability and security. It also enables you to achieve a significant reduction of networ/ overhead in
client?server applications.
What Are the %ypes '1?#01 9ode ,loc/s?
%here are H types of '1?#01 code bloc/s*
48
• Anonymous ,loc/ + A bloc/ of codes with no name. It may contain a declaration part, an e-ecution part, and e-ception
handlers.
• #tored 'rogram 2nit + A bloc/ of codes with a name. It is similar to an anonymous bloc/. ,ut it can ta/e parameters
and return values.
• %rigger + A bloc/ of code that can be defined to fire based an specific event.
:ow %o Define an Anonymous ,loc/?
An anonymous bloc/ must have an e-ecution part, which is a group of other '1?#01 statements enclosed in the ,$I. ...
.D statement. :ere is a script on how to define a simple anonymous bloc/ with #01@'lus*
SQL) set ser(eroutput onI
SQL) ,egin
2 d,BsGoutput.putGlineL/ello CorldML!I
N endI
K .
/ello CorldM
PL.SQL procedure success7ully coBpleted.
Cset serveroutput on7C allows dbms6output.put6line!" to wor/.
C?C runs the anonymous bloc/, which print the C:ello worldXC message.
:ow <any Anonymous ,loc/s 9an ,e Defined?
An anonymous bloc/ is stored in the user&s current session without any name. #o you can only define one anonymous bloc/
at any time. If you define another anonymous bloc/, the new bloc/ will replace the previously defined bloc/, as shown in the
following script*
SQL) set ser(eroutput onI
SQL) ,egin
2 d,BsGoutput.putGlineL/ello CorldML!I
N endI
K .
/ello CorldM
PL.SQL procedure success7ully coBpleted.
SQL) ,egin
2 d,BsGoutput.putGlineLThis is a PL.SQL <'Q.L!I
N endI
K .
This is a PL.SQL <'Q.
PL.SQL procedure success7ully coBpleted.
:ow %o 5un the Anonymous ,loc/ Again?
If you have an anonymous bloc/ defined in your session, you can run it any time by using the C?C command as shown in the
following script*
SQL) set ser(eroutput onI
SQL) ,egin
2 d,BsGoutput.putGlineLThis is a PL.SQL <'Q.L!I
N endI
K .
This is a PL.SQL <'Q.
PL.SQL procedure success7ully coBpleted.
SQL) .
This is a PL.SQL <'Q.
PL.SQL procedure success7ully coBpleted.
What Is #tored 'rogram 2nit?
A stored program unit is a named bloc/ of codes which*
• :as a name.
• 9an ta/e parameters, and can return values.
• Is stored in the data dictionary.
• 9an be called by many users.
:ow %o 9reate a #tored 'rogram 2nit?
If you want to create a stored program unit, you can use the 95A% '5O9D25 or (2.%IO. statement. %he e-ample
script below creates a stored program unit*
SQL) set ser(eroutput onI
SQL) CR0'T0 PR&C03:R0 /ello 'S
2 >0@12
N 3>=SG&:TP:T.P:TGL120L/ello CorldML!I
K 023I
% .
Procedure created.
:ow %o -ecute a #tored 'rogram 2nit?
49
If you want to e-ecute a stored program unit, you can use the F92% statement. %he e-ample script below shows how to
e-ecutes a stored program unit*
SQL) set ser(eroutput onI
SQL) CR0'T0 PR&C03:R0 /ello 'S
2 >0@12
N 3>=SG&:TP:T.P:TGL120L/ello CorldML!I
K 023I
% .
Procedure created.
SQL) 040C:T0 /elloI
/ello CorldM
:ow <any Data %ypes Are #upported?
'1?#01 supports two groups of data types*
• #01 Data %ypes + All data types used for table columns.
• '1?#01 #pecial Data %ypes + 1i/e ,OO1A. or '1#6I.%$5.
%he script below shows some data type e-amples*
SQL) set ser(eroutput onI
SQL) 30CL'R0
2 title ?'RC/'R#!I
N salary 2:=>0RI
K seeRingGXo, >&&L0'2I
% >0@12
J title :H L3>'LI
W salary :H %0000I
# seeRingGXo, :H TR:0I
" 3>=SG&:TP:T.P:TGL120LUo, Title: L EE title!I
10 3>=SG&:TP:T.P:TGL120L0+pected salary: L
11 EE T&GC/'Rsalary!!I
12 023I
1N .
Uo, Title: 3>'
0+pected salary: %0000
What Are the -ecution 9ontrol #tatements?
'1?#01 supports three groups of e-ecution control statements*
• I( #tatements + 9onditionally e-ecutes a bloc/ of statements.
• 9A# #tatements + #electively e-ecutes a bloc/ of statements.
• 1OO' #tatements + 5epeatedly e-ecutes a bloc/ of statements.
• $O%O #tatements + 2nconditional changes the e-ecution flow to a specified statement.
%he script below shows e-ecution control statements*
SQL) set ser(eroutput onI
SQL) 30CL'R0
2 total 2:=>0RI
N >0@12
K total :H 0I
% L&&P
J total :H totalY1I
W 1< total )H 10 T/02
# @&T& printI
" 023 1<I
10 023 L&&PI
11 T)
12 3>=SG&:TP:T.P:TGL120LTotal counts: L
1N EE T&GC/'Rtotal!!I
1K 023I
1% .
Total counts: 10
%his sample script shows you how to use 1OO', I(, and $O%O statements.
:ow %o 2se #01 #tatements in '1?#01?
#01 D<1 !Data <anipulation 1anguage" statements can be included in '1?#01 code bloc/s directly without any changes.
#ee the script below for e-amples*
SQL) CR0'T0 T'>L0 tip id 2:=>0R%! PR1='R9 A09$
2 su,Xect ?'RC/'R#0! 2&T 2:LL$
N description ?'RC/'R2%J! 2&T 2:LL!I
Ta,le created.
SQL) >0@12
2 12S0RT 12T& tip ?'L:0S1$ LPL.SQLL$
N L@ood 7or ,eginners.L!I
K :P3'T0 tip S0T description H L@ood 7or ,eginners.LI
% 023I
J .
PL.SQL procedure success7ully coBpleted.
50
SQL) C&L su,Xect <&R='T '12I
SQL) C&L description <&R='T '2KI
SQL) S0L0CT * <R&= tipI
13 S:>U0CT 30SCR1PT1&2
---------- ------------ -------------------
1 PL.SQL @ood 7or ,eginners.
SQL) 3R&P T'>L0 tipI
Ta,le dropped.
%his script e-ample actually has H parts*
• 'art A + A single DD1 !Data Definition 1anguage" statement, 95A% %A,1, outside the '1?#01 code bloc/.
• 'art ; + A '1?#01 code bloc/ defined and e-ecuted to insert and updated a record.
• 'art H + A couple of #01 statements outside the '1?#01 code bloc/ to view the record and trop the table.
:ow %o 'rocess 0uery 5esult in '1?#01?
)ou can run 4ueries !#19% statements" in a '1?#01 code bloc/s, and process the results a loop as shown in the
following script e-ample*
SQL) set ser(eroutput onI
SQL) >0@12
2 <&R roC 12
N S0L0CT * <R&= eBployees ;/0R0 BanagerGid H 101!
K L&&P
% 3>=SG&:TP:T.P:TGL120L2aBe H L EE roC.lastGnaBe!I
J 023 L&&PI
W 023I
# .
2aBe H @reen,erg
2aBe H ;halen
2aBe H =a(ris
2aBe H >aer
2aBe H /iggins
%he loop statement in this script automatically sets a temporary variable CrowC each row in the result set returned from the
#19% statement.
:ow %o 9reate an Array in '1?#01?
If you want create an array data structure, you can use the collection type 8A55A). 8A55A) stands for 8ariable Array.
:ere is a sample script on how to use 8A55A)*
SQL) set ser(eroutput onI
SQL) 30CL'R0
2 T9P0 list 1S ?'RR'9N! &< ?'RC/'R1J!I
N doBainGlist listI
K >0@12
% doBainGlist :H listLgoogleL$L7yicenterL$LByspaceL!I
J 3>=SG&:TP:T.P:TGL120LSecond doBain: L
W EE doBainGlist2!!I
# 023I
" .
Second doBain: 7yicenter
As you can see, to define an array variable, you need to create your own array type, and use that type to define array
variables.
:ow %o <anage %ransaction Isolation 1evel?
%ransaction isolation level can be managed in a procedure by using the #% %5A.#A9%IO. and 9O<<I% statements.
:ere is a sample script on how to manage transaction isolation level*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 /R.T&T'LGS'L'R9 'S
2 totalGsalary 2:=>0R12$2!I
N >0@12
K S0T TR'2S'CT1&2 R0'3 &2L9I
% S0L0CT S:= salary! 12T& totalGsalary <R&= eBployeesI
J 3>=SG&:TP:T.P:TGL120LTotal salary 1: L
W EE totalGsalary!I
# -- &ther sessions Bay change salaries o7 soBe records
" S0L0CT S:= salary! 12T& totalGsalary <R&= eBployeesI
10 3>=SG&:TP:T.P:TGL120LTotal salary 2: L
11 EE totalGsalary!I
12 C&==1TI
1N 023I
1K .
SQL) 040C:T0 T&T'LGS'L'R9I
Total salary 1: J"1K00
Total salary 2: J"1K00
C5AD O.1)C transaction level ta/es a read only snapshot of the database. %his allows other sessions to update the
database without any loc/s. All 4ueries in the session will produces identical results. #o both #19% statements in this
script will return the same value guaranteed.
:ow %o 'ass 'arameters to 'rocedures?
#tore procedures or functions can ta/e parameters. )ou need to define parameters while defining the procedure, and
providing values to parameters while calling the procedure. %he script below shows you how to do this*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 3>'GT'SA day ?'RC/'R2! 'S
51
2 >0@12
N 1< day H L=&23'9L T/02
K 3>=SG&:TP:T.P:TGL120LChecRing log 7iles.L!I
% 0LS1< day H L<R13'9L T/02
J 3>=SG&:TP:T.P:TGL120LRe,uild inde+es.L!I
W 0LS0
# 3>=SG&:TP:T.P:TGL120LReading soBe papers.L!I
" 023 1<I
10 023I
11 .
SQL) 040C:T0 3>'GT'SAL=&23'9L!I
ChecRing log 7iles.
SQL) 040C:T0 3>'GT'SALS:23'9L!I
Reading soBe papers.
As you can see, procedures with parameters can ma/e procedures more fle-ible.
:ow %o Define a 'rocedure inside Another 'rocedure?
Define a procedure inside another procedure is supported by '1?#01. %he following tutorial script shows you an e-ample*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 /R.3>'G;00A 'S
2 PR&C03:R0 3>'GT'SA day ?'RC/'R2! 'S
N >0@12
K 1< day H L=&23'9L T/02
% 3>=SG&:TP:T.P:TGL120LChecRing log 7iles.L!I
J 0LS1< day H L<R13'9L T/02
W 3>=SG&:TP:T.P:TGL120LRe,uild inde+es.L!I
# 0LS0
" 3>=SG&:TP:T.P:TGL120LReading soBe papers.L!I
10 023 1<I
11 023I
12 >0@12
1N 3>'GT'SAL=&23'9L!I
1K 3>'GT'SALT:0S3'9L!I
1% 023I
1J .

SQL) 040C:T0 3>'G;00AI
ChecRing log 7iles.
Reading soBe papers.
5emember that procedures used inside a procedure must be defined in the declaration bloc/.
What Do )ou %hin/ about '1?#01?
After following through the tutorials in the (A0 collection, you probably agree that '1?#01 is indeed a general purpose
database programming language. '1?#01 is a natural e-tension of #01. It is very useful for D,A to automate specific
administration tas/s or for developers to developer simple business bac/end applications.
What Is Oracle #01 Developer?
Oracle #01 Developer is a new, free graphical tool that enhances productivity and simplifies database development tas/s.
With #01 Developer, you can browse database ob3ects, run #01 statements and #01 scripts, and edit and debug '1?#01
statements. )ou can also run any number of provided reports, as well as create and save your own.
Oracle #01 Developer is formerly called 'ro3ect 5aptor.
What Operating #ystems Are #upported by Oracle #01 Developer?
Oracle #01 Developer is available for three types of operating #ystems*
• Windows
• 1inu-
• <ac O#F
:ow %o Download Oracle #01 Developer?
If you want to download a copy of Oracle #01 Developer, visit http*??www.oracle.com?technology?software?products?s4l?.
If you are using Windows systems, clic/ the COracle #01 Developer for WindowsC lin/. %his allows you to download the
Windows version in KI' format with file name of s4ldeveloper+vA.M.M.zip in GG,;VG,J;A bytes.
:ow %o Install Oracle #01 Developer?
Once you have s4ldeveloper+vA.M.M.zip downloaded, e-tract the zip file into the root directory* N.
When the e-traction is done, the installation is completed. )our copy of Oracle #01 Developer is installed in directory*
Ns4ldeveloper, and ready to run.
:ow %o #tart Oracle #01 Developer?
%o start Oracle #01 Developer, go to Ns4ldeveloper and clic/ s4ldeveloper.e-e. %he Oracle #01 Developer window shows
up. )our copy of Oracle #01 Developer is running now. %here will nothing under the 9onnections icon, because you haven&t
defined any connections yet.
)ou should visit different menu items to e-plore the user interface. One nice place to visit is the About item under the :elp
menu. It provides some very useful information about your copy of Oracle #01 Developer.
Is Oracel #01 Developer written in =ava?
Oracel #01 Developer is written in =ava. It re4uires =DD A.G, which is already included in your download file.
:ow %o 9onnect to a 1ocal Oracle AMg F #erver?
If you have your Oracle AMg F server running on your local machine, you can connect your Oracle #01 Developer to the
server with the following steps*
• #tart Oracle #01 Developer
• 5ight+clic/ on 9onnections
• #elect .ew Database 9onnection
52
• nter 9onnection .ame as* 1ocal6F
• nter 2sername as* :5
• nter 'assword as* fyicenter
• nter :ostname as* A;J.M.M.A
• nter #ID as* F
• 9lic/ %est
• 9lic/ 9onnect, if you see #tatus changed to #uccess
)ou will see the #01 #tatement area opened ready to ta/e any #01 statements.
:ow %o 9onnect to a 5emote #erver?
If you have an Oracle server running remotely on a networ/, and you /now all the access information needed, you can
following steps to connect your Oracle #01 Developer*
• #tart Oracle #01 Developer
• 5ight+clic/ on 9onnections
• #elect .ew Database 9onnection
• nter 9onnection .ame as* 5emote6#erver
• nter 2sername as* 6username6
• nter 'assword as* 6password6
• nter :ostname as* 6hostname6
• nter #ID as* 6sidname6
• 9lic/ %est
• 9lic/ 9onnect, if you see #tatus changed to #uccess
)ou will see the #01 #tatement area opened ready to ta/e any #01 statements.
:ow %o 5un #01 #tatements with Oracle #01 Developer?
Once a connection is established with an Oracle server, you can enter any #01 statements in the #01 #tatement area. %ry
yourself with the following steps*
• $o to the #01 #tatement area
• nter #19% username, default6tablespace (5O< 2#562#5#7
• 'ress (V or clic/ the -ecute #tatement icon
)ou will see the statement results in the 5esults area.
:ow %o -port )our 9onnection Information to a (ile?
#01 Developer allows you to e-port your connection information into an F<1 file. :ere is how to do this*
• 5ight+clic/ on 9onnections
• #elect -port 9onnection...
• nter (ile .ame as* NtempNconnections.-ml
• 9lic/ OD
Open NtempNconnections.-ml, you should see an F<1 message li/e this*
T\+Bl (ersion H L1.0L\)
TM3&CT9P0 connections)
Tconnections)
Tconnection)
TU3>CGP&RT)1%21T.U3>CGP&RT)
T/&ST2'=0)12W.0.0.1T./&ST2'=0)
TConnectionType)U3>CT.ConnectionType)
T3eployPassCord)7alseT.3eployPassCord)
Tuser)/RT.user)
TConnection2aBe)LocalG40T.Connection2aBe)
TS13)40T.S13)
TUd,c3ri(er)oracle.Xd,c.dri(er.&racle3ri(er
T.Ud,c3ri(er)
TP;3)
TM5C3'T'50%1"3"1''N0"><1%0'"0%K><%%<#JNW10K"J...66)
T.P;3)
T&R'CL0GU3>CGT9P0)thinT.&R'CL0GU3>CGT9P0)
T.connection)
T.connections)
:ow %o 5un #01@'lus 9ommands in #01 Developer?
<ost of the time, you only run #01 statements in #01 Wor/sheet, the #01 statement area. ,ut you can also run some
#01@'lus commands in #01 Wor/sheet. %he e-ample below shows you how to run the D95I, command in #01
Developer*
• $o to #01 Wor/sheet + #01 statement area
• nter D#95I, 2#562#5#
• 'ress (V to run the command
)ou will see the following output*
2aBe 2ull Type
------------------------------ -------- ------------------
:S0R2'=0 2&T 2:LL ?'RC/'R2N0!
:S0RG13 2&T 2:LL 2:=>0R
53
'CC&:2TGST'T:S 2&T 2:LL ?'RC/'R2N2!
L&CAG3'T0 3'T0
04P1R9G3'T0 3'T0
30<':LTGT'>L0SP'C0 2&T 2:LL ?'RC/'R2N0!
T0=P&R'R9GT'>L0SP'C0 2&T 2:LL ?'RC/'R2N0!
CR0'T03 2&T 2:LL 3'T0
121T1'LGRSRCGC&2S:=0RG@R&:P ?'RC/'R2N0!
04T0R2'LG2'=0 ?'RC/'R2K000!
%he D#95I, command returns you the detailed information about the specified table.
:ow %o Wor/ with Data Ob3ects Interactively?
)ou can wor/ with data ob3ects through #01 statements in statement area. If you want to wor/ with data ob3ects
interactively, you should use the ob3ect browser. %he following tutorial steps help you to browse data ob3ects*
• 9lic/ the 9onnetions tab.
• Open the 9onnections icon.
• Open a connection, li/e 1ocal6F. )ou see a list of data ob3ect types.
• Open the 9onnections icon.
• Open %ables. )ou see list of e-isting tables.
• Open a table, li/e <'1O)#. )ou see a list of columns.
• Double+clic/ a table, li/e <'1O)#. )ou see a list of tabs opened for this table.
Once a table opened, #01 Developer offers you many views on this table, li/e 9olumns, Data, Inde-es, 9onstraints,
#tatistics, %riggers, etc.
:ow %o $et a 95A% #tatement for an -isting %able?
)ou have an e-isting table and want to get a 95A% statement for that table, you can use the #01 tab in the ob3ect view.
%he following tutorial steps show you how to use #01 Developer to generate a 95A% statement on an e-isting table*
• Double+clic/ the table name =O,# in the ob3ect tree area.
• 9lic/ the #01 tab in the ob3ect view area.
In a moment, you will get the following 95A% statements*
R0= /R U&>S
CR0'T0 T'>L0 Q/RQ.QU&>SQ
Q13Q ?'RC/'R210 >9T0!$
QT1TL0Q ?'RC/'R2N% >9T0! C&2STR'12T QT1TL0G22Q
2&T 2:LL 02'>L0$
Q=12GS'L'R9Q 2:=>0RJ$0!$
Q='4GS'L'R9Q 2:=>0RJ$0!$
C&2STR'12T Q13GPAQ PR1='R9 A09 Q13Q! 02'>L0
! I

R0= /R 13GPA
CR0'T0 :21Q:0 12304 Q/RQ.Q13GPAQ &2 Q/RQ.QU&>SQ Q13Q!
I
:ow %o 9reate a %able Interactively?
If you don&t want to use #01 statements to create a new table, you use #01 Developer to create one interactively. (ollow
the steps below to create a new table called* %I'
• 5ight+clic/ on the %ables in the ob3ect tree area.
• #elect 9reate %A,1. 9reate %able window shows up.
• nter .ame as* %I'
• nter 9olumn .ame as* ID, for column A.
• #elect %ype as* I.%$5, for column A.
• 9lic/ 'rimary as* 9hec/ed, for column A.
• 9lic/ Add 9olumn to add column ;.
• nter 9olumn .ame as* #2,=9%, for column ;.
• #elect %ype as* 8A59:A5;, for column ;.
• #elect #ize as* RM, for column ;.
• 9lic/ OD.
:ow %o nter a .ew 5ow into a %able Interactively?
If you don&t li/e to use the I.#5% statement to enter a new row into a table, you can use the ob3ect view to enter it
interactively. (ollow the steps below to enter new row into table %I'*
• Double+clic/ on the table name %I'.
• 9lic/ the Data tab in the ob3ect view.
• 9lic/ the Insert 5ow icon, the T sign.
• nter ID as* AMA.
• nter #2,=9% as* ,ac/up UA.
• 9lic/ the 9ommit 9hanges icon.
)ou /now the new row is serted, because the log area shows you*
12S0RT 12T& Q/RQ.QT1PSQ 13$ S:>U0CT! ?'L:0S L101L$ L>acRup Z1L!
CoBBit Success7ul
54
What Is the 5eports 8iew in Oracle #01 Developer?
%he 5eports view lets you browse database information that organized by the server as special views. Information can be
browsed include*
• Database parameters
• #torage information
• #ession information
• 9ursors
• Data ob3ects
• 2ser accounts
• #ecurity informaiton
:ow %o $et a 1ist of All %ables in the Database?
If you don&t li/e to use a #19% statement to get a list of all tables in the current database, you can use the 5eports view to
do this as shown in the following tutorial e-ample*
• 9lic/ menu 8iew.
• #elects 5eports from the menu.
• Open 5eports.
• Open Data Dictionary 5eports.
• Open %able.
• Double+clic/ 2ser %ables. )ou see the nter ,ind 8alues window.
• 9lic/ Apply.
)ou should get a list all current tables in the database.
:ow %o $et a 1ist of All 2ser Accounts in the Database?
If you don&t li/e to use a #19% statement to get a list of all user accounts in the current database, you can use the
5eports view to do this as shown in the following tutorial e-ample. )ou need to connect to the server as #)#%< to do this*
• 9lic/ menu 8iew.
• #elects 5eports from the menu.
• Open 5eports.
• Open Data Dictionary 5eports.
• Open Database Administration.
• Open 2sers.
• Double+clic/ All 2sers.
)ou should get a list all current user accounts in the database.
:ow %o $et a 1ist of All ,ac/ground #essions in the Database?
If you don&t li/e to use a #19% statement to get a list of all bac/ground sessions in the current database, you can use the
5eports view to do this as shown in the following tutorial e-ample. )ou need to connect to the server as #)#%< to do this*
• 9lic/ menu 8iew.
• #elects 5eports from the menu.
• Open 5eports.
• Open Data Dictionary 5eports.
• Open Database Administration.
• Open #essions.
• Double+clic/ ,ac/ground #essions.
)ou should get a list all current bac/ground sessions in the database.
:ow %o 9reate )our Own 5eports in #01 Developer?
Oracle #01 Developer also lets you create your own reports. #ee the following steps on how to do this*
• 9lic/ menu 8iew.
• #elects 5eports from the menu.
• Open 5eports.
• 5ight+clic/ on 2ser Defined 5eports.
• #elect Add 5eport.
• nter .ame as* <y %eam.
• nter Description as* 1ist of employees in my team.
• nter #01 as* #19% @ (5O< :5.<'1O)# W:5 <A.A$56ID>AMM
• 9lic/ Apply.
)our report C<y %eamC is ready.
:ow <any (ile (ormats Are #upported to -port Data?
Oracle #01 Developer can allow to e-port table data into files in the following formats*
• %F% + %ab delimited fields file format.
• 9#8 + 9omma #eparated 8alues !9#8" file format.
• 1OAD5 + (ile format used by #01@1oader.
• F<1 + F<1 file format.
55
• I.#5% + I.#5% statements format.
:ow %o -port Data to a 9#8 (ile?
If you want to e-port data from a table to a file in 9#8 format, you can use the following steps*
• 5ight+clic/ the table name, <'1O)#, in the ob3ect tree view.
• #elect -port.
• #elect 9#8. %he -port Data window shows up.
• 9lic/ (ormat tab.
• #elect (ormat as* 9#8.
• nter (ile as* NtempN<y%eam.csv.
• 9lic/ 9olumns tab.
• 9hec/ columns* (I5#%6.A<, 1A#%6.A<, <A.A$56ID
• 9lic/ Where tab.
• nter Where clause as* <A.A$56ID>AMM.
• 9lic/ Apply.
)our 9#8 file, NtempN<y%eam.csv, is ready. Open it, you will see*
Q<1RSTG2'=0Q$QL'STG2'=0Q$Q='2'@0RG13Q
Q@eraldQ$QCaB,raultQ$Q100Q
QLe+Q$Q3e /aanQ$Q100Q
Q'l,ertoQ$Q0rraPuriPQ$Q100Q
Q'daBQ$Q<rippQ$Q100Q
Q=ichaelQ$Q/artsteinQ$Q100Q
QPayaBQ$QAau7lingQ$Q100Q
Q2eenaQ$QAochharQ$Q100Q
...
9#8 files can be open by <icrosoft -cel.
:ow %o -port Data to an F<1 (ile?
If you want to e-port data from a table to a file in F<1 format, you can use the following steps*
• 5ight+clic/ the table name, <'1O)#, in the ob3ect tree view.
• #elect -port.
• #elect F<1. %he -port Data window shows up.
• 9lic/ (ormat tab.
• #elect (ormat as* F<1.
• nter (ile as* NtempN<y%eam.-ml.
• 9lic/ 9olumns tab.
• 9hec/ columns* (I5#%6.A<, 1A#%6.A<, <A.A$56ID
• 9lic/ Where tab.
• nter Where clause as* <A.A$56ID>AMM.
• 9lic/ Apply.
)our F<1 file, NtempN<y%eam.-ml, is ready. Open it, you will see*
T\+Bl (ersionHL1.0L encodingHLCp12%2L \)
Tresults)
TroC)
T<1RSTG2'=0)TM5C3'T'5@erald66)T.<1RSTG2'=0)
TL'STG2'=0)TM5C3'T'5CaB,rault66)T.L'STG2'=0)
T='2'@0RG13)TM5C3'T'510066)T.='2'@0RG13)
T.roC)
TroC)
T<1RSTG2'=0)TM5C3'T'5Le+66)T.<1RSTG2'=0)
TL'STG2'=0)TM5C3'T'53e /aan66)T.L'STG2'=0)
T='2'@0RG13)TM5C3'T'510066)T.='2'@0RG13)
T.roC)
T.results)
:ow %o 9reate a 'rocedure Interactively?
If you want to create a stored procedure interactively, you can use the following steps*
• Open you connection name, li/e 1ocal6F.
• 5ight+clic/ 'rocedures.
• #elect 9reate '5O9D25. %he 9reate 'rocedure window shows up.
• nter .ame as* :ello
• 9lic/ OD. %he script area opens up with an empty procedure.
• 'lace the empty template with the following procedure.
CR0'T0 &R R0PL'C0 PR&C03:R0 /R./0LL& 'S
>0@12
3>=SG&:TP:T.P:TGL120L/ello CorldML!I
3>=SG&:TP:T.P:TGL120L;elcoBe to PL.SQLML!I
023I
9lic/ the 9ompile icon. %he procedure is created.
:ow %o 5un a #tored 'rocedure Interactively?
56
If have an e-isting stored procedure and you want to run it interactively, the tutorial steps listed below will help you out*
• Open you connection name, li/e 1ocal6F.
• Open 'rocedures.
• 5ight+clic/ the procedure name* :11O.
• #elect 5un. %he 5un '1?#01 window shows up. #01 Developer creates a simple anonymous code bloc/ for you to run
the stored procedure.
• 9lic/ OD. #01 Developer runs the anonymous code bloc/, which calls your store procedure.
)ou should see the following in the 5unning 1og area*
Connecting to the data,ase LocalG40.
/ello CorldM
;elcoBe to PL.SQLM
Process e+ited.
3isconnecting 7roB the data,ase LocalG40.
:ow %o 5un #tored 'rocedures in Debug <ode?
If have an e-isting stored procedure and you want to debug it interactively, you can use the debug feature provided in #01
Developer. %he following e-ercise shows you how to start the debug mode*
• Open you connection name, li/e 1ocal6F.
• Open 'rocedures.
• 5ight+clic/ the procedure name* :11O.
• #elect Debug. %he Debug '1?#01 window shows up. #01 Developer creates a simple anonymous code bloc/ for you
to run the stored procedure.
• 9lic/ OD.
)ou may get the following errors. 5ead the ne-t tutorial on how to fi- the errors.
Connecting to the data,ase LocalG40.
0+ecuting PL.SQL: 'LT0R S0SS1&2 S0T PLSQLG30>:@HTR:0
0+ecuting PL.SQL: C'LL 3>=SG30>:@GU3;P.C&220CTGTCP...!
&R'-010N1: insu77icient pri(ileges
&R'-0J%12: at QS9S.3>=SG30>:@GU3;PQ$ line J#
&R'-0J%12: at line 1
This session re-uires 30>:@ C&220CT S0SS1&2 and 30>:@ '29
PR&C03:R0 user pri(ileges.
Process e+ited.
3isconnecting 7roB the data,ase LocalG40.
:ow %o Assign Debug 'rivileges to a 2ser?
In order to run #01 Developer in debug mode, the session must be connected with a user who has debug privileges. %he
following script shows you how to assign debug privileges*
• 9onnect to the default database, F, as #)#%<.
• 5un C$5A.% debug any procedure, debug connect session %O :57C.
• 5e+connect to the default database, F, as :5.
)ou new session should have enough privileges to debug stored procedures now.
:ow %o #et 2p ,rea/points in Debug <ode?
%o debug a stored procedure, you need to set brea/points at different statements in the code bloc/, so that e-ecution will be
stopped at the statement mar/ed with a brea/point. When e-ecution is stopped, you can then investigate different variables
to see their current values. %he tutorial below shows you how to set up brea/points*
• 5ight+clic/ the procedure name* :11O.
• #elect Open. %he source code of the stored procedure shows up in the code area.
• #et your mouse cursor to the C'2%61I.!&:ello worldX&"7C statement.
• 9lic/ Debug menu.
• #elect %oggle ,rea/point. )ou should see a red dot shows up in left margin of the statement where the mouse cursor
is. %his red dot represents a brea/point.
• #et another brea/point at the C'2%61I.!&Welcome to '1?#01X&"7C statement.
• 9lic/ the debug icon above the code area. %he debug icon loo/ li/e a bug. %he Debug '1?#01 dialog shows up.
• 9lic/ OD. %he debug session starts and stopped at the first brea/point. )ou should see an array in left margin
indicating where the e-ecution is stopped.
• 'ress (R !#tep Over". %he e-ecution continues for one statement.
• 'ress (V !5esume". %he e-ecution continues to the end of the procedure.
)ou should see the following in the Debugging 1og area*
Connecting to the data,ase LocalG40.
0+ecuting PL.SQL: 'LT0R S0SS1&2 S0T PLSQLG30>:@HTR:0
0+ecuting PL.SQL: C'LL 3>=SG30>:@GU3;P.C&220CTGTCP L12W.0.0.1L$ LNJ#%L !
3e,ugger accepted connection 7roB data,ase on port NJ#%.
Processing %" classes that ha(e already ,een prepared...
<inished processing prepared classes.
Source ,reaRpoint occurred at line N o7 /0LL&.pls.
Source ,reaRpoint occurred at line K o7 /0LL&.pls.
/ello CorldM
;elcoBe to PL.SQLM
Process e+ited.
3isconnecting 7roB the data,ase LocalG40.
57
3e,ugger disconnected 7roB data,ase.
What Do )ou %hin/ about Oracle #01 Developer?
%o conclude this introductory (A0 collection, you should thin/ about Oracle #01 Developer in comparison with other client
tools li/e #01@'lus and Oracle Web interface. #01 Developer is definitely better than the other tools, more functionality, and
much easier to use. %he script debugging mode is very useful.
What Is '1?#01 1anguage 9ase #ensitive?
'1?#01 language is not case sensitive*
• 5eserved words are not case sensitive. (or e-ample* 9A# and 9ase are identical.
• 8ariable names and other names are not case sensitive. (or e-ample* %O%A16#A1A5) and total6salary are identical.
,ut values in string literals are case sensitive. (or e-ample* &D,A& and &dba& are different.
:ow %o 'lace 9omments in '1?#01?
%here are two ways to place comments into '1?#01 codes*
• #01 #tatement #tyle* #tarts you comment any where in the line but prefi-ed with &++&. %he comment ends at the end of
the line.
• 9 1anguage #tyle* #tarts you comment any where in the line with &?@& and ends it with &@?& in the same line or some lines
below.
:ere is some e-ample of '1?#01 comments*
>0@12
-- This is a coBBent
.* To do:
2eed to Crite soBe codes here
*.
023I
What Are the %ypes '1?#01 9ode ,loc/s?
%here are H types of '1?#01 code bloc/s*
• Anonymous ,loc/ + A bloc/ of codes with no name. It may contain a declaration part, an e-ecution part, and e-ception
handlers.
• #tored 'rogram 2nit + A bloc/ of codes with a name. It is similar to an anonymous bloc/. ,ut it can ta/e parameters
and return values.
• %rigger + A bloc/ of code that can be defined to fire based an specific event.
What Is an Anonymous ,loc/?
An anonymous bloc/ is a '1?#01 code bloc/ with no name. It consists of three parts*
• Declaration 'art + Defining local variables and local procedures. Declaration part is optional.
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
:ere how a complete anonymous bloc/ should loo/ li/e*
30CL'R0
-- 3eclaration stateBents
>0@12
-- 0+ecuta,le stateBents
04C0PT1&2
-- 0rror handling stateBents
023I
What Is a .amed 'rogram 2nit?
A named program unit is a '1?#01 code bloc/ with an name. It consists of three parts*
• Declaration 'art + Defining the program unit name, calling parameters, local variables and local procedures. Declaration
part is re4uired.
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
%here are two types of named program units*
• 'rocedure + :as no return values.
• (unction + :as a return value.
What Is a 'rocedure?
A procedure is a named program unit. It consists of three parts*
• Declaration 'art + Defining the procedure name, calling parameters, local variables and local procedures. Declaration
part is re4uired.
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
:ere how a complete procedure should loo/ li/e*
PR&C03:R0 naBe paraBeterG1$ paraBeterG2! 'S
-- 3eclaration stateBents
>0@12
-- 0+ecuta,le stateBents
04C0PT1&2
-- 0rror handling stateBents
023I
What Is a (unction?
A function is a named program unit. It consists of three parts*
• Declaration 'art + Defining the function name, calling parameters, return value type, local variables and local
procedures. Declaration part is re4uired.
58
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
:ere how a complete procedure should loo/ li/e*
<:2CT1&2 naBe paraBeterG1$ paraBeterG2! R0T:R2 type 'S
-- 3eclaration stateBents
>0@12
-- 0+ecuta,le stateBents
R0T:R2 (alueI
04C0PT1&2
-- 0rror handling stateBents
023I
:ow %o Declare a 1ocal 8ariable?
A local variable can be defined in the declaration part with a declaration statement, which is a variable name followed a data
type identifier. ,elow are some e-amples of declaration statements*
PR&C03:R0 procG(arG1 'S
doBain ?'RC/'R2#0!I
price R0'LI
isG7orGsale C/'RI
>0@12
-- 0+ecuta,le stateBents
023I
:ow %o Initialize 8ariables with Default 8alues?
%here are two ways to assign default values to variables at the time of declaration*
• 2sing /ey word D(A21% + Appending CD(A21% valueC to the end of declaration statements.
• 2sing assignment operator + Appending C*> valueC to the end of declaration statements.
%he script below show you some e-amples of declaration statements with default values*
PR&C03:R0 procG(arG1 'S
doBain ?'RC/'R2#0! :H L7yicenter.coBLI
price R0'L 30<':LT """""".""I
isG7orGsale C/'R :H L2LI
>0@12
-- 0+ecuta,le stateBents
023I
:ow %o Assign 8alues to 8ariables?
)ou can use assignment statements to assign values to variables. An assignment statement contains an assignment
operator C*>C, which ta/es the value specified on the right to the variable on left. %he script below show you some e-amples
of assignment statements*
PR&C03:R0 procG(arG2 'S
isGdone >&&L0'2I
counter 2:=>0R :H 0I
Bessage ?'RC/'R2#0!I
>0@12
isGdone :H <'SL0I
counter :H counter Y 1I
Bessage :H L/ello CorldMLI
023I
What Are the Arithmetic Operations?
%here are I basic arithmetic operations on numeric values as shown in the following sample script*
PR&C03:R0 procGarithBetic 'S
addition 2:=>0RI
su,traction 2:=>0RI
Bultiplication 2:=>0RI
di(ision 2:=>0RI
>0@12
addition :H W Y #I
su,traction :H addition - WI
Bultiplication :H su,traction * %I
di(ision :H Bultiplication . #I
-- di(ision should contain % noC
023I
What Are the .umeric 9omparison Operations?
'1?#01 supports L basic numeric comparison operations as shown in the following sample script*
PR&C03:R0 procGcoBparison 'S
res >&&L0'2I
>0@12
res :H 1 H 2I
res :H 1 T 2I
res :H 1 ) 2I
res :H 1 TH 2I
res :H 1 )H 2I
res :H 1 T) 2I
-- Bore stateBents
023I
What Are the 1ogical Operations?
'1?#01 supports H logical operations as shown in the following sample script*
PR&C03:R0 procGcoBparison 'S
+ >&&L0'2 :H TR:0I
59
y >&&L0'2 :H <'LS0I
res >&&L0'2I
>0@12
res H + '23 yI
res H + &R yI
res H 2&T +I
-- Bore stateBents
023I
:ow <any 9ategories of Data %ypes?
'1?#01 data types are grouped into I categories*
• #calar Data %ypes* A scalar data type holds a single value.
• 9omposite Data %ypes* A composite data type has internal components, such as the elements of an array.
• 1O, Data %ypes* A 1O, data type holds a lob locator that specify the location of a large ob3ect.
• 5eference Data %ypes* A reference data type holds a pointer that points to another data ob3ect.
:ow <any #calar Data %ypes Are #upported in '1?#01?
'1?#01 supports many scalar data types divided into I groups*
• .umeric %ypes* ,I.A5)6DO2,1, ,I.A5)6(1OA%, ,I.A5)6I.%$5, D9, D9I<A1, DO2,1 '59I#IO.,
(1OA%, I.%, I.%$5, .A%25A1, .A%25A1., .2<,5, .2<5I9, '1#6I.%$5, 'O#I%I8, 'O#I%I8.,
5A1, #I$.%)', #<A11I.%.
• 9haracter %ypes* 9:A5, 9:A5A9%5, 1O.$, 1O.$ 5AW, .9:A5, .8A59:A5;, 5AW, 5OWID, #%5I.$,
25OWID, 8A59:A5, 8A59:A5;.
• ,oolean %ypes* ,OO1A..
• Date %ypes* DA%, %I<#%A<', %I<#%A<' WI%: %I<KO., %I<#%A<' WI%: 1O9A1 %I<KO.,
I.%58A1 )A5 %O <O.%:, I.%58A1 DA) %O #9O.D.
:ow %o 9onvert 9haracter %ypes to .umeric %ypes?
)ou can convert character types to numeric types in two ways*
• -plicitly by using %O6.2<,5!" function.
• Implicitly by putting character data in a numeric operation.
%he sample script below shows you how to convert character types to numeric types*
PR&C03:R0 procGcon(ertG1 'S
startGtiBe C/'R%!I
7inishGtiBe C/'R%!I
elapsedGtiBe 2:=>0R%!I
>0@12
startGtiBe :H L120%2LI
7inishGtiBe :H L1%N1KLI
elapsedGtiBe :H T&G2:=>0R7inishGtiBe!
- T&G2:=>0RstartGtiBe!I
elapsedGtiBe :H 7inishGtiBe - startGtiBeI -- saBe as a,o(e
023I
What Are the -ecution 9ontrol #tatements?
'1?#01 supports three groups of e-ecution control statements*
• I( #tatements + 9onditionally e-ecutes a bloc/ of statements.
• 9A# #tatements + #electively e-ecutes a bloc/ of statements.
• 1OO' #tatements + 5epeatedly e-ecutes a bloc/ of statements.
• $O%O #tatements + 2nconditional changes the e-ecution flow to a specified statement.
%he script below shows some e-ecution control statements*
30CL'R0
total 2:=>0RI
>0@12
total :H 0I
L&&P
total :H totalY1I
1< total )H 10 T/02
@&T& printI
023 1<I
023 L&&PI
T)
3>=SG&:TP:T.P:TGL120LTotal counts: L EE T&GC/'Rtotal!!I
023I
%his script should print this*
Total counts: 10
Y
:ow %o 2se CI(C #tatements on <ultiple 9onditions?
If you have multiple bloc/s of codes to be e-ecuted based on different conditions, you can use the CI( ... 1#I(C statement.
:ere is a sample script on I( statements*
30CL'R0
day ?'RC/'R2I
>0@12
day :H LS:23'9LI
1< day H LT/:RS3'9L T/02
3>=SG&:TP:T.P:TGL120LChecRing log 7iles.L!I
0LS1< day H LT:0S3'9L T/02
60
3>=SG&:TP:T.P:TGL120L/elping de(elopers.L!I
0LS1< day H L<R13'9L T/02
3>=SG&:TP:T.P:TGL120LRe,uild inde+es.L!I
0LS0
3>=SG&:TP:T.P:TGL120LReading soBe papers.L!I
023 1<I
023I
%his script should print this*
Reading soBe papers.
:ow %o 2se CW:I1C #tatements?
If you have a bloc/ of codes to be e-ecuted repeatedly based a condition, you can use the CW:I1 ... 1OO'C statement.
:ere is a sample script on W:I1 statements*
30CL'R0
total 2:=>0RI
>0@12
total :H 0I
;/1L0 total T 10 L&&P
total :H totalY1I
023 L&&PI
3>=SG&:TP:T.P:TGL120LTotal counts: L EE T&GC/'Rtotal!!I
023I
%his script should print this*
Total counts: 10
:ow %o 2se C(O5C #tatements?
If you have a bloc/ of codes to be e-ecuted repeatedly over a range of values, you can use the C(O5 ... 1OO'C statement.
:ere is a sample script on (O5 statements*
30CL'R0
total 2:=>0R :H 0I
>0@12
<&R i 12 1..10 L&&P
total :H total Y iI
023 L&&PI
3>=SG&:TP:T.P:TGL120LTotal: L EE T&GC/'Rtotal!!I
023I
.ote that temporary variable CiC used in the (O5 loop needs no declaration. %his script should print this*
What Is .211 in '1?#01?
.211 is a reserved /ey word and it stands for two things in '1?#01*
• .211 is an e-ecutable statement, and means doing nothing.
• .211 is a data balue, and means no value.
%he following sample script shows you e-amples of using .211 /eyword*
30CL'R0
ne+tGtasR C/'R#0!I
>0@12
ne+tGtasR :H 2:LLI -- 2:LL (alue
1< ne+tGtasR 1S 2&T 2:LL T/02
3>=SG&:TP:T.P:TGL120L1 aB ,usy.L!I
0LS0
3>=SG&:TP:T.P:TGL120L1 aB 7ree.L!I
023 1<I

1< ne+tGtasR 1S 2:LL T/02
2:LLI -- 2:LL stateBent
0LS0
3>=SG&:TP:T.P:TGL120L... CorRing on L EE ne+tGtasR!I
023 1<I
023I
%his script should print this*
1 aB 7ree.
:ow %o %est .211 8alues?
%here ate two special comparison operators you can use on .211 values*
• Cvariable I# .211C + 5eturns %52 if the variable value is .211.
• Cvariable I# .O% .211C + 5eturn %52 if the variable value is not .211.
%he following sample script shows you e-amples of comparing .211 values*
30CL'R0
ne+tGtasR C/'R#0!I
>0@12
ne+tGtasR :H 2:LLI
1< ne+tGtasR 1S 2&T 2:LL T/02
3>=SG&:TP:T.P:TGL120L1 aB ,usy.L!I
0LS0
3>=SG&:TP:T.P:TGL120L1 aB 7ree.L!I
023 1<I

1< ne+tGtasR 1S 2:LL T/02
2:LLI
61
0LS0
3>=SG&:TP:T.P:TGL120L... CorRing on L EE ne+tGtasR!I
023 1<I
023I
.ote that Cvariable > .211C is not a valid operation. %his script should print this*
1 aB 7ree.
What Is a 'rocedure?
A procedure is a named program unit. It consists of three parts*
• Declaration 'art + Defining the procedure name, calling parameters, local variables and local procedures. Declaration
part is re4uired.
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
:ere how a complete procedure should loo/ li/e*
PR&C03:R0 naBe paraBeterG1$ paraBeterG2! 'S
-- 3eclaration stateBents
>0@12
-- 0+ecuta,le stateBents
04C0PT1&2
-- 0rror handling stateBents
023I
What Is a (unction?
A function is a named program unit. It consists of three parts*
• Declaration 'art + Defining the function name, calling parameters, return value type, local variables and local
procedures. Declaration part is re4uired.
• -ecution 'art + Defining e-ecution logic with e-ecutable statements. -ecution part is re4uired.
• -ception 'art + Defining error handling logics. -ception part is optional.
:ere how a complete procedure should loo/ li/e*
<:2CT1&2 naBe paraBeterG1$ paraBeterG2! R0T:R2 type 'S
-- 3eclaration stateBents
>0@12
-- 0+ecuta,le stateBents
R0T:R2 (alueI
04C0PT1&2
-- 0rror handling stateBents
023I
:ow %o Define an Anonymous 'rocedure without 8ariables?
Anonymous procedure is a procedure without any name. If you don&t have any variables to declare, you can define an
anonymous procedure by using the ,$I. /eyword directly in #01@'lus as shown in the following tutorial script*
SQL) set ser(eroutput onI
SQL) ,egin
2 d,BsGoutput.putGlineL;elcoBe to <91CenterML!I
N endI
K .
;elcoBe to <91CenterM
PL.SQL procedure success7ully coBpleted.
Cset serveroutput on7C allows dbms6output.put6line!" to wor/.
C?C runs the anonymous bloc/, which print the CWelcome to ()I9enterXC message.
:ow %o Define an Anonymous 'rocedure with 8ariables?
Anonymous procedure is a procedure without any name. If you have some variables to declare, you can define an
anonymous procedure by using the D91A5 /eyword in #01@'lus as shown in the following tutorial script*
SQL) set ser(eroutput onI
SQL) declare
2 site char#0! :H L<91CenterLI
N ,egin
K d,BsGoutput.putGlineL;elcoBe to L EE site!I
% endI
J .
;elcoBe to <91Center
PL.SQL procedure success7ully coBpleted.
SQL) .
;elcoBe to <91Center
PL.SQL procedure success7ully coBpleted.
C?C runs the anonymous bloc/, which print the CWelcome to ()I9enterC message.
:ow %o 9reate a #tored 'rocedure?
A stored procedure is a procedure with a specified name and stored into the current database. If you want to create a stored
procedure, you can use the 95A% '5O9D25 statement. %he e-ample script below creates a stored procedure*
SQL) CR0'T0 PR&C03:R0 @reeting 'S
2 >0@12
N 3>=SG&:TP:T.P:TGL120L;elcoBe to <91CenterML!I
K 023I
% .
62
Procedure created.
:ow %o -ecute a #tored 'rocedure?
If you want to e-ecute a stored procedure, you can use the F92% statement. %he e-ample script below shows how to
e-ecutes a stored procedure*
SQL) set ser(eroutput onI
SQL) CR0'T0 PR&C03:R0 @reeting 'S
2 >0@12
N 3>=SG&:TP:T.P:TGL120L;elcoBe to <91CenterML!I
K 023I
% .
Procedure created.
SQL) 040C:T0 @reetingI
;elcoBe to <91CenterM
:ow %o Drop a #tored 'rocedure?
If there is an e-isting stored procedure and you don&t want it any more, you can remove it from the database by using the
D5O' '5O9D25 statement as shown in the following script e-ample*
SQL) CR0'T0 PR&C03:R0 @reeting 'S
2 >0@12
N 3>=SG&:TP:T.P:TGL120L;elcoBe to <91CenterML!I
K 023I
% .
Procedure created.
SQL) 3R&P PR&C03:R0 @reetingI
Procedure dropped.
:ow %o 'ass 'arameters to 'rocedures?
#tore procedures or functions can ta/e parameters. )ou need to define parameters while defining the procedure, and
providing values to parameters while calling the procedure. %he script below shows you how to do this*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 3>'GT'SA day ?'RC/'R2! 'S
2 >0@12
N 1< day H L=&23'9L T/02
K 3>=SG&:TP:T.P:TGL120LChecRing log 7iles.L!I
% 0LS1< day H L<R13'9L T/02
J 3>=SG&:TP:T.P:TGL120LRe,uild inde+es.L!I
W 0LS0
# 3>=SG&:TP:T.P:TGL120LReading soBe papers.L!I
" 023 1<I
10 023I
11 .
SQL) 040C:T0 3>'GT'SAL=&23'9L!I
ChecRing log 7iles.
SQL) 040C:T0 3>'GT'SALS:23'9L!I
Reading soBe papers.
As you can see, procedures with parameters can ma/e procedures more fle-ible.
:ow %o 9reate a #tored (unction?
A stored function is a function with a specified name and stored into the current database. If you want to create a stored
function, you can use the 95A% (2.9%IO. statement. %he e-ample script below creates a stored procedure*
SQL) CR0'T0 &R R0PL'C0 <:2CT1&2 @0TGS1T0
2 R0T:R2 ?'RC/'R2 'S
N >0@12
K R0T:R2 L<91Centere.coBLI
% 023I
J .
<unction created.
:ow %o 9all a #tored (unction?
A stored function can be called as part of e-pression in any '1?#01 statement. One simplest way to call a stored function is
to a dummy #19% statement as shown in the following tutorial script using #01@'lus*
SQL) CR0'T0 &R R0PL'C0 <:2CT1&2 @0TGS1T0
2 R0T:R2 ?'RC/'R2 'S
N >0@12
K R0T:R2 L<91Centere.coBLI
% 023I
J .
<unction created.
SQL) S0L0CT getGsite! <R&= dualI
@0TGS1T0!
---------------------------------
<91Centere.coB
D2A1 is not a real table or view. It is commonly used to with #19% statement to evaluate e-pressions.
:ow %o Drop a #tored (unction?
If there is an e-isting stored function and you don&t want it any more, you can remove it from the database by using the
D5O' (2.9%IO. statement as shown in the following script e-ample*
SQL) CR0'T0 &R R0PL'C0 <:2CT1&2 @0TGS1T0
63
2 R0T:R2 ?'RC/'R2 'S
N >0@12
K R0T:R2 L<91Centere.coBLI
% 023I
J .
<unction created.
SQL) 3R&P <:2CT1&2 @0TGS1T0I
<unction dropped.
:ow %o 9all a #tored (unction with 'arameters?
)ou can define a function that ta/es parameters, provide values to those parameters when calling the function. :ere is a
good e-ample of a function with a parameter*
SQL) CR0'T0 &R R0PL'C0 <:2CT1&2 @0TG3&:>L04 2:=>0R!
2 R0T:R2 2:=>0R 'S
N >0@12
K R0T:R2 4 * 2I
% 023I
J .
<unction created.
SQL) S0L0CT @0TG3&:>L0NJ! <R&= 3:'LI
@0TG3&:>L0NJ!
--------------
W2
:ow %o Define a #ub 'rocedure?
A sub procedure is a named procedure defined and used inside another procedure or function. )ou need to define a sub
procedure in the declaration part of the enclosing procedure or function. #ub procedure definition starts with the
'5O9D25 /ey word. :ere is a sample script showing you how to define and use a sub procedure*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 /R.3>'G;00A 'S
2 PR&C03:R0 3>'GT'SA day ?'RC/'R2! 'S
N >0@12
K 1< day H L=&23'9L T/02
% 3>=SG&:TP:T.P:TGL120LChecRing log 7iles.L!I
J 0LS1< day H L<R13'9L T/02
W 3>=SG&:TP:T.P:TGL120LRe,uild inde+es.L!I
# 0LS0
" 3>=SG&:TP:T.P:TGL120LReading soBe papers.L!I
10 023 1<I
11 023I
12 >0@12
1N 3>'GT'SAL=&23'9L!I
1K 3>'GT'SALT:0S3'9L!I
1% 023I
1J .

SQL) 040C:T0 3>'G;00AI
ChecRing log 7iles.
Reading soBe papers.
:ow %o 9all a #ub 'rocedure?
%o call a sub procedure, 3ust use the sub procedure name as a statement. :ere is another e-ample of calling a sub
procedure*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ;0LC&=0 'S
2 PR&C03:R0 ;0LC&=0GPR12TS C/'R! 'S
N >0@12
K 3>=SG&:TP:T.P:TGL120L;elcoBe to L EE S!I
% 023I
J >0@12
W ;0LC&=0GPR12TL<91CenterL!I
# 023I
" .
SQL) 040C:T0 ;0LC&=0I
;elcoBe to <91Center
:ow %o Define a #ub (unction?
A sub function is a function defined and used inside another procedure or function. )ou need to define a sub function in the
declaration part of the enclosing procedure or function. #ub function definition starts with the (2.9%IO. /ey word. :ere is a
sample script showing you how to define and use a sub function*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 S:=GT0ST 'S
2 <:2CT1&2 =9GS:=4 2:=>0R$ 9 2:=>0R!
N R0T:R2 2:=>0R 'S
K >0@12
% R0T:R2 4 Y 9I
J 023I
W >0@12
# 3>=SG&:TP:T.P:TGL120LN Y % H L EE
" T&GC/'R=9GS:=N$%!!!I
10 3>=SG&:TP:T.P:TGL120L% Y N H L EE
11 T&GC/'R=9GS:=%$N!!!I
12 023I
64
1N .

SQL) 040C:T0 S:=GT0STI
N Y % H #
% Y N H #
9an #ub 'rocedure?(unction ,e 9alled 5ecursively?
'1?#01 allows sub procedures or functions to be called recursively. %he tutorial e-ample below shows you how to calculate
factorial values with a recursive sub function*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 <'CT&R1'LGT0ST 'S
2 <:2CT1&2 <'CT&R1'L2 2:=>0R!
N R0T:R2 2:=>0R 'S
K >0@12
% 1< 2 TH 1 T/02
J R0T:R2 1I
W 0LS0
# R0T:R2 2*<'CT&R1'L2-1!I
" 023 1<I
10 023I
11 >0@12
12 3>=SG&:TP:T.P:TGL120LNM H L EE
1N T&GC/'R<'CT&R1'LN!!!I
1K 3>=SG&:TP:T.P:TGL120L10M H L EE
1% T&GC/'R<'CT&R1'L10!!!I
1J 3>=SG&:TP:T.P:TGL120LJKM H L EE
1W T&GC/'R<'CT&R1'LJK!!!I
1# 023I
1" .

SQL) 040C:T0 <'CT&R1'LGT0STI
NM H J
10M H NJ2##00
JKM H 12J##J"N21#%##K1JK10NKNNN#"NN%1J1K#0#02000000000000...
%here must be something wrong with the (A9%O5IA1!" definition that causes those many e-tra &M&s in the &LIX& result.
What :appens If 5ecursive 9alls $et Out of 9ontrol?
What happens if your code has bug on recursive procedure calls, which causes an infinite number nested procedure calls?
%he answer is so good. Oracle server seems to offer no protection calling stac/ limit. %he script below shows you a badly
coded recursive procedure. If you run it on an Oracle AMg F server on Windows, your server will out of control and /eep
using virtual memory to satisfy the growing calling stac/. )ou have to reboot your server to control bac/.
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ST'CAGT0ST 'S
2 --;arning: do not run this procedure on your ser(er
N PR&C03:R0 ST'CA 'S
K >0@12
% ST'CAI
J 023I
W >0@12
# ST'CAI
" 023I
10 .
SQL) 040C:T0 ST'CAGT0STI
your ser(er Reep running Cith 100V CP: and BeBory usage!
What Is the Order of Defining 1ocal 8ariables and #ub 'rocedures?(unctions?
In the declaration part, you must define all local variables before defining any sub procedures or sub functions. #ee the
following sample script*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ;0LC&=0 'S
2 S1T0 C/'R#0! :H L<91CenterLI
N PR&C03:R0 ;0LC&=0GPR12TS C/'R! 'S
K >0@12
% 3>=SG&:TP:T.P:TGL120L;elcoBe to L EE S!I
J 023I
W >0@12
# ;0LC&=0GPR12TS1T0!I
" 023I
10 .
SQL) 040C:T0 ;0LC&=0I
;elcoBe to <91Center
.otice that variable #I% should be declared before procedure W19O<6'5I.%
What Is the Difference between (ormal 'arameters and Actual 'arameters?
(ormal parameter and actual parameter are two different terms related parameters used in the procedures and functions*
• A formal parameter is a term used to refer to a parameter defined in the procedure or function declaration statement.
• An actual parameter is a term used to refer to a parameter provided by the calling statement to a procedure or a
function.
What Are the 'arameter <odes #upported by '1?#01?
'1?#01 supports H parameter modes on procedure?function parameters*
• I.* %his is the default mode. I. parameters allow the calling code to pass values into the procedure or function.
• O2%* O2% parameters allow the procedure or function to pass values bac/ to the calling code.
65
• I. O2%* I. O2% parameters allow the calling code to pass values into and receive values from procedure or function.
:ow %o 2se CI.C 'arameter 'roperly?
:ere are the rules about I. parameters*
• A formal I. parameter acts li/e constant. It can not be assigned with new values.
• An actual I. parameter can ta/e a value or a variable.
• An actual I. parameter is passed by reference to the specified value or the value of the specified variable.
• An actual I. parameter will not receive any value from the formal parameter.
:ere is good e-ample of a procedure with an I. parameter*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ;0LC&=0 'S
2 S1T0 C/'R#0! :H L<91Center.coBLI
N PR&C03:R0 ;0LC&=0GPR12TS 12 C/'R! 'S
K >0@12
% 3>=SG&:TP:T.P:TGL120L;elcoBe to L EE S!I
J -- S :H L@oogle.coBLI -- 2ot alloCed
W 023I
# >0@12
" ;0LC&=0GPR12TL=ySpace.coBL!I
10 ;0LC&=0GPR12TS1T0!I
11 023I
12 .
SQL) 040C:T0 ;0LC&=0I
;elcoBe to =ySpace.coB
;elcoBe to <91Center.coB
:ow %o 2se CO2%C 'arameter 'roperly?
:ere are the rules about O2% parameters*
• A formal O2% parameter acts li/e an un+initialized variable. It must be assigned with new values before the end of the
procedure or function.
• An actual O2% parameter must be a variable.
• An actual O2% parameter will not pass any value to the formal parameter.
• An actual O2% parameter will receive a copy of the value from the formal parameter at the end of the procedure or
function.
:ere is good e-ample of a procedure with an O2% parameter*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ;0LC&=0 'S
2 S1T0 C/'RK0! :H L<91Center.coBLI
N =0SS'@0 C/'R#0!I
K PR&C03:R0 ;0LC&=0GPR12TS 12 C/'R$ = &:T C/'R! 'S
% >0@12
J = :H L;elcoBe to L EE SI
W 023I
# >0@12
" ;0LC&=0GPR12TL=ySpace.coBL$ =0SS'@0!I
10 3>=SG&:TP:T.P:TGL120=0SS'@0!I
11 ;0LC&=0GPR12TS1T0$ =0SS'@0!I
12 3>=SG&:TP:T.P:TGL120=0SS'@0!I
1N 023I
1K .
SQL) 040C:T0 ;0LC&=0I
;elcoBe to =ySpace.coB
;elcoBe to <91Center.coB
:ow %o 2se CI. O2%C 'arameter 'roperly?
:ere are the rules about I. O2% parameters*
• A formal I. O2% parameter acts li/e an initialized variable.
• An actual I. O2% parameter must be a variable.
• An actual I. O2% parameter passes a copy of its value to the formal parameter when entering the procedure or
function.
• An actual I. O2% parameter will receive a copy of the value from the formal parameter at the end of the procedure or
function.
:ere is good e-ample of a procedure with I. O2% parameters*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 S;'PGT0ST 'S
2 ' 2:=>0R :H NI
N > 2:=>0R :H #I
K PR&C03:R0 =9GS;'P4 12 &:T 2:=>0R$9 12 &:T 2:=>0R! 'S
% T 2:=>0RI
J >0@12
W T :H 4I
# 4 :H 9I
" 9 :H TI
10 023 =9GS;'PI
11 >0@12
12 =9GS;'P'$>!I
1N 3>=SG&:TP:T.P:TGL120L' H L EE T&GC/'R'!!I
1K 3>=SG&:TP:T.P:TGL120L> H L EE T&GC/'R>!!I
66
1% 023I
1J .
SQL) 040C:T0 S;'PGT0STI
' H #
> H N
:ow %o Define Default 8alues for (ormal 'arameters?
If you have an I. parameter, you can ma/e it as an optional parameter for the calling statement by defining the formal
parameter with the D(A21% clause. %his gives you the freedom of not providing the actual parameter when calling this
procedure or function. #ee the following tutorial script shows you an e-ample procedure with an optional parameter*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 ;0LC&=0 'S
2 PR&C03:R0 @R00T12@S 12 C/'R 30<':LT L<91CenterL! 'S
N >0@12
K 3>=SG&:TP:T.P:TGL120L;elcoBe to L EE S!I
% 023I
J >0@12
W @R00T12@L=ySpace.coBL!I
# @R00T12@I
" 023I
10 .
Procedure created.
SQL) 040C:T0 ;0LC&=0I
;elcoBe to =ySpace.coB
;elcoBe to <91Center
What Are .amed 'arameters?
.amed parameters are actual parameters specified not by position but by providing formal parameter names when calling
the procedure or function. %he main advantage of named parameters is that the caller don&t have to remember the position
of each parameter. ,ut the caller have to remember the formal parameter names. %he script below illustrates how to use
named parameters*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 S;'PGT0ST 'S
2 ' 2:=>0R :H NI
N > 2:=>0R :H #I
K PR&C03:R0 =9GS;'P4 12 &:T 2:=>0R$9 12 &:T 2:=>0R! 'S
% T 2:=>0RI
J >0@12
W T :H 4I
# 4 :H 9I
" 9 :H TI
10 023 =9GS;'PI
11 >0@12
12 =9GS;'P9H)>$ 4H)'!I -- saBe as 4H)'$ 9H>!$ &R '$>!
1N 3>=SG&:TP:T.P:TGL120L' H L EE T&GC/'R'!!I
1K 3>=SG&:TP:T.P:TGL120L> H L EE T&GC/'R>!!I
1% 023I
1J .
SQL) 040C:T0 S;'PGT0STI
' H #
> H N
What Is the #cope of a 1ocal 8ariable?
%he scope of a variable can be described with these rules*
• A variable is valid within the procedure or function where it is defined.
• A variable is also valid inside a sub procedure or function defined.
• If a variable name is collided with another variable in a sub procedure or function, this variable becomes not visible in
that sub procedure or function.
:ere is a sample script to show you those rules*
%he script below illustrates how to use named parameters*
SQL) CR0'T0 &R R0PL'C0 PR&C03:R0 P'R02T 'S
2 4 C/'R10! :H L<91LI
N 9 2:=>0R :H """""".00I
K PR&C03:R0 C/1L3 'S
% 9 C/'R10! :H LC02T0RLI
J O 2:=>0R :H -1I
W >0@12
# 3>=SG&:TP:T.P:TGL120L4 H L EE 4!I -- 4 7roB P'R02T
" 3>=SG&:TP:T.P:TGL120L9 H L EE 9!I -- 9 7roB C/1L3
10 3>=SG&:TP:T.P:TGL120LO H L EE T&GC/'RO!!I
11 023I
12 >0@12
1N 3>=SG&:TP:T.P:TGL120L4 H L EE 4!I -- 4 7roB P'R02T
1K 3>=SG&:TP:T.P:TGL120L9 H L EE T&GC/'R9!!I
1% -- 3>=SG&:TP:T.P:TGL120LO H L EE T&GC/'RO!!I
1J C/1L3I
1W 023I
1# .
SQL) 040C:T0 P'R02TI
67
4 H <91
9 H """"""
4 H <91
9 H C02T0R
O H -1
9an D<1 #tatements ,e 2sed in '1?#01?
)es, you can run almost any D<1 statements in '1?#01 directly. %o manipulate Oracle database data you can include
I.#5%, 2'DA%, and D1% statements, directly in '1?#01 programs, without any special notation, as shown in the
following sample code*
Connect to 40 Cith SQL*Plus!
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
S0L0CT C&:2T*! <R&= studentI
C&:2T*!
----------
0
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
>0@12
12S0RT 12T& student ?'L:0S2"$ L>o,L$ L/enryL!I
12S0RT 12T& student ?'L:0SN0$ LUoeL$ L>ushL!I
:P3'T0 student S0T 7irstGnaBe H L<yiL ;/0R0 id H N0I
30L0T0 <R&= student ;/0R0 id H 2"I
023I
.
S0L0CT * <R&= studentI
13 <1RSTG2'=0 L'STG2'=0
-------- ----------- ----------
N0 <yi >ush
9an DD1 #tatements ,e 2sed in '1?#01?
.o, you can not run any DD1 statements is '1?#01 directly. If you try to use the D5O' %A,1 statement inside '1?#01,
you will get a compilation error as shown below*
Connect to 40 Cith SQL*Plus!
>0@12
3R&P T'>L0 studentI -- coBpilation error
023I
.
9an 8ariables ,e 2sed in #01 #tatements?
)es, you can use variables in #01 statements as part of any e-pressions. %he tutorial script provides you some good
e-amples*
Connect to 40 Cith SQL*Plus!
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
30CL'R0
(arGid 2:=>0RI
(arGnaBe C/'R10!I
>0@12
(arGid :H 2"I
(arGnaBe :H L>o,LI
12S0RT 12T& student ?'L:0S(arGid$ (arGnaBe$ L/enryL!I
(arGnaBe :H LUoeLI
12S0RT 12T& student ?'L:0S(arGidY1$ (arGnaBe$ L>ushL!I
(arGnaBe :H L<yiLI
:P3'T0 student S0T 7irstGnaBe H (arGnaBe
;/0R0 id H (arGidY1I
30L0T0 <R&= student ;/0R0 id H (arGidI
023I
.
S0L0CT * <R&= studentI
13 <1RSTG2'=0 L'STG2'=0
-------- ----------- -----------
N0 <yi >ush
What :appens If 8ariable .ames 9ollide with %able?9olumn .ames?
When a variable name collides with a column name, '1?#01 will use it as the variable if it is used where variable is allowed7
It will be used as the column, if it is used where variable is not allowed but column is allowed. :ere is a good e-ample of
variable names collide with column names*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
68
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
30CL'R0
id 2:=>0RI
7irstGnaBe C/'R10!I
>0@12
id :H 2"I
7irstGnaBe :H L>o,LI
12S0RT 12T& student ?'L:0Sid$ 7irstGnaBe$ L/enryL!I
7irstGnaBe :H LUoeLI
12S0RT 12T& student ?'L:0SidY1$ 7irstGnaBe$ L>ushL!I
7irstGnaBe :H L<yiLI
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe ;/0R0 id H 2"I
-- 1 roC updated
-- >oth L7irstGnaBeLs are treated as coluBn naBes
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe
;/0R0 id H idY1I
-- 0 roCs updated
-- >oth QidQs are treated as (aria,le naBes

30L0T0 <R&= student ;/0R0 id H idI
-- 2 roCs deleted
023I
.
S0L0CT * <R&= studentI
0 roCs selected
.oticed that Cid > idTAC in the W:5 clause will be evaluated to (A1#, because both CidCs are treated as variables.
#imilarly Cid > idC will also be evaluated to %52 in the W:5 clause. ,ut both Cfirst6nameCs in the #% clause will be
treated as column names.
:ow %o 5esolve .ame 9onflicts between 8ariables and 9olumns?
%he best way to resolve name conflicts is to avoid using column names for variables.
:ow %o Assign 0uery 5esults to 8ariables?
If you want to assign results from #19% statements to variables, you can use the I.%O clause, which an e-tension of
#19% statements for '1?#01. %he sample code below shows some good e-ample on I.%O clause*
30CL'R0
total 2:=>0RI
noC 3'T0I
7naBe ?'RC/'R210!I
lnaBe ?'RC/'R210!I
>0@12
S0L0CT C&:2T*! 12T& total <R&= eBployeesI
3>=SG&:TP:T.P:TGL120LCount H L EE T&GC/'Rtotal!!I
S0L0CT S9S3'T0 12T& noC <R&= 3:'LI
3>=SG&:TP:T.P:TGL120L2oC H L EE T&GC/'RnoC$ LSSSSSL!!I
S0L0CT 7irstGnaBe$ lastGnaBe 12T& 7naBe$ lnaBe
<R&= eBployees
;/0R0 eBployeeGid H 100I
3>=SG&:TP:T.P:TGL120L2aBe H L EE 7naBe EE L L EE lnaBe!I
023I
.
Count H 10W
2oC H #2NW%
2aBe H Ste(en Aing
9an )ou Assign <ultiple 0uery 5esult 5ows %o a 8ariable?
)ou can use C#19% ... I.%O variableC to assign 4uery results to variables. ,ut what happens if the #19% statements return
multiple rows? %he answer is that you will get a run time error. %he following tutorial e-ercise shows this error condition*
30CL'R0
7naBe ?'RC/'R210!I
lnaBe ?'RC/'R210!I
>0@12
S0L0CT 7irstGnaBe$ lastGnaBe 12T& 7naBe$ lnaBe
<R&= eBployees ;/0R0 eBployeeGid H 100I
3>=SG&:TP:T.P:TGL120L2aBe H L EE 7naBe EE L L EE lnaBe!I
S0L0CT 7irstGnaBe$ lastGnaBe 12T& 7naBe$ lnaBe
<R&= eBployees ;/0R0 eBployeeGid ) 100I
3>=SG&:TP:T.P:TGL120L2aBe H L EE 7naBe EE L L EE lnaBe!I
023I
.
&R'-01K22: e+act 7etch returns Bore than re-uested nuB,er
o7 roCs
&R'-0J%12: at line #
2aBe H Ste(en Aing
:ow %o 5un #01 (unctions in '1?#01?
Of course, you can run #01 functions in #01 statements. ,ut many #01 functions can also be e-ecuted in regular '1?#01
statements, as shown in the following sample script*
69
30CL'R0
noC 3'T0I
id 2:=>0RI
str ?'RC/'R2K0!I
>0@12
noC :H S9S3'T0I
3>=SG&:TP:T.P:TGL120LTiBe Z1 H L EE
T&GC/'RnoC$L//2K:=1:SSL!!I
S0L0CT S9S3'T0 12T& noC <R&= 3:'LI
3>=SG&:TP:T.P:TGL120LTiBe Z2 H L EE
T&GC/'RnoC$L//2K:=1:SSL!!I
id :H :13I
3>=SG&:TP:T.P:TGL120L:ser id Z2 H L EE T&GC/'Rid!!I
S0L0CT :13 12T& id <R&= 3:'LI
3>=SG&:TP:T.P:TGL120L:ser id Z2 H L EE T&GC/'Rid!!I

str :H C/RW0!EEC/R#"!EEC/RWN!I
3>=SG&:TP:T.P:TGL120LString Z1 H L EE str!I
S0L0CT C/RW0!EEC/R#"!EEC/RWN! 12T& str <R&= 3:'LI
3>=SG&:TP:T.P:TGL120LString Z2 H L EE str!I
023I
.
TiBe Z1 H 21:K1:1J
TiBe Z2 H 21:K1:1J
:ser id Z2 H NN
:ser id Z2 H NN
String Z1 H <91
String Z2 H <91
:ow %o 5etrieve the 9ount of 2pdated 5ows?
After running an 2'DA% statement, the database server returns a count of updated rows. )ou can retrieve this count from
a special predefined variable called #01W5OW9O2%, as shown in the following tutorial*
CR0'T0 T'>L0 eBpGteBp 'S S0L0CT * <R&= eBployeesI
>0@12
:P3'T0 eBpGteBp S0T salary H salary * 1.0%
;/0R0 salary T %000I
3>=SG&:TP:T.P:TGL120LZ o7 roCs updated: L EE
SQLVR&;C&:2T!I
023I
.
Z o7 roCs updated: K"
What Is the Implicit 9ursor?
%he implicit cursor is the cursor automatically defined by '1?#01 for you. Whenever a #01 statement is e-ecuted, this
cursor will be assigned to represent the e-ecution of this statement. %his implicit cursor is called #01. It has many attributes
representing some good information about the e-ecution li/e*
• #01W(O2.D + %rue, if the #01 statement has changed any rows.
• #01W.O%(O2.D + %rue, if the #01 statement has not changed any rows.
• #01W5OW9O2.% + %he number of rows affected by the #01 statement.
#ee the e-ample below on how to use the implicit cursor*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
30CL'R0
id 2:=>0RI
7irstGnaBe C/'R10!I
>0@12
id :H 2"I
7irstGnaBe :H L>o,LI
12S0RT 12T& student ?'L:0Sid$ 7irstGnaBe$ L/enryL!I
7irstGnaBe :H LUoeLI
12S0RT 12T& student ?'L:0SidY1$ 7irstGnaBe$ L>ushL!I
7irstGnaBe :H L<yiLI
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe ;/0R0 id H 2"I
1< SQLV<&:23 T/02
3>=SG&:TP:T.P:TGL120LZ o7 roCs updated: L EE
SQLVR&;C&:2T!I
023 1<I
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe
;/0R0 id H idY1I
1< SQLV2&T<&:23 T/02
3>=SG&:TP:T.P:TGL120L2o records updated.L!I
023 1<I

70
30L0T0 <R&= student ;/0R0 id H idI
3>=SG&:TP:T.P:TGL120LZ o7 roCs updated: L EE
SQLVR&;C&:2T!I
023I
.
Z o7 roCs updated: 1
2o records updated.
Z o7 roCs updated: 2
:ow %o Assign Data of the Deleted 5ow to 8ariables?
If a D1% statement is deleting a single row, you can assign column values of the deleted row to variables by using the
5%25.I.$ clause, which an e-tension of D1% statements for '1?#01. %he tutorial script shows you how to do this*
CR0'T0 T'>L0 eBpGteBp 'S S0L0CT * <R&= eBployeesI
30CL'R0
7naBe ?'RC/'R210!I
lnaBe ?'RC/'R210!I
>0@12
30L0T0 <R&= eBpGteBp ;/0R0 eBployeeGid H 100I
R0T:R212@ 7irstGnaBe$ lastGnaBe 12T& 7naBe$ lnaBeI
3>=SG&:TP:T.P:TGL120L2aBe deleted H L EE 7naBe EE L L
EE lnaBe!I
-- This Cill not CorR ,ecause Bultiple roCs deleted
-- 30L0T0 <R&= eBpGteBp ;/0R0 eBployeeGid ) 100I
-- R0T:R212@ 7irstGnaBe$ lastGnaBe 12T& 7naBe$ lnaBeI
023I
.
2aBe deleted H Ste(en Aing
#imilar to #19% ... I.%O, 5%25.I.$ ... I.%O will not wor/ if multiple rows are deleted.
What Is a 59O5D in '1?#01?
59O5D is a composite data type in '1?#01. It can have many fields representing data elements with different data types.
8ariables of 59O5D type can be designed to hold data from database table rows. %o use 59O5D data type, you need to
define a specific 59O5D type with each field defined with a name and a data type. %hen you can use your specific
59O5D type to define your 59O5D type variables.
:ow %o Define a #pecific 59O5D %ype?
If you want to define a specific 59O5D type, you need to use the %)' ... I# 59O5D statement in the declaration part of
any procedure or function. %he following e-ample script defines a 59O5D type called #%2D.%*
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
T9P0 student 1S R0C&R3
id 2:=>0R%!$
7irstGnaBe ?'RC/'R#0!$
lastGnaBe ?'RC/'R#0!
!I
>0@12
2:LLI
023I
.
:ow %o Define a 8ariable of a #pecific 59O5D %ype?
Once you have your specific 59O5D type defined, you can define new variables with this specific 59O5D type li/e any
other data type. In the sample script below, several variables are defined with a regular data type and a specific 59O5D
type*
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
T9P0 student 1S R0C&R3
id 2:=>0R%!$
7irstGnaBe ?'RC/'R#0!$
lastGnaBe ?'RC/'R#0!
!I
,estGstudent studentI
anotherGstudent studentI
classGnaBe ?'RC/'R2#0!I
>0@12
2:LLI
023I
.
:ow %o Assign 8alues to Data (ields in 59O5D 8ariables?
If a variable is a 59O5D variable, you can assign values to its data fields by using fields names prefi-ed with variable
name as Cvariable.field6nameC. :ere is a sample script assigning values to data fields of 59O5D variables*
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
T9P0 student 1S R0C&R3
id 2:=>0R%!$
7irstGnaBe ?'RC/'R#0!$
lastGnaBe ?'RC/'R#0!
!I
,estGstudent studentI
anotherGstudent studentI
classGnaBe ?'RC/'R2#0!I
>0@12
classGnaBe :H L<91 Center 200JLI
,estGstudent.7irstGnaBe :H LTheLI
71
,estGstudent.lastGnaBe :H L>estLI
023I
.
:ow %o 5etrieve 8alues from Data (ields in 59O5D 8ariables?
If a variable is a 59O5D variable with data fields assigned values, you can retrieve those values out of its data fields by
using fields names prefi-ed with variable name as Cvariable.field6nameC. :ere is a sample script showing you how to
retrieve field values from 59O5D variables*
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
T9P0 student 1S R0C&R3
id 2:=>0R%!$
7irstGnaBe ?'RC/'R#0!$
lastGnaBe ?'RC/'R#0!
!I
,estGstudent studentI
anotherGstudent studentI
classGnaBe ?'RC/'R2#0!I
>0@12
classGnaBe :H L<91 Center 200JLI
,estGstudent.7irstGnaBe :H LTheLI
,estGstudent.lastGnaBe :H L>estLI
3>=SG&:TP:T.P:TGL120L>est student H L EE
,estGstudent.7irstGnaBe EE L L
EE ,estGstudent.lastGnaBe!I
023I
.
>est student H The >est
:ow %o Define a Data (ield as .O% .211?
When defining a specific 59O5D type, you can define a data field as .O% .211 to ma/e sure variables with this
59O5D type to always have values in this field. A field defined as .O% .211 must have a default value. :ere is a tutorial
script showing you how to define a data field as .O% .211*
CR0'T0 &R R0PL'C0 PR&C03:R0 /0LL& 'S
T9P0 student 1S R0C&R3
id 2:=>0R%! 2&T 2:LL 30<':LT 0$
7irstGnaBe ?'RC/'R#0!$
lastGnaBe ?'RC/'R#0!
!I
,estGstudent studentI
anotherGstudent studentI
classGnaBe ?'RC/'R2#0!I
>0@12
classGnaBe :H L<91 Center 200JLI
,estGstudent.7irstGnaBe :H LTheLI
,estGstudent.lastGnaBe :H L>estLI
3>=SG&:TP:T.P:TGL120L>est student 13 H L
EE ,estGstudent.id!I
3>=SG&:TP:T.P:TGL120L>est student H L EE
,estGstudent.7irstGnaBe EE L L
EE ,estGstudent.lastGnaBe!I
023I
.
>est student 13 H 0
>est student H The >est
:ow %o Define a 59O5D 8ariable to #tore a %able 5ow?
If you have a table, and want to define a 59O5D variable to store all the data elements of a row from that table, you can
use table6nameW5OW%)' to define the 59O5D variable as shown in the following sample script*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
,estGstudent studentVR&;T9P0I
anotherGstudent studentVR&;T9P0I
classGnaBe ?'RC/'R2#0!I
>0@12
classGnaBe :H L<91 Center 200JLI
,estGstudent.7irstGnaBe :H LTheLI
,estGstudent.lastGnaBe :H L>estLI
3>=SG&:TP:T.P:TGL120L>est student 13 H L
EE ,estGstudent.id!I
3>=SG&:TP:T.P:TGL120L>est student H L EE
,estGstudent.7irstGnaBe EE L L
EE ,estGstudent.lastGnaBe!I
023I
.
>est student 13 H
>est student H The >est
:ow %o Assign a %able 5ow to a 59O5D 8ariable?
72
If you have a table, and want to assign a data row of that table to a 59O5D variable, you need to define this 59O5D
variable to match the table column structure, then use the #19% ... I.%O statement to assign a data row that 59O5D
variable. %he script below shows you how to do this*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
Banager eBployeesVR&;T9P0I
>0@12
S0L0CT * 12T& Banager <R&= eBployees
;/0R0 eBployeeGid H 100I
3>=SG&:TP:T.P:TGL120L=y Banager H L EE
Banager.7irstGnaBe EE L L EE Banager.lastGnaBe!I
023I
.
=y Banager H Ste(en Aing
:ow %o Insert a 5ecord into a %able?
If you have a 59O5D variable with data fields matching a table structure, you can insert a row to this table with this
59O5D variable using the I.#5% statement as shown in the e-ample below*
CR0'T0 T'>L0 eBpGteBp 'S S0L0CT * <R&= eBployeesI
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
Banager eBployeesVR&;T9P0I
>0@12
S0L0CT * 12T& Banager <R&= eBployees
;/0R0 eBployeeGid H 100I
Banager.eBployeeGid :H 2""I
12S0RT 12T& eBpGteBp ?'L:0S BanagerI
3>=SG&:TP:T.P:TGL120LZ roCs inserted H L
EE SQLVR&;C&:2T!I
023I
.
Z roCs inserted H 1
:ow %o 2pdate a %able 5ow with a 5ecord?
If you have a 59O5D variable with data fields matching a table structure, you can update a row in this table with this
59O5D variable using the 2'DA% ... #% 5OW statement as shown in the sample script below*
CR0'T0 T'>L0 eBpGteBp 'S S0L0CT * <R&= eBployeesI
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
Banager eBployeesVR&;T9P0I
>0@12
S0L0CT * 12T& Banager <R&= eBployees
;/0R0 eBployeeGid H 100I
Banager.eBployeeGid :H 2""I
12S0RT 12T& eBpGteBp ?'L:0S BanagerI
Banager.7irstGnaBe :H L<91LI
Banager.lastGnaBe :H LCenterLI
:P3'T0 eBpGteBp S0T R&; H Banager ;/0R0 eBployeeGid H 2""I
3>=SG&:TP:T.P:TGL120LZ roCs updated H L EE SQLVR&;C&:2T!I
023I
.
Z roCs updated H 1
:ow %o Define a 8ariable to <atch a %able 9olumn Data %ype?
If you have a table, and want to define some variables to have e-actly the same data types as some columns in that table,
you can use table6name.column6nameW%)' as data types to define those variables. %he tutorial sample below shows
you how to do this*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
ByGeBail eBployees.eBailVT9P0I
ByGsalary eBployees.salaryVT9P0I
>0@12
S0L0CT eBail$ salary 12T& ByGeBail$ ByGsalary
<R&= eBployees ;/0R0 eBployeeGid H 101I
3>=SG&:TP:T.P:TGL120L=y eBail H L EE ByGeBail!I
3>=SG&:TP:T.P:TGL120L=y salary H L EE ByGsalary!I
023I
.
=y eBail H 2A&C//'R
=y salary H 1W000
What Is a 9ursor?
A cursor loo/s li/e a variable, but it is not a variable. A cursor loo/s li/e a procedure, but it is not a procedure. A cursor is a
cursor. It is a logical representation of a resource connects to a set of data rows related to a D<1 statement. A cursor is consists
of*
• A D<1 statement with or without parameters.
• A status to indicate whether it is connected or not.
• A pointer to indicate the current row in the resource set.
• A number of attributes about the cursor, li/e (O2.D, .O%(O2.D and 5OW9O2.%.
:ow <any %ypes of 9ursors #upported in '1?#01?
'1?#01 supports two types of cursors*
• %he implicit cursor + A single default cursor that automatically connects to the last D<1 statement e-ecuted.
• -plicit cursors + 2ser defined cursors with specific D<1 statements and e-ecution statuses.
73
What Is the Implicit 9ursor?
%here is only one implicitly cursor in a session. %he implicit cursor is the cursor automatically defined by '1?#01 for you.
Whenever a #01 statement is e-ecuted, this cursor will be assigned to represent the e-ecution of this statement. %his
implicit cursor is called #01. It has many attributes representing some good information about the e-ecution li/e*
• #01W(O2.D + %rue, if the #01 statement has changed any rows.
• #01W.O%(O2.D + %rue, if the #01 statement has not changed any rows.
• #01W5OW9O2.% + %he number of rows affected by the #01 statement.
• WI#O'. + %rue, if there is a #01 statement being associated to the cursor.
:ow %o 2se Attributes of the Implicit 9ursor?
5ight after e-ecuting a D<1 statement, you retrieve any attribute of the implicit cursor by using #01Wattribute6name, as
shown in the following tutorial e-ercise*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL!I
Ta,le created.
30CL'R0
id 2:=>0RI
7irstGnaBe C/'R10!I
>0@12
id :H 2"I
7irstGnaBe :H L>o,LI
12S0RT 12T& student ?'L:0Sid$ 7irstGnaBe$ L/enryL!I
7irstGnaBe :H LUoeLI
12S0RT 12T& student ?'L:0SidY1$ 7irstGnaBe$ L>ushL!I
3>=SG&:TP:T.P:TGL120LZ o7 roCs inserted: L
EE SQLVR&;C&:2T!I

7irstGnaBe :H L<yiLI
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe ;/0R0 id H 2"I
1< SQLV<&:23 T/02
3>=SG&:TP:T.P:TGL120LZ o7 roCs updated: L
EE SQLVR&;C&:2T!I
023 1<I
:P3'T0 student S0T 7irstGnaBe H 7irstGnaBe
;/0R0 id H idY1I
1< SQLV2&T<&:23 T/02
3>=SG&:TP:T.P:TGL120L2o records updated.L!I
023 1<I

30L0T0 <R&= student ;/0R0 id H idI
3>=SG&:TP:T.P:TGL120LZ o7 roCs deleted: L
EE SQLVR&;C&:2T!I
023I
.
Z o7 roCs inserted: 1
Z o7 roCs updated: 1
2o records updated.
Z o7 roCs deleted: 2
:ow %o 1oop through Data 5ows in the Implicit 9urosr?
)ou use the (O5 ... I. ... 1OO' statement to loop through data rows in the implicit cursor as the following synta-*
<&R roC 12 dBlGstateBent L&&P
stateBent ,locR Cith roC.7ield!
023 L&&PI
:ere CrowC is a local 59O5D type variable with fields automatically defined to match the fields in the data rows resulted
from the D<1 statement. :ere is a good tutorial e-ercise on loop through data rows with the implicit cursor*
>0@12
<&R roC 12 S0L0CT * <R&= eBployees
;/0R0 BanagerGid H 101! L&&P
3>=SG&:TP:T.P:TGL120L2aBe H L EE roC.lastGnaBe!I
023 L&&PI
023I
.
2aBe H @reen,erg
2aBe H ;halen
2aBe H =a(ris
2aBe H >aer
2aBe H /iggins
:ow %o Define an -plicit 9ursor?
An e-plicit cursor must be defined in the declaration part of a procedure or function with the 925#O5 ... I# statement as
shown in the following sample script*
30CL'R0
C:RS&R cGlist 1S S0L0CT * <R&= countriesI
C:RS&R tGlist 1S S0L0CT * <R&= eBployees
;/0R0 eBployeeGid H 100I
>0@12
2:LLI
74
023I
.
:ow %o Open and 9lose an -plicit 9ursor?
An e-isting cursor can be opened or closed by the O'. or 91O# statement as shown in the following sample script*
30CL'R0
C:RS&R cGlist 1S S0L0CT * <R&= countriesI
C:RS&R tGlist 1S S0L0CT * <R&= eBployees
;/0R0 eBployeeGid H 100I
>0@12
&P02 cGlistI
&P02 tGlistI
CL&S0 cGlistI
CL&S0 tGlistI
023I
.
:ow %o 5etrieve Data from an -plicit 9ursor?
If you have a cursor opened ready to use, you can use the (%9: ... I.%O statement to retrieve data from the cursor into
variables. (%9: statement will*
• 5etrieve all the fields from the row pointed by the current cursor pointer and assign them to variables listed in the I.%O
clause.
• <ove the cursor pointer to the ne-t row.
• 2pdate cursor attributes li/e (O2.D and .O%(O2.D.
:ere is a sample script showing you how to use (%9: statement*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R tGlist 1S S0L0CT 7irstGnaBe$ lastGnaBe
<R&= eBployeesI
7GnaBe ?'RC/'R210!I
lGnaBe ?'RC/'R210!I
>0@12
&P02 tGlistI
<0TC/ tGlist 12T& 7GnaBe$ lGnaBeI
3>=SG&:TP:T.P:TGL120L2aBe H L EE 7GnaBe EE L L
EE lGnaBe!I
<0TC/ tGlist 12T& 7GnaBe$ lGnaBeI
3>=SG&:TP:T.P:TGL120L2aBe H L EE 7GnaBe EE L L
EE lGnaBe!I
-- <0TC/ tGlist 12T& lGnaBeI -- Bust ha(e tCo (aria,les
CL&S0 tGlistI
023I
.
2aBe H 0llen ',el
2aBe H Sundar 'nde
:ow %o 5etrieve Data from an 9ursor to a 59O5D?
If you have a cursor opened ready to use, you can also use the (%9: statement to retrieve data from the cursor into a
59O5D variable as shown in the tutorial e-ercise below*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R tGlist 1S S0L0CT 7irstGnaBe$ lastGnaBe
<R&= eBployeesI
T9P0 naBeGrec 1S R0C&R3
7GnaBe ?'RC/'R210!$
lGnaBe ?'RC/'R210!
!I
n naBeGrecI
>0@12
&P02 tGlistI
<0TC/ tGlist 12T& nI
3>=SG&:TP:T.P:TGL120L2aBe H L EE n.7GnaBe EE L L
EE n.lGnaBe!I
<0TC/ tGlist 12T& nI
3>=SG&:TP:T.P:TGL120L2aBe H L EE n.7GnaBe EE L L
EE n.lGnaBe!I
CL&S0 tGlistI
023I
.
2aBe H 0llen ',el
2aBe H Sundar 'nde
:ow %o 2se (%9: #tatement in a 1oop?
If you have a cursor opened ready to use, you can also use the (%9: statement in a loop to retrieve data from the cursor
more efficiently. ,ut you need to remember to use an FI% statement brea/ the loop when the cursor pointer reaches the
end. %he script below gives you a good e-ample*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R eBpGcur 1S S0L0CT * <R&= eBployees
;/0R0 BanagerGid H 101I
eBpGrec eBployeesVR&;T9P0I
>0@12
&P02 eBpGcurI
L&&P
<0TC/ eBpGcur 12T& eBpGrecI
75
041T ;/02 eBpGcurV2&T<&:23I
3>=SG&:TP:T.P:TGL120L2aBe H L EE
eBpGrec.7irstGnaBe EE L L EE eBpGrec.lastGnaBe!I
023 L&&PI
CL&S0 eBpGcurI
023I
.
2aBe H 2ancy @reen,erg
2aBe H Uenni7er ;halen
2aBe H Susan =a(ris
2aBe H /erBann >aer
2aBe H Shelley /iggins
:ow %o 2se an -plicit 9ursor without O'. #tatements?
If you want to open a cursor and loop through its data rows in 4uic/ way, you can use the (O5 ... I. ... 1OO' statement in
the same way as the implicit cursor. %he following tutorial e-ercise gives you a good e-ample*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R eBpGcur 1S S0L0CT * <R&= eBployees
;/0R0 BanagerGid H 101I
>0@12
<&R roC 12 eBpGcur L&&P
3>=SG&:TP:T.P:TGL120L2aBe H L EE
roC.7irstGnaBe EE L L EE roC.lastGnaBe!I
023 L&&PI
023I
.
2aBe H 2ancy @reen,erg
2aBe H Uenni7er ;halen
2aBe H Susan =a(ris
2aBe H /erBann >aer
2aBe H Shelley /iggins
9an <ultiple 9ursors ,eing Opened at the #ame %ime?
)es, multiple cursors can be opened at the same time. #ee the following e-ample*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R eBpGcur 1S S0L0CT * <R&= eBployeesI
eBpGrec eBployeesVR&;T9P0I
C:RS&R dptGcur 1S S0L0CT * <R&= departBentsI
dptGrec departBentsVR&;T9P0I
>0@12
&P02 eBpGcurI
&P02 dptGcurI
<0TC/ eBpGcur 12T& eBpGrecI
<0TC/ dptGcur 12T& dptGrecI
3>=SG&:TP:T.P:TGL120L3epartBent naBe H L EE
dptGrec.departBentGnaBe!I
3>=SG&:TP:T.P:TGL120L0Bployee naBe H L EE
eBpGrec.7irstGnaBe EE L L EE eBpGrec.lastGnaBe!I
CL&S0 eBpGcurI
CL&S0 dptGcurI
023I
.
3epartBent naBe H 'dBinistration
0Bployee naBe H Ste(en Aing
:ow %o 'ass a 'arameter to a 9ursor?
When you define a cursor, you can set a formal parameter in the cursor. %he formal parameter will be replaced by an actual
parameter in the O'. cursor statement. :ere is a good e-ample of a cursor with two parameters*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
C:RS&R eBpGcurloC 2:=>0R$ high 2:=>0R!
1S S0L0CT * <R&= eBployees ;/0R0 salary )H loC
'23 salary TH highI
>0@12
<&R roC 12 eBpGcur12000$1%000! L&&P
3>=SG&:TP:T.P:TGL120roC.7irstGnaBe EE L L
EE roC.lastGnaBe
EE L: L EE roC.salary!I
023 L&&PI
023I
.
2ancy @reen,erg: 12000
Uohn Russell: 1K000
Aaren Partners: 1N%00
'l,erto 0rraPuriP: 12000
=ichael /artstein: 1N000
Shelley /iggins: 12000
What Is a 9ursor 8ariable?
A cursor variable is a variable of a specific 5( 925#O5 data type, which is a pointer to a data structure resource connects
to 4uery statement result, similar to the 925#O5 data type.. %he advantage of using cursor variables is that cursor
variables can be used as variables to pass between procedures and functions.
:ow %o Define a 9ursor 8ariable?
76
%o define cursor variable, you must decide which 5( 925#O5 data type to use. %here are H ways to select a 5(
925#O5 data type*
• Define your own specific 5( 925#O5 types using the %)' ... 5%25. statement.
• Define your own generic 5( 925#O5 type using the %)' ... statement.
• 2se the system defined 5( 925#O5 type* #)#65(925#O5.
%he follwoing tutorial e-ercise defines H cursor variables in H different ways*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
T9P0 eBpGre7 1S R0< C:RS&R R0T:R2 eBployeesVR&;T9P0I
T9P0 anyGre7 1S R0< C:RS&RI
eBpGcur eBpGre7I
anyGcur anyGre7I
sysGcur S9SGR0<C:RS&RI
>0@12
2:LLI
023I
.
:ow %o Open a 9ursor 8ariable?
A cursor variable must be opened with a specific 4uery statement before you can fetch data fields from its data rows. %o
open a cursor variable, you can use the O'. ... (O5 statement as shown in the following tutorial e-ercise*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
T9P0 eBpGre7 1S R0< C:RS&R R0T:R2 eBployeesVR&;T9P0I
T9P0 anyGre7 1S R0< C:RS&RI
eBpGcur eBpGre7I
anyGcur anyGre7I
sysGcur S9SGR0<C:RS&RI
>0@12
&P02 eBpGcur <&R S0L0CT * <R&= eBployeesI
&P02 anyGcur <&R S0L0CT * <R&= eBployeesI
&P02 sysGcur <&R S0L0CT * <R&= eBployeesI
CL&S0 sysGcurI
CL&S0 anyGcurI
CL&S0 eBpGcurI
023I
.
:ow %o 1oop through a 9ursor 8ariable?
Once a cursor variable is opened with a 4uery statement, it will have the same attributes as a normal cursor and it can be
used in the same way a normal cursor too. %he following sample script shows you how to loop through a cursor variable*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
T9P0 eBpGre7 1S R0< C:RS&R R0T:R2 eBployeesVR&;T9P0I
eBpGcur eBpGre7I
eBpGrec eBployeesVR&;T9P0I
>0@12
&P02 eBpGcur <&R S0L0CT * <R&= eBployees
;/0R0 BanagerGid H 101I
L&&P
<0TC/ eBpGcur 12T& eBpGrecI
041T ;/02 eBpGcurV2&T<&:23I
3>=SG&:TP:T.P:TGL120L2aBe H L EE
eBpGrec.7irstGnaBe EE L L EE eBpGrec.lastGnaBe!I
023 L&&PI
CL&S0 eBpGcurI
023I
.
2aBe H 2ancy @reen,erg
2aBe H Uenni7er ;halen
2aBe H Susan =a(ris
2aBe H /erBann >aer
2aBe H Shelley /iggins
:ow %o 'ass a 9ursor 8ariable to a 'rocedure?
A cursor variable can be passed into a procedure li/e a normal variable. %he sample script below gives you a good e-ample*
CR0'T0 &R R0PL'C0 PR&C03:R0 <91GC02T0R 'S
sysGcur S9SGR0<C:RS&RI
PR&C03:R0 eBpGprintcur S9SGR0<C:RS&R! 'S
eBpGrec eBployeesVR&;T9P0I
>0@12
L&&P
<0TC/ cur 12T& eBpGrecI
041T ;/02 curV2&T<&:23I
3>=SG&:TP:T.P:TGL120L2aBe H L EE
eBpGrec.7irstGnaBe EE L L EE eBpGrec.lastGnaBe!I
023 L&&PI
023I
>0@12
&P02 sysGcur <&R S0L0CT * <R&= eBployees
;/0R0 BanagerGid H 101I
eBpGprintsysGcur!I
CL&S0 sysGcurI
023I
77
.
2aBe H 2ancy @reen,erg
2aBe H Uenni7er ;halen
2aBe H Susan =a(ris
2aBe H /erBann >aer
2aBe H Shelley /iggins
Why 9ursor 8ariables Are asier to 2se than 9ursors?
9ursor variables are easier to use than cursors because*
• 9ursor variables are easier to define. .o need to give a specific 4uery statement.
• 9ursor variables are easier to open. )ou can specify the 4uery dynamically at the time of open.
• 9ursor variables can be passed into procedures or functions.
What Is the #implest %ool to 5un 9ommands on Oracle #ervers?
%he simplest tool to connect to an Oracle server and run commands to manage data is #01@'lus. It is an Oracle database
client tool that wor/s as a command+line user interface to the database server. #01@'lus allows you*
• (ormat, perform calculations on, store, and print from 4uery results.
• -amine table and ob3ect definitions.
• Develop and run batch scripts.
• 'erform database administration.
What Is the 0uic/est Way to -port a %able to a (lat (ile?
%he 4uic/est way to e-port a table to a flat file is probably to use the #01@'lus #'OO1 command. It allows you to record
#19% 4uery result to a te-t file on the operating system. %he following tutorial e-ercise shows you how control the output
format, start the spooler, and dump all record from a table to a flat te-t file*
)BRdir *oracle+e*test
)s-lplus .nolog
SQL) connect /R.7yicenter
SQL) S0T /0'312@ &<<I
SQL) S0T <003>'CA &<<I
SQL) S0T L120S1O0 1000I
SQL) SP&&L *oracle+e*test*eBployees.t+tI
SQL) S0L0CT * <R&= 0=PL&900SI
......
SQL) SP&&L &<<I
)ou should get all records in employees.t-t with fi-ed length fields.
:ow %o -port Data with a (ield Delimiter?
%he previous e-ercise allows you to e-port data with fi-ed field lengths. If you want e-port data with variable field lengths
and field delimiters, you can concatenate your fields with an e-pression in the #19% clause as shown in the tutorial
e-ercise bellow*
SQL) S0T /0'312@ &<<I
SQL) S0T <003>'CA &<<I
SQL) S0T L120S1O0 1000I
SQL) SP&&L *oracle+e*test*7yiGlinRs.t+tI
SQL) S0L0CT id EEL$LEE url EEL$LEE notes EEL$LEE counts
EEL$LEE created <R&= 7yiGlinRsI
......
SQL) SP&&L &<<I
)ou should see all records in fyi6lin/s.t-t with &,& delimited fields as shown here*
101$7yicenter.coB$Session 1$$1W-='9-0J
110$center7yi.coB$Session 1$$1W-='9-0J
What Is #01@1oader?
#01@1oader is a database tool that allows to load data from e-ternal files into database tables. #01@1oader is available as
part of the free Oracle AMg -pression dition. It has some interesting features as*
• 9an load data from multiple data files into multiple tables in one load session.
• 9an specify character set of the data.
• 9an generate sophisticated error reports.
• 9an load data directly to Oracle datafiles, bypassing normal record inserting process.
What Is a #01@1oader 9ontrol (ile?
A #01@1oader control file a te-t that defines how data files should be loaded into the database. It allows you to specify*
• Where is the input data file.
• %he format of the input date file.
• %he target table where the data should be loaded.
• :ow input data fields should be mapped to target table columns.
• #elect criteria to select input records for loading.
• Where to output errors.
:ow %o 1oad Data with #01@1oader?
1et&s say you have a table defined as*
CR0'T0 T'>L0 student id 2:=>0R%! PR1='R9 A09$
7irstGnaBe ?'RC/'R#0! 2&T 2:LL$
lastGnaBe ?'RC/'R#0! 2&T 2:LL$
,irthGdate 3'T0 2&T 2:LL$
78
socialGnuB,er ?'RC/'R#0! :21Q:0 2&T 2:LL!I
%here is an input data file stored at Noracle-eNte-tNstudent.t-t with the following records*
1$Ste(en$Aing$1W-U:2-WW$%1%.12N.K%JW
2$2eena$Aochhar$21-S0P-W"$%1%.12N.K%J#
N$Le+$3e /aan$1N-U'2-#N$%1%.12N.K%J"
K$'le+ander$/unold$0N-U'2-#0$%"0.K2N.K%JW
%$>ruce$0rnst$21-='9-#1$%"0.K2N.K%J#
J$3a(id$'ustin$2%-U:2-#W$%"0.K2N.K%J"
W$?alli$Pata,alla$0%-<0>-##$%"0.K2N.K%J0
#$3iana$LorentP$0W-<0>-#"$%"0.K2N.%%JW
"$2ancy$@reen,erg$1W-':@-#K$%1%.12K.K%J"
10$3aniel$<a(iet$1J-':@-#K$%1%.12K.K1J"
)ou can create a control file at Noracle-eNtestNstudent.ctl as*
L&'3 3'T'
'PP023 12T& T'>L0 ST:302T
<10L3S T0R=12'T03 >9 L$L
id$ 7irstGnaBe$ lastGnaBe$ ,irthGdate$ socialGnuB,er!
When you are ready to load data, you can run the #01@1oader with the Cs4lldrC command*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)s-lldr useridHhr.7yicenter$
controlH*oracle+e*test*student.ctl$
dataH*oracle+e*test*student.t+t$
logH*oracle+e*test*student.log
SQL*Loader: Release 10.2.0.1.0 -
CoBBit point reached - logical record count 10
%o see details of the loading process, you should chec/ the log file Noracle-eNtestNstudent.log.
What Is an -ternal %able?
An e-ternal table is a table defined in the database with data stored outside the database. Data of an e-ternal table is stored
in files on the operating systems. Accessing data of e-ternal tables are done through data access drivers. 9urrently, Oracle
supports two data access drivers* O5A9161OAD5 and O5A916DA%A'2<'.
-ternal tables can be used to load data from e-ternal files into database, or unload data from database to e-ternal files.
:ow %o 1oad Data through -ternal %ables?
If you have data stored in e-ternal files, you can load it to database through an e-ternal table by the steps below*
• 9reate an e-ternal table with columns matching data fields in the e-ternal file.
• 9reate a regular table with the same columns.
• 5un an I.#5% I.%O ... #19% statement to load data from the e-ternal file to the regular table through the e-ternal
table.
What Are the 5estrictions on -ternal %able 9olumns?
When creating e-ternal table columns, you need to watch out some restrictions*
• C'5I<A5) D)C is not allowed.
• C.O% .211C is not allowed.
• CD(A21% valueC is not allowed.
What Is a Directory Ob3ect?
A directory ob3ect is a logical alias for a physical directory path name on the operating system. Directory ob3ects can be
created, dropped, and granted access permissions to different users. %he following tutorial e-ercise shows you some good
e-amples*
)s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) CR0'T0 31R0CT&R9 testGdir 'S L.oracle+e.testLI
3irectory created.
SQL) @R'2T R0'3 &2 31R0CT&R9 testGdir T& hrI
@rant succeeded.
SQL) @R'2T ;R1T0 &2 31R0CT&R9 testGdir T& hrI
@rant succeeded.
SQL) CR0'T0 31R0CT&R9 teBpGdir 'S L.oracle+e.teBpLI
3irectory created.
SQL) 3R&P 31R0CT&R9 teBpGdirI
3irectory dropped.
:ow %o Define an -ternal %able with a %e-t (ile?
)ou can use the 95A% %A,1 statement to create e-ternal tables. ,ut you need to use O5$A.IKA%IO. F%5.A1
clause to specify the e-ternal file location and the data access driver. %he tutorial e-ercise below shows you how to define
an e-ternal table as a te-t file*
)s-lplus .nolog
SQL) connect /R.7yicenter
SQL) CR0'T0 T'>L0 e+tG7yiGlinRs
id 2:=>0RK!$
url ?'RC/'R21J!$
notes ?'RC/'R21J!$
79
counts 2:=>0RK!$
created 3'T0
! &R@'21O'T1&2 04T0R2'L
T9P0 &R'CL0GL&'30R
30<':LT 31R0CT&R9 testGdir
L&C'T1&2 Le+tG7yiGlinRs.t+tL!
!I
Ta,le created.
SQL) S0L0CT ta,leGnaBe$ ta,lespaceGnaBe$ nuBGroCs
<R&= :S0RGT'>L0SI
T'>L0G2'=0 T'>L0SP'C0G2'=0 2:=GR&;S
--------------------- ---------------------- ----------
R0@1&2S :S0RS K
L&C'T1&2S :S0RS 2N
30P'RT=02TS :S0RS 2W
U&>S :S0RS 1"
0=PL&900S :S0RS 10W
U&>G/1ST&R9 :S0RS 10
<91GL12AS :S0RS 2
04TG<91GL12AS
C&:2TR10S 2%
:ow %o 5un 0ueries on -ternal %ables?
If you have an e-ternal table defined as a te-t file with the O5A9161OAD5 driver, you can add data to the te-t file, and
4uery the te-t file through the e-ternal table. ,y default, data fields in the te-t file should be terminated by &,&. %he tutorial
e-ercise below shows you how add data to the e-ternal table defined in the previous e-ercise*
)edit .oracle+e.test.e+tG7yiGlinRs.t+t
1101$d,a.7yicenter$LinR Z1$##$0W-='9-0J
1110$de(.7yicenter$LinR Z2$##$0W-='9-0J
)s-lplus .nolog
SQL) connect /R.7yicenter
SQL) S0L0CT * <R&= e+tG7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
--------- ---------------- ----------- -------- ---------
1101 d,a.7yicenter LinR Z1 ## 0W-='9-0J
1110 de(.7yicenter LinR Z2 ## 0W-='9-0J
:ow %o 1oad Data from -ternal %ables to 5egular %ables?
Once you have your data entered in a te-t file, and an e-ternal table defined to this te-t file, you can easily load data from
this te-t file to a regular table. %he following tutorial e-ercise shows you how to load data from the te-t file and the e-ternal
table defined in the previous e-ercises to a regular table*
SQL) CR0'T0 T'>L0 7yiGlinRs
id 2:=>0RK! PR1='R9 A09$
url ?'RC/'R21J! 2&T 2:LL$
notes ?'RC/'R21J!$
counts 2:=>0RK!$
created 3'T0 30<':LT sysdate!
!I
SQL) 12S0RT 12T& 7yiGlinRs S0L0CT * <R&= e+tG7yiGlinRsI
2 roCs created.
SQL) S0L0CT * <R&= 7yiGlinRsI
13 :RL 2&T0S C&:2TS CR0'T03
--------- ---------------- ----------- -------- ---------
101 7yicenter.coB Session 1 0W-='9-0J
110 center7yi.coB Session 1 0W-='9-0J
1101 d,a.7yicenter LinR Z1 ## 0W-='9-0J
1110 de(.7yicenter LinR Z2 ## 0W-='9-0J
What Is the Data 'ump -port 2tility?
Oracle Data 'ump -port utility is a standalone programs that allows you to e-port data ob3ects from Oracle database to
operating system files called dump file set, which can be imported bac/ to Oracle database only by Oracle Data 'ump
Import utility.
%he dump file set can be imported on the same system or it can be moved to another system and loaded there.
%he dump file set is made up of one or more dis/ files that contain table data, database ob3ect metadata, and control
information. %he files are written in a proprietary, binary format. During an import operation, the Data 'ump Import utility
uses these files to locate each database ob3ect in the dump file set. ,ecause the dump files are written by the server, rather
than by the client, the data base administrator !D,A" must create directory ob3ects.
What Is the Data 'ump Import 2tility?
Oracle Data 'ump Import utility is a standalone programs that allows you to import data ob3ects from an Oracle dump file set
into Oracle database. Oracle dump file set is written in a proprietary binary format by the Data 'ump -port utility.
Import can also be used to load a target database directly from a source database with no intervening dump files. %his
allows e-port and import operations to run concurrently, minimizing total elapsed time. %his is /nown as a networ/ import.
Data 'ump Import enables you to specify whether a 3ob should move a subset of the data and metadata from the dump file
set or the source database !in the case of a networ/ import", as determined by the import mode. %his is done using data
filters and metadata filters, which are implemented through Import commands.
:ow %o Invo/e the Data 'ump -port 2tility?
80
%he Data 'ump -port utility is distributed as e-ecutable file called Ce-pdp.e-eC. %o invo/e this utility, you should go to the
CbinC directory of your Oracle server installation and run the Ce-pdpC command. :ere is tutorial e-ercise on how to invo/e the
e-port utility*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)e+pdp helpHy
0+port: Release 10.2.0.1.0 -
The 3ata PuBp e+port utility pro(ides a BechanisB 7or
trans7erring data o,Xects ,etCeen &racle data,ases. The
utility is in(oRed Cith the 7olloCing coBBand:
0+aBple: e+pdp scott.tiger 31R0CT&R9HdBpdir
3:=P<1L0Hscott.dBp
9ou can control hoC 0+port runs ,y entering the Le+pdpL
coBBand 7olloCed ,y (arious paraBeters:
<orBat: e+pdp A09;&R3H(alue or
A09;&R3H(alue1$(alue2$...$(alue2!
0+aBple: e+pdp scott.tiger 3:=P<1L0Hscott.dBp
31R0CT&R9HdBpdir
SC/0='SHscott or T'>L0SHT1:P1$T1:P2!
:S0R13 Bust ,e the 7irst paraBeter on the coBBand line.
AeyCord 3escription 3e7ault!
------------------------------------------------------------
'TT'C/ 'ttach to e+isting Xo,$ e.g. 'TT'C/ 5HXo, naBe
C&=PR0SS1&2 Reduce siPe o7 duBp7ile contents Chere (alid
ReyCord (alues are: =0T'3'T'G&2L9! and 2&20.
31R0CT&R9 3irectory o,Xect to ,e used 7or duBp7iles
3:=P<1L0 List o7 destination duBp 7iles e+pdat.dBp!.
<L'S/>'CAGSC2 SC2 used to set session snapshot ,acR to.
<:LL 0+port entire data,ase 2!.
/0LP 3isplay /elp Bessages 2!.
......
:ow %o Invo/e the Data 'ump Import 2titlity?
%he Data 'ump Import utility is distributed as e-ecutable file called Cimpdp.e-eC. %o invo/e this utility, you should go to the
CbinC directory of your Oracle server installation and run the CimpdpC command. :ere is tutorial e-ercise on how to invo/e the
import utility*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)iBpdp helpHy
1Bport: Release 10.2.0.1.0 -
The 3ata PuBp 1Bport utility pro(ides a BechanisB 7or
trans7erring data o,Xects ,etCeen &racle data,ases. The
utility is in(oRed Cith the 7olloCing coBBand:
0+aBple: iBpdp scott.tiger 31R0CT&R9HdBpdir
3:=P<1L0Hscott.dBp
9ou can control hoC 1Bport runs ,y entering the LiBpdpL
coBBand 7olloCed ,y (arious paraBeters.
<orBat: iBpdp A09;&R3H(alue or
A09;&R3H(alue1$(alue2$...$(alue2!
0+aBple: iBpdp scott.tiger 31R0CT&R9HdBpdir
3:=P<1L0Hscott.dBp
:S0R13 Bust ,e the 7irst paraBeter on the coBBand line.
AeyCord 3escription 3e7ault!
----------------------------------------------------------
'TT'C/ 'ttach to e+isting Xo,$ e.g. 'TT'C/ 5HXo, naBe6
31R0CT&R9 3irectory o,Xect to ,e used 7or duBp$ log$ ...
3:=P<1L0 List o7 duBp7iles to iBport 7roB e+pdat.dBp!
<:LL 1Bport e(erything 7roB source 9!.
/0LP 3isplay help Bessages 2!.
......
What Are Data 'ump -port and Import <odes?
Data pump e-port and import modes are used to determine the type and portions of database to be e-ported and imported.
Oracle AMg supports G e-port and import modes*
• (ull* -ports and imports a full database. 2se the (211 parameter to specify this mode.
81
• #chema* nables you to e-port and import all ob3ects that belong to a schema. 2se the #9:<A# parameter to
specify this mode. %his is the default mode.
• %able* nables you to e-port and import specific tables and partitions. 2se the %A,1# parameter to specify this
mode.
• %ablespace* nables a privileged user to move a set of tablespaces from one Oracle database to another. 2se the
%A,1#'A9# parameter to specify this mode.
• %ablespace* nables a privileged user to move metadata from the tables within a specified set of tablespaces from one
Oracle database to another. 2se the %5A.#'O5%6%A,1#'A9# parameter to specify this mode.
:ow %o stimate Dis/ #pace .eeded for an -port =ob?
If you 3ust want to /now how much dis/ space for the dump without actually e-porting any data, you can use the
#%I<A%6O.1)>y parameter on the e-pdp command. %he following tutorial e-ercise shows you how a system user
wants to see the dis/ space estimates on a full database e-port*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)e+pdp S9ST0=.7yicenter <:LLHy 0ST1='T0G&2L9Hy
Starting QS9ST0=Q.QS9SG04P&RTG<:LLG01Q: S9ST0=.**** <:LLHy
0ST1='T0G&2L9Hy
0stiBate in progress using >L&CAS Bethod...
Processing o,Xect 3'T'>'S0G04P&RT.SC/0='.T'>L0.T'>L0G3'T'
. estiBated Q<L&;SG020100Q.Q;;?G<L&;GP'@0GPL:@SQ K2 =>
. estiBated Q<L&;SG020100Q.Q;;?G<L&;GST0PG1T0=SQ 2W =>
. estiBated Q<L&;SG020100Q.Q;;?G<L&;GST0PGPR&C0SS12@Q 1J =>
......
. estiBated QS9ST0=Q.QSQLPL:SGPR&3:CTGPR&<1L0Q 0 A>
. estiBated QTS=S9SQ.QSRS[Q 0 A>
Total estiBation using >L&CAS Bethod: 1J".# =>
Uo, QS9ST0=Q.QS9SG04P&RTG<:LLG01Q success7ully coBpleted
.ow you /now that you need AJM <, dis/ space to e-port the entire data base.
Oracle also records the screen output in a log file called e-port.log at Noracle-eNappNoracleNadminNFNdpdump.
:ow %o Do a (ull Database -port?
If you are ready to do a full database e-port, you can use the (211>y parameter on the e-pdp command, as shown in the
following tutorial e-ercise*
)e+pdp S9ST0=.7yicenter <:LLHy 0ST1='T0G&2L9Hy
Starting QS9ST0=Q.QS9SG04P&RTG<:LLG01Q: S9ST0=.**** <:LLHy
0stiBate in progress using >L&CAS Bethod...
Processing o,Xect 3'T'>'S0G04P&RT.SC/0='.T'>L0.T'>L0G3'T'
Total estiBation using >L&CAS Bethod: 1J".# =>
Processing o,Xect type 3'T'>'S0G04P&RT.T'>L0SP'C0
Processing o,Xect type 3'T'>'S0G04P&RT.S9SG:S0R.:S0R
Processing o,Xect type 3'T'>'S0G04P&RT.SC/0='.:S0R
......
. . e+ported <L&;SG020100.;;?G<L&;GP'@0GPL:@S N2.%1=>
. . e+ported <L&;SG020100.;;?G<L&;GST0PG1T0=S 21.J#=>
. . e+ported <L&;SG020100.;;?G<L&;GST0PGPR&C0SS12@ 11.1W=>
......
=aster ta,le QS9ST0=Q.QS9SG04P&RTG<:LLG01Q unloaded
**********************************************************
3uBp 7ile set 7or S9ST0=.S9SG04P&RTG<:LLG01 is:
C:*&R'CL040*'PP*&R'CL0*'3=12*40*3P3:=P*04P3'T.3=P
Uo, QS9ST0=Q.QS9SG04P&RTG<:LLG01Q success7ully coBpleted
Where Is the -port Dump (ile 1ocated?
If you are not specifying the dump directory and file name, the dump file will be stored in the default dump directory with the
default file name. %he tutorial e-ercise below tells you find what is your default dump directory and locate the dump file.
)s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) C&L oCner <&R='T '#I
SQL) C&L directoryGnaBe <&R='T '1JI
SQL) C&L directoryGpath <&R='T 'K0I
SQL) S0L0CT * <R&= d,aGdirectoriesI
&;20R 31R0CT&R9G2'=0 31R0CT&R9GP'T/
----- -------------- -------------------------------------
S9S 3'T'GP:=PG31R *oracle+e*app*oracle*adBin*40*dpduBp*
S9S T0STG31R .oracle+e.test
S9S &R'CL0CLR31R *oracle+e*app*oracle*product*10.2.0*
ser(er*,in*clr
Obviously, the default dump directory is directory ob3ect defined to Noracle-eNappNoracleNadminNFNdpdumpN. If you go to that
directory, you will find the full database dump file is called Ce-pdat.dmpC.
:ow %o -port )our Own #chema?
If you have a non+system user account and you want to e-port all data ob3ects in the schema associated with your account,
you can use the Ce-pdpC command with the #9:<A# parameter. 5unning Ce-pdpC command with a non+system user
account re4uires a directory ob3ect granted to this user account. %he following tutorial e-ercise shows you how to define a
directory ob3ect and e-port a schema*
)BRdir *oracle+e*hrGduBp
82
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)s-lplus .nolog
SQL) connect S9ST0=.7yicenter
SQL) CR0'T0 31R0CT&R9 hrGduBp 'S L*oracle+e*hrGduBpLI
3irectory created.
SQL) @R'2T R0'3 &2 31R0CT&R9 hrGduBp T& hrI
@rant succeeded.
SQL) @R'2T ;R1T0 &2 31R0CT&R9 hrGduBp T& hrI
@rant succeeded.
SQL) -uit
)e+pdp hr.7yicenter SC/0='SHhr
31R0CT&R9HhrGduBp 3:=P<1L0HscheBa.dBp L&@<1L0HscheBa.log
Starting Q/RQ.QS9SG04P&RTGSC/0='G01Q:
hr.******** SC/0='SHhr 31R0CT&R9HhrGduBp
3:=P<1L0HscheBa.dBp
L&@<1L0HscheBa.log
0stiBate in progress using >L&CAS Bethod...
Processing o,Xect type SC/0='G04P&RT.T'>L0.T'>L0G3'T'
Total estiBation using >L&CAS Bethod: "J0 A>
Processing o,Xect type SC/0='G04P&RT.PR0GSC/0='.PR&C'CTGSC/
Processing o,Xect type SC/0='G04P&RT.S0Q:02C0.S0Q:02C0
Processing o,Xect type SC/0='G04P&RT.T'>L0.T'>L0
......
. . e+ported Q/RQ.QC&:2TR10SQ J.0#% A> 2% roCs
. . e+ported Q/RQ.Q30P'RT=02TSQ J.JN2 A> 2W roCs
. . e+ported Q/RQ.Q0=PL&900SQ 1%.WJ A> 10W roCs
. . e+ported Q/RQ.Q0=PL&900SGT0=PQ 1%.#J A> 10W roCs
......
=aster ta,le Q/RQ.QS9SG04P&RTGSC/0='G01Q loaded.unloaded
*********************************************************
3uBp 7ile set 7or /R.S9SG04P&RTGSC/0='G01 is:
C:*&R'CL040*/RG3:=P*SC/0='.3=P
Uo, Q/RQ.QS9SG04P&RTGSC/0='G01Q success7ully coBpleted
:ow %o -port #everal %ables %ogether?
If you don&t want to e-port the entire schema and only want to e-port several tables only, you can use the Ce-pdpC command
with the C%A,1#C parameter as shown in the following tutorial e-ercise*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)e+pdp hr.7yicenter T'>L0SHeBployees$departBents
31R0CT&R9HhrGduBp 3:=P<1L0Hta,les.dBp L&@<1L0Hta,les.log
Starting Q/RQ.QS9SG04P&RTGT'>L0G01Q: hr.********
T'>L0SHeBployees$departBents 31R0CT&R9HhrGduBp
3:=P<1L0Hta,les.dBp
L&@<1L0Hta,les.log
0stiBate in progress using >L&CAS Bethod...
Processing o,Xect type T'>L0G04P&RT.T'>L0.T'>L0G3'T'
Total estiBation using >L&CAS Bethod: 12# A>
Processing o,Xect type T'>L0G04P&RT.T'>L0.T'>L0
Processing o,Xect type T'>L0G04P&RT.T'>L0.12304.12304
Processing o,Xect type T'>L0G04P&RT.T'>L0.C&2STR'12T.C&2...
Processing o,Xect type T'>L0G04P&RT.T'>L0.12304.ST'T1ST1...
Processing o,Xect type T'>L0G04P&RT.T'>L0.C&==02T
Processing o,Xect type T'>L0G04P&RT.T'>L0.C&2STR'12T.R0<...
Processing o,Xect type T'>L0G04P&RT.T'>L0.TR1@@0R
Processing o,Xect type T'>L0G04P&RT.T'>L0.ST'T1ST1CS.T'>...
. . e+ported Q/RQ.Q30P'RT=02TSQ J.JN2 A> 2W roCs
. . e+ported Q/RQ.Q0=PL&900SQ 1%.WJ A> 10W roCs
=aster ta,le Q/RQ.QS9SG04P&RTGT'>L0G01Q loaded.unloaded
***********************************************************
3uBp 7ile set 7or /R.S9SG04P&RTGT'>L0G01 is:
C:*&R'CL040*/RG3:=P*T'>L0S.3=P
Uo, Q/RQ.QS9SG04P&RTGT'>L0G01Q success7ully coBpleted
What :appens If the Imported %able Already -ists?
If the import process tries to import a table that already e-ists, the Data 'ump Import utility will return an error and s/ip this
table. %he following e-ercise shows you a good e-ample*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)iBpdp hr.7yicenter T'>L0SHeBployees
31R0CT&R9HhrGduBp 3:=P<1L0Hta,les.dBp L&@<1L0Hta,les.log
=aster ta,le Q/RQ.QS9SG1=P&RTGT'>L0G01Q loaded.unloaded
Starting Q/RQ.QS9SG1=P&RTGT'>L0G01Q: hr.** T'>L0SHeBployees
83
31R0CT&R9HhrGduBp 3:=P<1L0Hta,les.dBp L&@<1L0Hta,les.log
Processing o,Xect type T'>L0G04P&RT.T'>L0.T'>L0
&R'-N"1%1: Ta,le Q/RQ.Q0=PL&900SQ e+ists. 'll dependent
Betadata and data Cill ,e sRipped due to ta,leGe+istsGaction
o7 sRip
Processing o,Xect type T'>L0G04P&RT.T'>L0.T'>L0G3'T'
Processing o,Xect type T'>L0G04P&RT.T'>L0.12304.12304
......
:ow %o Import One %able ,ac/ from a Dump (ile?
If you only want to import one table bac/ to the database, you can use a dump file that was created by full e-port, schema
e-port or a table e-port. %he following tutorial e-ercise shows you how to import the Cfyi6lin/sC table from a dump file created
by a schema e-port*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)s-lplus .nolog
SQL) connect /R.7yicenter
SQL) 3R&P T'>L0 7yiGlinRsI
Ta,le dropped.
SQL) e+itI
)iBpdp hr.7yicenter T'>L0SH7yiGlinRs 31R0CT&R9HhrGduBp
3:=P<1L0HscheBa.dBp L&@<1L0Hta,les.log
=aster ta,le Q/RQ.QS9SG1=P&RTGT'>L0G01Q loaded.unloaded
Starting Q/RQ.QS9SG1=P&RTGT'>L0G01Q: hr.** T'>L0SH7yiGlinRs
31R0CT&R9HhrGduBp 3:=P<1L0HscheBa.dBp L&@<1L0Hta,les.log
Processing o,Xect type SC/0='G04P&RT.T'>L0.T'>L0
Processing o,Xect type SC/0='G04P&RT.T'>L0.T'>L0G3'T'
. . iBported Q/RQ.Q<91GL12ASQ J.NW% A> K roCs
Processing o,Xect type SC/0='G04P&RT.T'>L0.C&2STR'12T.C&2...
Processing o,Xect type SC/0='G04P&RT.T'>L0.12304.ST'T1ST1...
Processing o,Xect type SC/0='G04P&RT.T'>L0.ST'T1ST1CS.T'>...
Uo, Q/RQ.QS9SG1=P&RTGT'>L0G01Q success7ully coBpleted.
What Are the Original -port and Import 2tilities?
Oracle original -port and Import utilities are standalone programs that provide you a simple way for you to transfer data
ob3ects between Oracle databases, even if they reside on platforms with different hardware and software configurations.
When you run -port against an Oracle database, ob3ects !such as tables" are e-tracted, followed by their related ob3ects
!such as inde-es, comments, and grants", if any. %he e-tracted data is written to an e-port dump file. %he Import utility reads
the ob3ect definitions and table data from the dump file.
An e-port file is an Oracle binary+format dump file that is typically located on dis/ or tape. %he dump files can be transferred
using (%' or physically transported !in the case of tape" to a different site. %he files can then be used with the Import utility
to transfer data between databases that are on systems not connected through a networ/. %he files can also be used as
bac/ups in addition to normal bac/up procedures.
-port and Import utilities are now being replaced by Data 'ump -port and Import utilities in Oracle AMg. ,ut you can still
use them.
:ow %o Invo/e the Original -port Import 2tilities?
If you really want to run the original e-port import utilities, you can still go to CbinC directory of the Oracle server path and run
the Ce-pC or CimpC command. %he tutorial e-ercise below tells you how to run the e-port and import utilities in help modes*
)cd *oracle+e*app*oracle*product*10.2.0*ser(er*>12
)e+p helpHy
9ou can let 0+port proBpt you 7or paraBeters ,y entering the
04P coBBand 7olloCed ,y your usernaBe.passCord:
0+aBple: 04P SC&TT.T1@0R
&r$ you can control hoC 0+port runs ,y entering the 04P
coBBand 7olloCed ,y (arious arguBents. To speci7y paraBeters$
you use:
<orBat: 04P A09;&R3H(alue or
A09;&R3H(alue1$(alue2$...$(alue2!
0+aBple: 04P SC&TT.T1@0R @R'2TSH9 T'>L0SH0=P$30PT$=@R!
or T'>L0SHT1:P1$T1:P2!$ i7 T1 is partitioned
ta,le
......
)iBp helpHy
......
What Is Open Database 9ommunication !OD,9"?
OD,9, Open Database 9ommunication, a standard A'I !application program interface" developed by <icrosoft for Windows
applications to communicate with database management systems.
Oracle offers OD,9 drivers to allow Windows applications to connect Oracle server through OD,9.
:ow %o Install Oracle OD,9 Drivers?
Oracle offers different OD,9 drivers for different versions of Oracle servers. When you install an Oracle server or a client
tools on your Windows system, the appropriate OD,9 driver will be installed for you automatically.
84
If you want to install a specific OD,9 driver, you need to go to
http*??www.oracle.com?technology?software?tech?windows?odbc?inde-.html. Download the right OD,9 driver and follow the
instructions to install it.
:ow %o (ind Out What Oracle OD,9 Drivers Are Installed?
%o find out what Oracle OD,9 drivers are installed on your Windows system, you can use the OD,9 manager to loo/ at
them*
• $o to 9ontrol 'anel.
• $o to Administrative %ools.
• 5un Data #ources !OD,9".
• $o to #ystem D#. tab.
• 9lic/ the Add button.
)ou should a list of all OD,9 drivers installed on your system. Oracle OD,9 drivers should be on the list. (or e-ample, your
installed OD,9 driver list could loo/ li/e the one the picture bellow with two Oracle OD,9 drivers, COracle in
Ora9lientAMg6homeAC and COracle in FC*

:ow 9an Windows Applications 9onnect to Oracle #ervers?
A Windows application can connect to an Oracle server directly, if it /nows how to use the Oracle %.# technology.
A Windows application can connect to an Oracle server indirectly through Windows OD,9 manager, becaused offers OD,9
drivers to support the OD,9 A'I. %he diagram below shows how <# Access can connect to an Oracle server through the
OD,9 driver*

:ow %o 9reate %ables for OD,9 9onnection %esting?
If you want to follow the tutorial e-ercises in the sections below, you need to create a user account and a table for OD,9
connection testing as shown here*
SQL) C&220CT systeB.retneciy7
Connected.
SQL) CR0'T0 :S0R 7yi 1302T1<103 >9 retneciy7 'CC&:2T :2L&CAI
:ser created.
SQL) @R'2T CR0'T0 S0SS1&2 T& 7yiI
@rant succeeded.
SQL) @R'2T CR0'T0 T'>L0 T& 7yiI
@rant succeeded.
SQL) 'LT0R :S0R 7yi 30<':LT T'>L0SP'C0 :S0RSI
:ser altered.
SQL) 'LT0R :S0R de( Q:&T' K= &2 :S0RSI
:ser altered.
SQL) connect 7yi.retneciy7I
Connected.
SQL) CR0'T0 T'>L0 de(G7a- id 2:=>0R!I
SQL) 12S0RT 12T& de(G7a- ?'L:0S N!I
SQL) 12S0RT 12T& de(G7a- ?'L:0S %!I
85
SQL) 12S0RT 12T& de(G7a- ?'L:0S W!I
:ow %o 9hec/ the Oracle %.# #ettings?
If you have installed an Oracle server or an Oracle client tool on your local system, the %.# is automatically installed with a
simple configuration file, tnsnames.ora, to define Oracle connect identifiers.
(or e-ample, if you have Oracle F server installed, you will have the tnsnames.ora located at
Noracle-eNappNoracleNproductNAM.;.MNserverN.%WO5DNAD<I.. It should contain a connect identifier called F defined as*
40 H
30SCR1PT1&2 H
'33R0SS H
PR&T&C&L H TCP!/&ST H localhost!
P&RT H 1%21!
!
C&220CTG3'T' H
S0R?0R H 3031C'T03!
S0R?1C0G2'=0 H 40!
!
!
:ow %o Define a Data #ource .ame !D#." in OD,9 <anager?
D#. !Data #ource .ame" is an OD,9 connection identifier for Windows applications. :ere is how you can define a D#. on
your Windows system*
• $o to 9ontrol 'anel.
• $o to Administrative %ools.
• 5un Data #ources !OD,9".
• $o to #ystem D#. tab.
• 9lic/ the Add button.
• #elect the COracle in FC driver.
• nter Data #ource .ame* ()I6D#..
• nter Description* ()Icenter D#. Oracle #etting.
• nter %.# #ervice .ame* F.
• 9lic/ the %est 9onnection button.
• nter 2ser .ame* fyi.
• nter 'assword* retneciyf.
• 9lic/ the OD button.
)ou should see a C9onnection successfulC as shown in the following picture*

:ow %o 9onnect <# Access to Oracle #ervers?
Once you got a D#. defined in the OD,9 manager that connects to an Oracle server, you can connect a normal <#
Access document to the Oracle server, and lin/ an Access table to Oracle table. %he tutorial below gives you a good
e-ample*
• #tart <# Access with a new database file.
• $o to (ile menu.
• #elect $et -ternal Data.
• #elect Import.... %he Import dialog bo- shows up.
• #elect (iles of type* OD,9 Database!". %he #elect Data #ource dialog bo- shows up.
• 9lic/ the <achine Data #ource tab. )ou should see the D#. name C()I6D#.C you defined earlier.
• #elect C()I6D#.C.
• nter 2ser .ame* fyi.
• nter 'assword* retneciyf.
86
)ou should see the Oracle OD,9 Driver 9onnect dialog bo- as shown in the picture below*

9lic/ the OD button to continue. )ou should see a list of tables available for you to import from the Oracle server as shown
in the picture below*

:ow %o 9onnect A#' 'ages to Oracle #ervers?
If you are running Windows II# Web server and serving A#' Web pages, you can get data from Oracle servers into your
A#' pages through OD,9 drivers. %o do this, you need to install the correct Oracle OD,9 driver and define a D#. on the
II# Web server.
%hen you can use ADOD, ob3ects to connect to the Oracle server over the OD,9 driver in your A#' pages. %he tutorial
e-ample below gives you a good e-ample*
TV
Set oConn H Ser(er.Create&,XectQ'3&3>.ConnectionQ!
oConn.&pen Q3S2H<91G3S2I:13H7yiIP;3Hretneciy7Q
Set oRS H oConn.0+ecuteQS0L0CT * <R&= de(G7a-Q!
Response.CriteQTp)3ata 7roB &racle ser(er (ia &3>C:Q!
Response.CriteQTpre)Q!
3o ;hile 2&T oRS.0&<
Response.;riteoRSQ13Q! S (,crl7!
oRS.=o(e2e+t
Loop
Response.CriteQT.pre)Q!
oRS.close
oConn.close
V)
http://dev.fyicenter.com/faq/oracle/
87