oracle的基本查询~上

SQL> --查询一下当前登录的用户名
SQL> show user;
USER 为 "SCOTT"
SQL> --查询当前用户下有哪些表
SQL> select * from tab;

TNAME                                                        TABTYPE           
------------------------------------------------------------ --------------    
 CLUSTERID                                                                     
----------                                                                     
DEPT                                                         TABLE             
                                                                               
                                                                               
EMP                                                          TABLE             
                                                                               
                                                                               
BONUS                                                        TABLE

TNAME                                                        TABTYPE           
------------------------------------------------------------ --------------    
 CLUSTERID                                                                     
----------                                                                     
SALGRADE                                                     TABLE

SQL> --如果出来的结果不好看 你可以自己去设置和调整
SQL> --查询当前一行显示多少个字符
SQL> show linesize;
linesize 80
SQL> set linesize 120;
SQL> show linesize;
linesize 120
SQL> select * from tab;

TNAME                                                        TABTYPE         CLUSTERID                                 
------------------------------------------------------------ -------------- ----------                                 
DEPT                                                         TABLE                                                     
EMP                                                          TABLE                                                     
BONUS                                                        TABLE                                                     
SALGRADE                                                     TABLE

SQL> --设置某一个字段的宽度
SQL> col tname for a8;
SQL> select * from tab;

TNAME    TABTYPE         CLUSTERID                                                                                     
-------- -------------- ----------                                                                                     
DEPT     TABLE                                                                                                         
EMP      TABLE                                                                                                         
BONUS    TABLE                                                                                                         
SALGRADE TABLE

SQL> --dept 部门表emp 员工表
SQL> -- 查询所有的员工信息
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          
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                    20          
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400         30          
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                    30          
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                    10          
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                    20          
      7839 KING                 PRESIDENT                     17-11月-81           5000                    10          
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0         30          
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                    20

EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO          
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------          
      7900 JAMES                CLERK                    7698 03-12月-81            950                    30          
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                    20          
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                    10

已选择14行。

SQL> --查询所有的部门信息
SQL> select * from dept;

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

SQL> col deptno for 99;
SQL> select * from dept;

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

SQL> --查看表的结构
SQL> desc dept;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 DEPTNO                                                            NOT NULL NUMBER(2)
 DNAME                                                                      VARCHAR2(14)
 LOC                                                                        VARCHAR2(13)

SQL> desc emp;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 EMPNO                                                             NOT NULL NUMBER(4)
 ENAME                                                                      VARCHAR2(10)
 JOB                                                                        VARCHAR2(9)
 MGR                                                                        NUMBER(4)
 HIREDATE                                                                   DATE
 SAL                                                                        NUMBER(7,2)
 COMM                                                                       NUMBER(7,2)
 DEPTNO                                                                     NUMBER(2)

SQL> col deptno for 9999999;
SQL> select * from dept;

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

SQL> col deptno for 99999999;
SQL> select * from dept;

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

SQL> col ename for a8;
SQL> select * from dept;

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

SQL> col dname for a20;
SQL> select * from dept;

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

SQL> --查询所有的另一种形式
SQL> select deptno,dname,loc from dept;

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

SQL> /*
SQL> SQL 优化的第一点: 如果再有查询所有的操作优先采用列出所有列明的方法查询,尽量少用* 代替所有。
SQL> */
SQL> --投影:查询一个大表里面的部分数据(列为单位)
SQL> --查询部门表里面的部门ID
SQL> select deptno from dept;

DEPTNO                                                                                                              
---------                                                                                                              
       10                                                                                                              
       20                                                                                                              
       30                                                                                                              
       40

SQL> select deptno,dname from dept;

DEPTNO DNAME                                                                                                        
--------- --------------------                                                                                         
       10 ACCOUNTING                                                                                                   
       20 RESEARCH                                                                                                     
       30 SALES                                                                                                        
       40 OPERATIONS

SQL> --查询 所有员工的 ID NAME SAL COMM 年薪
SQL> desc emp;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 EMPNO                                                             NOT NULL NUMBER(4)
 ENAME                                                                      VARCHAR2(10)
 JOB                                                                        VARCHAR2(9)
 MGR                                                                        NUMBER(4)
 HIREDATE                                                                   DATE
 SAL                                                                        NUMBER(7,2)
 COMM                                                                       NUMBER(7,2)
 DEPTNO                                                                     NUMBER(2)

