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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:09:01  来源:igfitidea点击:

what is the right data type for unique key in postgresql DB?

databasepostgresqldatabase-designdata-warehouse

提问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 serialtype for automatically incrementing unique ids.

使用serial自动增加唯一 ID的类型。

If you plan to have more than two billion entries, use bigserial. serialis the PostgresSQL equivalent of MySQL's AUTO_INCREMENT.

如果您计划拥有超过 20 亿个条目,请使用bigserial. serial是 MySQL 的AUTO_INCREMENT.

PostgresSQL Documentation: Numeric Types

PostgresSQL 文档:数字类型

回答by a_horse_with_no_name

bigint(or bigserialif 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),并潜在地节省一些硬盘空间。