sqlplus的基本使用

一、概述
Connecting to Oracle Database from SQL*Plus

SQL*Plus is a client program with which you can access Oracle Database. This section shows how to start SQL*Plus and connect to Oracle Database.

If you are on a Windows system, display a Windows command prompt.

At the command prompt, type sqlplus and press the key Enter.

SQL*Plus starts and prompts you for your user name.

Type your user name and press the key Enter.

SQL*Plus prompts you for your password.

Type your password and press the key Enter.

Note:
For security, your password is not visible on your screen.
The system connects you to an Oracle Database instance.

You are in the SQL*Plus environment. At the SQL> prompt, you can enter and run SQL*Plus commands, SQL statements, PL/SQL statements, and operating system

commands.

To exit SQL*Plus, type exit and press the key Enter.

Note:
Exiting SQL*Plus ends the SQL*Plus session, but does not shut down the Oracle Database instance.

例子:
> sqlplus
SQL*Plus: Release 11.2.0.0.1 - Beta on Mon Jun 9 15:31:26 2008

Copyright (c) 1982, 2008, Oracle.  All rights reserved.

Enter user-name: your_user_name
Enter password: your_password

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.0.0 - Development
With the Partitioning, Data Mining and Real Application Testing options

SQL> select count(*) from employees;

  COUNT(*)
----------
       107

SQL> exit

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.0.0 - Development
With the Partitioning, Data Mining and Real Application Testing options
>


sqlplus /nolog

这个参数表示是不用no login的意思,表示不用登陆数据服务器,只打开sqlplus即可。

打开sqlplus后,就需要用conn连接数据库服务器了


二、sqlplus专用命令

1.disc[onnect],退出数据库登陆,但不退出sqlplus;

2.exit 或者 quit,退出sqlplus;

3.c[hange] /旧字符串/新字符串,把当前行的旧字符串替换为新字符串

4.cl[ear] 清除
  cl buff[er] 清除缓冲区中的内容;

5.del n 删除行n
      n m 删除从n到m行之间的内容
      * 删除当前行
      last 删除最后一行

  l[ist] 显示内容
   l * 显示当前行的内容

6.i[nput]  添加一行内容

7.exec[ute] statement 执行存储过程

8.pro[mpt] text 显示文本消息

9.desc[ribe] object 列出表、视图、同义词的列,或者函数和存储过程的定义;

10.def[ine] variable|varable=text 定义一个变量或者显示变量的值;

11.acc[ept] 提示用户输入值,并把值放到变量中
   acc pswd char pro '用户名'

12.set 设置系统变量,例如:
   set system_variable value
   set timing on;

13.r[un] 运行缓冲区中的内容;

14.在sqlplus中执行sql语句,用;分号结束或者是/,不过/这个要单独占一行;

15.对数据库执行DML操作(insert,update,delete)语句时,不用显示commit。我们可以设置set autocommit on

16.如果一条sql语句长时间运行,想终止它,可以ctrl+c组合;

17.如何在sqlplus中执行操作系统命令?

   host 操作系统命令  例如:

   host dir c:

18.sql缓冲区,当用户执行完一条sql语句时,这条语句就被缓存到了sql缓冲区,知道输入新的sql语句,覆盖原先的sql语句。

19.执行缓冲区中的slq语句,可以输出“/”

20.把缓存区的内容存储到文件中

   save 'c:\test.sql'
  
   如果文件已存在会报错,必须用 save 'c:\test.sql' append 追加,或者save 'c:\test.sql' replace覆盖

21.如果编辑缓存区中的内容,输入edit就会用系统自带的记事本打开内容进行编辑;

22.sql脚本文件,可以把多条sql语句放到后缀为.txt的文本中,例如:
   select * from emp;
    select sysdate from dual;
  执行时:
  @c:\sss.txt 或者 start c:\sss.txt

   在脚本中还可以调用其他脚本;

23.怎么把sql语句的执行结果存储到文件中,首先 spool c:\result.txt 执行这个命令;
   之后所有sql语句执行的结果都会存储到改文件,最后spool off关闭即可;

24.如何知道sql语句的执行速度?
  set timing on

   这样当一条语句执行完毕后,就会附加显示执行的时间。

25.如何查看sqlplus 的环境变量?

   show all 显示所有环境变量

   show variablename 显示单个变量的值

   环境变量控制着sqlplus的行为,对服务器没有任何影响

26.设置环境变量用set,例如
    set serveroutput on
   只对当前会话有效,如果重新登录,必须重新设置。

27.如何改变sqlplus的启动项?
   可以修改glogin.sql文件,sqlplus启动时,会读取改文件

28.如何把sql脚本的内容载入sql缓冲区?

   get F:\test.sql

29.如何查出sqlplus使用的默认编辑器?
   
   预定义的变量 _EDITOR指示的默认编辑器,我们可以用Define命令查看

   define _EDITOR
 
   define _EDITOR = vi (这样可以修改默认编辑器)

30.如何把环境变量保存到文件中?

   store set F:\v_store

   恢复环境变量

   start v_store

  

猜你喜欢

转载自longcxm.iteye.com/blog/1462939