SQL> select empno,ename,sal,comm,sal*12 + comm
  2  form emp;
form emp
     *
第 2 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字

SQL> 2
  2* form emp
SQL> c /form/from;
  2* from emp
SQL> /

EMPNO ENAME           SAL       COMM SAL*12+COMM                                                                  
---------- -------- ---------- ---------- -----------                                                                  
      7369 SMITH           800                                                                                         
      7499 ALLEN          1600        300       19500                                                                  
      7521 WARD           1250        500       15500                                                                  
      7566 JONES          2975                                                                                         
      7654 MARTIN         1250       1400       16400                                                                  
      7698 BLAKE          2850                                                                                         
      7782 CLARK          2450                                                                                         
      7788 SCOTT          3000                                                                                         
      7839 KING           5000                                                                                         
      7844 TURNER         1500          0       18000                                                                  
      7876 ADAMS          1100

EMPNO ENAME           SAL       COMM SAL*12+COMM                                                                  
---------- -------- ---------- ---------- -----------                                                                  
      7900 JAMES           950                                                                                         
      7902 FORD           3000                                                                                         
      7934 MILLER         1300

已选择14行。

SQL> -- 总结c(change)命令 首先定位到你错误的哪一行(直接输入错误的行号回车), c空格/错误的单词/正确的单词
SQL> --/可以用来运行上一条sql语句
SQL> /*
SQL> 如果你在做算术运算的时候,某一个字段为空(null),那么他的到的结果就是null
SQL> 在oracle中null 永远不等于null
SQL> */
SQL> --如果你要解决null的问题你需要用到一个oracle的虑空函数
SQL> select empno,ename,sal,comm, sal*12 + nvl(comm,0)
  2  from emp;

EMPNO ENAME           SAL       COMM SAL*12+NVL(COMM,0)                                                           
---------- -------- ---------- ---------- ------------------                                                           
      7369 SMITH           800                          9600                                                           
      7499 ALLEN          1600        300              19500                                                           
      7521 WARD           1250        500              15500                                                           
      7566 JONES          2975                         35700                                                           
      7654 MARTIN         1250       1400              16400                                                           
      7698 BLAKE          2850                         34200                                                           
      7782 CLARK          2450                         29400                                                           
      7788 SCOTT          3000                         36000                                                           
      7839 KING           5000                         60000                                                           
      7844 TURNER         1500          0              18000                                                           
      7876 ADAMS          1100                         13200

EMPNO ENAME           SAL       COMM SAL*12+NVL(COMM,0)                                                           
---------- -------- ---------- ---------- ------------------                                                           
      7900 JAMES           950                         11400                                                           
      7902 FORD           3000                         36000                                                           
      7934 MILLER         1300                         15600

已选择14行。

SQL> --查询comm为null的员工信息
SQL> select *
  2  from emp
  3  where comm = null;

未选定行

SQL> select *
  2  from emp
  3  where comm is null;

EMPNO ENAME    JOB                       MGR HIREDATE              SAL       COMM    DEPTNO                       
---------- -------- ------------------ ---------- -------------- ---------- ---------- ---------                       
      7369 SMITH    CLERK                    7902 17-12月-80            800                   20                       
      7566 JONES    MANAGER                  7839 02-4月 -81           2975                   20                       
      7698 BLAKE    MANAGER                  7839 01-5月 -81           2850                   30                       
      7782 CLARK    MANAGER                  7839 09-6月 -81           2450                   10                       
      7788 SCOTT    ANALYST                  7566 19-4月 -87           3000                   20                       
      7839 KING     PRESIDENT                     17-11月-81           5000                   10                       
      7876 ADAMS    CLERK                    7788 23-5月 -87           1100                   20                       
      7900 JAMES    CLERK                    7698 03-12月-81            950                   30                       
      7902 FORD     ANALYST                  7566 03-12月-81           3000                   20                       
      7934 MILLER   CLERK                    7782 23-1月 -82           1300                   10

已选择10行。

SQL> spool off;

上一篇:01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类


下一篇:计算机网络协议分析 第八课 ICMPv6 UDP