The DML, DDL, and DCL statements we usually call are SQL * plus statements. After they are executed, they can be stored in a memory area called SQL buffer, in addition, only one SQL statement can be saved recently. We can modify the SQL statement stored in SQL buffer and execute it again. sqlplus generally deals with databases.
Frequently used:
Sqlplus username/password for example: normal user login sqlplus scott/tiger
Sqlplus username/password @ net_service_name For example: sqlplus scott/tiger @ orcl
Sqlplus username/password as sysdba For example: sqlplus sys/admin as sysdba
Sqlplus username/password @ // host: port/sid
Note: sys and system must be logged on with sysdba
After entering SQL * plus, you can use conn to connect to other users, such as conn sys/admin as sysdba.
In the DOS environment, enter "sqlplus /?", As follows:
C: \ Documents ents and Settings \ HH> sqlplus /?
SQL * Plus: Release 11.2.0.1.0 Production on Saturday August 13 16:56:46 2011
Copyright (c) 1982,201 0, Oracle. All rights reserved.
SQL * Plus: Release 11.2.0.1.0 Production
Copyright (c) 1982,201 0, Oracle. All rights reserved.
Use SQL * Plus to execute SQL, PL/SQL, and SQL * Plus statements.
Usage 1: sqlplus-H |-V
-H displays the SQL * Plus version and usage help.
-V displays the SQL * Plus version.
Usage 2: sqlplus [[<option>] [{logon |/nolog}] [<start>]
<Option>: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]
-C <version> sets the compatibility of affected commands to the specified version. This version is in the "x. y [. z]" format. For example,-C 10.2.0
-L only attempts to log on once, instead of prompting again when an error occurs.
-M "<options>" sets the output automatic HTML Tag. The format of the options is:
HTML [ON | OFF] [HEAD text] [BODY text] [TABLE text] [ENTMAP {ON | OFF}] [SPOOL {ON | OFF}] [PRE [FORMAT] {ON | OFF}]
-R <level> sets the restricted mode to disable the SQL * Plus command that interacts with the file system. The level can be 1, 2, or 3. The maximum limit is-R 3, which disables all user commands that interact with the file system.
-S sets the no-Prompt mode. In this mode, the SQL * Plus mark of the command is hidden, and the prompt and echo display are displayed.
<Logon> {<username> [/<password>] [@ <connect_identifier>] |/} [AS {SYSDBA | SYSOPER | SYSASM}] [EDITION = value]
Specifies the database account username, password, and database connection identifier. If there is no connection identifier, SQL * Plus will connect to the default database.
As sysdba, as sysoper, and as sysasm options are database management permissions.
<Connect_identifier> can be a Net service name or easily connected.
@ [<Net_service_name> | [//] Host [: Port]/<service_name>]
<Net_service_name> is a simple name of the service, which is parsed as a connection descriptor.
Example: use the Net service name to connect to the database, and the database Net service name is ORCL.
Sqlplus myusername/mypassword @ ORCL
Host specifies the Host name or IP address of the database server computer.
Port specifies the listening Port on the database server.
<Service_name> specify the service name of the database to be accessed.
Example: connect to the database with ease and the service name is ORCL.
Sqlplus myusername/mypassword @ Host/ORCL
The/NOLOG option enables SQL * Plus instead of connecting to the database.
EDITION specifies the value of the session version.
<Start> is: @ <URL >|< filename> [. <ext>] [<parameter>...]
Run the specified SQL * Plus script from the Web server (URL) or local file system (filename. ext) using the specified parameter that will be assigned to the substitution variable in the script.
After SQL * Plus is started and the CONNECT command is executed, the site profile (for example, $ ORACLE_HOME/sqlplus/admin/glogin) is run. SQL) and user profile, such as login in the working directory. SQL ). These files contain SQL * Plus commands.
In addition to the sqlplus statement, other statements executed in SQL * plus are called the SQL * plus command. After they are executed, they are not stored in the memory area of the SQL buffer. They are generally used to format and display the output results to facilitate the preparation of reports.
The following describes some common SQL * plus commands:
1. Execute an SQL script file
SQL> start file_name
SQL> @ file_name
We can save multiple SQL statements in a text file, so that when we want to execute all the SQL statements in this file, we can use any of the following commands, which is similar to batch processing in dos.
2. Edit the current input.
SQL> edit
3. Run the last SQL statement again.
SQL>/
4. output the displayed content to the specified file.
SQL> SPOOL file_name
All content on the screen is included in this file, including the SQL statement you entered.
5. Disable spool output
SQL> SPOOL OFF
The output content is displayed in the output file only when spool output is disabled.
6. display the structure of a table
SQL> desc table_name
7. COL command:
It mainly formats the display of columns.
This command has the following options:
COL [UMN] [{column | expr} [option...]
The Option can be the following clause:
ALI [AS] alias
CLE [AR]
FOLD_A [FTER]
FOLD_ B [EFORE]
FOR [MAT] format
HEA [DING] text
JUS [cipher] {L [EFT] | C [ENTER] | C [ENTRE] | R [IGHT]}
LIKE {expr | alias}
NEWL [INE]
NEW_V [ALUE] variable
NOPRI [NT] | PRI [NT]
NUL [L] text
OLD_V [ALUE] variable
ON | OFF
WRA [PPED] | WOR [D_WRAPPED] | TRU [NCATED]
1). Change the default Column Title
COLUMN column_name HEADING column_heading
For example:
SQL> select * from dept;
DEPTNO DNAME LOC
-----------------------------------------------
10 ACCOUNTING NEW YORK
SQL> col LOC heading location
SQL> select * from dept;
Deptno dname location
------------------------------------------------
10 ACCOUNTING NEW YORK
2) Change the column NAME to the new column NAME "employee name" and put the new column NAME on two rows:
SQL> select * from emp
Department name Salary
------------------------------
10 aaa 11
SQL> COLUMN ENAME HEADING 'employee | name'
SQL> select * from emp
Employee
Department name Salary
------------------------------
10 aaa 11
Note: the col heading turn into two lines from one line.
3). Change the display length of the column:
FOR [MAT] format
SQL> select empno, ename, job from emp;
EMPNO ENAME JOB
-----------------------------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
SQL> col ename format a40
EMPNO ENAME JOB
-----------------------------------------------------------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
4). Set the column title alignment
JUS [cipher] {L [EFT] | C [ENTER] | C [ENTRE] | R [IGHT]}
SQL> col ename justify center
SQL>/
EMPNO ENAME JOB
-----------------------------------------------------------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
For NUMBER-type columns, the column title is on the right by default, and for other types of columns, the column title is on the left by default.
5). prevent a column from being displayed on the screen.
NOPRI [NT] | PRI [NT]
SQL> col job noprint
SQL>/
EMPNO ENAME
--------------------------------------------------
7369 SMITH
7499 ALLEN
7521 WARD
6). format the display of NUMBER columns:
SQL> COLUMN SAL FORMAT $99,990
SQL>/
Employee
Department Name Salary Commission
---------------------------------------
30 ALLEN $1,600 300
7). If the column value is NULL when the column value is displayed, replace the NULL value with the text value.
Comm nul [L] text
SQL> COL COMM NUL [L] text
. Set the rewinding Method for a column
WRA [PPED] | WOR [D_WRAPPED] | TRU [NCATED]
COL1
--------------------
How are you?
SQL> COL COL1 FORMAT A5
SQL> COL COL1 WRAPPED
COL1
-----
HOW
RE YO
U?
SQL> COL COL1 WORD_WRAPPED
COL1
-----
HOW
ARE
YOU?
SQL> COL COL1 WORD_WRAPPED
COL1
-----
HOW
9). display the current display attribute value of the column
SQL> COLUMN column_name
10). Set the display attribute of all columns to the default value.
SQL> CLEAR COLUMNS
8. Block the same value displayed in a column
Break on break_column
SQL> BREAK ON DEPTNO
SQL> SELECT DEPTNO, ENAME, SAL
FROM EMP
Where sal <1, 2500
Order by deptno;
DEPTNO ENAME SAL
------------------------------
10 CLARK 2450
MILLER 1300
20 SMITH 800
ADAMS 1100
9. The display of the same values displayed in a column is shielded above. Each time the column value changes, n blank rows are inserted before the value changes.
Break on break_column SKIP n
SQL> BREAK ON DEPTNO SKIP 1
SQL>/
DEPTNO ENAME SAL
------------------------------
10 CLARK 2450
MILLER 1300
20 SMITH 800
ADAMS 1100
10. Display BREAK settings
SQL> BREAK
11. Delete settings 6 and 7
SQL> CLEAR BREAKS
12. Set command:
This command contains many sub-commands:
SET system_variable value
System_variable value can be one of the following clauses:
APPI [NFO] {ON | OFF | text}
ARRAY [SIZE] {15 | n}
AUTO [COMMIT] {ON | OFF | IMM [EDIATE] | n}
AUTOP [RINT] {ON | OFF}
AUTORECOVERY [ON | OFF]
AUTOT [RACE] {ON | OFF | TRACE [ONLY]} [EXP [LAIN] [STAT [ISTICS]
BLO [CKTERMINATOR] {. | c}
CMDS [EP] {; | c | ON | OFF}
COLSEP {_ | text}
COM [PATIBILITY] {V7 | V8 | NATIVE}
CON [CAT] {. | c | ON | OFF}
COPYC [OMMIT] {0 | n}
COPYTYPECHECK {ON | OFF}
DEF [INE] {& | c | ON | OFF}
DESCRIBE [DEPTH {1 | n | ALL}] [LINENUM {ON | OFF}] [INDENT {ON | OFF}]
ECHO {ON | OFF}
EDITF [ILE] file_name [. ext]
EMB [EDDED] {ON | OFF}
ESC [APE] {| c | ON | OFF}
FEED [BACK] {6 | n | ON | OFF}
FLAGGER {OFF | ENTRY | INTERMED [IATE] | FULL}
FLU [SH] {ON | OFF}
HEA [DING] {ON | OFF}
HEADS [EP] {| c | ON | OFF}
INSTANCE [instance_path | LOCAL]
LIN [ESIZE] {80 | n}
LOBOF [FSET] {n | 1}
LOGSOURCE [pathname]
LONG {80 | n}
LONGC [HUNKSIZE] {80 | n}
MARK [UP] HTML [ON | OFF] [HEAD text] [BODY text] [ENTMAP {ON | OFF}] [SPOOL
{ON | OFF}] [PRE [FORMAT] {ON | OFF}]
NEWP [AGE] {1 | n | NONE}
NULL text
NUMF [ORMAT] format
NUM [WIDTH] {10 | n}
PAGES [IZE] {24 | n}
PAU [SE] {ON | OFF | text}
RECSEP {WR [APPED] | EA [CH] | OFF}
RECSEPCHAR {_ | c}
SERVEROUT [PUT] {ON | OFF} [SIZE n] [FOR [MAT] {WRA [PPED] | WOR [D _
WRAPPED] | TRU [NCATED]}]
SHIFT [INOUT] {VIS [visible] | INV [ISIBLE]}
SHOW [MODE] {ON | OFF}
SQLBL [ANKLINES] {ON | OFF}
SQLC [ASE] {MIX [ED] | LO [WER] | UP [PER]}
SQLCO [NTINUE] {> | text}
SQLN [UMBER] {ON | OFF}
SQLPRE [FIX] {# | c}
SQLP [ROMPT] {SQL> | text}
SQLT [ERMINATOR] {; | c | ON | OFF}
SUF [FIX] {SQL | text}
TAB {ON | OFF}
TERM [OUT] {ON | OFF}
TI [ME] {ON | OFF}
TIMI [NG] {ON | OFF}
TRIM [OUT] {ON | OFF}
TRIMS [POOL] {ON | OFF}
UND [ERLINE] {-| c | ON | OFF}
VER [IFY] {ON | OFF}
WRA [P] {ON | OFF}
1) set whether the current session will automatically submit the modified data
SQL> SET AUTO [COMMIT] {ON | OFF | IMM [EDIATE] | n}
2) Whether to display the SQL statements being executed in the script when you run an SQL script using the start command
SQL> SET ECHO {ON | OFF}
3). Whether to display the number of rows queried or modified by the current SQL statement
SQL> SET FEED [BACK] {6 | n | ON | OFF}
By default, only rows with more than 6 results are displayed. If set feedback 1 is returned, no matter how many rows are queried. When it is off, the number of queried rows is not displayed.
4). Whether to display the column title
SQL> SET HEA [DING] {ON | OFF}
When set heading off, Column Titles are not displayed on each page, instead of blank rows.
5) set the number of characters that a row can hold
SQL> SET LIN [ESIZE] {80 | n}
If the output content of a row is greater than the number of characters that can be accommodated in a set row, the line is displayed.
6). Set the separation between pages
SQL> SET NEWP [AGE] {1 | n | NONE}
When set newpage 0, a small black box is displayed at the beginning of each page.
When set newpage n is set, there are n blank lines between the page and the page.
When set newpage none is set, there is no interval between the page and the page.
7). Replace the NULL value with the text value during display.
SQL> SET NULL text
. Set the number of rows on a page
SQL> SET PAGES [IZE] {24 | n}
If it is set to 0, all output content is one page and the column title is not displayed.
9). Whether to display the output information using the DBMS_OUTPUT.PUT_LINE package.
SQL> SET SERVEROUT [PUT] {ON | OFF}
When writing a stored procedure, we sometimes use dbms_output.put_line to output necessary information to debug the stored procedure. The information can be displayed on the screen only after the serveroutput variable is set to on. Dbms_output.put_line will "eat" the leading space? Add the format wrapped parameter after set serveroutput on!
10) Whether to intercept the SQL statement when the length of the SQL statement is greater than LINESIZE.
SQL> SET WRA [P] {ON | OFF}
When the length of the output line is greater than the length of the set line (set with the set linesize n command), when set wrap on, more than the characters of the output line will be displayed in another line, otherwise, the output line is removed from the output line and will not be displayed.
11). Whether to display the output content on the screen is mainly used in combination with SPOOL.
SQL> SET TERM [OUT] {ON | OFF}
When you use the spool command to output the content in a large table to a file, it takes a lot of time to output the content on the screen. After setting set termspool off, the output content is saved only in the output file and not displayed on the screen, greatly improving the spool speed.
12) Remove unnecessary spaces behind each line in the SPOOL output.
SQL> SET TRIMS [OUT] {ON | OFF}
13) display the execution time of each SQL statement
Set TIMING {ON | OFF}
14. Modify the first string in the current row of SQL buffer
C [HANGE]/old_value/new_value
SQL> l
1 * select * from dept
SQL> c/dept/emp
1 * select * from emp
15. Edit the SQL statement in SQL buffer
EDI [T]
16. display the SQL statements in SQL buffer. list n displays the nth row in SQL buffer and makes the nth row the current row.
L [IST] [n]
17. Add one or more rows under the current row of SQL buffer
I [NPUT]
18. Add the specified text to the current row of the SQL buffer.
A [PPEND]
SQL> select deptno,
2 dname
3 from dept;
DEPTNO DNAME
------------------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
SQL> L 2
2 * dname
SQL> a, loc
2 * dname, loc
SQL> L
1 select deptno,
2 dname, loc
3 * from dept
SQL>/
DEPTNO DNAME LOC
-------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
19. Save the SQL statement in SQL buffer to a file.
SAVE file_name
20. Import SQL statements in a file into SQL buffer
GET file_name
21. Execute the SQL statement that has just been executed again
RUN
Or
/
22. Execute a stored procedure
EXECUTE procedure_name
23. Connect to the specified database in SQL * plus
CONNECT user_name/passwd @ db_alias
24. Set the top title of each report
TTITLE
25. Set the end title of each report
BTITLE
26. Write a comment
REMARK [text]
27. output the specified information or an empty row to the screen.
PROMPT [text]
28. Pause the execution process and wait for the user to respond.
PAUSE [text]
SQL> PAUSE Adjust paper and press RETURN to continue.
29. copy some data from a database to another database (for example, copy data from one table to another)
COPY {FROM database | TO database | FROM database TO database}
{APPEND | CREATE | INSERT | REPLACE} destination_table
[(Column,...)] USING query
SQL> COPY FROM SCOTT/TIGER @ HQ TO JOHN/CHROME @ WEST
Create emp_temp
Using select * FROM EMP
30. If you do not exit SQL * plus, execute an operating system command in SQL * plus:
HOST
SQL> host hostname
This command may be supported in windows.
31. In SQL * plus, switch to the operating system command prompt. After running the operating system command, you can switch back to SQL * plus again:
!
SQL>!
$ Hostname
$ Exit
SQL>
This command is not supported in windows.
32. display the help of the SQL * plus command
HELP
How to install the Help file:
SQL> @? Sqlplusadminhelphlpbld. SQL? Sqlplusadminhelphelpus. SQL
SQL> help index
33. display the SQL * plus System variable value or SQL * plus environment variable value
Syntax
SHO [W] option
Where option represents one of the following terms or clses:
System_variable
ALL
BTI [TLE]
ERR [ORS] [{FUNCTION | PROCEDURE | package body |
TRIGGER | VIEW | type body} [schema.] name]
LNO
PARAMETERS [parameter_name]
PNO
REL [statement]
REPF [OOTER]
REPH [EADER]
SGA
SPOO [L]
SQLCODE
TTI [TLE]
USER
1). display the value of the current environment variable:
Show all
2). displays the error information of the currently created function, stored procedure, trigger, package, and other objects.
Show error
When an error occurs in creating a function or stored procedure, you can use this command to view the error and related error information at that place, modify the information, and compile the code again.
3). display the value of the initialization parameter:
Show PARAMETERS [parameter_name]
4). display the database version:
Show REL [Rules]
5). display the SGA size
Show SGA
6). display the current user name
Show user
_________________
Xsb note:
@ 2. Difference Between SQL and @ 2. SQL:
For example, in e: sqlplus @ e: temp1. SQL
1. @ 2 in SQL. The script called by SQL is located under e:
@ 2. The script called by SQL is located in the e: temp directory.