数据库设计:用户与联系信息
我有一个数据库设计问题。
在我的应用程序中,用户的联系信息包括
- 电话号码、
- 电子邮件
- 地址、第 1
- 行邮政信箱、
- 地点(城市),
该信息当前位于“用户”表中,其中包含用户名、名字等其他信息……
事实是,用户必须拥有相同类型的信息(电话、电子邮件等),但属于他的公司。
向“用户”表添加附加字段似乎是多余的。由于数据相似,我可以制作一个包含以下字段的“ContactInfo”表:
- 电话
- 电子邮件
- 地址
- 邮政信箱
- ...
这是一个好主意吗?我应该如何处理“user”表和这个“contactInfo”表之间的关系?
编辑:我忘了说公司联系信息不是强制性的。用户可以根本没有公司。
I have a database design question.
In my application, a user has contact information including
- telephone number
- adress line 1
- postal box
- Locality (City)
This is currentl located in a "user" table with the other informations like username, first name, ...
The thing is that a user has to have the same kind of information (telephone, email, ...) but for his company.
It seems redundant to add additional fields to the "user" table. As the data are similar, I could make a "ContactInfo" table with the fields:
- phone
- address
- postal box
- ...
Is it a good idea and how should I do the relation between the "user" table and this "contactInfo" table ?
EDIT: I forgot to say that the company contact information is not obligatory. A user can have no company at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用户之间具有 1:M 关系的任何内容都应该位于其自己的表中。如果您只关心用户的主要联系信息,那么您可以将其保留在用户表中。
例如:如果可以接受两个电话号码(或更多),那么您将拥有另一个表,其中包含用户表的外键以及电话号码、电话类型和联系顺序优先事项。
Anything with a 1:M relationship between the user should be on its own table. If you only care about the user's primary contact information, then you can get away with keeping it on the User table.
For example: If it is acceptable to have two phone numbers (or more) then you will have another table with a foreign key to your users table and the phone number, the phone type, and the contact order priority.
您应该有一个单独的电话表、一个单独的电子邮件表和一个单独的地址表。人们拥有这三者中的不止一种。
You should have a separate phone table, a separate email table and a separate address table. People have more than one of all three.
单独的桌子是正确的选择。
如果您的数据库支持外键,则应该有一个从
user_contact_info.user_id
到users.id
的外键。user_contact_info.contact_type
将设置为work
或home
或您需要的任何其他类别。Separate tables are the right choice.
If your database supports foreign keys, there should be a foreign key from
user_contact_info.user_id
tousers.id
.user_contact_info.contact_type
would be set towork
orhome
or whatever other categories you need.