4

I checked this question here but unfortunately the link to the diagram in not working so I'm stuck.

I am trying to have multiple emails for one user (work, business, personal, etc) and I'm not sure how to best approach this situation.

I am thinking to have 4 tables: user, email, email_type, and user_has_email (user N:M email). I made two diagrams but I don't know which one would be the better one.

First diagram helps me if one user has the same email for both work and personal (because I don't have to store it twice). Second option is good as well but I would have to store emails twice or more even if one user uses the same email for work, business, personal, etc.

I am planning to use the same idea for storing addresses, which occupy more space than emails and I am thinking that the diagram 1 is more suitable for this.

What do you think?

Diagram 1
-explanation of user_has_email: I chose to make the email_type PK because there may be the case when a user has the same email for work or personal. If I don't PK the email_type I would only be able to have one email_type per user. Did I complicated it too much?

Diagram 1


Diagram 2 Diagram 2

Community
  • 1
  • 1
Cristian
  • 2,390
  • 6
  • 27
  • 40
  • 1
    Both of your diagrams implement a many-to-many relationship. Do you need to support the possibility that a single email address is shared by more than one person? – Dan Pichelman Jun 14 '13 at 17:24
  • @Dan Pichelman - Yes that would be a possibility but I don't think that in the emails case. Since I plan to use the same model for storing user's addresses I am sure that there will be users sharing the same address. Or, at least there will be users that use an address for both personal and business. – Cristian Jun 14 '13 at 19:35

3 Answers3

4

Instead I would use

user (user_id, first_name, last_name)
user_emails (user_id, email_type_id, email)
email_types (email_type_id, email_type)
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Okay. I got your point. That would eliminate a table and keep thing simple. However, if I want to do the same for addresses (which hold much more information), in case I would have users sharing an address I would have to store the same thing more than once. Same thing applies for emails. – Cristian Jun 14 '13 at 19:33
  • This does not allow the same e-mail to have multiple types (e.g. to be both "work" and "personal" e-mail). – Branko Dimitrijevic Jun 16 '13 at 00:32
1

I would prefer Diagram 1 for the following reasons.

  1. You can make the email field UNIQUE so that you can store it only once regardless of the type.
  2. It does not seem right to make the email and the email type tightly coupled, if you face a situation where you have to establish a one-to-one relationship between the user and the email for some other feature.
  3. Any kind of validation for the user-email relationship should be handled in the business logic (even if you have constraints in the database).
Slowcoder
  • 2,060
  • 3
  • 16
  • 21
1

The following structure should fit the bill:

enter image description here

There is a 1:N relationship between users and e-mails, and each user's e-mail can have zero or more types, from the set of shared types.

If the e-mail types don't need to be shared among users, the model can be further simplified:

enter image description here

BTW, the case for using M:N for addresses is not clear either, due to the inherent "fuzziness" of addresses - see this post for some musings on the subject.

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Thanks for your answer. For the first structure, in the email table, since email_id is PK I won't be able to have the same email for different users unless I would make user_id PK as well! I noticed your structure is very similar with my first one. Is there any difference between the two? – Cristian Jun 20 '13 at 17:20
  • Disregard my first comment. For emails, the first structure works, but for addresses it won't. However, your answer made me realize that I was going way to far from what I need to do. So, I will only share an address between the same user (work / personal/ billing etc) and in case two or more users share the same address I will just have to create another one. This way it will be easier when someone needs to change their stored address. – Cristian Jun 20 '13 at 17:31