Sql server 2012 依赖于同一主键的多个外键

Sql server 2012 依赖于同一主键的多个外键,sql-server-2012,primary-key,foreign-key-relationship,composite-primary-key,Sql Server 2012,Primary Key,Foreign Key Relationship,Composite Primary Key,假设我有一个使用SQLServer作为数据库的web应用程序 我有一个表,它有两个外键,其中包含与另一个表相同的值 表章节 IDdepartment, IDstore, codTax, accountSell, accountTax, status, m1, m2 -> PK (IDdepartment, IDstore, codTax) FK (accountSell, accountTax) on AccountPlan (accounCompany) 表AccountPlan A

假设我有一个使用SQLServer作为数据库的web应用程序

我有一个表,它有两个外键,其中包含与另一个表相同的值

章节

IDdepartment, IDstore, codTax, accountSell, accountTax, status, m1, m2

-> PK (IDdepartment, IDstore, codTax)
FK (accountSell, accountTax) on AccountPlan (accounCompany)
AccountPlan

AccountCod, account, description, account_desc, accountCompany, type, mov) 

-> PK (accountCompany)
accountSell
accountTax
取决于
accountCompany
。两者都可以包含
accountCompany
中可用的所有值

当我进行查询时,我想知道该
会计公司的描述,如何实现这一点

事实上,我使用了两个具有重复数据的表,我认为只有一个表可以做同样的事情。希望这是可能的


谢谢…

老实说,我真的希望您没有两个表包含重复的数据。两个FK指向同一个表没有问题:

create table Section
(
  accountSell int not null  foreign key references AccountPlan(accountCompany), 
  accountTax int not null  foreign key references AccountPlan(accountCompany)
)
您可以通过两次连接到相同的表来获得描述:

select
  sell.description as accountSell_description,
  tax.description as accountTax_description
from 
  Section s
  inner join AccountPlan sell on sell.accountCompany = s.accountSell
  inner join AccountPlan tax on tax.accountCompany = s.accountTax

首先。。。谢谢你的帮助。。。是非常重要的,我应该做DB规范化的所有理论步骤。。。因为一开始时间不够,我不得不“敲打”,直到现在这些问题才显现出来!!这是人生的一课。。。