sqlplus 简单查询

1. sqlplus 连接

1). 环境变量

[root@wyzc Desktop]# su - oracle
[oracle@wyzc ~]$ echo $ORACLE_SID

[oracle@wyzc ~]$ echo $ORACLE_HOME

[oracle@wyzc ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:
/usr/sbin:/sbin:/home/oracle/bin
[oracle@wyzc ~]$ tail -l /etc/oratab
wyzc10g:/u01/oracle/10g:N
wyzc11g:/u01/oracle/11g:N       #  line added by Agent
[oracle@wyzc ~]$ export ORACLE_HOME=/u01/oracle/11g
[oracle@wyzc ~]$ export ORACLE_SID=wyzc11g
[oracle@wyzc ~]$ export PATH=$ORACLE_HOME/bin:$PATH
[oracle@wyzc ~]$ which sqlplus
/u01/oracle/11g/bin/sqlplus

2). 连接

[oracle@wyzc ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on 星期一 2月 19 10:15:20 
2018 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
连接到: 
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production 
With the Partitioning, Oracle Label Security, OLAP, 
Data Mining,Oracle Database Vault and Real Application Testing options

SQL> show user  # 查看当前用户
USER 为 "SYS"
SQL> conn scott/scott;
已连接。
SQL> show user
USER 为 "SCOTT"
SQL> conn hr/hr
已连接。
SQL> show user
USER 为 "HR"
SQL> exit
[oracle@wyzc ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.3.0 Production on 星期一 2月 19 10:24:10 2018
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
SQL> show user
USER 为 ""
SQL> conn scott/scott
已连接。
SQL> show user
USER 为 "SCOTT"
SQL> select * from tab;
TNAME                  TABTYPE  CLUSTERID
------------------------------ ------- ----------
BONUS                  TABLE
DEPT                   TABLE
EMP                TABLE
SALGRADE               TABLE
SQL> desc emp
SQL> select deptno , loc , dname from dept;
SQL> select dname , loc from dept;
DNAME          LOC
-------------- -------------
ACCOUNTING     NEW YORK
RESEARCH       DALLAS
SALES          CHICAGO
OPERATIONS     BOSTON

SQL> c/dept/DEPT  # from子句更改表名为大写,不影响结果
  1* select dname , loc from DEPT
SQL> /
DNAME          LOC
-------------- -------------
ACCOUNTING     NEW YORK
RESEARCH       DALLAS
SALES          CHICAGO
OPERATIONS     BOSTON
SQL> select * from "dept"; # oracle内部存储都是大写,双引号里的字符是区分大小写的
select * from "dept" 
第 1 行出现错误:
ORA-00942: 表或视图不存在

SQL> select ename, sal, comm ,sal+nvl(comm,0) from emp;
SQL> select ename,sal,comm,(sal+nvl(comm,0))*12 as sals from emp;
SQL> clear scr; # 清屏 
SQL> select deptno,dname from dept;
    DEPTNO DNAME
---------- --------------
    10 ACCOUNTING
    20 RESEARCH
    30 SALES
    40 OPERATIONS

SQL> select deptno "deptno",dname "dname" from dept;
 # 使用别名,双引号里设定了别名为小写的字符串

    deptno dname
---------- --------------
    10 ACCOUNTING
    20 RESEARCH
    30 SALES
    40 OPERATIONS

3) .中文环境

[oracle@wyzc ~]$ vim .nls #设置环境变量,让oracle正常显示中文
export NLS_LANG='simplified chinese_china.al32utf8' 
[oracle@wyzc ~]$ ./.nls
[oracle@wyzc ~]$ sqlplus  scott/scott
SQL*Plus: Release 11.2.0.3.0 Production on 星期一 2月 19 12:27:44 2018
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
连接到: 
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> select deptno "员工ID" ,dname "员工姓名" from dept;
    员工ID 员工姓名
---------- --------------
    10 ACCOUNTING
    20 RESEARCH
    30 SALES
    40 OPERATIONS

SQL> create table e1 as select deptno,sum(sal) from emp group by deptno; 
create table e1 as select deptno,sum(sal) from emp group by deptno 
第 1 行出现错误:
ORA-00998: 必须使用列别名命名此表达式
SQL> create table e1 as select deptno,sum(sal) tsal from emp 
          group by deptno;
表已创建。
SQL> select 'name : ' || empno from emp;
'NAME:'||EMPNO
-----------------------------------------------
name : 7369
name : 7499
name : 7521
name : 7566

4). 简单查询

