210 likes | 364 Views
SQL and SQL*Plus Interaction. SQL Statements. SQL Statements. Server. Query Results. SQL*Plus Commands. Formatted Report. Buffer. SQL*Plus. SQL Statements Vs SQL*Plus Commands. SQL A command language used for communication with the Oracle Server to access data
E N D
SQL and SQL*Plus Interaction SQL Statements SQL Statements Server Query Results SQL*Plus Commands Formatted Report Buffer SQL*Plus
SQL Statements Vs SQL*Plus Commands SQL • 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 statements SQL buffer SQL*Plus commands SQL*Plus buffer
SQL Statements Vs SQL*Plus Commands SQL • 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
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. Overview of SQL*Plus
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 Name Null? Type ----------------- -------- ------------ DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)
Displaying Table Structure SQL> DESCRIBE dept Name Null? Type ----------------- -------- ------------ DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)
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 • ntext • 0 text
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 • 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 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 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 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 Result: 1* SELECT FEATURE, PAGE
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 Check the whole query again with: list 1 SELECT FEATUER, PAGE 2 FROM NEWSPAPER 3* WHERE SECTION = ‘F’; To delete the current line: del Check the whole query again with: list 1 SELECT FEATUER, PAGE 2* FROM NEWSPAPER
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 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’ SQL> list 1 SELECT FEATUER, PAGE 2 FROM NEWSPAPER 3* where section = ‘A’ Run it SQL>/
SQL*Plus File Commands SAVE filename GET filename START filename @ filename EDIT filename SPOOL filename
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 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.