用用户输入的数据实现多对多查找表的最佳方法 [英] Best way to implement many-to-many lookup table with user-entered data

查看:81
本文介绍了用用户输入的数据实现多对多查找表的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我希望用户选择一种或多种联系方式(电子邮件,电话,传真等).如果他们选择其他方式,则可以输入自己的一种联系方式.将其存储在数据库中的最佳方法是什么?我看到三种可能性:

Say I want users to pick one or more contact methods (email, phone, fax, other, etc). And if they choose other, then they can enter a single contact method of their own. What's the best way to store this in a database? I see three possibilities:

  1. 使用set数据类型列以及一个"other_contact" varchar列来存储用户输入的可选值.
  2. 使用多对多和用户输入的列.因此,将有一个用户表,一个contact_method表和一个将两者连接的user_contact_method表.加上一个user.other_contact varchar列来存储可选的用户输入值.
  3. 只需使用多对多.与2相同的设置,但允许用户将条目添加到contact_method表中.但这意味着我必须添加一列以跟踪系统"值(用户无法更改或删除这些值,只有这些值会显示在下拉列表中).另外,我必须添加额外的逻辑以允许更改其用户输入的值.

对以上内容有何评论,还是有更好的解决方案?

Any comments on the above, or is there a better solution?

推荐答案

如果您有一个用户和几种联系方式,则很可能是一对多关系. (由于可能会增加额外的复杂性以及查询此类模型的速度,我会尽可能避免多对多.)

If you have one user and several contact method possibilities, it's most probably a one-to-many relationship. (I would avoid many-to-many wherever you can, because of the additional complexity and the slow down of querying such models.)

users_table 
id INTEGER
name VARCHAR 
etc.

contacts_table
user_id INTEGER -- foreign key on user.id
contact_method ENUM('email', 'phone', 'mail', 'other')
contact_address VARCHAR
etc.

这篇关于用用户输入的数据实现多对多查找表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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