C
Using SQL*Plus
Copyright © Oracle Corporation, 2001. All rights
Objectives
Objectives
After
After completing
completing this
this appendix,
appendix, you
you should
should be
be able
able to
to
do
do the
the following:
following:
•• Log
Log in
in to
to SQL*Plus
SQL*Plus
•• Edit
Edit SQL
SQL commands
commands
•• Format
Format output
output using
using SQL*Plus
SQL*Plus commands
commands
•• Interact
Interact with
with script
script files
files
C-2 Copyright © Oracle Corporation, 2001. All rights
SQL
SQL and
and SQL*Plus
SQL*Plus Interaction
Interaction
SQL statements
Server
SQL*Plus
Query results
Buffer
SQL
scripts
C-3 Copyright © Oracle Corporation, 2001. All rights
SQL
SQL Statements
Statements versus
versus SQL*Plus
SQL*Plus
Commands
Commands
SQL SQL*Plus
• A language • An environment
• ANSI standard • Oracle proprietary
• Keywords cannot be • Keywords can be
abbreviated abbreviated
• Statements manipulate • Commands do not
data and table allow manipulation of
definitions in the values in the database
database
SQL SQL SQL*Plus SQL*Plus
statements buffer commands buffer
C-4 Copyright © Oracle Corporation, 2001. All rights
Overview
Overview of
of SQL*Plus
SQL*Plus
•• Log
Log in
in to
to SQL*Plus.
SQL*Plus.
•• Describe
Describe the
the table
table structure.
structure.
•• Edit
Edit your
your SQL
SQL statement.
statement.
•• Execute
Execute SQL
SQL from
from SQL*Plus.
SQL*Plus.
•• Save
Save SQL
SQL statements
statements toto files
files and
and append
append SQL
SQL
statements
statements to
to files.
files.
•• Execute
Execute saved
saved files.
files.
•• Load
Load commands
commands from
from file
file to
to buffer
buffer
to
to edit.
edit.
C-5 Copyright © Oracle Corporation, 2001. All rights
Logging
Logging In
In to
to SQL*Plus
SQL*Plus
• From a Windows environment:
• From a command line:
sqlplus [username[/password
[@database]]]
C-6 Copyright © Oracle Corporation, 2001. All rights
Displaying
Displaying Table
Table Structure
Structure
Use
Use the
the SQL*Plus DESCRIBE command
SQL*Plus DESCRIBE command to
to display
display the
the
structure
structure of
of aa table.
table.
DESC[RIBE] tablename
C-7 Copyright © Oracle Corporation, 2001. All rights
Displaying
Displaying Table
Table Structure
Structure
SQL> DESCRIBE departments
Name Null? Type
----------------------- -------- ------------
DEPARTMENT_ID NOT NULL NUMBER(4)
DEPARTMENT_NAME NOT NULL VARCHAR2(30)
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)
C-8 Copyright © Oracle Corporation, 2001. All rights
SQL*Plus
SQL*Plus Editing
Editing Commands
Commands
•• A[PPEND]
A[PPEND] text
text
•• C[HANGE]
C[HANGE] // old
old // new
new
•• C[HANGE]
C[HANGE] // text
text //
•• CL[EAR]
CL[EAR] BUFF[ER]
BUFF[ER]
•• DEL
DEL
•• DEL
DEL nn
•• DEL
DEL mm nn
C-9 Copyright © Oracle Corporation, 2001. All rights
SQL*Plus
SQL*Plus Editing
Editing Commands
Commands
•• I[NPUT]
I[NPUT]
•• I[NPUT]
I[NPUT] text
text
•• L[IST]
L[IST]
•• L[IST]
L[IST] nn
•• L[IST]
L[IST] mm nn
•• R[UN]
R[UN]
•• nn
•• nn text
text
•• 00 text
text
C-10 Copyright © Oracle Corporation, 2001. All rights
Using
Using LIST, n, and
LIST, n, and APPEND
APPEND
SQL> LIST
1 SELECT last_name
2* FROM employees
SQL> 1
1* SELECT last_name
SQL> A , job_id
1* SELECT last_name, job_id
SQL> L
1 SELECT last_name, job_id
2* FROM employees
C-11 Copyright © Oracle Corporation, 2001. All rights
Using
Using the CHANGE Command
the CHANGE Command
SQL> L
1* SELECT * from employees
SQL> c/employees/departments
1* SELECT * from departments
SQL> L
1* SELECT * from departments
C-12 Copyright © Oracle Corporation, 2001. All rights
SQL*Plus
SQL*Plus File
File Commands
Commands
•• SAVE
SAVE filename
filename
•• GET
GET filename
filename
•• START
START filename
filename
•• @@ filename
filename
•• EDIT
EDIT filename
filename
•• SPOOL
SPOOL filename
filename
•• EXIT
EXIT
C-13 Copyright © Oracle Corporation, 2001. All rights
Using
Using the SAVE and
the SAVE START Commands
and START Commands
SQL> L
1 SELECT last_name, manager_id, department_id
2* FROM employees
SQL> SAVE my_query
Created file my_query
SQL> START my_query
LAST_NAME MANAGER_ID DEPARTMENT_ID
------------------------- ---------- -------------
King 90
Kochhar 100 90
...
20 rows selected.
C-14 Copyright © Oracle Corporation, 2001. All rights
Summary
Summary
Use
Use SQL*Plus
SQL*Plus as
as an
an environment
environment to:
to:
•• Execute
Execute SQL
SQL statements
statements
•• Edit
Edit SQL
SQL statements
statements
•• Format
Format output
output
•• Interact
Interact with
with script
script files
files
C-15 Copyright © Oracle Corporation, 2001. All rights