SQL Server : how to manage foreign key? When we need merge data from different locations?











up vote
0
down vote

favorite












I develop an inventory software in C# using Visual Studio 2017.



The Database used is SQL Server 2016. The software runs at different locations on local SQL Servers.



Server1                     server2              server3

Location A Location B Location C

Db1 Db2 Db3

Customer Customer Customer

CustId Name CustId Name CustId Name
--------------- ----------------- -------------------
1 John 1 Peter 1 Watson


For example, I have generated invoice 1001 from Server1, 1002 from Server2 and 1003 from Server 3 (all three invoice's CustId are 1 but name are different)



After merge above records to Location D are as below:



                   Db4 Location D           Customer Table 
InvNo InvId CustId CustId CustomerName GUID
1001 1 1 1 John x Server1
1002 21 1 1 Peter x1 Server2
1003 31 1 1 Watson x2 Server3


CustomerId in Invoice table is a foreign key reference.



My problem is: the Customer table has three duplicate Id (1) for different names.



Suppose I used query to my software



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustId = InvoiceMst.CustId


then it is wrong after the merge.



If I use CustomerName Instead of CustomerId as foreignKey then there is no problem. I can write query as



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustomerNAME = InvoiceMst.CustomerName


so Is it right way to use CustomerName as foreign key? Or any other best option?



Thanks.










share|improve this question




















  • 1




    The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
    – montewhizdoh
    Nov 11 at 15:38












  • If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
    – comphonia
    Nov 11 at 16:00










  • Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
    – Dhiren Naik
    Nov 11 at 16:12










  • The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
    – marc_s
    Nov 11 at 19:16

















up vote
0
down vote

favorite












I develop an inventory software in C# using Visual Studio 2017.



The Database used is SQL Server 2016. The software runs at different locations on local SQL Servers.



Server1                     server2              server3

Location A Location B Location C

Db1 Db2 Db3

Customer Customer Customer

CustId Name CustId Name CustId Name
--------------- ----------------- -------------------
1 John 1 Peter 1 Watson


For example, I have generated invoice 1001 from Server1, 1002 from Server2 and 1003 from Server 3 (all three invoice's CustId are 1 but name are different)



After merge above records to Location D are as below:



                   Db4 Location D           Customer Table 
InvNo InvId CustId CustId CustomerName GUID
1001 1 1 1 John x Server1
1002 21 1 1 Peter x1 Server2
1003 31 1 1 Watson x2 Server3


CustomerId in Invoice table is a foreign key reference.



My problem is: the Customer table has three duplicate Id (1) for different names.



Suppose I used query to my software



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustId = InvoiceMst.CustId


then it is wrong after the merge.



If I use CustomerName Instead of CustomerId as foreignKey then there is no problem. I can write query as



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustomerNAME = InvoiceMst.CustomerName


so Is it right way to use CustomerName as foreign key? Or any other best option?



Thanks.










share|improve this question




















  • 1




    The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
    – montewhizdoh
    Nov 11 at 15:38












  • If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
    – comphonia
    Nov 11 at 16:00










  • Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
    – Dhiren Naik
    Nov 11 at 16:12










  • The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
    – marc_s
    Nov 11 at 19:16















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I develop an inventory software in C# using Visual Studio 2017.



The Database used is SQL Server 2016. The software runs at different locations on local SQL Servers.



Server1                     server2              server3

Location A Location B Location C

Db1 Db2 Db3

Customer Customer Customer

CustId Name CustId Name CustId Name
--------------- ----------------- -------------------
1 John 1 Peter 1 Watson


For example, I have generated invoice 1001 from Server1, 1002 from Server2 and 1003 from Server 3 (all three invoice's CustId are 1 but name are different)



After merge above records to Location D are as below:



                   Db4 Location D           Customer Table 
InvNo InvId CustId CustId CustomerName GUID
1001 1 1 1 John x Server1
1002 21 1 1 Peter x1 Server2
1003 31 1 1 Watson x2 Server3


CustomerId in Invoice table is a foreign key reference.



My problem is: the Customer table has three duplicate Id (1) for different names.



Suppose I used query to my software



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustId = InvoiceMst.CustId


then it is wrong after the merge.



If I use CustomerName Instead of CustomerId as foreignKey then there is no problem. I can write query as



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustomerNAME = InvoiceMst.CustomerName


so Is it right way to use CustomerName as foreign key? Or any other best option?



Thanks.










share|improve this question















I develop an inventory software in C# using Visual Studio 2017.



The Database used is SQL Server 2016. The software runs at different locations on local SQL Servers.



Server1                     server2              server3

Location A Location B Location C

Db1 Db2 Db3

Customer Customer Customer

CustId Name CustId Name CustId Name
--------------- ----------------- -------------------
1 John 1 Peter 1 Watson


For example, I have generated invoice 1001 from Server1, 1002 from Server2 and 1003 from Server 3 (all three invoice's CustId are 1 but name are different)



After merge above records to Location D are as below:



                   Db4 Location D           Customer Table 
InvNo InvId CustId CustId CustomerName GUID
1001 1 1 1 John x Server1
1002 21 1 1 Peter x1 Server2
1003 31 1 1 Watson x2 Server3


CustomerId in Invoice table is a foreign key reference.



My problem is: the Customer table has three duplicate Id (1) for different names.



Suppose I used query to my software



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustId = InvoiceMst.CustId


then it is wrong after the merge.



If I use CustomerName Instead of CustomerId as foreignKey then there is no problem. I can write query as



select Customername, InvNo
from InvoiceMst
left join CustomerMst on CustomerMst.CustomerNAME = InvoiceMst.CustomerName


so Is it right way to use CustomerName as foreign key? Or any other best option?



Thanks.







c# sql .net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:14









marc_s

567k12810981249




567k12810981249










asked Nov 11 at 15:11









Dhiren Naik

13




13








  • 1




    The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
    – montewhizdoh
    Nov 11 at 15:38












  • If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
    – comphonia
    Nov 11 at 16:00










  • Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
    – Dhiren Naik
    Nov 11 at 16:12










  • The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
    – marc_s
    Nov 11 at 19:16
















  • 1




    The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
    – montewhizdoh
    Nov 11 at 15:38












  • If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
    – comphonia
    Nov 11 at 16:00










  • Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
    – Dhiren Naik
    Nov 11 at 16:12










  • The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
    – marc_s
    Nov 11 at 19:16










1




1




The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
– montewhizdoh
Nov 11 at 15:38






The fastest way to fix this is to redesign your tables to use GUID instead of INT as the CustomerId. Why? The GUID should not repeat and it is made for this situation. Then you can use the CustomerId as the foreign key as you should. If you use CustomerName it will break when you have more than one person with the same name. forums.asp.net/t/…
– montewhizdoh
Nov 11 at 15:38














If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
– comphonia
Nov 11 at 16:00




If its just 3 ids that are identical, why dont you fix them and add an auto increment to it? should have been there from the start if its a primary key
– comphonia
Nov 11 at 16:00












Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
– Dhiren Naik
Nov 11 at 16:12




Thanks Montewhizdoh. this is usefull. I will change my table structure. thanks again
– Dhiren Naik
Nov 11 at 16:12












The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
– marc_s
Nov 11 at 19:16






The typical way to handle this would be to add a LocationID column to all relevant tables, and make the (LocationId, CustomerId) pair the primary key for the Customer table (and thus reference it from other tables by using both columns, too). Give each of your locations a unique LocationID, and your problems are solved
– marc_s
Nov 11 at 19:16



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250081%2fsql-server-how-to-manage-foreign-key-when-we-need-merge-data-from-different-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250081%2fsql-server-how-to-manage-foreign-key-when-we-need-merge-data-from-different-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine