0% found this document useful (0 votes)
65 views8 pages

Oracle: SQL Plus Tutorial: Logging On/Off

This document provides an overview of using SQL*Plus and performing basic SQL operations. It discusses how to log into SQL*Plus, describes common SQL*Plus commands like DESCRIBE and EDIT, and how to execute SQL statements. It also summarizes how to create and modify database tables, insert, update, and delete data, and do basic queries with functions like SELECT, JOIN, and GROUP BY.

Uploaded by

Pedro Pineda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

Oracle: SQL Plus Tutorial: Logging On/Off

This document provides an overview of using SQL*Plus and performing basic SQL operations. It discusses how to log into SQL*Plus, describes common SQL*Plus commands like DESCRIBE and EDIT, and how to execute SQL statements. It also summarizes how to create and modify database tables, insert, update, and delete data, and do basic queries with functions like SELECT, JOIN, and GROUP BY.

Uploaded by

Pedro Pineda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Oracle: SQL*Plus

Tutorial
Under construction
For additional information, the following sites can be accessed:

Yahoo's Oracle index section identifies a number of Oracle related sites


Oracle faq contains several hints on Oracles products, including SQL Plus and Oracle SQL
Oracle Underground contains many Oracle related links.

[Logging on/off |SQL Plus Commands |Data Definition |Data Manipulation ]

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.

SQL Plus Commands


[Introduction |Line Editing |Using Notepad for editing |Sending results to a file |Retrieving
a file ]

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

Using Notepad for Editing

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.

Sending the Results to 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.

Last modified: December 11, 2000


Dirk Baldwin, MIS, UW-Parkside, [email protected]

Oracle Data Definition


[Creating Tables |Altering Tables |Removing Tables]

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,

Create table Request


( CallNo VARCHAR2(10) Constraint fk_callno References Book
(CallNo),
PID NUMBER(6) Constraint fk_pid References Patron (PID),
RDate DATE,
Constraint pk_Request PRIMARY KEY (CallNo, PID))
Notice that Constraints are placed after the attributes. The References constraint says that
the PID in Request must match a PID in patron (i.e. Foreign Key). The data type of RDate
is a special data type called DATE.

More complex table specifications can also be created.


Legal Data Types

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:

CONSTRAINT constraintname the constraint specifications

The following are some typical constraint specifications:

 PRIMARY KEY--Attribute is unique, not null and indexed


 UNIQUE--Attribute is unique and indexed but not the key
 NOT NULL--Attrbute value cannot be left blank
 REFERENCES--Foreign key constraint. The attribute must match a value of a
specified attribute

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

ALTER TABLE Patron


DROP CONSTRAINT nn_name
It does not seem to be possible to change the name of an attribute or remove an attribute
from a table.

Dropping Tables
To remove a table from the database use

DROP TABLE tablename

Last modified: September 1, 1998


Dirk Baldwin, MIS, UW-Parkside, [email protected]

SQL Data Manipulation


[Updating Data | Querying Data]

Updating data
Insert Command

This command is used to add rows to a table. The format:

INSERT INTO table


Values (list of values separated by commas).

INSERT INTO table


Select statement that retrieves values from another table

INSERT INTO Patron


Values (100, 'Marsha', 'F')

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:

DELETE FROM tablename


Where condition

DELETE FROM Patron


Where PID = 100

Update Command

This command changes data in one or more rows

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.

Special Set Operators

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

Oracle supports the following group functions in a SQL query:


 AVG -- average
 COUNT
 MAX -- Returns the maximum value
 MIN -- Returns the minimum value
 STDDEV -- Standard Deviation
 SUM -- The total of some column
 VARIANCE

All of the above can be used with the DISTINCT option.

Other Useful 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

 CHR(n)--Returns the character with the binary equivalent to n


 CONCAT(char1, char2)--char1 concatenated with char2
 INITCAP(char)--Returns char with first letter in uppercase
 LOWER(char)--Returns char with all letters in lowercase
 LTRIM(char[,set]--Removes characters from the left of char, with initial characters
removed up to the first character not in set
 NLS_INITCAP(char)--First letter of each word in char is in uppercase
 NLS_LOWER(char)--All letters in lower case for every word
 NLS_UPPER(char)--All letters in lower case
 REPLACE(char,search_string,replacement_string)
 RTRIM(char [,set]--Similar to LTRIM, except trims on right
 SUBSTR(char,m [,n])--Portion of char, beginning at character m, n characters long.
m and n are integers. If n left off it returns all characters to end of char.
 UPPER(char)--char with all letters in upper case

String to Number Functions

 ASCII(char)--decimal representatin of char


 INSTR(char1,char2, n, m)--The position of the mth char2 in char1 beggining from
nth position.
 LENGTH(char)--The length of char

Date Functions

 ADD_MONTHS(d,n)--The date d plus n months


 MONTHS_BETWEEN(d1,d2)
 SYSDATE--Returns the current date and time

Last modified: September 1, 1998


Dirk Baldwin, MIS, UW-Parkside, [email protected]

You might also like