Oracle type conversion rules:
- For insert and update operations, Oracle converts the value to the type of the affected column.
- For select operations, Oracle will convert the type of the column value to the type of the target variable.
Look at the following experiment:
1. Create a table, the type of the field id is number, create an index for the id field, and insert a piece of test data
create table test(id number);
create index idx_test_id on test(id);
insert into test values(1);
2. We do the following query, and the value of id is set to character type '1'
3. View the execution plan:
Is it surprising that Oracle did not perform type conversion. An index scan is used, and '1' is regarded as a numerical 1.
In fact, any numeric type can be converted to a character type. Therefore, adding the to_char function to a numeric field is redundant.
The foreigner summarized a picture showing which types can be directly converted, and which ones need to be converted by adding functions to the columns, which is very good:
-----------------------------------------------------------------------------------------------------------------------------
Add a nvarchar2(10) field name
alter table TEST add NAME VARCHAR2(10);
Insert test data
insert into test values(1,'1');
Inquire
explain plan for select * from test where name='1';
select * from table(dbms_xplan.display);
Inquire
explain plan for select * from test where name=1;
select * from table(dbms_xplan.display);