Mysql:多个表的一个枚举

假设我有两个表:</ p>

  mice 
id (int)petname(varchar)性别(enum(男,女))
1现货男
2米女

men
id(int)名称(varchar)性别(enum(男,女))
1 bob male

2 jane female
3 steve male
</ code> </ pre>

性别枚举存在两次。 每桌一次。 因此,如果想要满足sjw观众的需求,我需要使用新值扩展性别枚举,这意味着我必须手动编辑几个表中的枚举。 这不是最理想的。 </ p>

有没有办法定义枚举值一次并在几个表中使用它们?</ p>

或者也许创建一个名为genders的新表并放入 那里的性别,然后在插入和更新到老鼠和男人期间,它只允许性别列包含性别表中的值。 然而,此检查需要自动进行(与enum列类型一样),因此我不必先执行其他查询,以检查性别表中是否存在已使用的性别,在每次更新或插入我想要运行的查询之前, 在小鼠或男性的性别列中插入一个值。</ p>

这可以用mysql完成吗?</ p>
</ div>

展开原文

原文

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?

dongyan2469
dongyan2469 外键dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html?
2 年多之前 回复

1个回答



您可以忘记SQL的 enum </ code>。 相反,只需使用 VARCHAR(50)</ code>等,并在PHP代码中使用 const </ code>来定义 enum </ code>值。</ p> \ n

我很少使用SQL的 enum </ code>,在大多数情况下,它的好处小于它的复杂性。</ p>
</ div>

展开原文

原文

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。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