简体   繁体   English

SQL不同表的一个外键

[英]Sql one foreign key for different tables

First of all, I do realize that there are plenty of questions regarding this problem and there is a good chance that I already know one of the answers, but after I've tried a couple of them, none of them worked for me. 首先,我确实意识到有关此问题有很多问题,而且很有可能我已经知道其中一个答案,但是在尝试了其中几个之后,没有一个对我有用。

In my application I am handling communication between user and customer. 在我的应用程序中,我正在处理用户与客户之间的通信。 Both users and customers can have multiple e-mail addresses. 用户和客户都可以具有多个电子邮件地址。 Also, both users and customers can be assigned to multiple conversations at the same time. 同样,用户和客户都可以同时分配给多个对话。 After user or customer sends an e-mail message, system receives information about that and creates a ConversationMessage with basic info about the message and EmailMessage with specific data (subject, FROM, TO etc.). 用户或客户发送电子邮件消息后,系统会接收有关该消息的信息,并创建包含有关该消息的基本信息的ConversationMessage和包含特定数据(主题,FROM,TO等)的EmailMessage。

数据库架构

Right now, fields FROM and TO are just varchar fields with e-mail addresses. 现在,FROM和TO字段只是带有电子邮件地址的varchar字段。 I would like to change it so that FROM and TO fields are foreign keys. 我想更改它,以便FROM和TO字段是外键。 I was planning to create an EmailAddresses table that would store basic info about the address and UserEmailAddresses and ContactPersonEmailAddresses tables with specific info about address (like host, port etc.), but I realized that EmailAddresses table would contain just an Id. 我打算创建一个EmailAddresses表,该表将存储有关地址的基本信息以及UserEmailAddresses和ContactPersonEmailAddresses表以及有关地址的特定信息(例如主机,端口等),但是我意识到EmailAddresses表将仅包含一个ID。 Is it a good approach? 这是一个好方法吗? Am I missing something? 我想念什么吗?

Are there any better solutions to this kind of problem? 对于这种问题有更好的解决方案吗?

Thank you for any help! 感谢您的任何帮助!

There are numerable issues with the design but let's limit the discussion to the email addresses. 该设计存在许多问题,但让我们将讨论范围限于电子邮件地址。 You store the addresses in two tables. 您将地址存储在两个表中。 This, unsurprisingly, is causing problems. 毫不奇怪,这会引起问题。

An email address is an entity. 电子邮件地址是一个实体。 Store them in a table. 将它们存储在表中。 Anywhere a single address is needed, place a FK to the one address table there. 在需要单个地址的任何地方,将FK放在那里的一个地址表中。 Where multiple addresses is or may be needed, place an intersection table there. 在需要或可能需要多个地址的地方,在此放置一个相交表。

create table EmailAddresses(
  ID     int   auto_generating primary key,
  Addr   varchar not null unique,
  Active bool
);

Are such fields as Login, Port, etc. really attributes of an email address? “登录名”,“端口”等字段是否真的是电子邮件地址的属性? Maybe you could clarify those if you would. 如果可以的话,也许您可​​以澄清一下。

Then the tables UserEmailAddresses and ContactEmailAddresses would be intersection tables and look something like this: 然后,表UserEmailAddresses和ContactEmailAddresses将是交集表,如下所示:

create table [User|Contact]EmailAddresses(
  UserID   int  not null references [Users|Contacts]( ID )],
  AddrID   int  not null references EmailAddresses( ID )
);

As an email message can only have one "From" value, that would be in the EmailMessages row as it is now except it can now be a FK to the one address table. 由于一封电子邮件只能有一个“发件人”值,该值将立即显示在EmailMessages行中,但它现在可以是一个地址表的FK。 A message may have one or many "To" values so that would be implemented also as an intersection table: 一条消息可能具有一个或多个“ To”值,因此也可以实现为交集表:

create table EmailTo(
  EmailID  int  not null references EmailMessages( ID )],
  ToID     int  not null references EmailAddresses( ID ),
  ToType   enum( To, CC, BCC )
);

There are probably other requirements that would require some constraints on any or all of the tables above but those depend on your usage. 可能还有其他要求,可能需要对上面任何或所有表进行一些约束,但这些约束取决于您的用法。 For example, even though email applications allow the same address to exist more than once in a From list, you may want to catch and restrict such occurrences. 例如,即使电子邮件应用程序允许同一地址在“发件人”列表中不止一次存在,您仍可能希望捕获并限制这种情况。 This would be implemented by a unique constraint on (EmailID, ToID) 这将通过对(EmailID,ToID)的唯一约束来实现

Possible extensions would be the addition of email lists which themselves contain a group of addresses. 可能的扩展将是添加电子邮件列表,该电子邮件列表本身包含一组地址。 That would require the EmailAddresses table to split but because all addresses are now located in one table, such a redesign would certainly be easier that having them located in two tables. 这将需要拆分EmailAddresses表,但是由于现在所有地址都位于一个表中,因此进行重新设计无疑比将它们位于两个表中更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM