Best way to implement many-to-many lookup table with user-entered data

风流意气都作罢 提交于 2019-12-13 06:29:41

问题


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. Use a set datatype column, plus a single "other_contact" varchar column to store the optional user-entered value.
  2. Use many-to-many, and a user-entered column. So there would be a user table, a contact_method table, and a user_contact_method table that connects the two. Plus a user.other_contact varchar column to store the optional user-entered value.
  3. Just use many-to-many. Same setup as 2 but let users add entries to the contact_method table. But this means I'll have to add a column to keep track of "system" values (these cannot be changed or deleted by users, and only these show up in a dropdown). Plus I'll have to add extra logic to allow their user-entered value(s) to be changed.

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


回答1:


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.


来源:https://stackoverflow.com/questions/1186736/best-way-to-implement-many-to-many-lookup-table-with-user-entered-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!