根据this documentation , PostgreSQL 不支持无符号整数。
虽然我知道它使类型解析系统变得不那么复杂,但我不明白这对于自动递增 ID 来说有何实际意义。
添加无符号整数作为自动增量 ID 不会导致使用常规 int 的可能记录数量增长两倍吗? (4294967296
而不是 2147483648
)
我知道它只意味着 1 位之间的差异,但它仍然是您目前永远不会使用的位。
谢谢!
最佳答案
按照惯例,ID 是从 1 开始的正整数(据我所知,标准并未强制执行这一点,但如果确实如此,也不会感到完全惊讶)。 PostgreSQL serial
数据类型实现了此约定,并且值也是“自动生成的”。
如果您确实希望实现自己的方法,可以这样做:
create sequence epictable_seq MINVALUE -2147483648 start -2147483648 increment 1 NO MAXVALUE CACHE 1;
✓
CREATE TABLE epictable ( mytable_key INT unique not null, moobars VARCHAR(40) not null, foobars DATE );
✓
insert into epictable(mytable_key, moobars,foobars) values (nextval('epictable_seq'),'delicious moobars','2012-05-01') , (nextval('epictable_seq'),'worldwide interblag','2012-05-02') ;
2 rows affected
select * from epictable ;
mytable_key | moobars | foobars ----------: | :------------------ | :--------- -2147483648 | delicious moobars | 2012-05-01 -2147483647 | worldwide interblag | 2012-05-02
db<> fiddle here
关于sql - 为什么 PostgreSQL 不使用无符号整数作为 ID?这不是会提供两倍的可能记录吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54337193/