chapter 2 sql plus

4
2 SQL*Plus Introduction SQL*Plus is the interactive (low-level) user interface to the Oracle database management sstem! "#icall$ SQL*Plus is used to issue ad-hoc %ueries and to view the %uer result on the screen! Some of the features of SQL*Plus are& ' built-in command line editor can be used to edit (incorrect) SQL %ueries! Instead of this line editor an editor installed on the com#uter can be invoed! ' "here are numerous commands to format the out#ut of a %uer! ' SQL*Plus #rovides an online-hel#! ' Quer results can be stored in files which then can be #rinted! Queries that are fre%uentl issued can be saved to a file and invoed later! Queries can be #arameteried such that it is #ossible to invoe a saved %uer with a #arameter! +inimal ,ser uide .efore ou start SQL*Plus mae sure that the following ,/I0 shell variables are #ro#erl set (shell variables can be checed using the env command$ e!g!$ env 1 gre# O3L4)& ' O3L4 5O+4$ e!g!$ O3L4 5O+467usr7#g7oracle789: ' O3L4 SI;$ e!g$ O3L4 SI;6#rod In order to invoe SQL*Plus from a ,/I0 shell$ the command s%l#lus has to be issued! SQL*Plus then dis#las some information about the #roduct$ and #rom#ts ou for our user name and #assword for the Oracle sstem! gert(catbert)<:& s%l#lus SQL*Plus& elease 9!9!:!=!> - Production on Sun ;ec 2= >?&>@&<2 >??A 3o#right (c) Oracle 3or#oration >?8?$ >??@! ll rights reserved! 4nter user-name& scott 4nter #assword& 3onnected to& Oracle8 Server elease 8!9!:!=!> - Production elease Bith the distributed o#tion PL7SQL elease 2!9!:!=!= - Production SQLC 2= SQLC is the #rom#t ou get when ou are connected to the Oracle database sstem! In SQL*Plus ou can divide a s tatement into se#arate lines$ each continuing line is indicated b a #rom#t such 2C$ 9C etc! n SQL statement must alwas be terminated b a semicolon (D)! In addition to the SQL statements discussed in the #revious section$ SQL*Plus #rovides some s#ecial SQL*Plus commands! "hese commands need not be terminated b

Upload: mhussain

Post on 03-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

8/12/2019 Chapter 2 SQL Plus

http://slidepdf.com/reader/full/chapter-2-sql-plus 1/4

2 SQL*PlusIntroductionSQL*Plus is the interactive (low-level) user interface to the Oracledatabase management

sstem! "#icall$ SQL*Plus is used to issue ad-hoc %ueries and to viewthe %uer result onthe screen! Some of the features of SQL*Plus are&' built-in command line editor can be used to edit (incorrect) SQL%ueries! Instead of this line editor an editor installed on the com#uter can be invoed!' "here are numerous commands to format the out#ut of a %uer!' SQL*Plus #rovides an online-hel#!' Quer results can be stored in files which then can be #rinted!Queries that are fre%uentl issued can be saved to a file and invoedlater! Queries can be

#arameteried such that it is #ossible to invoe a saved %uer with a#arameter!

+inimal ,ser uide.efore ou start SQL*Plus mae sure that the following ,/I0 shellvariables are #ro#erl set(shell variables can be checed using the env command$ e!g!$ env 1 gre#O3L4)&' O3L4 5O+4$ e!g!$ O3L4 5O+467usr7#g7oracle789:' O3L4 SI;$ e!g$ O3L4 SI;6#rodIn order to invoe SQL*Plus from a ,/I0 shell$ the command s%l#lus has to

be issued!SQL*Plus then dis#las some information about the #roduct$ and #rom#tsou for our username and #assword for the Oracle sstem!gert(catbert)<:& s%l#lusSQL*Plus& elease 9!9!:!=!> - Production on Sun ;ec 2= >?&>@&<2 >??A3o#right (c) Oracle 3or#oration >?8?$ >??@! ll rights reserved!4nter user-name& scott4nter #assword&3onnected to&Oracle8 Server elease 8!9!:!=!> - Production elease

Bith the distributed o#tionPL7SQL elease 2!9!:!=!= - ProductionSQLC2=SQLC is the #rom#t ou get when ou are connected to the Oracledatabase sstem! InSQL*Plus ou can divide a statement into se#arate lines$ each continuingline is indicated ba #rom#t such 2C$ 9C etc! n SQL statement must alwas be terminatedb a semicolon (D)!In addition to the SQL statements discussed in the #revious section$SQL*Plus #rovides somes#ecial SQL*Plus commands! "hese commands need not be terminated b

8/12/2019 Chapter 2 SQL Plus

http://slidepdf.com/reader/full/chapter-2-sql-plus 2/4

a semicolon! ,##erand lower case letters are onl im#ortant for string com#arisons! n SQL%uer can alwas beinterru#ted b using E3ontrolC3! "o eFit SQL*Plus ou can either t#eeFit or %uit!

