简体   繁体   中英

Inserting several foreign keys in Fact Table

I try to assign foreign key of one table named 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] . 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 . 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
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__FactTable__Count__1940BAED". The conflict occurred in database "CustomsDataDW", table "DIMENSION.Countries", column 'CountryCodes'.

Can anybody help me how to solve this problem and add one more additional foreign key in to FACT.FactTable ?

Also here I want to mention that I can connect this table with ordinary join command with this line of code

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

INNER JOIN shows only matched rows. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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