简体   繁体   English

ER diagramm建模:FK到多个表

[英]ER diagramm modeling: FK to more than one table

I hava web application with database on SQL Server 2008. I am analyzing ER model of given application. 我在SQL Server 2008上使用数据库进行Web应用程序。我正在分析给定应用程序的ER模型。

There is a table "Table1" with columns "ContactType" and "ContactID". 有一个表“Table1”,其中包含“ContactType”和“ContactID”列。 Based on ContactType value "ContactID" points to different tables (Table_ContactType1 or Table_ContactType2). 基于ContactType值“ContactID”指向不同的表(Table_ContactType1或Table_ContactType2)。

So, in this case we cannot define foreign key. 所以,在这种情况下我们无法定义外键。 I wonder if this is good practice? 我想知道这是不是很好的做法? If not, should I change model somehow? 如果没有,我应该以某种方式改变模型吗? Would it cause some problems later if there are no foreign key defined? 如果没有定义外键,它会在以后引起一些问题吗?

EDIT: For now it is very difficult to combine ContactType1 and ContactType2 tables into one tables. 编辑:目前很难将ContactType1和ContactType2表组合到一个表中。 Because CotnactType1 is a table that was used earlier and it contains users who has access to the system (login name and password, email and other information). 因为CotnactType1是之前使用过的表,它包含有权访问系统的用户(登录名和密码,电子邮件和其他信息)。 IT is a table with many fields. IT是一个包含许多领域的表。 Unfourtunately new requirements came and new table ContactType2 as created. 不幸的是,新的要求来了,新表ContactType2已创建。 This table has only three columns and those are organizations names without any access to system. 此表只有三列,这些是没有任何系统访问权限的组织名称。 It would be very difficult to combine those, becuase it would affect much functionality. 将它们组合起来是非常困难的,因为它会影响很多功能。 The simpliest thing - when contact to contacttype1 table is added then email, username, password cannot be empty etc. But also much other. 最简单的事情 - 当联系contacttype1表时,添加电子邮件,用户名,密码不能为空等等。还有很多其他的。

You can create third table that will combine ContactType1 and ContactType2 for their common columns ( Type and ID ) and then you can make FK from your Table1 to the new combined table (let's call it ContactTypeUnified ) 您可以创建第三个表,将结合ContactType1ContactType2为他们共同的列( TypeID从你的),然后你可以FK Table1到合并后的新表(姑且称之为ContactTypeUnified

ContactType1 and ContactType2 can both remain as is, and also have their IDs as FK to the new ContactTypeUnified table. ContactType1ContactType2都可以保持原样,并且它们的ID也可以作为新的ContactTypeUnified表的FK。

You only trouble here is that data manipulation on ContactType1 and ContactType2 will also have to be reflected on ContactTypeUnified 您唯一的麻烦是ContactType1ContactType2上的数据操作也必须反映在ContactTypeUnified

In general database designs without integrity are untrustworthy and need to be avoided at all costs - so yes you do need to apply the foreign key. 一般而言,没有完整性的数据库设计是不值得信任的,需要不惜一切代价避免 - 所以是的,你确实需要应用外键。 In this case I would just combine the two contact type tables as it is unlikely that these should be separated. 在这种情况下,我只是将两个联系人类型表组合在一起,因为它们不太可能分开。

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

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