在 MySQL 中使用表提供枚举值?
有没有办法将 MySQL 表的列内容之一映射到 MySQL 中另一个表的枚举?我认为这是理所当然的,但似乎没有任何关于这个主题的信息。
关于此事的任何建议或帮助都会很酷,如果不可能,有人知道为什么不可能的内部原因吗?
向大家致以最诚挚的问候:)
加里
Is there a way to map one of the the columns contents of a MySQL table to an enum on another table in MySQL? I thought this would be a no brainer, but there doesn't seem to be any info that I can find on the subject.
Any advice or help on this matter would be cool and if it's not possible, does anyone know of an internal reason why it wouldn't be possible?
Best regards everyone :)
Gary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
enum 类型一次性使用很方便,但它不能很好地扩展到多个表,也不是标准 SQL。
这里最好的办法是使用普通的表和关系:
外键关系将限制可能值的列表;而且由于外键和关系是绝对标准的 SQL,因此这种方法还会有其他好处 - 例如报告工具可以识别外键并了解如何使用相关数据。
The enum type is handy as a one-off, but it doesn't scale well to multiple tables and isn't standard SQL either.
Best thing to do here is to use normal tables and relations:
The foreign key relation will do the job of restricting to a list of possible values; and because foreign keys and relations are absolutely standard SQL, this approach will have other benefits - for example reporting tools can recognise the foreign key and understand how to use the related data.
如果它不这样做,就不要这样做
当然,您只需要一个可能的键表,然后是一个映射到该表的外键。
如果您想要一个具有可能的枚举值和限制的表,请通过另一个表或同一个表中的 groupid 进行分组(如果组成员是唯一的)。
尽管明智地加入,但闻起来像餐桌臭味。也许最好在存储过程或应用程序代码中执行此操作并将其映射到本机值?
If it doesn't do it, don't do it
Surely you just want a table of possible keys and then a foreign key mapping to that.
If you want a table with possible enum values and restrictions, go for groupings via another table or a groupid in the same table (if group members are unique).
Smells like table-stink though JOIN wise. Maybe best doing this in a stored procedure or in the app code and mapping it to a native value?