Oracle vs PostgreSQL,研发注意事项(7)- 类型转换

2023-05-20,,

本节以数值型相互转换以及数值型和字符型的转换为例大体介绍了Oracle和PostgreSQL类型转换上的部分异同,可据此思路推广到其他类型。

一、数值类型转换

下面以数值类型为例子说明,包括运算结果的转换和强制类型转换.
运算结果
以除运算为例说明.
PostgreSQL的除运算

testdb=# select 1/4;
 ?column? 
----------
        0
(1 row)

Oracle的除运算

TEST-orcl@server4>select 1/4 from dual;

       1/4
----------
       .25

两个整型值1和4参与除法运算,结果PostgreSQL为整型的0,Oracle为浮点型的0.25,两者的行为不一致.
为何PostgreSQL执行整型运算返回的结果是整型?当然,这是PG的机制(整型/整型=整型)使然,在PG中,运算的结果类型可查询pg_operator获得:

testdb=# \x
Expanded display is on.
testdb=# select * from pg_operator where oprname = '/' and oprleft=21 and oprright = 21;
-[ RECORD 1 ]+--------
oprname      | / -->运算符
oprnamespace | 11
oprowner     | 10
oprkind      | b
oprcanmerge  | f
oprcanhash   | f
oprleft      | 21 -->int2(占用2个字节的整型,通过select * from pg_type where oid=21查询可得)
oprright     | 21 -->同上
oprresult    | 21 -->整型/整型,结果也是整型
oprcom       | 0
oprnegate    | 0
oprcode      | int2div
oprrest      | -
oprjoin      | -

在PostgreSQL中,要想获得0.25的结果,需要进行转换:

testdb=# select 1/4::float;
 ?column? 
----------
     0.25
(1 row)

二、强制类型转换

以字符型->整型为例说明.
PostgreSQL

testdb=# drop table if exists t_cast ;
DROP TABLE
testdb=# create table t_cast (c_int int,c_s varchar(20));
CREATE TABLE
testdb=# insert into t_cast values(1,'1');
INSERT 0 1
testdb=# insert into t_cast values(2,'2');
INSERT 0 1
testdb=# select * from t_cast where c_int = 1;
 c_int | c_s 
-------+-----
     1 | 1
(1 row)

testdb=# select * from t_cast where c_s = 1;
ERROR:  operator does not exist: character varying = integer -->可变长字符型转换为整型
LINE 1: select * from t_cast where c_s = 1;
                                       ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Oracle

TEST-orcl@server4>drop table t_cast;

Table dropped.

TEST-orcl@server4>create table t_cast (c_int int,c_s varchar2(20)) tablespace users;

Table created.

TEST-orcl@server4>insert into t_cast values(1,'1');

1 row created.

TEST-orcl@server4>insert into t_cast values(2,'2');

1 row created.

TEST-orcl@server4>select * from t_cast where c_int = 1;

     C_INT C_S
---------- --------------------
         1 1

TEST-orcl@server4>select * from t_cast where c_s = 1;

     C_INT C_S
---------- --------------------
         1 1

PG,整型不能转换为字符型,而Oracle可以.
PG可以通过显式类型转换或者自定义类型转换的机制实现字符型->整型的转换:

-- 显式转换
testdb=# select * from t_cast where c_s = 1::varchar;
 c_int | c_s 
-------+-----
     1 | 1
(1 row)
-- 自定义类型转换
testdb=# create cast(varchar as integer) with inout as implicit;
CREATE CAST
testdb=# select * from t_cast where c_s = 1;
 c_int | c_s 
-------+-----
     1 | 1
(1 row)

通过数据字典表pg_cast可查询PG支持的类型转换.

testdb=# select oid,a.* from pg_cast a where castsource=1043 and casttarget = 23;
  oid  | castsource | casttarget | castfunc | castcontext | castmethod 
-------+------------+------------+----------+-------------+------------
 16774 |       1043 |         23 |        0 | i           | i --> 这是新加的记录

三、参考资料

CREATE CAST
PostgreSQL 自定义自动类型转换(CAST)

《Oracle vs PostgreSQL,研发注意事项(7)- 类型转换.doc》

下载本文的Word格式文档,以方便收藏与打印。