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.
c# sql .net
add a comment |
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.
c# sql .net
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 theCustomer
table (and thus reference it from other tables by using both columns, too). Give each of your locations a uniqueLocationID
, and your problems are solved
– marc_s
Nov 11 at 19:16
add a comment |
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.
c# sql .net
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
c# sql .net
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 theCustomer
table (and thus reference it from other tables by using both columns, too). Give each of your locations a uniqueLocationID
, and your problems are solved
– marc_s
Nov 11 at 19:16
add a comment |
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 theCustomer
table (and thus reference it from other tables by using both columns, too). Give each of your locations a uniqueLocationID
, 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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theCustomer
table (and thus reference it from other tables by using both columns, too). Give each of your locations a uniqueLocationID
, and your problems are solved– marc_s
Nov 11 at 19:16