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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题