Oracle: SQL Plus Tutorial: Logging On/Off
Oracle: SQL Plus Tutorial: Logging On/Off
Tutorial
Under construction
For additional information, the following sites can be accessed:
Logging On/Off
In windows, find the Oracle group and click on SQL Plus. You will be prompted for your
UserID, password, hoststring. Enter the information indicated in class. If successful, you
will see the SQL> prompt.
Introduction
At the SQL prompt, you can begin typing any SQL command. Upon hitting return (i.e.,
enter key) the SQL prompt will change to line number prompts. When you are finished
typing a command, type / or RUN to execute the SQL command. Also, a semicolon at the
end of the SQL command will execute the command immediately after hitting return. In
addition to SQL commands, /, and RUN, other commands can be issued at the SQL prompt
(a semicolon does not have to follow the nonSQL commands).
DESCRIBE tablename--lists the attributes and their specifications of tablename
EDIT--Places you in an editor (Notepad). The buffer contents are put into the editor
GET filename--Retrieves a file and places it into the buffer
SAVE filename--Saves the buffer to the name file
CLEAR BUFFER--Clears the buffer
Line Editing
One way to change an SQL statement in the buffer is by using the line editor. The
following are a list of line edit commands.
LIST or L--Lists the contents of the buffer
LIST n or L n--Lists the contents of line number n in the buffer and makes the line
current
LIST * or L *--Lists the current line
LIST m n--Lists the range from m to n line
Append text or A text--Adds to the end of the current line (e.g., "A ," adds a comma
to the end of line
INPUT or I--Adds one or more lines after the current line so you can begin adding
the text.
CHANGE /text--Deletes text from the current line
CHANGE /oldtext/newtext--Replaces oldtext with newtext in the current line
DEL -- Deletes the current line
You can use Notepad to edit the contents of the buffer. There are two ways to enter
Notepad.
Use the menus Edit/editor/invoke. In addition here, you can use Edit/editor/define to
change the editor. You might check Edit/editor/define prior to editing a buffer to
make sure it is set up the way you would like.
Type Edit at the SQL Prompt
When you are finished editing, click on File/exit (in Notepad). Next, type GET buffername
at the SQL prompt. The buffer name will be listed a few lines up in SQL Plus upon
returning to SQL Plus. Type RUN, to execute the command.
Notepad can also be used to save the buffer contents. Use File/Save As to create a
permanent file.
Multiple SQL commands can be typed in Notepad. End each SQL command (except the
last one) with a semicolon. After exiting notepad, type Start buffername instead of GET
buffername to run all of the commands. Start filename can also be used to execute SQL
command that are stored in a file.
To send queries and their results to a file for later printing (e.g., turning in homework
assigments), the spool command is used. On the menu go to FILE/Spool to begin
spooling--it will ask for a file name. To quit spooling, go to FILE/Spool and then click on
end Spool.
Retrieving a File
To retrieve SQL command from a file use GET filename or START filename. GET
filename places the file into the buffer. START filename executes the commands in the file.
Creating Tables
Easy Format
Create Table tablename
(attribute name datatype constraint,
attribute name datatype constraint,
.
.
.)
An example table is
Create Table Patron
(PID NUMBER(6) Constraint pk_patron PRIMARY KEY,
Name VARCHAR2(30) Constraint nn_Name NOT NULL
Gender VARCHAR2(1))
The above table is called patron. It contains a PID that is the key, a name attribute and a
gender attribute. PID is defined to be a 6 digit integer. Name and gender are defined to be
character strings with 30 and 1 char. length respectively. Note that all constraints (e.g.,
Primary Key) must have a constraint name (e.g., pk_patron).
In the case where a table has a composite primary key, the constraint must be formed
differently. For example,
VARCHAR2(size)
Variable length character string having max. length of size
CHAR(size)
Fixed length character string with number of bytes equal to size
NUMBER(p,s)
Number having a precision p and s digits to the right of the decimal. If you leave off
p and s (e.g., NUMBER), then it is a floating point number.
LONG
Character data of variable length up to 2 gigabytes (cannot be a key)
DATE
A date field
RAW(size)
Raw binary data of length size. Max. size is 255 bytes
LONG RAW
Raw binary data up to 2 gigabytes (cannot be a key)
Constraint clause
This clause is part of the create or alter table commands. The format is:
More complex constraint clauses can also be added to the schema specification
Altering Tables
This command can be used to add, drop, and modify attributes and constraints. The format
is:
ALTER TABLE tablename
ADD attributename datatype constraint
ADD tableconstraint
MODIFY attributename datatype constraint
DROP PRIMARY KEY
DROP UNIQUE attributename (drops the unique constraint on
an attribute)
DROP CONSTRAINT constraintname
Dropping Tables
To remove a table from the database use
Updating data
Insert Command
Note that the default format for dates is day-month-year, where month is a three digit
character abbreviation. The date should be between single quotes (e.g., 20-JUL-95).
Delete Command
This command removes rows from a table. The format:
Update Command
UPDATE tablename
SET attributename = some expression (can be an equation)
Where condition
Note that you can have more than one set statement per UPDATE command.
UPDATE Checkout
SET DueDate = '10-Dec-95'
Where PID = 100
Data Querying
Select Query
The select query used in Oracle is standard SQL so you should look in your text book for
syntax and examples. The following sections illustrate some capabilities or syntax
variations in Oracle that are not in the text.
Outer Joins
Unlike Outer Joins in the text, outer joins in Oracle are declared in the Where clause
through the use of the (+) symbol. An example follows:
Select b.title, c.pid
From Book b, Checkout c
Where b.callno = (+) c.callno
The =(+) says to join the book callno with the checkout callno. But if there is no checkout
callno to match book callno, use a null in the checkout attributes. Notice that the above
example would be considered a left outer join in our book even though the (+) is to the
right of the equal sign.
Oracle supports union, intersect and minus set operations on two select statements.
The System Catalog
Oracle keeps the names of your tables and other information in a meta table called
user_tables. To see this information, use a simple select statement. The following statement
list the tablenames:
Select TABLE_NAME from USER_TABLES;
To find out all the information on your tables, type:
Select * from USER_TABLES;
Group Functions
Mathematical Functions
ABS(n)--Absolute value of n
CEIL(n)--Smallest integer greater than or equal to n
EXP(n)--Returns e raised to the nth power
LN(n)--The natural log of n
LOG(m,n)--Returns the logarithm base m of n
MOD(m,n)--The remainder of m divided by n
POWER(m,n)--m raised to the nth power
ROUND(n[,m])--n rounded to m places right of the decimal. m omitted equals 0
places
SIGN(n)-- Returns -1 if n<0, 0 if n=0 and 1 if n>0
SQRT(n)--Square root
TRUNC(n[,m])--n truncated to m decimal places
String Functions
Date Functions