简体   繁体   English

在事实表中插入几个外键

[英]Inserting several foreign keys in Fact Table

I try to assign foreign key of one table named FactTable .我尝试分配一个名为FactTable的表的外键。 You can see how is look like table and what type of data contain.您可以查看表格的外观以及包含的数据类型。

在此处输入图像描述

On this table I already assign foreign key with another table named [DIMENSION].[HS] .在这个表上,我已经为另一个名为[DIMENSION].[HS]的表分配了外键。 For this purpose I used this line of code为此,我使用了这行代码

ALTER TABLE FACT.FactTable
    ADD FOREIGN KEY (TenDigits) REFERENCES [DIMENSION].[HS](TenDigits)

So far so good.到目前为止,一切都很好。 But problem arise when I try to connect this table with another table named [DIMENSION].Countries .但是当我尝试将此表与另一个名为[DIMENSION].Countries的表连接时,就会出现问题。 You can see how is look like this table你可以看到这张桌子的样子

在此处输入图像描述

Here I try to connect this two tables with code similar like code above在这里,我尝试使用类似于上面的代码的代码连接这两个表

ALTER TABLE FACT.FactTable
ADD FOREIGN KEY (CountryCodes) 
    REFERENCES [DIMENSION].[Countries] (CountryCodes)

I get this error:我收到此错误:

Msg 547, Level 16, State 0, Line 238消息 547,第 16 级,State 0,第 238 行
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__FactTable__Count__1940BAED". ALTER TABLE 语句与 FOREIGN KEY 约束“FK__FactTable__Count__1940BAED”冲突。 The conflict occurred in database "CustomsDataDW", table "DIMENSION.Countries", column 'CountryCodes'.冲突发生在数据库“CustomsDataDW”、表“DIMENSION.Countries”、列“CountryCodes”中。

Can anybody help me how to solve this problem and add one more additional foreign key in to FACT.FactTable ?谁能帮我解决这个问题并在FACT.FactTable中再添加一个外键?

Also here I want to mention that I can connect this table with ordinary join command with this line of code另外在这里我想提一下,我可以用这行代码用普通的join命令连接这个表

SELECT *
FROM FACT.FactTable as fa
INNER JOIN [DIMENSION].Countries AS co ON co.CountryCodes=fa.CountryCodes

I want to mention that I can connect this table with ordinary join command with this line of code我要提一下,我可以用普通的join命令用这行代码连接这个表

INNER JOIN shows only matched rows. INNER JOIN只显示匹配的行。 Missing values could be identified with:缺失值可以通过以下方式识别:

SELECT DISTINCT fa.CountryCodes
FROM FACT.FactTable as fa
LEFT JOIN [DIMENSION].Countries AS co ON co.CountryCodes=fa.CountryCodes
WHERE co.CountyCodes IS NULL;

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

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