SQL> alter user scott account unlock;
用户已更改。 #我用tiger密码无法登陆,所以需要修改密码
SQL> alter user scott identified by Password;
用户已更改。
SQL> conn scott/Password #connect username/password@sid 使用默认数据库可不用服务名
已连接。
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> select * from salgrade;
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
#由于3列数据类型均为NUMBER所以使用“9”,99是格式化模式,每个“9”表示一位数字
SQL> col grade for 99 #表示占用两位数字的宽度
SQL> col losal for 9999 #表示占用四位数字的宽度
SQL> col hisal for 9999
SQL> select * from salgrade;
GRADE LOSAL HISAL
----- ----- -----
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
SQL> col hisal for 9999.99 #表示带小数点
SQL> select * from sAlgrade;
GRADE LOSAL HISAL
----- ----- --------
1 700 1200.00
2 1201 1400.00
3 1401 2000.00
4 2001 3000.00
5 3001 9999.00
SQL> col object_name for a20 #格式化为20个字符宽度
SQL> select object_name,object_type from user_objects
2 where object_type = 'TABLE';
OBJECT_NAME OBJECT_TYPE
-------------------- -------------------
BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
SQL> col losal for $9999
SQL> col hisal for $9999
SQL> select * from salgrade;
GRADE LOSAL HISAL
----- ------ ------
1 $700 $1200
2 $1201 $1400
3 $1401 $2000
4 $2001 $3000
5 $3001 $9999
SQL> col losal for L9999
SQL> col hisal for L9999
SQL> select * from salgrade;
GRADE LOSAL HISAL
----- --------------- ---------------
1 ¥700 ¥1200
2 ¥1201 ¥1400
3 ¥1401 ¥2000
4 ¥2001 ¥3000
5 ¥3001 ¥9999
查看当前数据库支持的字符集
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
ZHS16GBK<语言><比特位><编码>
SQL> col losal
COLUMN losal ON
FORMAT L9999
SQL> col hisal
COLUMN hisal ON
FORMAT L9999
SQL> col losal clear
SQL> col hisal clear
SQL> col losal
SP2-0046: COLUMN 'losal' 未定义
SQL> col hisal
SP2-0046: COLUMN 'hisal' 未定义
SQL> col losal heading '低工资'
SQL> col hisal heading '高工资'
SQL> select * from salgrade;
GRADE 低工资 高工资
----- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
JUS[TIFY] 靠左 居中 偏右
奇怪的是对我没用
SQL> col dname jus center
SQL> col loc jus center
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
用的少
SQL> col dname newline
SQL> select * from dept;
DEPTNO
----------
DNAME LOC
-------------- -------------
10
ACCOUNTING NEW YORK
SQL> col loc noprint
SQL> col dname clear
SP2-0046: COLUMN 'dname' 未定义
SQL> select * from dept;
DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
SQL> insert into dept (deptno,dname,loc) values (50,'Marcketing','');
已创建 1 行。
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
50 Marcketing
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> col loc null 'TEMP';
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
50 Marcketing TEMP #填充数据只是显示,不会修改实际数据
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
OFF后,之前的格式化操作将取消,之后格式化修改将无效
ex: SQL> col loc off
run会显示sql语句反馈,“/”不会
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
50 Marcketing TEMP
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> run
1* select * from dept
DEPTNO DNAME LOC
---------- -------------- -------------
50 Marcketing TEMP
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> /
DEPTNO DNAME LOC
---------- -------------- -------------
50 Marcketing TEMP
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select empno,ename,job,mgr,hiredate,sal
2 from emp
3 where job ='MANAGER';
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7566 JONES MANAGER 7839 02-4月 -81 2975
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7782 CLARK MANAGER 7839 09-6月 -81 2450
SQL> list
1 select empno,ename,job,mgr,hiredate,sal
2 from emp
3* where job ='MANAGER'
SQL> 1 #定位到第一行
1* select empno,ename,job,mgr,hiredate,sal
前面已经定位到第一行,使用change命令将列sal改为deptno
SQL> ch /sal/deptno
1* select empno,ename,job,mgr,hiredate,deptno
SQL> /
EMPNO ENAME JOB MGR HIREDATE DEPTNO
---------- ---------- --------- ---------- -------------- ----------
7566 JONES MANAGER 7839 02-4月 -81 20
7698 BLAKE MANAGER 7839 01-5月 -81 30
7782 CLARK MANAGER 7839 09-6月 -81 10
n为SQL语句的行号,随后输入替换的行
SQL> list
1 select empno,ename,job,mgr,hiredate,deptno
2 from emp
3* where job ='MANAGER'
SQL> 1 select empno,ename,job,mgr
SQL> list
1 select empno,ename,job,mgr
2 from emp
3* where job ='MANAGER'
SQL> list
1 select empno,ename,job,mgr
2 from emp
3* where job ='MANAGER'
SQL> 1
1* select empno,ename,job,mgr
SQL> a,deptno #将,loc添加到第一行的SQL末尾
1* select empno,ename,job,mgr,deptno
SQL> list
1 select empno,ename,job,mgr,deptno
2 from emp
3* where job ='MANAGER'
SQL> list
1 select empno,ename,job,mgr,deptno
2 from emp
3* where job ='MANAGER'
SQL> del 3
SQL> list
1 select empno,ename,job,mgr,deptno
2* from emp
格式:set line {80/n}
将查询的数据输出设置为n个字符宽度显示,默认80个字符宽度输出
当输出无法显示为一行时,可使用此变量,将数值设置较大些即可
SQL> list
1 select empno,ename,job,mgr,deptno
2* from emp
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-12月-80 800
20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300
SQL> set line 100#设置显示行的长度为100个字符,即可显示在一行
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800
20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
将用户输入的sql语句和查询结果存储到指定的文件中
SQL> show spool
spool OFF
SQL> spool d:\spool_test
SQL> show spool
spool ON
SQL> select empno,ename,job,mgr,sal
2 from emp
3 where job = 'MANAGER';
EMPNO ENAME JOB MGR SAL
---------- ---------- --------- ---------- ----------
7566 JONES MANAGER 7839 2975
7698 BLAKE MANAGER 7839 2850
7782 CLARK MANAGER 7839 2450
SQL> spool off
SQL> select empno,ename,job,mgr,hiredate,sal
2 from emp
3 where job = 'MANAGER'
4 order by sal;
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7566 JONES MANAGER 7839 02-4月 -81 2975
#save命令创建
SQL> save d:\SELECT_emp
已创建 file d:\SELECT_emp.sql
SQL> @d:\SELECT_emp
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7566 JONES MANAGER 7839 02-4月 -81 2975
SQL> start d:\SELECT_tmp
SP2-0310: 无法打开文件 "d:\SELECT_tmp.sql"
SQL> start d:\SELECT_emp
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7566 JONES MANAGER 7839 02-4月 -81 2975
SQL> get d:\SELECT_emp
1 select empno,ename,job,mgr,hiredate,sal
2 from emp
3 where job = 'MANAGER'
4* order by sal
SQL> 1
1* select empno,ename,job,mgr,hiredate,sal
SQL> change \sal\depno
1* select empno,ename,job,mgr,hiredate,depno
SQL> list #实际脚本文件未修改
1 select empno,ename,job,mgr,hiredate,depno
2 from emp
3 where job = 'MANAGER'
4* order by sal
扩展~
echo打开时脚本语句会输出显示
SQL> show echo
echo OFF
SQL> @d:\SELECT_emp
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7566 JONES MANAGER 7839 02-4月 -81 2975
SQL> set echo on
SQL> show echo
echo ON
SQL> @d:\SELECT_emp
SQL> select empno,ename,job,mgr,hiredate,sal
2 from emp
3 where job = 'MANAGER'
4 order by sal
5 /
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ---------- -------------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450
7698 BLAKE MANAGER 7839 01-5月 -81 2850
7566 JONES MANAGER 7839 02-4月 -81 2975
控制查询输出的数据行数是否显示,以及记录数达到什么值时才显示行数
SQL> show feedback
用于 6 或更多行的 FEEDBACK ON
SQL> select* from salgrade;
GRADE 低工资 高工资
----- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
由于数据行数小于6,所以不显示数据行数,设置FEEDBACK参数为3
SQL> set feedback 3
SQL> select * from salgrade;
GRADE 低工资 高工资
----- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
已选择5行。