dongshi4773 2018-03-09 08:39
浏览 186

Mysql:多个表的一个枚举

Let's say I have two tables:

mice
id (int) petname (varchar) gender (enum (male, female))
1        spot            male
2        rice           female

men
id (int) name (varchar) gender (enum (male, female))
1        bob            male
2        jane           female
3        steve          male

The gender enum exists twice. Once for each table. So if wanted to cater to say the sjw audience, I would need to expand the gender enum with new values, which would mean that I would have to manually edit the enums in several tables. This is suboptimal.

Is there any way to define the enum values once and use them in several tables?

Or perhaps to create a new table named genders and put the genders there, and then during inserts and updates into mice and men, it would only allow the gender column to contain values form the gender table. This check however needs to happen automatically (like with enum column type), so I don't have to execute additional queries first to check if the used gender exists in the gender table, before each update or insert query I want to run, which inserts a value into a gender column in mice or men.

Can this be done with mysql?

  • 写回答

1条回答 默认 最新

  • dongyi2006 2018-03-09 08:44
    关注

    You can forget about SQL's enum. Instead, just use VARCHAR(50), etc, and use const in your PHP code to define the enum values.

    I seldom use SQL's enum, in most cases, its benefit is less than its complexity。

    评论

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示