ORACLE基础SQL命令


1、连接相关用户

sqlplus / as sysdba;
--连接到管理员账户
conn usera/user123;
--连接到usera用户
alter user usera identified by tiger;
--若密码错误,可在管理员账户修改用户密码

2、查看表结构

desc tablename;
--desc scott查看scott表结构

3、查看某用户有多少表

select * from tab;
--因为显示出来的TNAME列比较长,可以通过col调整
col tname for a30;

4、将屏幕输出保存到指定文件中

spool /tmp/a.txt;
select * from tab;
spool off;
ho cat /tem/a.txt;  --ho/host是调用系统命令
spool /tmp/a.txt rep --替换内容
spool /tmp/a.txt append  --增加内容

5、清屏、替换字符、定义参数

cl[ear] scr[een];
--括号表示可缩写,清理屏幕
c[hange]/old_value/new_value;
--更改第一个出现的字符,之后键入'/'之后执行
define val=1;
--定义val值,select * from tablename where no=&val;
undefine val;
--取消定义val值

6、show、set用法

set heading on|off;
--设置是否显示列标题
set newpage 1|n|none
--设置语句和结果之间的行数
set feedback on|n|off
--设置输出数据集行数x>n时,结果集后面显示 x rows selected,如果不大于则不显示,n是feedback设置的值
set pagesize n
--设置n行开始分页,列标题、分隔行--、数据、空行都算
set linesize n
--设置一个行有多少字符
set autocommit on|off
--设置自动提交
show all
--查看所有参数值

7、col(column)设置左右对齐

col dname just left|right|center
--将dname列靠左、右、中对齐。just是justify的缩写
col dname for a30
--将dname的宽度设置为30,for是format的缩写

8、查看数据库是否启动

sqlplus / as sysdba;
select open_mode from v$database;
--ORA-01034:oracle not available表示数据库没有启动
--ORA-01507:databse not mounted表示数据库启动在nomounted阶段,可以通过将其修改成mount->open状态
select instance_name from v$instance;
--查看数据库实例名,也可以执行 ho ps -ef|grep ora_|head -2;最后一个下划线后面的就是实例名