Quick Reference to SQL and SQL*Plus
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIX A ■ ■ ■ Quick Reference to SQL and SQL*Plus This appendix offers quick references for SQL*Plus and the SQL language. It is far from a com- plete reference, but it should still prove useful. The Oracle documentation contains (besides the comprehensive and complete references) an SQL Quick Reference and a SQL*Plus Quick Reference, but this appendix is much more concise than those two Oracle quick references. You may abbreviate most SQL*Plus commands and their components, as long as you don’t introduce ambiguity. This appendix does not show all SQL*Plus command abbreviation possibilities explicitly, in order to enhance its readability. For example, where this appendix lists the COMPUTE command, it should show it as COMP[UTE], because you can abbreviate the SQL*Plus COMPUTE command to COMP. Abbreviation is available only for SQL*Plus commands; you must always enter SQL commands completely. Refer to SQL*Plus User’s Guide and Reference for all of the abbreviations for SQL*Plus commands and their components. ■Tip When writing SQL*Plus scripts, use the full SQL*Plus commands and the full command component names instead of their abbreviations. This will enhance the readability of your scripts. When you are using SQL*Plus interactively, you may want to use the abbreviations. There are some differences between SQL*Plus and iSQL*Plus; however, this appendix does not list those differences. Refer to SQL*Plus User’s Guide and Reference for more details. The Oracle documentation also explicitly shows all abbreviations for SQL*Plus commands and their components. After a section about the syntax conventions used in this appendix, the commands are organized as follows: • Starting and stopping; entering and executing commands • Working with the SQL*Plus editor, manipulating SQL*Plus scripts, and SQL*Plus inter- activity commands • Variables and parameters • Formatting query results 349 350 APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS • SQL: data manipulation (DML), transactions, and queries • SQL: data definition (DDL) • SQL: other commands • SQL: operators, functions, and regular expressions • Rules for naming Oracle database objects; SQL reserved words Note that this appendix also shows constructs, operators, and commands that are not covered in the chapters of this book. Syntax Conventions Used in This Appendix Table A-1 shows the general syntax conventions used in this appendix. Table A-2 identifies the characters and formatting that have special meaning in this appendix. Table A-1. General Syntax Conventions Element Meaning CREATE Command name or keyword, to be entered exactly as spelled (not necessarily in uppercase) str Alphanumeric literal between quotes (string) cond Boolean expression (condition) d, d1, d2 Expressions of datatype DATE expr, e1, e2 Arbitrary expressions m, n Numeric expressions query Query embedded within a command (subquery) txt Alphanumeric expression (with or without quotes) Table A-2. Characters and Formatting with Special Meaning Element Example Meaning [ ] [NOWAIT] Brackets enclose one or more optional items. Options are separated by vertical bars. Underlining [ASC|DESC] Underscored value is the default used if you don’t make a choice yourself. { } {expr1|expr2} Braces enclose two or more items, separated by vertical bars. Choose precisely one of them. ... expr[,...] Horizontal ellipsis indicates that you may repeat the preced- ing item as many times as you like, or that irrelevant parts of the code are omitted. You should not enter the characters of Table A-2 with a special meaning (square brackets, braces, vertical lines, and ellipsis) themselves. Regular parentheses (like the ones surrounding this text), commas, periods, single and double quotation marks (' and "), at signs (@), APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS 351 ampersands (&), and any other “exotic” characters have no special meaning in the syntax descriptions in this appendix; therefore, you should enter them verbatim. Starting and Stopping SQLPLUS {-HELP|-VERSION} SQLPLUS [-COMPATIBILITY x.y.z] [/NOLOG|name[/passwd]] [@script] [-MARKUP "options"] [-RESTRICT {1|2|3}] [-SILENT] script ::= {file[.ext]|url} [arg1 arg2 ...]} Start a SQL*Plus session or display help/version information. CONNECT name[/passwd][@database-spec] Create a new SQL*Plus session from the SQL*Plus prompt with the given name/password. DISCONNECT Commit transaction and close database session, but do not exit SQL*Plus. {EXIT|QUIT} [SUCCESS|FAILURE|WARNING|n|var] [COMMIT|ROLLBACK] Close database session and leave SQL*Plus; by default, with a COMMIT and a SUCCESS exit code for the calling environment. PASSWORD [user] Change your password (screen echo suppressed) or change the password for a specific user. SHUTDOWN [ABORT|IMMEDIATE|NORMAL|TRANSACTIONAL [LOCAL]] Shut down the Oracle instance. STARTUP [FORCE] [RESTRICT] [QUIET] [OPEN [READ ONLY]] ... Start up the Oracle instance. There are several additional options for this command. Entering and Executing Commands / Entering a forward slash executes the SQL command in the SQL buffer, without listing the command. DESCRIBE [schema.]obj-name Display the definition/structure of the specified object. Use the SET DESCRIBE command to influence the DESCRIBE command. EXECUTE plsql-statement 352 APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS Execute a single PL/SQL statement. HOST [os-command] Execute a single operating system command and return to the SQL*Plus prompt. Without an argument, the HOST command starts a subsession at the operating system level. RUN Display and execute the SQL command in the SQL buffer. TIMING [START txt|SHOW|STOP] Record timing data, list current timer’s name and timing data, or list the number of active timers. Working With the SQL*Plus Editor These commands are not available in iSQL*Plus. Note that this section explicitly shows the SQL*Plus command abbreviations (as opposed to the other sections of this appendix) because these commands are used often. A[PPEND] txt Add txt at the end of the current line in the buffer. Use two spaces to enter a space before txt. C[HANGE] /old[/[new[/]]] Change old on the current line into new. Any nonalphanumeric character is allowed as the separator instead of the slash (/). The third separator is optional; you need it only when new ends with one or more spaces. DEL [n [m]] Delete one or more lines from the buffer. The default is to delete only the current line. Instead of line numbers (n, m), you can also use the following expressions: LAST (last line) or * (current line). I[NPUT] [txt] Add lines to the SQL buffer under the current line. L[IST] [n [m]] Display one or more buffer lines. Without arguments, LIST displays all lines in the buffer. Instead of line numbers, you can use the same special expressions as with the DEL command: * and LAST. APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS 353 Manipulating SQL*Plus Scripts Note that most commands listed in this section are not available in iSQL*Plus. EDIT [file[.ext]] Start an external editor and open file.ext. Without an argument, EDIT edits the SQL buffer. GET file[.ext] [LIST|NOLIST] Load the contents of a file into the SQL buffer. REMARK [txt] Enter comments. Oracle ignores txt until the end of the current line. SAVE file[.ext] [CREATE|REPLACE|APPEND] Save the content of the SQL buffer in file.ext. STORE SET file[.ext] [CREATE|REPLACE|APPEND] Save the current SQL*Plus settings in file.ext. {START|@|@@} {url|script[.ext]} [arg1 arg2 ...] Execute script (default file extension SQL). Oracle substitutes arguments for variables &1, &2, and so on. WHENEVER OSERROR {EXIT [SUCCESS|FAILURE|n] [COMMIT|ROLLBACK] |CONTINUE [COMMIT|ROLLBACK|NONE]} Indicate what SQL*Plus should do in case of operating system error conditions. WHENEVER SQLERROR {EXIT [SUCCESS|FAILURE|WARNING|n] [COMMIT|ROLLBACK] |CONTINUE [COMMIT|ROLLBACK|NONE]} Indicate what SQL*Plus should do when SQL commands create error situations. SQL*Plus Interactivity Commands ACCEPT var [NUMBER|CHAR|DATE|BINARY_FLOAT|BINARY_DOUBLE] [FORMAT fmt] [DEFAULT dflt] [PROMPT txt|NOPROMPT] [HIDE] Prompt with txt and assign entered value to variable var. DEFINE [x[=txt]] 354 APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS Assign value txt to variable x; show current value for x, or show all defined variables. PAUSE [txt] Display txt (or an empty line) and wait for an Enter keypress. PROMPT [txt] Write txt (or an empty line) to the screen. UNDEFINE x Undefine variable x. Variables and Parameters &n Refer to a command-line parameter in a SQL*Plus script: &1 for the first argument, &2 for the second, and so on. Actually, the ampersand (&) is the default setting for the DEFINE character. See the SQL*Plus SET command. &var, &&var Refer to the value of variable var. If var is undefined, SQL*Plus prompts for a value; with & this happens each time, and with && only once (&& is an implicit DEFINE). A period is the separator between variable names and following text (the CONCAT character). PRINT [var] Display the value of bind variable var, or all current bind variable values. SET sys-var value Assign values to SQL*Plus system variables. The following are common SQL*Plus system variables: APPINFO {ON|OFF|txt] PAGESIZE {14|n} ARRAYSIZE {15|n} PAUSE {ON|OFF|txt} AUTOCOMMIT {ON|OFF|IMMEDIATE|n} RECSEP {WRAPPED|EACH|OFF} AUTOTRACE {ON|OFF|TRACEONLY} [EXPLAIN] RECSEPCHAR { |c} [STATISTICS] CMDSEP {;|c|ON|OFF} SERVEROUTPUT {ON|OFF} [SIZE n] [FORMAT {WRAPPED|WORD_WRAPPED|TRUNC}] COLSEP { |txt} SHOWMODE {ON|OFF} APPENDIX A ■ QUICK REFERENCE TO SQL AND SQL*PLUS 355 CONCAT {.|c|ON|OFF}