postgresql DB 中唯一键的正确数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11778102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the right data type for unique key in postgresql DB?
提问by socksocket
which data type should I choose for a unique key (id of a user for example) in postgresql database's table?
does bigint is the one?
我应该为 postgresql 数据库表中的唯一键(例如用户 ID)选择哪种数据类型?
bigint 是一个吗?
thanks
谢谢
回答by Mark Loiseau
Use the serial
type for automatically incrementing unique ids.
使用serial
自动增加唯一 ID的类型。
If you plan to have more than two billion entries, use bigserial
. serial
is the PostgresSQL equivalent of MySQL's AUTO_INCREMENT
.
如果您计划拥有超过 20 亿个条目,请使用bigserial
. serial
是 MySQL 的AUTO_INCREMENT
.
回答by a_horse_with_no_name
bigint
(or bigserial
if you need auto-incrementing keys) is just fine.
bigint
(或者bigserial
如果您需要自动递增键)就好了。
If knowfor certainthat you are not going to load too many rows, you might consider integer
(or a regular serial
) and potentially save some harddisk space.
如果知道了一定的,你是不会加载太多行,你可能会考虑integer
(或定期serial
),并潜在地节省一些硬盘空间。