现在的位置: 首页 > 综合 > 正文

Oracle学习<一>

2012年07月25日 ⁄ 综合 ⁄ 共 2116字 ⁄ 字号 评论关闭

一、解锁普通用户

  在sqlplus中输如用户名 sys/bjsxt as sysdba

  键入 alter user scott account unlock;

  注意要加分号。即可将scott解锁。

  登陆 conn scott/tiger

 

 

二、table structure

  1、查询语句

    -|描述一张表 desc + 表名

                属性:列名 | 是否为空 | 数据类型   

    -|select * from + 表名

      *表示取出所有数据

     -| 利用表dual进行算术计算

       如:select 2*3 from dual;

    -| 利用上表进行日期查询

       select sysdate from dual; 

    -| 给计算过的列取别名 

      select ename , sal*12 anuual_sal from emp;

          sal为月薪,annual_sal为sal*12的别名,这样便于阅读理解。

      期间 annual_sal中的_不能用空格代替。若要包含其他字符,先用双引号括起,其保持原来格式的作用。字母也不会默认从小学变大写

      select ename , sal*12 “anuual sal" from emp;

       ps:任何具有空值的表达式计算结果必然为空值

    -| ||两条竖线等同于java string 类中的 + 运算符

      select ename || sal from emp;

      或select ename ||'sadasdas' from emp;

    -|distinct

      select deptno from emp;
      select distinct deptno from emp;

      去掉deptno中重复字段,结果唯一显示。

      select distinct deptno from emp;
      select distinct deptno ,job from emp
      去掉deptno,job两者组合的重复。更多的项,就是这么多项的组合的不重复组合。

   -| where语句

      为过滤条件。可过滤相关记录,更加方便查找

      如 select * from emp where deptno = 10; 即在emp表中查找deptno等于10的表项。

      如 select * from emp where ename = ‘CLARK’;取出名字为clark的表项,其中,字符串的比较要加引号。

      

      范围查询

       select * from emp where sal > 1500;(运算符中<>为不等于的意思)

      select enamel,sal from emp where sal >=800 and sal<=1500;

      效果等于select ename,sal from emp where sal between 800 and 1500;

      

      空值处理

      查找空值所在

      select * from emp where comm is null;显示comm值为null的所有表项

      select * from emp where comm is not null;你懂的

 

      select * from emp where sal in (800,1500,2000);查找emp表中sal值为800,1500,2000的所有表项。

      字符串亦可

      select * from emp where ename in ('smith','king','clark');

 

      日期范围处理

      select * from emp where hiredate(存储日期的列)  > '20-2月-81'; 必须按照规定的日期的格式来写

 

      模糊查询

      select * from emp where ename like '%all%';

      取出ename中字符串中含有all的表项 

      其中%表示零个或多个字母而_代表一个字母。

      如果ename中的表项本来含有& _ 这类符号,需要加入转义字符。

      转义字符默认为反斜杠也可以自己指定,语法为escape '*';引号中的为自定义转义字符

      可用转义字符.\%. 还可以用escape '$'比如:select ename from emp where ename like '%$a%' escape '$';

 

 

      where中的判断条件可以用and or not条件限制。效果同。

 

    -| order by

      数据的排序

        select * from dept order by deptno desc;降序排列

        如果是select * from dept order by deptno asc;为升序排列

        其中升序可以不写 默认为升序

         select ename,sal,deptno from emp order by deptno asc,ename desc;

        先排列deptno后排列ename并不是同时排列

 

抱歉!评论已关闭.