postgresql - 在 postgresql 9.3 中是否有任何技术可以使它开始返回 1 和 0 而不是 bool 类型的 "t"和 "f"

标签 postgresql postgresql-9.3

实际上,我正在将我的 ms sql server 迁移到 postgresql 9.3,我们使用 int 数据类型来存储 1 和 0 作为 bool 值,但在 postgresql9.3 中这是不可能的。当我对 postgresql 的数据类型进行一些研究时,我得到了什么:

In Postgresql:-
For Integer type datatype:-

insert into ask(aint) values(1)         working
insert into ask(aint) values('1')       working

insert into ask(aint) values(true)      not working

select * from ask where aint=1;     working
select * from ask where aint='1';   working
select * from ask where aint=true;  working


*For smallint type datatype:-

insert into ask(asmall) values(1);      working
insert into ask(asmall) values('1');    working
insert into ask(asmall) values(true);   working

select * from ask where asmall = 1      working
select * from ask where asmall = '1'    working
select * from ask where asmall = true   not working

因此,如果我想使用 smallint,那么我遇到的问题是,我无法比较任何 bool 字段,如果我使用整数,那么即使我可以比较 bool 字段,我也无法插入任何 bool 值。和

我不能使用 bool 数据类型,因为它返回“t”表示真,“f”表示假 所以我想要的是?

  1. 有什么方法可以比较 smallint 和 boolean 吗?
  2. 或者有没有办法在整数类型数据类型中插入 bool 值。
  3. 或者我们可以做些什么,让 bool 类型开始返回 1 表示真,0 表示假。

所以有人可以帮助我吗?这是紧急情况!谢谢

最佳答案

默认情况下,没有这样的转换 - 默认情况下,隐式转换仅用于类似组的数据类型(例如 integerbigint)。

幸运的是,如果确实需要,您始终可以添加新的隐式转换。

首先,我们将创建一个新函数来将 smallint 转换为 boolean。为此,我们将使用现有的 C 函数 bool_int4

CREATE OR REPLACE FUNCTION int2(boolean)
  RETURNS smallint AS
'bool_int4'
  LANGUAGE internal IMMUTABLE STRICT
  COST 1;

现在我们可以创建从 boolean 到 smallint 的新隐式转换:

CREATE CAST (boolean AS smallint) WITH FUNCTION int2(boolean) as implicit;

最后,我们准备直接比较 smallint 和 boolean:

select 1::smallint=true

PostgreSQL documentation警告大家当你不明智地使用隐式转换时可能会发生坏事:

It is wise to be conservative about marking casts as implicit. An overabundance of implicit casting paths can cause PostgreSQL to choose surprising interpretations of commands, or to be unable to resolve commands at all because there are multiple possible interpretations. A good rule of thumb is to make a cast implicitly invokable only for information-preserving transformations between types in the same general type category.

您可以从 pg_catalog 和表 pg_cast 中获取指定类型的转换列表:

select 
  castsource::regtype,
  casttarget::regtype,
  castcontext
from 
  pg_catalog.pg_cast
where
  castsource::regtype in ('boolean','smallint','integer') and
  casttarget::regtype in ('boolean','smallint','integer')
order by
  castsource::regtype,
  casttarget::regtype;

这将返回 (9.1.9):

castsource;casttarget;castcontext;
boolean;integer;e
smallint;integer;i
integer;boolean;e
integer;smallint;a

关于postgresql - 在 postgresql 9.3 中是否有任何技术可以使它开始返回 1 和 0 而不是 bool 类型的 "t"和 "f",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573327/

相关文章:

java - 在数据库事务期间进行 api 调用不是一个好习惯吗?

postgresql - 我是否需要将 ALL 授予 PostgreSQL 9.3 数据库中某个表的所有者?

C++ 和 ODB : Creating a vector of objects

node.js - Sequelize - 支持的最高 PostgreSQL 版本

sql - 使用 LAG() 和 PARTITION BY 返回日期后 10 天内的行

postgresql - AWS postgres 数据库实例未监听 5432 端口

java - 如何将CellStyle对象存储到数据库中?

sql - PostgreSQL:更新...缺少插入?

postgresql - 创建序列时在 AS 整数处或附近出现语法错误

ruby-on-rails-4 - 用于识别孤立子记录的 ActiveRecord 查询