PL/SQL. in This Tutorial, You Will Interact With Oracle Database, Thru SQL Plus, by Issuing A
PL/SQL. in This Tutorial, You Will Interact With Oracle Database, Thru SQL Plus, by Issuing A
Oracle is one the most popular Relational Database Management System (RDBMS). Some other
famous RDBMS includes Microsoft SQL Server, Sybase, MySQL, PostgreSQL, etc.
Essentially, all the aforementioned RDBMS employs Structural Query Language (SQL) as their
query interface. Users usually issue their queries by SQL through a "client". Different RDBMS
offer different forms of clients. For example, MS SQL Server offers a GUI interface for user to
type in their SQL language, and their queries would be executed after pressing the "Execute"
button on the client. Oracle provides both GUI client and command-line client. In this lesson,
we will study the command-line client, SQL*Plus. In addition, Oracle extends the standard
SQL (e.g. select * from table) with its application-specific commands (e.g. checking how many
table you have been created in your Oracle account) into a Oracle specific language called
PL/SQL. In this tutorial, you will interact with Oracle database, thru SQL*Plus, by issuing a
number of PL/SQL queries.
Microsoft Windows
Creating Tables
In SQL*Plus we can execute any SQL command. One simple type of command creates a table
(relation). The form is
);
You may enter text on one line or on several lines. If your command runs over several lines, you
will be prompted with line numbers until you type the semicolon that ends any command.
(Warning: An empty line terminates the command but does not execute it; see editing
commands in the buffer.) An example table-creation command is:
i int,
s char(10)
);
Note that SQL is case insensitive, so CREATE TABLE TEST and create table test are the
same. This command creates a table named test with two attributes. The first attribute, named
i, is an integer, and the second, named s, is a character string of length (up to) 10.
Exercise 1:
Inserting Tuples
Having created a table, we can insert tuples into it. The simplest way to insert is with the INSERT
command:
INSERT INTO <tableName>
Trick: Try to insert a record into test with the following SQL:
Updating Tuples
WHERE <Predicate>;
For example, we can update the tuple (10, 'hi world') in relation test by
Update the record of 'Eric Lo' in relation Student such that his salary change to 1234
Deleting Tuples
Having insert / update a tuple, we can delete it as well. The simplest way to delete is with the
DELETE command:
For example, you can delete the record with i=10 in table test with the the following SQL:
Exercise 4:
Delete the record of 'Eric Lo' in relation Student.
Trick: Does that record really deleted successfully? Let's check it out by using SELECT
command (we will cover it in next section).
Retrieving Tuples
SELECT <attributes-separated-by-comma>/<wildcard>
FROM <tableName>;
For instance, after the above CREATE, INSERT, DELETE and UPDATE statements, the
command
SELECT * FROM test;
produces the result
I S
11 ha 'world
Exercise 5:
Question: Do data values also case insensitive? i.e., can a student with name "Hammer" be
retrieved by the following SQL or not?
Trick: Does the record of 'Eric Lo' exist when you do exercise 5? If yes, congratulation! You
"seems" that you have deleted that tuple successfully... Now, please open a new SQL*Plus, and
redo exercise 5 to see if the record 'Eric Lo' still be there? What happen?
It provides a good back-door for you to revert the changes you have done on the data. Therefore,
unless you have issued COMMIT, the changed data would not be visible to any other session
except your own. Conversely, you can rollback all the changes by issuing the ROLLBACK
command.
Exercise 6:
Issue the COMMIT command in the SQL*Plus that you have done insert/delete/update before,
and see if the effects is now visible by the new SQL*Plus?
Dropping Tables
Caution: Table dropping is a DML statement, which is an action that you cannot rollback. Since
dropping a table will also delete all data in that table, issue the DROP TABLE command with
cares.
The system keeps information about your own database in certain system tables. The most
important for now is USER_TABLES. You can recall the names of your tables by issuing the query:
Quitting SQL*Plus
quit;
in response to the SQL> prompt.
Instead of executing SQL commands typed at a terminal, it is often more convenient to type the
SQL command(s) into a file and cause the file to be executed. There are two ways to do so.
The first is to provide your login name, password and the name of the file in the command line
with which you open SQL*Plus. The form of the command is:
A second, safer way, is for sally to log in to SQL*Plus as usual. He then types, in response to
the SQL> prompt:
@foo.sql
and the file foo.sql's contents will be executed.
If you end a command without a semicolon, but with an empty new line, the command goes into
a buffer. You may execute the command in the buffer by either the command RUN or a single
slash (/).
You may also edit the command in the buffer before you execute it. Here are some useful editing
commands. They are shown in upper case but may be either upper or lower.
L[IST] Lists the command buffer, and makes the last line in the buffer the "current" line
L[IST] n Prints line n of the command buffer, and makes line n the current line
L[IST] m n Prints lines m through n, and makes line n the current line
I[NPUT] Enters a mode that allows you to input text following the current line; you must
terminate the sequence of new lines with a pair of "returns"
C[HANGE] /old/new Replaces the text "old" by "new" in the current line
R[UN] or / Displays and runs the current SQL statement in the buffer
SAV[E]
filename[.ext] Saves current contents of SQL buffer to a file. The default extension is .sql.
GET filename[.ext] Writes the contents of a previously saved file to the SQL buffer. The default
extension for the filename is .sql.
STA[RT]
filename[.ext] Runs a previously saved command file
ED[IT] Invokes the editor and saves the buffer contents to a file named afiedt.buf
ED[IT]
[filename[.ext]] Invokes the editor to edit contents of a saved file
In response to the SQL> prompt, type help followed by a keyword. If you are lucky, the keyword
will be one of those for which help exists, and you will get a (somewhat) helpful message,
usually ending in an example or two. To see all the possible commands, for each of which help is
available, type
Data Types
Joining Tables
As mentioned in the lectures, joining is an essential step to retrieve information across different
tables. In SQL, there is no operator dedicated for joining. Therefore, in order to join on two
tables, you have do a Cartesian product on two tables and then specifying the conditions.
For example, you have another two tables table1 and table2 which is created by the
following SQL:
Now, type 'ed' and open a text file to insert the following tuples into table1 and table2:
Then, you have join the two tables with the following SQL:
Stream_Code Stream_Name
E Ecom
I Icom
2) Join the table Student and Streams to list the information the
sid
name
job
salary
stream
Functions
Oracle offers some numeric and string functions for users to display the result in a more
desirable format. For example, the SUBSTR(A, 1, 4) function returns the first four
characters for every data value. Therefore, the SQL:
XXX
----------
Eco
Ico
The following table gives you an overview of a subset of functions Oracle provided:
5. Find the total number of student in each Stream that have more than 2 students
To create a table that declares the set of attributes (a,b,c) to be a primary key:
Creating Index
In fact, a unique key is created automatically when you define a PRIMARY KEY in table
definition.
Summary
During this session, astute students may notice that some queries are highlighted in blue, and
some queries are highlighted in green. In fact, only the green queries are in the SQL standard.
This is because different vendors may implement the standard differently. For example, Oracle
implement the SUBSTRING function with the name "SUBSTR" instead of "SUBSTRING".
Other than having different naming convention between implementation and specification, some
vendors may even skip some operators. For example, Oracle does not implement the
union/intersection/except operators. On the other hand, the queries in blue are PL/SQL, a
language that incorporate the SQL and oracle commands together. Luckily, what you have learnt
in lectures is the core of SQL specification and supported by most DBMS.
Cheer!