小言_互联网的博客

oracle 的常用操作

427人阅读  评论(0)

1 oracle的增删改查

1.1增

1.1.1 新建表

creata table t_rule
(
id varchar2(50) not null,
ic_code VARCHAR2(200)
);

1.1.2 增加表字段

alert table 表 add 字段 varchar2(100);

1.1.3 增加数据

insert into t_inter_bank_legal(pk_id, inter_bank_id, legal_id, examine_result)
(select sys_guid(), ‘111111111’, s.pk_id, ‘’
from t_legal_doc_review s where s.pk_id in (‘1’,‘2’))

1.2 删

1.2.1 删除字段

alter table drop 字段 varchar2(100);

1.2.2 删除数据

delete from table_name where t.id = ‘1’;
delete d from t_detail d,t_rule r where d.name = r.name;
使用delete删除数据,索引不会变;
truncate table tt;
truncate 直接释放整个数据页,没有日志记录,不会产生日志无法回滚。

1.3 改

1.3.1 更新表名

rename table old_table to new_table;
alter table old_table rename as new_table;

1.3.2 更新表字段类型

alert table 表名 modify 列名 varchar2(32);

1.3.3 更新表字段名

alter table table_name change column old_name new_name varchar(255);

1.3.4 更新表数据

update table_name t set t.finish_time=‘2019’ where t.pk_id=‘222’;

1.4 查

1.4.1 语法格式

select column,group_function(column)
from table
[where condiion]
[group by group_by_expression]
[having group_condition]
[order by column]

1.4.2 执行顺序

from - where - group by - having - select - order by

having 过滤分组后的数据,只能出现在group by后面,where要在group by之前。
oracle 没有内部排序,同一条sql,在数据量大的时候数据顺序可能不同。

2 多表查询

2.1 笛卡尔积

select * from dept,emp;
左表中的每一条数据和右表中的所有数据对应一次;

2.2 等值查询

2.2.1 where

select * from dept d,emp e where d.id= e.id;

2.2.2 inner join on

select * from dept inner join emp on d.name = e.name;

2.2.3 using

select * from dept inner join emp using(name);

a. 两个表同名字段,需在该字段前面加上 “表名.” 前缀
b. 可以使用and 增加查询条件;
c. 使用表别名简化查询,使用"表名." 或 表别名提高查询效率

2.3 外连接

2.3.1 left join

2.3.1.1 left join on

select * from m left join n on m.name = n.name;

2.3.1.2 left outer join on

select * from m left outer join n on m.name = n.name;

2.3.1.3 where

select * from m ,n where m.name = n.name(+);

2.3.2 right join

2.3.2.1 right join on

select * from m right join n on m.name = n.name;

2.3.2.2 right outer join on

select * from m right outer join n on m.mame = n.name;

2.3.2.3 where

select * from m,n where m.name(+) = n.name;

2.3.3 full outer join

slect * from m full outer join n on m…name = n.name;

2.4 內连

select * from m natural join n;
on的条件为两张表中字段名相同的字段

2.5 表连接与子查询

为了使表连接和where子查询区分开,多表查询一般不使用where的方法。

3 oracle数据类型

3.1 varchar,varchar2,nvarchar2

3.1.1 介绍

varchar(50) 定长,即占用空间不变。空值不处理,最大长度2000。50 指字节。
varchar2(50) 可变长度,即占用空间随内容长度变化。空值返回null,最大长度4000。50 指字节。
nvarchar2(50) 可变长度。不论中文、英文都能存50个。

3.1.2 整理

varchar 和varchar2 使用utf-8编码,汉字占3个字节;nvarchar2 占2个字节。
查询数据库编码方式:
select userenv(‘language’) from dual;

4 常用的sql

4.1 分页,利用rownum

4.1.1 查询方法

select * from (select t.*,rownum indexNum from (T_S_ANALYSIS_TEMP_CONF)
t) where indexNum >0 and indexNum<2;
rownum 从1开始

4.1.2 整理

t.rownum 错误,rownum不能以任何表的名称作为前缀

4.2 树形结构

4.2.1 查询方法

向下递归,以org_code为根节点
select * from s_org s start with s.org_code = ‘111111111’
connect by prior s.org_code = s.suporg_code;

4.2.2 排序

order siblings by

4.3 case when then else end

eg.
case when tl.LEGAL_DIC_TYPE = ‘01’ then tl.check_file_num else 0 end
如果 值为’01’,结果为后面的 then,其他情况为0;
扩展
case
when 条件1 then 返回值1
when 条件1 then 返回值2
else 返回值3
end
类似于if – else if – else

4.4 decode

eg. decode(t.id,‘100’,‘hello’,‘world’) 和上面的类似
可以用作排序
order by decode(subject,‘tb_1’,1,‘tb_2’,2,3); 1、2、3 为数字

5 mybatis 常用语句

5.1 < where></ where>

eg.
select * from from s_org t
< where>
and t.org_merspl = ‘0’
</ where>
可以使用where 1=1 代替

5.2 < if></ if>

条件判断
eg.
select * from s_org t
< where>
< if test=“orgCode == ‘111111111’”>
t.flag=‘1’ and t.suporg_code=‘111111111’
</ if>
</ where>
< if test="_parameter!=null and _parameter!=’’">
select org_name from s_org where org_code = #{firstLevelInst}
</ if>

5.3 include

eg.
(< include refid=“flowChartSQL”/>)
< sql id=“flowChartSQL”>
select * from t_s_flow_chart
where menu_no=‘001002012001’
union all
select * from t_s_flow_chart_over
where menu_no =‘001002012001’
</ sql>

5.4 < foreach></ foreach>

public void delLegalDoc(String[] pkId) {
sessionTemplate.delete(“delLegalDoc”,pkId);
}
delete t_inter_bank_legal c where c.pk_id in
< foreach item=“pkId” collection=“array” index=“index”
open="(" separator="," close=")">
#{pkId}
</ foreach>

5.5 < choose></ choose>

< choose>
< when test="flag!=null and flag!=’’ ">
and (t.org_code = #{orgCode})
</ when>
< otherwise>
< if test="code!=null and code!=’’ ">
and (t.org_code = #{orgCode})
</ if>
< if test="level!=null and orgLevel==‘3’.toString() ">
and (t.org_code = #{orgCode})
</ if>
</ otherwise>
</ choose>

5.6 <![CDATA[ >'0' ]]>

在mapping中,> 不识别


转载:https://blog.csdn.net/qq_20013637/article/details/101219471
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场