SQL> l
  1* select 'name : ' || empno from emp
SQL> select ename,'******' sal from emp;
ENAME      SAL
---------- ------
SMITH      ******
ALLEN      ******
WARD       ******
JONES      ******

SQL> select ename ||' is work '|| job from emp;
ENAME||'ISWORK'||JOB
----------------------------
SMITH is work CLERK
ALLEN is work SALESMAN
WARD is work SALESMAN
JONES is work MANAGER
SQL> select ename||' ''s work '||job from emp;
SQL> select ename ||' is work '|| job from emp where empno < 7600;
ENAME||'ISWORK'||JOB
----------------------------
SMITH is work CLERK
ALLEN is work SALESMAN
WARD is work SALESMAN
JONES is work MANAGER
  • distinct
SQL> select distinct  deptno from emp; # 关键字
SQL> select distinct(deptno) from emp; # 函数
SQL> select unique deptno from emp; # 关键字
SQL> select unique(deptno) from emp; # 函数
  • where =,<,>,BETWEEN,IN,LIKE,and NULL;Logical:AND,OR and NOT;
SQL> select ename,sal from emp where empno=7566;
SQL> select ename,sal from emp where sal>2000;
SQL> select ename,sal from emp where sal=3000;
SQL> select ename,sal from emp where sal<>2000; 
# 不等于有这三种表示方式:^=、!=、<> 
# between是包含两边临界值的,sal>=2000,sal<=3000
SQL> select ename,sal from emp where sal between  2000 and 3000;
# sal<>2000,sal<=3000
SQL> a and sal<>2000 # 这里的‘a’,表示对上面的select语句增加一个字句。
  1* select ename,sal from emp where sal between  2000 and 3000and sal<>2000
SQL> /
ENAME         SAL
---------- ----------
JONES        2975
BLAKE        2850
CLARK        2450
SCOTT        3000
FORD         3000
SQL> select ename,sal from emp where sal>2000 and sal<=3000;
# sal<2000 and sal>3000
SQL> select ename,sal from emp where sal not between 2000 and 3000; #取反
SQL> select ename,sal from emp where not sal between 2000 and 3000; 
#不建议使用这种查询方式,因为会运算两次:1.sal between 2000 and 3000;2.取反
  • in
SQL> select ename,sal from emp where sal in (3000,5000);
SQL> select ename,sal from emp where sal in (890,980,3000);
 #三个值分别和表匹配,这里会循环三次,注意先后顺序
ENAME         SAL
---------- ----------
SCOTT        3000
FORD         3000
SQL> select ename,sal from emp where sal not in (3000,5000); 
# 对(不是3000和5000)进行取反
SQL> select ename,sal from emp where not  sal in (3000,5000);
#不建议,这里会运算两次:1.先执行in(3000,5000);2.取反
  • is null
SQL> select ename,sal,comm from emp where comm is null;
ENAME         SAL       COMM
---------- ---------- ----------
SMITH         800
JONES        2975
BLAKE        2850
CLARK        2450
SCOTT        3000
KING         5000
ADAMS        1100
JAMES         950
FORD         3000
MILLER       1300
已选择10行。
SQL> c/is/is not #这里的'c',表示修改上文语句内容
  1* select ename,sal,comm from emp where comm is not null #buwei(is null)
SQL> /
ENAME         SAL       COMM
---------- ---------- ----------
ALLEN        1600        300
WARD         1250        500
MARTIN       1250       1400
TURNER       1500          0
SQL> select ename,sal,comm from emp where comm > 0;
ENAME         SAL       COMM
---------- ---------- ----------
ALLEN        1600        300
WARD         1250        500
MARTIN       1250       1400
  • like
