只有一个varchar作为外键的MySQL表 [英] MySQL table with only a varchar as a foreign key
问题描述
如果我添加一个VARCHAR(512)键到第二个表,并添加一个外键约束它将512字节长的数据被保存两次?
如果是的话,有没有办法只持有索引的引用而不是varchar本身?
总之,我的问题是,在InnoDB中有一个高效如何将外键保存到长VARCHAR字段中?
非常感谢,
Yaniv
是的,如果你在引用表上有一个 VARCHAR(512)
列,数据将存在两次。 我建议您使引用表的外键引用第一个表的整数主键,而不是512字节的数据。这是标准化的全部。
I have a table with a single unique VARCHAR(512) field. I want to have another table hold a foreign key reference to this first table. Both tables use InnoDB. If I add a VARCHAR(512) key to the second table and add a foreign key constraint on it will the 512 bytes long data be held twice?
If so, is there a way to hold only a reference to the index and not to the varchar itself?
In short, my question is, in InnoDB is there an efficient way to hold foreign keys to long VARCHAR fields?
Thank you very much,
Yaniv
Yes, if you have a VARCHAR(512)
column on the referencing table, the data will exist twice.
I recommend that you make the referencing table's foreign key refer to an integer primary key for the first table, not the 512-byte data. This is kind of what normalization is all about.
这篇关于只有一个varchar作为外键的MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!