查看一张表的表结构:
  desc     表名;
  desc     s_emp;
  SQL> desc     s_emp;
  Name                                                 Null?           Type
  -------------------------------------           --------           -------------
  ID                              员工编号           NOT NULL NUMBER(7)
  LAST_NAME            姓                      NOT NULL VARCHAR2(25)
  FIRST_NAME          名                                           VARCHAR2(25)
  USERID                    用户编                                  VARCHAR2(8)
  START_DATE           入职日期                              DATE
  COMMENTS             备注                                      VARCHAR2(255)
  MANAGER_ID          领导的员工编号                  NUMBER(7)
  TITLE                         职位                                      VARCHAR2(25)
  DEPT_ID                   部门编号                             NUMBER(7)
  SALARY                     月薪                                     NUMBER(11,2)
  COMMISSION_PCT 提成                                    NUMBER(4,2)
   select 语句
   A.  from 子句
         1.从数据库表中查询一个字段的值出来
            select  字段名 from  表名;
   select   salary     from    s_emp; 
         2.查询多个字段出来
           select  字段名1,字段名2   from  表名;
           select    first_name,salary  from s_emp;
         3. * 号可以代表所有的字段名
           select  *  from  s_emp;
         4.数学运算 
           + -  *  /
           select    salary/30 , salary*12   from  s_emp;
           select    salary*12+100  from s_emp;
           select   (salary+100)*12  from s_emp;
         5. 给字段或者表达式起别名 
           select    first_name   name    from s_emp; 
           select     (salary+100)*12   yearsal   from s_emp;  
           对一个字段或者表达式 别名 只能 有一个 
           如果想原样显示  则需要使用 ""
           select     (salary+100)*12   "year  sal"   from s_emp; 
          6.sql中如何表达字符串
             必须使用单引号  引起来
             'a'  ''   '  '  'hello world'    '_'    'test'
             select  first_name from s_emp;   
             字符串的拼接   
             把姓名连接之后显示
             select  first_name,last_name from s_emp;
             ||   字符串拼接符号
             select  first_name||last_name  from s_emp;
             select  first_name||'_'||last_name  from s_emp;
             拼接一个单引号 
             select  first_name||'''||last_name  from s_emp;  
             转义 -------%%(c)   ''''
             select  first_name||''''||last_name  from s_emp; 
          7.数据的排重
             select    salary  from  s_emp;
             重复的数据只显示一次  ------distinct
             select   distinct   salary  from  s_emp;   
             多字段联合排重   
             select   title ,salary  from s_emp;    
             select   distinct title ,salary  from s_emp; 
             select   distinct title ,distinct salary  from s_emp; //error
                           20                  18
             select   distinct id,title ,salary  from s_emp;  
          8.NULL 值的处理
             #define   NULL  (void*)0
             数据库中的NULL代表没有值 或者未知值
             把s_emp  表中的提成查询出来    
             select  commission_pct   from s_emp;  
             10000$*(1+commission_pct/100)
             考虑提成计算月薪
             select first_name, salary,salary*(1+commission_pct/100)  from s_emp;    
              为了处理null  值 引入了一个函数 
              nvl(par1,par2);
              要求两个参数的类型 必须一致
              当par1是NULL 则返回par2的值   如果par1不为null
               则返回par1本身的值
              select  commission_pct ,nvl(commission_pct,1) from  s_emp;    
              select first_name, salary, nvl(salary*(1+commission_pct/100),0)  sal from s_emp; 
              NULL 值要尽早处理   因为null值和任何值做运算都是NULL
              select first_name, salary, salary*(1+nvl(commission_pct,0)/100)  sal from s_emp;
              
                                         
   B.  where  子句
         限制表中数据的返回    where子句  条件子句   
         符合条件的就会被选中  不符合条件的就被过滤掉 
         1.查询 工资大于1400 的 人的 first_name,salary
          select  first_name,salary from s_emp;  
          select  first_name,salary from s_emp where 1=1;   
          select  first_name,salary from s_emp where 1=2;    
          select  first_name,salary from s_emp  where salary>1400; 
         2.找出部门号是50的 first_name,dept_id,salary
           select  first_name,dept_id,salary  from s_emp where dept_id=50;   
         3.找出first_name 叫 Carmen 的 first_name,salary
            select  first_name,salary   from s_emp where first_name='Carmen';     
            select  first_name,salary   from s_emp where first_name='carmen';    //logic error
            sql语句不区分大小写  但字符串的值要区分大小写
            SElecT  first_name,salary   from s_emp  where first_name='Carmen';    
         a.数学运算符  
           >  <  = >= <=   不等于 != ( 等同于 <> 与 ^= )            
         b.sql提供的运算符号
            表达一个闭区间   [a,b]    
            where  字段   between  a   and  b
            查询工资在 [1000,1400]  中的first_name,salary
            select   first_name,salary  from s_emp  where salary  between  1000  and  1400;    
            表达一个字段在一组值范围内
            查询 部门号 是  30,32,50  的 first_name,dept_id,salary
            字段  in (值列表);
            值列表 是用逗号隔开的一组值
             select  first_name,dept_id,salary from s_emp where dept_id in (30,32,50);         
             select  first_name,dept_id,salary from s_emp where dept_id in (50,32,30); 
             注意:我们应该把出现概率高的数据放前面
            
            判断NULL
            where   字段   is   null     判断字段的值是不是null
            找出manager_id  是null  的 员工的first_name 和salary  
            select   first_name,salary,manager_id   from  s_emp  where  manager_id is null;         
           模糊查询 
           通配符      ls   *.txt
           %              0-n 个任意字符
           _              一个任意字符
         
          东方不败      什么东东      东西南北
           
          找出 first_name 带a的  
          像  -------where  字段 like  '%a%';    
          select   first_name from s_emp where first_name like  '%a%';                                        
          找出first_name 第二个字符是a的 
          select   first_name from s_emp  where first_name like  '_a%';     
          desc    user_tables;
          table_name   
          s_emp 
          找出表名是s_ 开头的表 
          select table_name from   user_tables where  table_name like 'S_%'; 
          要对_ 进行转义
          select table_name from   user_tables where  table_name like 'S\_%' escape '\'; 
             
         between  a  and  b
         in (列表)
         is   null 
         like %  _
         
         逻辑条件连接符号 
         and   
         or 
         not   
         where   salary>=1000  and  salary<=1400;
         (1000,1400)
         where   salary>1000  and  salary<1400;    
         找出工资大于1000  并且部门号 大于30 的 
         first_name ,salary,dept_id
         select   first_name,salary,dept_id  from s_emp  where salary>1000  and  dept_id>30;   
         查询 部门号 是  30,32,50  的 first_name,dept_id,salary
          select   first_name,dept_id,salary from s_emp  where dept_id=30  or  dept_id=32 or  dept_id=50;
          not
          >                 <=
          <                 >=
           =                !=    <>   ^=
          between a and b    not  between a  and b
          in ()              not  in()   注意NULL  
          like               not  like    
          is  null           is   not   null 
          
          select     first_name  from  s_emp where first_name not  like   '%a%';
          找出提成不是null的员工的 first_name,commission_pct
          select     first_name,commission_pct    from s_emp
                 where   commission_pct   is  not  null;               
           SQL>edit      
           进入标准的vi操作 
           SQL> /
          改变逻辑运算的顺序         
           where    dept_id=41   or  dept_id=42   and  salary>1000;
           where    (dept_id=41   or  dept_id=42)  and  salary>1000;
   C.  order   by   子句
        数据的排序
        select   first_name,salary  from s_emp;  
        order    by    排序标准;
        select   first_name,salary  from s_emp where  salary>1000 order  by  salary;    
        默认的排序标准 是升序     升序的关键字   asc 
        select   first_name,salary  from s_emp where  salary>1000 order  by  salary asc;  
        降序的关键字 
        select   first_name,salary  from s_emp where  salary>1000 order  by  salary desc; 
        升序      自然顺序      字典顺序 
        NULL在排序中的处理,默认为最大
        select  first_name, manager_id   from s_emp  order by  manager_id;              
        select  first_name, manager_id   from s_emp  order by  manager_id  desc; 
         
        select  first_name,salary  from s_emp  where salary>1000  order  by  salary asc;  
         如果工资相同   则希望再通过first_name 降序排
         主排序   从排序字段
         多字段排序
         select   first_name,salary  from s_emp
               where   salary>1000  
                      order  by  salary asc,first_name desc;    
        order by    排序标准   排序类型;
        排序标准  日期   面积   数量 ....; 
        可以有多个字段的排序。 
        排序类型   升序 asc   降序 desc;
  
        null  值在排序中作为最大值处理         
        按照入职日期排序   如果入职日期相同 则按照工资降序排
        列出 first_name   start_date    salary
        select   first_name,start_date ,salary  
             from  s_emp  where 1=1  order by  start_date,salary desc; 
         select id,to_char(start_date,'yyyy-mm-dd')  from s_emp;   
   D.  单行函数
         单行函数:对sql语句影响的每一行都返回一个结果
                        sql语句影响多少行就返回多少个结果
         upper(字段)
         select  first_name,upper(first_name) from s_emp;
         select  first_name,upper(first_name) from s_emp where id=-1;
         组函数 :  对sql语句影响一组数据做处理  最终返回一个结果
         count(字段)
         select  count(first_name) from s_emp;        
         select  count(first_name) from s_emp where id=-1;   
        为了测试单行函数的功能      
        dual  单行单列的表
        desc  dual; 
        select * from dual;
        处理字符串的单行函数
  upper(字段/字符串) 让字符串变大写
   
        select upper('hello world')   from dual;
        create table testdual  as select  id from s_emp where  id=1;
        select upper('hello world')   from testdual;
  
        lower(字段/字符串) 让字符串变小写
        select   lower('TEST')   from dual;  
  
        initcap(字段/字符串) 把每个单词的首字母变大写 
        select  initcap('one  world one dream')  from dual; 
  
        concat(par1,par2) 字符串连接
        select   concat('hello ','world')   from dual;
        因为有 ||  所有这个函数很少使用
        select  'hello'||'world'||'hello'  from  dual;  
        select   concat(concat('hello ','world'),'hello')   from dual;  
  
        length(字符串)
        select length('hello')  from dual; 
  
        substr(par1,par2,par3)
        par1 要处理的字符串 或者字段 
        par2 要从哪个位置开始截取 从1开始编号  可以是负数
        par3 截取多长
        select   substr('hello',0,3)  from dual;  
        select   substr('hello',1,3)  from dual;   
        select   substr('hello',-2,3) from dual;   
        显示first_name 的 后三个字符
select first_name,substr(first_name,-3,3) from s_emp;
        结合length  和  substr   upper 显示 first_name 的 后三个字符
        并且大写显示
  select  first_name,substr(first_name,length(first_name)-2,3)   from s_emp;
        select  first_name,upper(substr(first_name,length(first_name)-2,3))   from s_emp;
                










