The use of SQL PLUS basic commands

The use of SQL PLUS basic commands

1. The difference between dba, all, user, in Oracle

1. 结论:'权限大小不同': dba_* > all_* > user_*
   (1) dba_* : 可以访问 '数据库' 中所有的对象(前提:该用户是 dba 用户)
   (2) all_* :某一用户 '拥有' 的或 '可以访问' 的所有的对象
   (3) user_*:某一用户 '拥有' 的所有对象

2. 查询是否是 dba 用户
   select * from dba_role_privs t where t.granted_role = 'DBA';

2、desc

desc:万能查看命令

查看dept表
SQL> desc dept
 Name                                      Null?    Type
 ----------------------------------------- -------- --------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(13)

Insert picture description here

3. Set the operating environment of SQL*PLUS, including the number of characters displayed on each line, the number of lines displayed on each page, and the number of blank lines before the title of each page.

Basic syntax of set command

In the Oracle 11g database, users can use the set command to set the operating environment of SQL*Plus;

set system_variable value;

system_variable:变量名;
value:变量值;

The environment variables set by the set command are temporary, not permanent;
when the user exits the SQL*Plus environment, the environment parameters set by the user will all disappear;

Use the set command to set the operating environment
pagesize变量:该变量用来设置从顶部标题至页结束之间的行数;
set pagesize value;
value的默认值为14

使用show pagesize命令显示当前SQL*Plus环境中的一页有多少行;
show pagesize;

同理:
newpage变量:该变量用来设置一页中空行的数量;
set newpage value;
value的默认值为1

show newpage;

linesize变量:
该变量用来设置在SQL*Plus环境中一行所显示的最多字符总数;
set linesize value;
value的默认值为80

show linesize;

pause变量:
该变量用来设置SQL*Plus输出结果是否滚动显示;
set pause value;

value变量值有以下三种情况:
- off:默认值,表示返回结果一次性输出完毕,中间的每一页不会暂停;
- on:表示输出结果的每一页都暂停,用户按后回车键后会继续显示;
- text:在设置pause的值为on之后,再设置text的值,则每次暂停都将显示该字符串; 
        当pause的值设置为off时,设置text的值没有任何意义;

Insert picture description here

oracle executes the sql script file with the command

When there are too many sql commands (the sql file is too large), the execution of plsql is slow and easy to time out. At this time, you can use the sqlplus command to directly execute the sql script file.

@ H:/sql/test.sql  (绝对路径)

Insert picture description here

The spool in oracle writes the queried data to the file

Under normal circumstances, we use the SPOOL method to export the tables in the database as a text file when using two methods, such as the following:

方法一:采用以下格式脚本
set colsep '' ------设置列分隔符
set trimspool on
set linesize 120
set pagesize 2000
set newpage 1
set heading off
set term off
spool 路径+文件名
select * from tablename;
spool off

方法二:采用以下脚本
set trimspool on
set linesize 120
set pagesize 2000
set newpage 1
set heading off
set term off
spool 路径+文件名
select col1||','||col2||','||col3||','||col4||'..' from tablename;
spool off


Insert picture description here
Insert picture description here

差别:

Comparing the above methods, the first method uses the set separator and then sqlplus uses the set separator to divide the field, and the second method combines the separator in the SELECT statement, that is, manually controls the output format.

In practice, I found that the data exported by method one has great uncertainty. When the data exported by this method is imported by sql, the possibility of error is more than 95%, especially for large-scale data tables. , Such as a table with 1 million records, even more so, and the exported data file is crazy.

The format of the data file exported by Method 2 is very regular, and the size of the data file may be about 1/4 of that of Method 1. When the data file exported by this method is imported by sqll, the possibility of error is very small, and it can basically be imported successfully.

Therefore, in practice, I suggest that you use Method 2 to manually control the format of the spool file, so as to reduce the possibility of errors and avoid many detours.

ttitle, btitle commands

它主要是对头标题与尾标题的设置,如下:
eg:为查询结果设置居中的头部标题、居右的当天日期作为尾部标题,并为查询结果列定义列标题。

SQL> ttitle center '结果';
SQL> btitle right '2021/03/30';
SQL> select * from dept; ;

                                      结果
    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON





                                                                      2021/03/30
如果想撤消ttitle与btitle:
SQL> ttitle off;
SQL> btitle off;
SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL>
                                                   

Insert picture description here

column command

format formatted output
format 选项
用于格式化指定的列
SQL> column sal format $99,99,99;
SQL> select empno,ename,sal from scott.emp;

     EMPNO ENAME             SAL
---------- ---------- ----------
      7369 SMITH           $8,00
      7499 ALLEN          $16,00
      7521 WARD           $12,50
      7566 JONES          $29,75
      7654 MARTIN         $12,50
      7698 BLAKE          $28,50
      7782 CLARK          $24,50
      7788 SCOTT          $30,00
      7839 KING           $50,00
      7844 TURNER         $15,00
      7876 ADAMS          $11,00

     EMPNO ENAME             SAL
---------- ---------- ----------
      7900 JAMES           $9,50
      7902 FORD           $30,00
      7934 MILLER         $13,00

14 rows selected.

heading 选项
用于定义列标题
SQL> col empno heading 雇员编号;
SQL> col ename heading 雇员姓名;
SQL> col sal heading 雇员薪水;
SQL> select empno,ename,sal from scott.emp;

  雇员编号 雇员姓名     雇员薪水
---------- ---------- ----------
      7369 SMITH           $8,00
      7499 ALLEN          $16,00
      7521 WARD           $12,50
      7566 JONES          $29,75
      7654 MARTIN         $12,50
      7698 BLAKE          $28,50
      7782 CLARK          $24,50
      7788 SCOTT          $30,00
      7839 KING           $50,00
      7844 TURNER         $15,00
      7876 ADAMS          $11,00

  雇员编号 雇员姓名     雇员薪水
---------- ---------- ----------
      7900 JAMES           $9,50
      7902 FORD           $30,00
      7934 MILLER         $13,00

14 rows selected.

Insert picture description here
Insert picture description here

Operations on the buffer area

1)查看当前缓存区中的SQL命令。
>list2)执行存储在缓存区的SQL命令。
>run3)将缓存区中的内容保存到一个SQL脚本文件(文件名自拟)。
>save C:/sql.sql4)把一个脚本文件的内容放进缓存区,并执行缓存区中的语句。
>get C:/sql.sql
>start C:/sql.sql5)清除缓存区中的内容。
>clear buffer

SQL> save H:/sql/test3.txt;
Created file H:/sql/test3.txt
SQL> get H:/sql/test.sql;
  1  select table_name from user_tables;
  2* desc dept
SQL> start H:/sql/test.sql;

TABLE_NAME
------------------------------
DEPT
EMP
BONUS
SALGRADE

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(13)

SQL> clear buffer;
buffer cleared
SQL>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45882303/article/details/115315474
Recommended