SQL> select ename,sal from emp where ename like 'A%'; # 以A开头的
SQL> select ename,sal from emp where ename like '%S'; # 以S结尾的
#1个下划线"_"代表一个字符长度;1个百分号"%"代表0个或多个字符.
SQL> select ename from emp where ename like '__A%'; 
# 查询第三个字符为A的字符串,这里前面有2个下划线
ENAME
----------
BLAKE
CLARK
ADAMS
SQL> insert into emp(empno,ename,sal) values(10,'A_10',3200);
SQL> c/10/11
  1* insert into emp(empno,ename,sal) values(11,'A_10',3200)
SQL> c/_/%
  1* insert into emp(empno,ename,sal) values(11,'A%10',3200)
SQL> /
SQL> select ename from emp where sal=3200;
ENAME
----------
A_10
A%10
SQL> select ename from emp where ename like '%/_%' escape '/'; 
# 转移字符可以是'/',也可以是其他字符,查询包含下划线'-'的字符串
SQL> select ename from emp where ename like '%1%%' escape '1';
 # 查询包含百分号'%'的字符串
  • 字符串区分大小写,而且值只能单引号,双引号会报错。
SQL> select ename,sal,comm from emp where ename='scott';
no rows selected
SQL> select ename,sal,comm from emp where ename='SCOTT';
ENAME         SAL       COMM
---------- ---------- ----------
SCOTT        3000
  • 日期使用单引号,oracle默认存储日期格式DD-MM-YY 天-月-年
SQL> select ename,sal,hiredate from emp where hiredate='03-DEC-81';
ENAME         SAL HIREDATE
---------- ---------- ---------
JAMES         950 03-DEC-81
FORD         3000 03-DEC-81
SQL> alter session set nls_date_format='yyyy-mm-dd';
Session altered.
SQL> select ename,sal,hiredate from emp where hiredate='03-DEC-81';
select ename,sal,hiredate from emp where hiredate='03-DEC-81' 
ERROR at line 1:
ORA-01858: a non-numeric character was found 
where a numeric was expected
SQL> select sysdate from dual;
SYSDATE
----------
2018-02-19
SQL> select ename,sal,hiredate from emp where hiredate='1981-12-03';

ENAME         SAL HIREDATE
---------- ---------- ----------
JAMES         950 1981-12-03
FORD         3000 1981-12-03

# 换成中文发现日期格式又变了

[oracle@wyzc ~]$  vim ./.nls
export NLS_LANG='simplified chinese_china.al32utf8'
[oracle@wyzc ~]$  chmod 777 .nls
[oracle@wyzc ~]$  ./.nls
[oracle@wyzc ~]$ 11g
[oracle@wyzc ~]$ sqlplus scott/scott@wyzc11g
SQL*Plus: Release 11.2.0.3.0 Production on 星期一 2月 19 16:57:03 2018
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
连接到: 
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

SQL> select sysdate from dual;  # 查看日期格式
SYSDATE
--------------
19-2月 -18
SQL> select ename,sal,hiredate from emp where hiredate='03-12月-81';        
ENAME         SAL HIREDATE
---------- ---------- --------------
JAMES             950 03-12月-81
FORD             3000 03-12月-81
  • 逻辑运算符
    AND 所有条件都满足,返回true
    OR 任意一个条件,即返回true

排序规则:
1.数字按照数字的大小顺序排列,如升序1到100
2.日期按照时间顺序排序,如升序1月1日到12月31日
3.字母按照字母顺序排序,如升序A到Z
4.排序认为null为最大值

格式:order by 字段 asc(升序,默认) 或者 desc(降序)
# 注意上文comm排序,升序时空值排后
# order by 使用别名排序
SQL> select ename,sal,(sal+nvl(comm,0))*12 total from emp order by total;

# 使用select后列所在位置排序,这里从1开始,下文即按sal排序
SQL> select ename,sal,comm from emp order by 2 desc;

# 使用多列排序

SQL> select ename,sal,comm from emp order by sal,comm;

