为什么无符号整数在PostgreSQL中不可用? [英] Why unsigned integer is not available in PostgreSQL?
问题描述
我碰到了这篇文章( MySQL中的tinyint,smallint,mediumint,bigint和int之间有什么区别?),并意识到PostgreSQL不支持无符号整数。
I came across this post (What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL?) and realized that PostgreSQL does not support unsigned integer.
在大多数情况下,我在MySQL中使用无符号整数作为自动递增的主键。在这种设计中,当我将数据库从MySQL移植到PostgreSQL时,该如何克服?
Most of the time, I use unsigned integer as auto incremented primary key in MySQL. In such design, how can I overcome this when I port my database from MySQL to PostgreSQL?
谢谢。
推荐答案
已经回答了为什么postgresql缺少无符号类型的问题。但是我建议对无符号类型使用域。
It is already answered why postgresql lacks unsigned types. However I would suggest to use domains for unsigned types.
http://www.postgresql.org/docs/9.4/static/sql-createdomain.html
CREATE DOMAIN name [ AS ] data_type
[ COLLATE collation ]
[ DEFAULT expression ]
[ constraint [ ... ] ]
where constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }
域就像一个类型,但有附加约束。
Domain is like a type but with an additional constraint.
举一个具体的例子,您可以使用
For an concrete example you could use
CREATE DOMAIN uint2 AS int4
CHECK(VALUE >= 0 AND VALUE < 65536);
当我尝试滥用该类型时,这是psql给出的。
Here is what psql gives when I try to abuse the type.
DS1 =#select(346346 :: uint2);
DS1=# select (346346 :: uint2);
错误:域uint2的值违反了检查约束 uint2_check
ERROR: value for domain uint2 violates check constraint "uint2_check"
这篇关于为什么无符号整数在PostgreSQL中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!