有没有办法将PostgreSQL列定义为枚举?
在PostgreSQL DB中,想象一下您要添加一个称为warter_type
的列,并且您只希望以下3个条目之一出现:car
,truck
/code>,或
摩托车
。我认为可以以某种方式定义这个方法,但是请尝试我可能会阅读很多文章,我不知道该怎么做。
注意:我正在与DBEAVER客户端合作,但也可以运行任何SQL命令。
有办法吗?
In a PostgreSQL DB, imagine you want to add a column called vehicle_type
and you only want one of the following 3 entries to ever possibly appear: car
, truck
, or motorcycle
. I thought there would be a way to define this somehow but try a I might, and reading lots of articles, I couldn't figure out how to do it.
Note: I'm working with the DBeaver client but also can run any SQL commands too.
Is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此处阅读手册。
em>“您只希望以下3个条目之一出现” ,也不会使其无效。
枚举
占据4个字节。 To keep the disk footprint to a minimum, you could alternatively use a效力:小,快速。
缺点:特定于Postgres的,不那么可读。
db<>
;
Read the manual here.
Since " you only want one of the following 3 entries to ever possibly appear", also make it NOT NULL.
An
enum
occupies 4 bytes. To keep the disk footprint to a minimum, you could alternatively use a"char"
field instead which occupies 1 byte;Upsides: small, fast.
Downsides: Postgres-specific, less readable.
db<>fiddle here
See: