Lab 2 – Queries, Arithmetic Expressions and Functions
1. Objective
This set of laboratory exercises illustrates some more advanced features of SQL and SQL*Plus expressions. The number of examples to try is large, yet you are advised to complete all of them, both in and outside your timetabled lab classes.
As with the previous handout, the main source of most of the exercises in this handout is the SQL*Plus User’s Manual published by the ORACLE Corporation.
In order to test all the suggested examples, you need to have the three tables existing on your Oracle database account, namely EMP, DEPT and SALGRADE. We now begin the tour of showing more advanced features of SQL*Plus commands first.
2. SQL*PLUS LINE EDITING COMMANDS
When you type a SQL command, Oracle stores it in an area called the SQL command buffer. The SQL command stays in the buffer until it is replaced by the next set of SQL commands that you enter. The command in the buffer is called the “current” SQL command. SQL*Plus provides some line editing commands to help you modify the buffer:
SQL*Plus Commands
COMMAND
ABBEVIATION
PURPOSE
APPEND
A text
Add text at the end of a line
CHANGE/old/new
C/old/new
Change old to new in the line
CHANGE/text
C text
Delete text from a line
CLEAR BUFFER
CL BUFF
Delete all lines
DEL
(none)
Delete a line
INPUT
I
Add one or more lines
LIST
L
List all lines in the SQL buffer
LIST n
L n OR n
List one line
LIST LAST
L LAST
List the last line
LIST m n
Lmn
List a range of lines (m to n)
2.1 Change the Current SQL Command Without Retyping It
Oracle allows you to modify the current SQL command without having to retype the entire command.
Enter a command with the column named DEPTNO mistakenly entered as DPTNO.
SQL > SELECT DPTNO, ENAME, SAL
2 FROM EMP
3 WHERE DEPTNO = 10;
Oracle responds to the typing mistake with an error message. Rather than retyping the entire command you can correct the mistake by modifying the command in the buffer. Firstly you must “position” to the line of the command that contains the error by using the LIST command (abbreviated L).
Position to line 1 (L1) of the current query SQL > L1
Once you have positioned to the line that contains the error you can fix the mistake using the CHANGE command.
1