Quick Oracle User Guide
Quick Oracle User Guide
[Database Management Systems (3rd Edition)] [WWW Resources] [Material for the Third Edition] [Material for the Firs
Oracle Guide
This Guide will introduce you to using Oracle for several basic tasks, as well as how to configure the Oracle environment and
use special features of its user interface. This guide is divided into the following sections:
Getting Connected
Tables
Creating Tables
Observing Table Information
Altering Tables
Deleting Tables
Sample Session
Data
Inserting Data
Querying Data
Altering Data
Deleting Data
Sample Session
Constraints
Adding Constraints
Observing Constraints
Using Constraints
Deleting Constraints
Sample Session
Using SQL*Plus
Ending Your Session
Changing Your Password
Editing Commands
Committing Data
Logging Sessions
Using Special Characters
Loading Commands From A File
Running Commands At Login
Changing Date Formatting
Changing Output Formatting
Getting Additional Help
Sample Session
When you are done, you may go back to the Welcome Page. You can also register for free and access the online Oracle
documentation at technet.oracle.com
Getting Connected
In order to interact with Oracle you should use the SQL*Plus Application. This application is available on both the windows
and Unix platforms.
On Windows go to the Start Menu, then select Start >Programs > Oracle - DBBOOK_HOME > Application
Development > SQL * Plus. Use your installation name in place of DBBOOK_HOME.
1 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
On Unix you can type sqlplus from a Unix Shell. Your instructor should have provided information on how to
configure your account and login to Unix.
You will be prompted for your username, connect string, and password. If you are using Oracle on windows with the CD
accompanying the text, then you may leave the connect string blank. On Unix the connect string will follow your username
and the @ symbol in the format username@connectstring.
Note: If you have installed Oracle on Windows, you may use it to connect to your Unix based server. In order to do this
copy the entries from the tnsnames.ora in the $ORACLE_HOME/network/admin directory on the Unix system to the
tnsnames.ora file on the Windows machine. Then, use the connect string as you would in Unix. There is also a version of
SQL*Plus on windows that works in command line mode, which is identical to the Unix version.
Sample Session
student|computer-one|~|[1]% sqlplus
Connected to:
Personal Oracle9i Release 9.0.1.1.1 - Production
With the Partitioning option
JServer Release 9.0.1.1.1 - Production
SQL>
Back to top
Tables
Creating Tables
Oracle data is stored in tables which represent your fundamental schema design. You create them with the create table
command:
where tablename is the name of your table, columnname is the name of your column and type is the type as specified in the
table below. There are additional types available, however these are the most common. Give some thought as to the order of
columns as it can effect the default order they are displayed and data is entered.
Type Description
variable length string up to n with maximum 2000 characters. Specified as
varchar2(n) 'characters' where characters
are the characters in the string.
date holds a date. By default they are specified as day-month-year as in '12-DEC-1990'
2 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
Back to top
The easiest way to get information about a table is with the DESCRIBE command:
DESCRIBE tablename
Back to top
Altering Tables
Back to top
Deleting Tables
To delete a table use the following:
To Drop all your tables you can use the following in a script:
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET MARKUP HTML OFF
SET ESCAPE \
SPOOL DELETEME.SQL
select 'drop table ', table_name, 'cascade constraints \;' from user_tables;
SPOOL OFF
@DELETEME
Back to top
Sample Session
SQL> create table sign_sales(Color varchar2(30),date_sold date,
2 price_each number);
Table created.
3 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
Table altered.
Table altered.
Table altered.
Table dropped.
Back to top
Data
Inserting Data
Working with data is fundamental to database systems. It is possible to load data with SQL. It can also be done using the
Oracle bulk loader. To load values in SQL*Plus use the following syntax:
where somevalue is a value to insert and tablename is the name of the table. Values are inserted in the order of the columns
of the table. The first value is inserted into the first column, etc.
Back to top
Querying Data
Querying data in SQL is one of a database systems major tasks, and can not be fully described here. In its most basic form a
query consists of the SELECT statement:
Where columnname is the name of a column or * for all columns and tablename is the name of the table.
Back to top
Altering Data
You can alter data with the update expression. The optional where clause limits the rows effected. The set clause describes
the change as follows:
update tablename set columnname=expression where whereclause;
Back to top
4 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
Deleting Data
You can delete data with the delete expression. The optional where clause limits the rows effected.
Back to top
Sample Session
SQL> insert into signs values(19.95,'White','Rectangle','Park Somewhere Else');
1 row created.
SQL>
SQL> select * from signs;
SQL>
SQL> update signs set price_each = 4.00 where price_each < 20;
1 row updated.
SQL>
SQL> delete from signs ;
1 row deleted.
SQL>
Back to top
Constraints
Adding Constraints
In order to finish realizing a schema in Oracle, it is necessary to add the required constraints. Oracle supports several types
of constraints, to enforce complex rules may require the use of active database features such as triggers. In this section the
examples are limited to key constraints.
Constraints can be added to new tables or after table creation. To add primary key constraints to a new table specify the
primary key after the columns similar to the following:
create table tablename ( columnname type, columnname type ..., primary key(keycolumn,keycolumn,...);
Foreign keys should refer to unique tuples. To create a new table with foreign keys in addition use:
create table tablename ( columnname type, columnname type ..., primary key(keycolumn,keycolumn,...),
foreign key(foreignkeycolumn,foreignkeycolumn,...) references foreigntable,
foreign key(foreignkeycolumn,foreignkeycolumn,...) references foreigntable,...);
5 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
where foreignkeycolumn is the column of the foreign key in table foreigntable. It must match a column in the current table.
You can also add constraints with the alter table command where tablename is the name of the table and tableconstraint is
the constraint definition:
Back to top
Observing Constraints
Often you may wish to see what constraints exist on a table. The view USER_CONSTRAINTS is maintained by Oracle and
has information about constraints. Some interesting columns include, the CONSTRAINT_NAME, CONSTRAINT_TYPE
where P refers to primary key, and R is a foreign key constraints, the TABLE_NAME on which it is defined, and the
R_CONSTRAINT_NAME for foreign key constraints. Type DESCRIBE USER_CONSTRAINTS for more informaion.
Once you know the CONSTRAINT_NAME, you can use the USER_CONS_COLUMNS view to find the COLUMN_NAME
and POSITION for that CONSTRAINT_NAME. You can query these views as you would any other table:
Back to top
Using Constraints
At times you may need to create constraints that are interdependent, such as two tables that refer to each others primary key
in a foreign key constraint. One way to deal with this problem is to defer constraints. They constraint must be made
deferrable and the constraints mode set to deferred. You can substitute the names of specific constraints or use the all
keyword. They will be checked at the end of the transaction instead of after each insert. To enable them make the mode
immediate:
Back to top
Deleting Constraints
Constraints can be deleted with the drop constraint command. where tablename is the table name and someconstraint is the
constraint name:
Back to top
Sample Session
SQL> create table bids( bid_id varchar2(10), bidder_id varchar2(10), item_id varchar2(10),
2 bid_amount number, primary key(bid_id), foreign key ( item_id ) references
3 auction_items, foreign key (bidder_id) references members(member_id) deferrable);
Table created.
SQL>
SQL> select constraint_name, constraint_type from user_constraints where
2 table_name='BIDS';
6 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
CONSTRAINT_NAME C
------------------------------ -
SYS_C001400 P
SYS_C001401 R
SYS_C001401 R
SQL>
SQL> alter table students drop constraint SYS_C001400;
Table altered.
Table altered.
Constraint set.
Back to top
Using SQL*Plus
SQL*Plus provides an interactive command line interface to Oracle. In it you type various commands to manipulate your
data. It has many built in features.
Back to top
quit
In the windows version you can select Exit from the file menu.
Back to top
passw
Back to top
Editing Commands
Commands in Oracle are stored in a buffer until ended with a semicolon ; and are also accessible immediately after
execution. They may use several lines. If you make a mistake it is easier to edit your command then type it all over. You can
use the edit command to edit a file, or select Edit >Editor > Invoke Editor in Windows.
edit
This will edit the command in the buffer. You may also specify a file as a parameter. By default the editors are vi in Unix
7 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
and notepad on Windows. You may change the editor where editorname is you editor by using the Define Editor menu in
Windows or:
You may execute the command in the buffer by either the command RUN or a single slash (/). You can use GET and SAV to
retrieve or save the buffer to a file. You can also change a single line with C to replace the old text with the new.
C/old/new
Back to top
Committing Data
Whenever you have finished a series of changes and you want to insure they are part of the database you should commit the
transaction. This will make the changes visible to other database sessions. type:
commit;
It will restore data to the last commit. Some commands automatically result in commit, such as creating a table.
Back to top
Logging Sessions
SQL*Plus can record your commands and output to a file. This is useful for class assignments that ask for a log of your
session, or in automated self generating scripts. To do this type:
spool filename
where filename is the name of the file to save the output. In windows you can use the File > Spool >Spool File Command.
To stop recording to a file select File >Spool> Spool Off or type
spool off
Back to top
Back to top
@filename.sql
8 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
where filename.sql contains the commands to run. The File > Open menu can also be used.
Back to top
Back to top
String Meaning
YYYY Year
MM Month
DD Day
HH Hour
HH24 Hour (24 hour clock)
MI Minuite
SS Second
Back to top
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
Back to top
help command
9 of 10 2/10/2011 2:10 PM
Quick Oracle User Guide http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/Oracle/user_g...
where command is the command you need help with. To obtain a list of commands with help available type:
help index
Back to top
Sample Session
SQL> spool answer.txt
SQL> DEFINE _EDITOR= pico
SQL> spool off
SQL> commit;
Commit complete.
SQL> c /junk/user_users
1* select username from user_users
SQL> /
USERNAME
------------------------------
STUDENTNAME
SQL> quit
Disconnected from Personal Oracle9i Release 9.0.1.1.1 - Production
With the Partitioning option
JServer Release 9.0.1.1.1 - Production
student|computer-one|~|[4]%
Back to top
10 of 10 2/10/2011 2:10 PM