SQL> update emp set sal=null where sal=3000;
SQL> select ename,sal from emp order by sal desc;
ENAME         SAL
---------- ----------
FORD            # null为最大值
SCOTT
KING         5000
JONES        2975
BLAKE        2850
CLARK        2450
ALLEN        1600
.......................
SQL> select ename,comm,sal+nvl(comm,0) from emp order by sal+nvl(comm,0);

# 这里select子句执行sal+nvl(comm,0)一次,from子句也执行一次。
SQL> select ename,comm,sal+nvl(comm,0) tsal from emp order by tsal;
SQL> select ename,comm,sal+nvl(comm,0) tsal from emp order by 3;
# 如果使用别名,只执行sal+nvl(comm,0)一次,避免多次运算。
结果均为:

ENAME        COMM SAL+NVL(COMM,0)
---------- ---------- ---------------
SMITH                 800
JAMES                 950
ADAMS                1100
MILLER               1300
TURNER          0        1500
WARD          500        1750
ALLEN         300        1900
CLARK                2450
MARTIN       1400        2650
BLAKE                2850
JONES                2975

ENAME        COMM SAL+NVL(COMM,0)
---------- ---------- ---------------
SQL> select ename,ename||' '||sal ename from emp order by ename;
select ename,ename||' '||sal ename from emp order by ename
ERROR at line 1:
ORA-00960: ambiguous column naming in select list 
# 排序列名不唯一,只能使用位置
SQL> c/by ename/by 2
  1* select ename,ename||' '||sal ename from emp order by 2
SQL> /
ENAME      ENAME
---------- ------------------
ADAMS      ADAMS 1100
ALLEN      ALLEN 1600
BLAKE      BLAKE 2850
CLARK      CLARK 2450
---------- ------------------
SQL> 
KING                 5000
FORD                    #空值
SCOTT                   #空值

#在oracle中,判断一个字段或者一个变量是否为null 语法是 is null。返回的结果是boolean型。如果使用=null,会返回null。如果在代码或语句中使用,可能会获取不到结果或者报错。
#另外,null并不表示不存在,而是未知,我们通常成为“UNKNOWN”。
SQL> update emp set sal=3000 where sal is null; # 正确
2 rows updated
SQL> update emp set sal=3000 where sal=null; # 错误

5). 替代变量

SQL> select ename,sal from emp where empno=&e;
Enter value for e: 7900
old   1: select ename,sal from emp where empno=&e
new   1: select ename,sal from emp where empno=7900
ENAME         SAL
---------- ----------
JAMES         950
SQL> select &e,sal from emp where &&e=7788;
Enter value for e: empno
Enter value for e: empno
old   1: select &e,sal from emp where &&e=7788
new   1: select empno,sal from emp where empno=7788

     EMPNO    SAL
---------- ----------
      7788   3000

SQL> select &&e,sal from emp where &e=7788;
old   1: select &&e,sal from emp where &e=7788
new   1: select empno,sal from emp where empno=7788

     EMPNO    SAL
---------- ----------
      7788   3000

SQL> undefine e
# 替代变量的执行顺序是从右向左
SQL> select &&e,sal from emp where &e=7788; 
Enter value for e: empno
old   1: select &&e,sal from emp where &e=7788
new   1: select empno,sal from emp where empno=7788

     EMPNO    SAL
---------- ----------
      7788   3000

SQL> show verify
verify ON
SQL> set verify off
SQL> show verify
verify OFF
SQL> define v1=7788
SQL> select ename,sal from emp where empno=&v1;
# 当verify为off时,这里不显示old、new提示.
ENAME         SAL
---------- ----------
SCOTT        3000

SQL> set  verify on
SQL> show verify 
verify ON
SQL> select ename,sal from emp where empno=&v1;
old   1: select ename,sal from emp where empno=&v1
new   1: select ename,sal from emp where empno=7788
ENAME         SAL
---------- ----------
SCOTT        3000
root@ubuntu:~# vim login.sql
set verify off
root@ubuntu:~# chmod 777 login.sql 
root@ubuntu:~# ./login.sql
root@ubuntu:~# sqlplus  scott/scott@wyzc11g
SQL> show verify
verify OFF

你可能感兴趣的:(sqlplus 简单查询)