首页 > 代码库 > Oracle语句

Oracle语句

Oracle语句-基本查询

--清屏

SQL> host cls

 

--当前用户

SQL> show user

USER "SCOTT"

--当前用户的表

SQL> select * from tab;                                           

 

--员工表的结构

SQL> desc emp

 

--查询所有的员工信息

SQL> select * from emp;

 

--设置行宽

SQL> show linesize

linesize 80

SQL> set linesize  150

--设置列宽

SQL> col ename format a8

SQL> col sal for 9999

SQL> /

 

     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                                                                   

     

 

--通过列名查询

SQL> select empno,ename,job,mgr,hiredate,sal,comm,deptno

  2  from emp;

 

--SQL优化的原则:

SQL> 1.尽量使用列名

SQL> host cls

 

--查询员工信息:

员工号 姓名 月薪 年薪

SQL> select empno,ename,sal,sal*12

  2  from emp;

 

SQL> --查询员工信息:员工号 姓名 月薪 年薪 奖金 年收入

SQL> select empno,ename,sal,sal*12,comm,sal*12+comm

  2  from emp;

 

     EMPNO ENAME      SAL     SAL*12       COMM SAL*12+COMM                                                                                           

---------- -------- ----- ---------- ---------- -----------                                                                                           

      7369 SMITH      800       9600                                                                                                                  

      7499 ALLEN     1600      19200        300       19500                                                                                           

      7521 WARD      1250      15000        500       15500                                                                                           

      7566 JONES     2975      35700                                                                                                                  

      7654 MARTIN    1250      15000       1400       16400                                                                                           

 

--SQL中的null值:

1. 包含null的表达式都为null

SQL> 2. null永远!=null

SQL> select empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0)

  2  from emp;

 

     EMPNO ENAME      SAL     SAL*12       COMM SAL*12+NVL(COMM,0)                                                                                    

---------- -------- ----- ---------- ---------- ------------------                                                                                    

      7369 SMITH      800       9600                          9600                                                                                    

      7499 ALLEN     1600      19200        300              19500                                                                                    

      7521 WARD      1250      15000        500              15500                                                                                    

      7566 JONES     2975      35700                         35700                                                                                    

 

--2. null永远!=null

SQL> --查询奖金为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                                                                   

      

--修改sql

--c

员工号 姓名 月薪

SQL> select empno,ename,sal

  2  form emp;

form emp

     *

2 行出现错误:

ORA-00923: 未找到要求的 FROM 关键字

 

SQL> --c命令 change

SQL> 2

  2* form emp

SQL> c /form/from

  2* from emp

SQL> /

 

--ed

已写入 file afiedt.buf

 

  1  select empno as "员工号",ename "姓名",sal 薪    水,sal*12,comm,sal*12+nvl(comm,0)

  2* from emp

SQL> /

select empno as "员工号",ename "姓名",sal 薪    水,sal*12,comm,sal*12+nvl(comm,0)

                                                *

1 行出现错误:

ORA-00923: 未找到要求的 FROM 关键字

 

 

SQL> ed

已写入 file afiedt.buf

 

  1  select empno as "员工号",ename "姓名",sal "薪    水",sal*12,comm,sal*12+nvl(comm,0)

  2* from emp

SQL> /

--distinct 去掉重复记录

SQL> select deptno from emp;

 

SQL> select distinct deptno from emp;

 

    DEPTNO                                                                                                                                            

----------                                                                                                                                            

        30                                                                                                                                            

        20                                                                                                                                            

        10                                                                                                                                            

 

SQL> select job from emp;

 

SQL> select distinct job from emp;

 

JOB                                                                                                                                                   

---------                                                                                                                                             

CLERK                                                                                                                                                 

SALESMAN                                                                                                                                              

PRESIDENT                                                                                                                                             

MANAGER                                                                                                                                               

ANALYST                                                                                                                                               

 

SQL> select distinct deptno,job from emp;

 

    DEPTNO JOB                                                                                                                                        

---------- ---------                                                                                                                                  

        20 CLERK                                                                                                                                      

        30 SALESMAN                                                                                                                                   

        20 MANAGER                                                                                                                                    

        30 CLERK                                                                                                                                      

        10 PRESIDENT                                                                                                                                  

        30 MANAGER                                                                                                                                    

        10 CLERK                                                                                                                                      

        10 MANAGER                                                                                                                                    

        20 ANALYST                                                                                                                                    

 

已选择 9 行。

 

--distinct 作用于后面所有的列

--连接符 ||

SQL> -- concat

SQL> select concat(‘Hello‘,‘  World‘);

select concat(‘Hello‘,‘  World‘)

                               *

1 行出现错误:

ORA-00923: 未找到要求的 FROM 关键字

 

SQL> select concat(‘Hello‘,‘  World‘) from emp;

--dual

SQL> select concat(‘Hello‘,‘  World‘) from dual;

 

CONCAT(‘HELL                                                                                                                                          

------------                                                                                                                                          

Hello  World                                                                                                                                          

 

SQL> select 3+2 from dual;

 

       3+2                                                                                                                                            

----------                                                                                                                                            

         5                                                                                                                                            

 

SQL> select * from tab;

 

TNAME                          TABTYPE  CLUSTERID                                                                                                     

------------------------------ ------- ----------                                                                                                     

DEPT                           TABLE                                                                                                                  

EMP                            TABLE                                                                                                                  

BONUS                          TABLE                                                                                                                  

SALGRADE                       TABLE                                                                                                                  

 

SQL> --dual表:伪表

SQL> --伪列

SQL> select ‘Hello‘||‘  World‘  字符串 from dual;

 

--字符串                                                                                                                                  

Hello  World                                                                                                                                          

 

SQL> --查询员工信息:***的薪水是****

SQL> select ename||‘的薪水是‘||sal 信息 from emp;

 

信息                                                                                                                                                  

----------------------------------------------------------                                                                                            

SMITH的薪水是800                                                                                                                                      

 

SQL> --字符串

SQL> select * from emp;

 

     EMPNO ENAME    JOB              MGR HIREDATE         SAL       COMM     DEPTNO                                                                   

---------- -------- --------- ---------- -------------- ----- ---------- ----------                                                                   

      7369 SMITH    CLERK           7902 17-12-80       800                    20        

 

--Spool记录笔记

SQL> spool e:\基本查询.txt                                                           

      SQL> spool off


本文出自 “qb的博客” 博客,谢绝转载!

Oracle语句