Oracle基本操作

来源:互联网 发布:python爬虫框架排行榜 编辑:程序博客网 时间:2024/05/17 07:08
1、分支语句:
select empno,ename,sal,case deptnowhen 10 then '财务部'when 20 then '研发部'else '其他部门'end 部门from scott.emp;
select empno,ename,sal,decode(deptno,10,'财务部',20,'研发部','其他部门') 部门from scott.emp;

2、分组函数不能和单行函数一起使用。
分组函数有:count,avg,max,min,sum。
并且出现在select列表中的字段或者出现在order by后面的字段,如果不是包含在分组函数中,那么

该字段必须同时在group by子句中。
如下例子:
select ename,avg(sal) from scott.emp 不合法
select initcap(ename),avg(sal) from scott.emp; 不合法。
select ename,avg(sal) from scott.emp group by ename; 合法

3、存储过程。在oracle中创建存储过程:
create or replaceprocedure myProcedure(myDeptno in number,sumPeople out number)asbeginselect count(*) into sumPeoplefrom scott.empwhere deptno=myDeptnogroup by deptno;end myProcedure;

Java代码调用存储过程:
Connection conn = OracleUtils.getConnection();        String sql = "{call myProcedure(?,?)}";        CallableStatement call = conn.prepareCall(sql);        call.registerOutParameter(2, java.sql.Types.INTEGER);        call.setInt(1, 10);        call.execute();        int sum = call.getInt(2);        System.out.println(sum);
4、oracle分页查询
select * from (select rownum no,e.* from  (select * from emp order by sal desc) e where rownum<=5 ) where no>=3; 


 此sql语句查询薪水在3到5名之间
select *  from  (select rownum no,e.* from (select * from emp order by sal desc) e)  where  no>=3 and no<=5; 

5、添加外键约束。

alter table test2 add constraint test1_test2_fk foreign key (id) references test1(id)

禁用约束:
alter table test2 disable constraint test1_test2_fk;
启用约束:
alter table test2 enable constraint test1_test2_fk;
删除约束:
alter table test2 drop constraint test1_test2_fk;

6、关于索引。通过索引可以快速定位数据,减少磁盘I/O,oracle自动使用和维护索引。在定义注主键或唯一约束时系统会自动在相应的字段上创建唯一索引。为某个字段添加索引后,可以提供查询速度。但是若在某个表进行频繁的增删改,索引会降低性能。因为每次更新时,数据库都会重新编排索引。
查询现有的索引:
select * from user_indexes;
可获知索引建立在哪些字段上
select * from user_ind_columns;
创建索引:
create index abc on emp(id);







原创粉丝点击