Oracle 隐式数据类型转换

Oracle类型转换规则:

  • 对于insert和update操作,oracle将值转换为受影响的的列的类型。
  • 对于select操作,oracle会将列的值的类型转换为目标变量的类型。

看如下实验:

1、创建一张表,字段id的类型为number,id字段创建索引,插入一条测试数据

create table test(id number);

create index idx_test_id on test(id);

insert into test values(1);

2、我们做如下查询,id的值设置为字符型的'1'

3、查看执行计划:

是不是很意外,Oracle没有进行类型转换。使用了索引扫描,把'1'认为是数值型的1。
其实任何的数值型都可以转换为字符型。因此在一个数值型的字段上,添加to_char函数是多余的。
老外总结了一张图,说明了哪些类型间可以直接转换,哪些需要在列上添加函数来转换,非常的好:

-----------------------------------------------------------------------------------------------------------------------------

增加一个nvarchar2(10)字段name

alter table TEST add NAME VARCHAR2(10);

插入测试数据

insert into test values(1,'1');

查询

explain plan for select * from test where name='1';
select * from table(dbms_xplan.display);

查询

explain plan for select * from test where name=1;
select * from table(dbms_xplan.display);

 

猜你喜欢

转载自blog.csdn.net/lw112190/article/details/108774781