Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL and SQL*Plus Interaction

Similar presentations


Presentation on theme: "SQL and SQL*Plus Interaction"— Presentation transcript:

1 SQL and SQL*Plus Interaction
SQL Statements Buffer SQL Statements Server SQL*Plus SQL*Plus Commands Query Results SQL and SQL*Plus SQL is a command language for communication with the Oracle Server from any tool or application. Oracle SQL contains many extensions. When you enter a SQL statement, it is stored in a part of memory called the SQL buffer and remains there until you enter a new statement. SQL*Plus is an Oracle tool that recognizes and submits SQL statements to the Oracle Server for execution and contains its own command language. Features of SQL Can be used by a range of users, including those with little or no programming experience Is a nonprocedural language Reduces the amount of time required for creating and maintaining systems Is an English-like language Features of SQL*Plus Accepts ad hoc entry of statements Accepts SQL input from files Provides a line editor for modifying SQL statements Controls environmental settings Formats query results into a basic report Accesses local and remote databases Formatted Report

2 SQL Statements Vs SQL*Plus Commands
A command language used for communication with the Oracle Server to access data When a SQL statement is entered, it is stored in a part of memory called the SQL buffer and remains there until a new statement is entered SQL*Plus An environment SQL*Plus is an Oracle tool that recognizes and submits SQL statements to Oracle Server for execution Provides a line editor for modifying SQL statements Is entered one line at a time, not stored in the SQL buffer SQL and SQL*Plus (continued) The following table compares SQL and SQL*Plus: SQL statements SQL buffer SQL*Plus commands SQL*Plus buffer

3 SQL Statements Vs SQL*Plus Commands
It is based on ANSI standard SQL Keyword can’t be abbreviated Statements manipulate data and table definitions in the database It does not have a continuation character Uses a termination character to execute command immediately Uses functions to perform some formatting SQL*Plus Oracle proprietary interface for executing SQL statements Keywords can be abbreviated Commands do not allow manipulation of values in the database Has a dash(-) as a continuation character if the command is longer than one line Does not require a termination characters. Commands are executed immediately Uses commands to format data SQL and SQL*Plus (continued) The following table compares SQL and SQL*Plus:

4 Overview of SQL*Plus Log in to SQL*Plus. Describe the table structure.
Edit your SQL statement. Execute SQL statements from SQL*Plus to: Retrieve, modify, add, and remove data from the database Format, perform calculations on, store, and print query results in the form of reports Save SQL statements to files Create script files to store SQL statements for repetitive use in the future Append SQL statements to files. Execute saved files. Load commands from file to buffer to edit. SQL*Plus SQL*Plus is an environment in which you can do the following: Execute SQL statements to retrieve, modify, add, and remove data from the database Format, perform calculations on, store, and print query results in the form of reports Create script files to store SQL statements for repetitive use in the future SQL*Plus commands can be divided into the following main categories: Class Management Note (for Page 1-27) Snippet: “Establishing a Database Session” Logging in to SQL*Plus: Release number may vary, depending on the version installed.

5 Logging In to SQL*Plus Logging In to SQL*Plus
How you invoke SQL*Plus depends on which type of operating system or Windows environment you are running. To log in through a Windows environment: 1. Click Start—>Programs—>Oracle for Windows NT—>SQL*Plus 8.0. 2. Fill in username, password, and database. To log in through a command-line environment: 1. Log on to your machine. 2. Enter the SQL*Plus command as shown in the slide. In the command: username is your database username password is your database password (if you enter your password here, it is visible) @database is the database connect string Note: To ensure the integrity of your password, do not enter it at the operating system prompt. Instead, enter only your username. Enter your password at the Password prompt. Once you are successfully logged in to SQL*Plus, you see the following message: SQL*Plus : Release Production on Mon Oct 06 16:03: (c) Copyright 1997 Oracle Corporation. All rights reserved.

6 Displaying Table Structure
Use the SQL*Plus DESCRIBE command to display the structure of a table. DESC[RIBE] tablename The result of the command is to see the column names, data types, as well as whether a column must contain data. Example: SQL> DESCRIBE dept Displaying Table Structure In SQL*Plus, you can display the structure of a table using the DESCRIBE command. The result of the command is to see the column names and datatypes as well as whether a column must contain data. In the syntax: tablename is the name of any existing table, view, or synonym accessible to the user Name Null? Type DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)

7 Displaying Table Structure
SQL> DESCRIBE dept Name Null? Type DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13) Data Type Description NUMBER(p, s) Number value having a maximum number of digits p, the number of digits to the right of the decimal point s VARCHAR2(S) Variable-length character value of maximum size s DATE Date and time value between January 1, 4712 BC and December 31, 9999 AD CHAR(S) Fixed-length character value of size s Displaying Table Structure In SQL*Plus, you can display the structure of a table using the DESCRIBE command. The result of the command is to see the column names and datatypes as well as whether a column must contain data. In the syntax: tablename is the name of any existing table, view, or synonym accessible to the user

