Introduction To Oracle: SQL Plus
Introduction To Oracle: SQL Plus
SQL*Plus
SQL*Plus is Oracle’s command-line interpreter. You may launch SQL*Plus
by issuing the sqlplus command in UNIX or using the ‘start’ menu in
Windows. In the ‘start’ menu, SQL*Plus is listed under programs > oracle
> application development > SQL Plus.
You will be prompted for your username and password, which should both
initially be set to your username. You will learn at a later stage how to
change your password. The last piece of information required by SQL*Plus
is the name (called host string ) of the database you want to use: type in
mcsora.
You are now connected to a shared database, on which you have an account
(called a schema ). You do not have to create a new database as you would
do with Postgres.
2003
c Jean-Marc Rosengard 1
Type description Oracle SQL Postgres SQL
variable-length char. string VARCHAR2(l)a VARCHAR(l)
fixed-length char. string CHAR(l) CHAR(l)
number NUMBER(p,s)b NUMERIC(p,s)
currency NUMBER(10,2) MONEY
date DATE DATE
a
length.
b
position, scale.
You should now be able to create a few tables and populate them.
Note the use of the SYSDATE function that returns the system’s current date
in the DEFAULT clause above. The ‘/’ character terminates an SQL state-
ment and submits it to SQL*Plus.
You should be already familiar with the syntax of the primary key and ref-
erential integrity constraints. They function in Oracle in a similar fashion
as in Postgres. pk books and fk books booksrev are constraint names.
Now check the schema of the tables you have just created (c.f. \dt in Post-
gres) using the desc <table name> command.
Next, we want to insert some data into books and books reviews:
2003
c Jean-Marc Rosengard 2
INSERT INTO books VALUES
(
’The Importance of Being Earnest’,
’Oscar Wilde’, -- this is a comment
9876543210,
’14-FEB-1895’
)
/
Editing Commands
Editing SQL*Plus’ buffer. As you may already have experienced, you
cannot recall statements after they have been submitted to SQL*Plus. The
ed command allows you to edit the SQL*Plus buffer in the system’s default
editor. After saving your changes, submit the statement with a ‘/’. Be
aware that only the last statement submitted to SQL*Plus may be edited.
2. save the file with the .sql extension in your home directory (e.g.
myfile.sql)—make sure that you get the correct extension, as some
editors will attempt to append a .txt extension.
Before starting the next section, you should practise creating and populating
some more tables.
2003
c Jean-Marc Rosengard 3
More Oracle SQL
You are now armed to attempt some more complex SQL expressions.
The following code creates a SEQUENCE that we will then use to insert some
more values in the books table.
2003
c Jean-Marc Rosengard 4
Apart from the the Oracle peculiarities we have already discussed, you can
re-use most of your knowledge of SQL. You may want for example to exper-
iment with the UPDATE and DELETE statements.
Introduction to Transactions
Transaction management is a broad topic to which you have been intro-
duced in the database lectures. You should refer to your notes for a more
detailed coverage of the subject, as we will here just remind a few points. A
transaction is a logical unit of work , that could be for example the placement
of an order. On completion, a transaction needs to be either confirmed —
making all the changes permanent—or cancelled —returning the database
into the state it was before starting the transaction.
These two actions are performed in SQL by issuing one of the two commands
COMMIT or ROLLBACK.
To experiment with transactions, you will need to work in pairs (say Alice
and Bob) and allow the other student to read the data in your books table.
So Alice will need to enter:
GRANT SELECT ON books TO bob
/
and Bob to enter:
GRANT SELECT ON books TO alice
/
Now Alice should enter some data in her books table. Bob can then
attempt to view the newly inserted data by typing:
SELECT * FROM alice.books
/
Note how you can prefix the table name with its schema to reference
other students’ tables. Can Bob view the changes Alice has made? What
happens if Alice COMMITs the transaction? Try also with ROLLBACK.
Try to relate your observations with your understanding of transactions.
2003
c Jean-Marc Rosengard 5
References
You can copy & paste the following URIs (note that you will need a user-
name/password to access Oracle’s web site. You can use data@base.com/database):
2003
c Jean-Marc Rosengard 6