只有一个varchar作为外键的MySQL表 [英] MySQL table with only a varchar as a foreign key

查看:169
本文介绍了只有一个varchar作为外键的MySQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个唯一的VARCHAR(512)字段的表。我想让另外一个表保存对这个第一个表的外键引用。两个表都使用InnoDB。
如果我添加一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