0

I have table called addresses that stores N:N sub-information from those two tables people and companies.

So I created another table called address_links that have the fields address_id and contact_id so that contact id could come from a people or from a companies record.

So I need another field that by the creation of the record, points to the correct table. But what can I do to automatize that interpretation when I make a query that shows the name of the owners of that addresses in a query list? I tried IF in order to case that 3rd field by selecting the table but it did not worked.

Explain Note: Some address may be house or workplace for more than one person.

4

1 回答 1

2

如果您需要链接到不同的表,那么通常您会希望每个表都有一个单独的链接表。这允许数据库强制执行引用完整性并消除对特殊 if 语句的需要,它还使数据库更容易理解,如果另一个开发人员查看它而无需了解特殊的实现细节。

示例表(列):

 Addresses (AddressId, Address, City, State, Zip, ...)
 Persons (PersonId, FirstName, ...)
 Companies (CompanyId, Name, ...)

 AddressPersonLinks (AddressId, PersonId)
 AddressCompanyLinks (AddressId, CompanyId)
于 2013-06-15T18:10:08.087 回答