8 SQL*Plus Editing Commands
A[PPEND] text C[HANGE] / old / new C[HANGE] / text / CL[EAR] BUFF[ER] DEL DEL n DEL m n I[NPUT] I[NPUT] text L[IST] L[IST] n L[IST] m n R[UN] n n text 0 text SQL*Plus Editing Commands SQL*Plus commands are entered one line at a time and are not stored in the SQL buffer. Guidelines If you press [Return] before completing a command, SQL*Plus prompts you with a line number. You terminate the SQL buffer by either entering one of the terminator characters (semicolon or slash) or pressing [Return] twice. You then see the SQL prompt.

9 SQL*Plus Editing Commands
A[PPEND] text Adds text to the end of the current line C[HANGE] / old / new Changes old text to new text in the current line C[HANGE] / text / Deletes text from the current line CL[EAR] BUFF[ER] Deletes all lines from the SQL buffer DEL Deletes current line DEL n DEL m n SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

10 SQL*Plus Editing Commands
I[NPUT] Inserts an indefinite number of lines I[NPUT] text Inserts a line consisting of text L[IST] Lists all lines in the SQL buffer L[IST] n Lists one line (specified by n) L[IST] m n Lists a range of lines (from m to n) R[UN] Displays and runs the current SQL statement in the buffer n Specifies the line to make the current line n text Replaces line n with text 0 text Inserts a line before line 1 SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

11 SQL*Plus Editing Commands
SELECT FEATUER, PAGE FROM NEWSPAPER WHERE SECTION = ‘F’; ORACLE responds: ERROR at line 1: ORA-0704: invalid column name TWO Solutions: 1. Retype the whole query 2. Use command to correct spelling of FEATUER SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

12 SQL*Plus Editing Commands
SELECT FEATUER, PAGE FROM NEWSPAPER WHERE SECTION = ‘F’; Solution: Name of command: list SQL>list Result: 1 SELECT FEATUER, PAGE 2 FROM NEWSPAPER 3* WHERE SECTION = ‘F’ SQL*Plus shows all three lines, and numbered them. It also places * next to line 3, which means it is the line your editing commands are able to affect. But we want to change line 1, so type: SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

13 SQL*Plus Editing Commands
SELECT FEATUER, PAGE FROM NEWSPAPER WHERE SECTION = ‘F’; Solution: Name of command: list 1 SQL>list 1 Result: 1* SELECT FEATUER, PAGE Line 1 is displayed and is now the current line. We change it by typing: CHANGE /FEATUER/FEATURE 1* SELECT FEATURE, PAGE SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

14 SQL*Plus Editing Commands
Check the whole query again with: list 1 SELECT FEATUER, PAGE 2 FROM NEWSPAPER 3* WHERE SECTION = ‘F’; Place / at SQL> prompt and query will be executed. SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

15 SQL*Plus Editing Commands
Check the whole query again with: list 1 SELECT FEATUER, PAGE 2 FROM NEWSPAPER 3* WHERE SECTION = ‘F’; To delete the current line: del 1 SELECT FEATUER, PAGE 2* FROM NEWSPAPER SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

16 SQL*Plus Editing Commands
To clear the whole select statement completely, type: clear buffer If you like to append something to the current line, type: SQL>list 1 1* SELECT FEATUER, PAGE SQL>append “where it is” 1* SELECT FEATUER, PAGE “where it is” append places its text right up against the end of the current line, with no spaces in between. To put a space in, type two spaces between the word append and the text. SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

17 SQL*Plus Editing Commands
We may also input a whole new line after the current line: SQL> list 1 SELECT FEATUER, PAGE 2* FROM NEWSPAPER SQL>input where section = ‘A’ 2 FROM NEWSPAPER 3* where section = ‘A’ Run it SQL> / SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].

18 SQL*Plus File Commands
SAVE filename GET filename START filename @ filename EDIT filename SPOOL filename SQL*Plus File Commands SQL statements communicate with the Oracle Server. SQL*Plus commands control the environment, format query results, and manage files. You can use the commands identified in the following table:

19 SQL*Plus File Commands
SAV[E] filename[.ext] [ REPLACE | APP[END] ] Saves current contents of SQL buffer to a file. Use APPEND to add to an existing file; use REPLACE to overwrite an existing file. The default extension is .sql. GET filename[.ext] Writes the contents of a previously saved file to the SQL buffer. The default extension is .sql. START filename[.ext] Runs a previously saved command file. @ filename Runs a previously saved command file (same as START). ED[IT] Invokes the editor and saves the buffer contents to a file named afiedt.buf ED[IT] filename[.ext] Invokes editor to edit contents of a saved file SPO[OL] [ filename[.ext] | OFF OUT ] Stores query results in a file. OFF closes the spool file. OUT closes the spool file and sends the file results to the system printer. EXIT Leaves SQL*Plus. SQL*Plus File Commands SQL statements communicate with the Oracle Server. SQL*Plus commands control the environment, format query results, and manage files. You can use the commands identified in the following table:

20 SQL*Plus File Commands
We can save the SQL created so far: SQL> save abc.sql SQL*PLUS responds: Wrote file abc.sql Now, the SQL statement is in file abc.sql. SQL*Plus Editing Commands (continued) You can enter only one SQL*Plus command per SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the current line with a hyphen (-). Class Management Note Show students the use of the commonly used editing commands, such as A[PPEND], C[HANGE], DEL, L[IST], and R[UN].


Download ppt "SQL and SQL*Plus Interaction"

Similar presentations


Ads by Google