如何在 postgresql 上创建类似 SET 的类型
我想创建一列工作日,以便我们可以选择更多的一天。
我知道枚举类型可以做到这一点,但它只能包含一项。
如何在 PostgreSQL 中创建一个数据类型,以便我可以拥有像多项选择枚举一样的功能,就像 MySQL 上的集合一样?
I would like to create a column of week days such that we can select more then only one day.
I know the enum type can do that, but it can only contain one item.
How can I create a datatype in PostgreSQL such that I can have something that fuctions like a multiple-choice enum, just like a set on MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
HSTORE
列类型。HSTORE
存储键/值对。如果您只关心检查键是否存在,则可以使用空值。请参阅https://www.postgresql.org/docs/current/static/hstore。 html。
例如,要询问Is 'x' in my hstore?,
我是否相信这是操作O(1)。相反,检查
ARRAY
类型列中的包含情况是 O(n)。Use the
HSTORE
column type.HSTORE
stores key/value pairs. You can use null values if you only care about checking if a key exists.See https://www.postgresql.org/docs/current/static/hstore.html.
For example, to ask Is 'x' in my hstore?, do
I believe this is operation is O(1). In contrast, checking for containment in an
ARRAY
-type column, is O(n).我猜想数组是与可怕的集合数据类型最接近的匹配。
但该解决方案并未标准化,因此您可能会遇到几个问题。我建议将其存储在正确规范化的表中,特别是如果您计划查询所选值或对此进行其他报告。
I guess an array is the closest match to the dreaded set data type.
But that solution is not normalized, and you will probably run into several issues because of that. I'd recommend to store that in a properly normalized table, especially if you plan to query on the selected values or do other reporting on that.
位字符串
BIT(n)
是PostgreSQL 与 MySQL 的SET
类型最接近。对于一周中的几天,请使用
BIT(7)
。与 MySQL 的
SET
不同,BIT
类型的宽度不限于 64 位或更少。Bit Strings
BIT(n)
are the closest PostgreSQL has to MySQL'sSET
types.For days of the week, use
BIT(7)
.Unlike MySQL's
SET
, the width of aBIT
type is not constrained to 64 bits or less.