Oracle基本操作
2012-05-03 13:40
281 查看
1、分支语句:
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中创建存储过程:
Java代码调用存储过程:
此sql语句查询薪水在3到5名之间
5、添加外键约束。
alter table test2 add constraint test1_test2_fk foreign key (id) references test1(id)
禁用约束:
6、关于索引。通过索引可以快速定位数据,减少磁盘I/O,oracle自动使用和维护索引。在定义注主键或唯一约束时系统会自动在相应的字段上创建唯一索引。为某个字段添加索引后,可以提供查询速度。但是若在某个表进行频繁的增删改,索引会降低性能。因为每次更新时,数据库都会重新编排索引。
查询现有的索引:
select empno,ename,sal, case deptno when 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 replace procedure myProcedure(myDeptno in number,sumPeople out number) as begin select count(*) into sumPeople from scott.emp where deptno=myDeptno group 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);
相关文章推荐
- Solaris下Oracle的基本操作
- oracle 基本操作 未完待续
- Oracle基本操作九:流程控制(分支控制与循环控制)
- oracle的一些基本操作
- Solaris下Oracle的基本操作
- Oracle数据库对表数据的基本操作和Oracle字符串、数值、日期、空值操作及它们相对应的函数操作
- Oracle基本操作十三:函数和包
- oracle基本操作手册
- 通过Navicat Premium 实现Oracle基本操作
- Oracle关于日志文件基本操作
- oracle 一些基本操作语句
- Oracle下SQL基本操作(二)
- ORACLE 01 基本操作
- ’;oracle表,视图,索引,序列,同义词等基本操作总结(作者:西西)
- oracle对表的基本操作
- 对Oracle的增删改查基本操作
- oracle 基本操作
- Linux下Oracle的基本操作
- oracle-pl/sql基本操作1
- Oracle 基本操作二:内连接,左连接,右连接,自连接,集合查询(union)