微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

本节以数值型相互转换以及数值型和字符型的转换为例大体介绍了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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