4ditor 3ommands "he most recentl issued SQL statement is stored in the SQL buffer$inde#endent of whether thestatement has a correct sntaF or not! Gou can edit the buffer using thefollowing commands&' lHist lists all lines in the SQL buffer and sets the current line (maredwith an JJ) tothe last line in the buffer!' lEnumberC sets the actual line to EnumberC' cHhange7Eold stringC7Enew stringC re#laces the first occurrence of

Eold stringC bEnew stringC (for the actual line)' aH##endEstringC a##ends EstringC to the current line' del deletes the current line' rHun eFecutes the current buffer contents' getEfileC reads the data from the file EfileC into the buffer' saveEfileC writes the current buffer into the file EfileC' edit invoes an editor and loads the current buffer into the editor! ftereFiting theeditor the modified SQL statement is stored in the buffer and can beeFecuted (command

r)! "he editor can be defined in the SQL*Plus shell b t#ing the commanddefine editor 6EnameC$ where EnameC can be an editor such as emacs$ vi$ Koe$ or

 Kove!

SQL*Plus 5el# Sstem and Other ,seful 3ommands' "o get the online hel# in SQL*Plus Kust t#e hel# EcommandC$ or Kusthel# to getinformation about how to use the hel# command! In Oracle ersion 8 onecan get the

com#lete list of #ossible commands b t#ing hel# command!' "o change the #assword$ in Oracle ersion 8 the commandalter user EuserC identified b Enew #asswordCDis used! In Oracle ersion A the command #assw EuserC #rom#ts the userfor theold7new #assword!' "he command descHribe EtableC lists all columns of the given tabletogether with theirdata t#es and information about whether null values are allowed or not!' Gou can invoe a ,/I0 command from the SQL*Plus shell b using hostE,/I0 commandC!

Mor eFam#le$ host ls -la *!s%l lists all SQL files in the current director!2>

8/12/2019 Chapter 2 SQL Plus

http://slidepdf.com/reader/full/chapter-2-sql-plus 3/4

' Gou can log our SQL*Plus session and thus %ueries and %uer results busing thecommand s#ool EfileC! ll information dis#laed on screen is then storedin EfileCwhich automaticall gets the eFtension !lst! "he command s#ool off turns

s#ooling off!' "he command co# can be used to co# a com#lete table! Mor eFam#le$the commandco# from scott7tiger create 4+PL using select from 4+PDco#ies the table 4+P of the user scott with #assword tiger into the relation4+PL! "herelation 4+P is automaticall created and its structure is derived based onthe attributeslisted in the select clause!' SQL commands saved in a file EnameC!s%l can be loaded into SQL*Plusand eFecutedusing the command NEnameC!' 3omments are introduced b the clause remHar (onl allowed betweenSQL statements)$or - - (allowed within SQL statements)!

Mormatting the Out#utSQL*Plus #rovides numerous commands to format %uer results and tobuild sim#le re#orts!Mor this$ format variables are set and these settings are onl valid duringthe SQL*Plus session!

 "he get lost after terminating SQL*Plus! It is$ however$ #ossible to save

settings in a file namedlogin!s%l in our home director! 4ach time ou invoe SQL*Plus this file isautomaticallloaded!

 "he command column Ecolumn nameC Eo#tion >C Eo#tion 2C ! ! ! isused to format columnsof our %uer result! "he most fre%uentl used o#tions are&' format EnC Mor al#hanumeric data$ this o#tion sets the length ofEcolumn nameC toEnC! Mor columns having the data t#e number$ the format command canbe used tos#ecif the format before and after the decimal #oint! Mor eFam#le$ format??$???!??s#ecifies that if a value has more than three digits in front of the decimal#oint$ digits arese#arated b a colon$ and onl two digits are dis#laed after the decimal#oint!' "he o#tion heading EteFtC relabels Ecolumn nameC and gives it a newheading!' null EteFtC is used to s#ecif the out#ut of null values (t#icall$ nullvalues are not

dis#laed)!' column Ecolumn nameC clear deletes the format definitions for

8/12/2019 Chapter 2 SQL Plus

http://slidepdf.com/reader/full/chapter-2-sql-plus 4/4

Ecolumn nameC! "he command set linesie EnumberC can be used to set the maFimumlength of a singleline that can be dis#laed on screen! set #agesie EnumberC sets thetotal number of lines

SQL*Plus dis#las before #rinting the column names and headings$res#ectivel$ of the selectedrows!Several other formatting features can be enabled b setting SQL*Plusvariables! "he commandshow all dis#las all variables and their current values! "o set a variable$t#e set EvariableCEvalueC! Mor eFam#le$ set timing on causes SQL*Plus to dis#la timingstatistics for eachSQL command that is eFecuted! set #ause on HEteFtC maes SQL*Pluswait for ou to #resseturn after the number of lines defined b set #agesie has beendis#laed! EteFtC is themessage SQL*Plus will dis#la at the bottom of the screen as it waits forou to hit eturn!22