Can't insert data for 2 foreign key for the same primary key in SQL Server











up vote
1
down vote

favorite












I can't insert data in a table where 2 foreign keys refer to one primary key...



The code is as follows:



create table Currency 
(
ID int primary key identity(1,1),
Code nvarchar(8) not null,
Name nvarchar(128) not null,
Is_Active bit not null default(0),
Is_Base_Currency bit not null default(0),
Country_id int foreign key(ID) references Country(ID) not null
)

Create table Currency_rate
(
ID int primary key identity(1,1),
Currency_id int foreign key(ID) references Currency(ID) not null,
Base_currency_id int foreign key(ID) references Currency(ID) not null,
Rate decimal(16,6) not null,
Ts datetime default getDate()
)

Insert into Currency_rate(Currency_id, Base_currency_id, Rate, Ts)
values (1, 1, 121212.212121, '2008-11-11 13:23:44.111'),
(2, 2, 232323.323232, '2009-11-11 13:23:44.222'),
(3, 3, 343434.434343, '2010-11-11 13:23:44.333')


This is the error I get:




Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Currency___Curre__239E4DCF". The conflict occurred in database "CryptoCurrencyData", table "dbo.Currency", column 'ID'.
The statement has been terminated.




Please help me - I can't find any solution surfing the internet...



Thank you all,



Regards,
Elias.H










share|improve this question
























  • hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
    – Zeki Gumus
    Nov 19 at 18:42






  • 2




    Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
    – scsimon
    Nov 19 at 19:03












  • You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
    – Eric
    Nov 19 at 20:31















up vote
1
down vote

favorite












I can't insert data in a table where 2 foreign keys refer to one primary key...



The code is as follows:



create table Currency 
(
ID int primary key identity(1,1),
Code nvarchar(8) not null,
Name nvarchar(128) not null,
Is_Active bit not null default(0),
Is_Base_Currency bit not null default(0),
Country_id int foreign key(ID) references Country(ID) not null
)

Create table Currency_rate
(
ID int primary key identity(1,1),
Currency_id int foreign key(ID) references Currency(ID) not null,
Base_currency_id int foreign key(ID) references Currency(ID) not null,
Rate decimal(16,6) not null,
Ts datetime default getDate()
)

Insert into Currency_rate(Currency_id, Base_currency_id, Rate, Ts)
values (1, 1, 121212.212121, '2008-11-11 13:23:44.111'),
(2, 2, 232323.323232, '2009-11-11 13:23:44.222'),
(3, 3, 343434.434343, '2010-11-11 13:23:44.333')


This is the error I get:




Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Currency___Curre__239E4DCF". The conflict occurred in database "CryptoCurrencyData", table "dbo.Currency", column 'ID'.
The statement has been terminated.




Please help me - I can't find any solution surfing the internet...



Thank you all,



Regards,
Elias.H










share|improve this question
























  • hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
    – Zeki Gumus
    Nov 19 at 18:42






  • 2




    Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
    – scsimon
    Nov 19 at 19:03












  • You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
    – Eric
    Nov 19 at 20:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I can't insert data in a table where 2 foreign keys refer to one primary key...



The code is as follows:



create table Currency 
(
ID int primary key identity(1,1),
Code nvarchar(8) not null,
Name nvarchar(128) not null,
Is_Active bit not null default(0),
Is_Base_Currency bit not null default(0),
Country_id int foreign key(ID) references Country(ID) not null
)

Create table Currency_rate
(
ID int primary key identity(1,1),
Currency_id int foreign key(ID) references Currency(ID) not null,
Base_currency_id int foreign key(ID) references Currency(ID) not null,
Rate decimal(16,6) not null,
Ts datetime default getDate()
)

Insert into Currency_rate(Currency_id, Base_currency_id, Rate, Ts)
values (1, 1, 121212.212121, '2008-11-11 13:23:44.111'),
(2, 2, 232323.323232, '2009-11-11 13:23:44.222'),
(3, 3, 343434.434343, '2010-11-11 13:23:44.333')


This is the error I get:




Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Currency___Curre__239E4DCF". The conflict occurred in database "CryptoCurrencyData", table "dbo.Currency", column 'ID'.
The statement has been terminated.




Please help me - I can't find any solution surfing the internet...



Thank you all,



Regards,
Elias.H










share|improve this question















I can't insert data in a table where 2 foreign keys refer to one primary key...



The code is as follows:



create table Currency 
(
ID int primary key identity(1,1),
Code nvarchar(8) not null,
Name nvarchar(128) not null,
Is_Active bit not null default(0),
Is_Base_Currency bit not null default(0),
Country_id int foreign key(ID) references Country(ID) not null
)

Create table Currency_rate
(
ID int primary key identity(1,1),
Currency_id int foreign key(ID) references Currency(ID) not null,
Base_currency_id int foreign key(ID) references Currency(ID) not null,
Rate decimal(16,6) not null,
Ts datetime default getDate()
)

Insert into Currency_rate(Currency_id, Base_currency_id, Rate, Ts)
values (1, 1, 121212.212121, '2008-11-11 13:23:44.111'),
(2, 2, 232323.323232, '2009-11-11 13:23:44.222'),
(3, 3, 343434.434343, '2010-11-11 13:23:44.333')


This is the error I get:




Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Currency___Curre__239E4DCF". The conflict occurred in database "CryptoCurrencyData", table "dbo.Currency", column 'ID'.
The statement has been terminated.




Please help me - I can't find any solution surfing the internet...



Thank you all,



Regards,
Elias.H







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:38









marc_s

569k12811001250




569k12811001250










asked Nov 19 at 18:34









elias hayar

82




82












  • hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
    – Zeki Gumus
    Nov 19 at 18:42






  • 2




    Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
    – scsimon
    Nov 19 at 19:03












  • You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
    – Eric
    Nov 19 at 20:31


















  • hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
    – Zeki Gumus
    Nov 19 at 18:42






  • 2




    Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
    – scsimon
    Nov 19 at 19:03












  • You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
    – Eric
    Nov 19 at 20:31
















hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
– Zeki Gumus
Nov 19 at 18:42




hi elias, check Currency table. Currency_Id 1,2 or 3 or non of them exists in the table. Because of that you came across with this error message.
– Zeki Gumus
Nov 19 at 18:42




2




2




Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
– scsimon
Nov 19 at 19:03






Those statements would fail... you need to create the tables and then add the constraints. And where is the table Country at? You have it as a FK reference... but haven't given the DDL for it. The point of your error is that you can't insert into the currentcy_rate where the FK is supposed to reference the Currency table. You need to insert it's parent row in the Currency table first
– scsimon
Nov 19 at 19:03














You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
– Eric
Nov 19 at 20:31




You are trying to insert into currency_rate without populating currency table. Probably learn what is foreign key and how it works.
– Eric
Nov 19 at 20:31












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Constrainsts are created to prevent corrupting data by inserting or updating your tables.



In your case, you are trying to insert data which is not existing. You are trying to insert into Currency_rate values ID's of Currency which are not existing in Currency table. So this is a whole goal of constraints - prevent corruption of data.



Just for demonstration purposes I've created Country table:



Create table Country (
ID int primary key identity(1,1),
CountryName nvarchar(50)
)


Then your first step would be:



INSERT INTO dbo.Country
(
--ID - this column value is auto-generated
CountryName
)
VALUES
(
-- ID - int
N'India' -- CountryName - nvarchar
)
, (N'Canada')
, (N'South America')


The second step will be:



INSERT INTO dbo.Currency
(
--ID - this column value is auto-generated
Code,
Name,
Is_Active,
Is_Base_Currency,
Country_id
)
VALUES
(
-- ID - int
N'Code1', -- Code - nvarchar
N'India Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
1 -- Country_id - int
)
, (
N'Code2', -- Code - nvarchar
N'Canada Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
2 -- Country_id - int
)
, (
N'Code3', -- Code - nvarchar
N'South America Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
3 -- Country_id - int
)


And the final step is the following:



Insert into Currency_rate(Currency_id,Base_currency_id,Rate,Ts)
values(1,1,121212.212121,'2008-11-11 13:23:44.111'),
(2,2,232323.323232,'2009-11-11 13:23:44.222'),
(3,3,343434.434343,'2010-11-11 13:23:44.333')





share|improve this answer



















  • 1




    thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
    – elias hayar
    Nov 20 at 19:24











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%2f53380709%2fcant-insert-data-for-2-foreign-key-for-the-same-primary-key-in-sql-server%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Constrainsts are created to prevent corrupting data by inserting or updating your tables.



In your case, you are trying to insert data which is not existing. You are trying to insert into Currency_rate values ID's of Currency which are not existing in Currency table. So this is a whole goal of constraints - prevent corruption of data.



Just for demonstration purposes I've created Country table:



Create table Country (
ID int primary key identity(1,1),
CountryName nvarchar(50)
)


Then your first step would be:



INSERT INTO dbo.Country
(
--ID - this column value is auto-generated
CountryName
)
VALUES
(
-- ID - int
N'India' -- CountryName - nvarchar
)
, (N'Canada')
, (N'South America')


The second step will be:



INSERT INTO dbo.Currency
(
--ID - this column value is auto-generated
Code,
Name,
Is_Active,
Is_Base_Currency,
Country_id
)
VALUES
(
-- ID - int
N'Code1', -- Code - nvarchar
N'India Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
1 -- Country_id - int
)
, (
N'Code2', -- Code - nvarchar
N'Canada Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
2 -- Country_id - int
)
, (
N'Code3', -- Code - nvarchar
N'South America Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
3 -- Country_id - int
)


And the final step is the following:



Insert into Currency_rate(Currency_id,Base_currency_id,Rate,Ts)
values(1,1,121212.212121,'2008-11-11 13:23:44.111'),
(2,2,232323.323232,'2009-11-11 13:23:44.222'),
(3,3,343434.434343,'2010-11-11 13:23:44.333')





share|improve this answer



















  • 1




    thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
    – elias hayar
    Nov 20 at 19:24















up vote
0
down vote



accepted










Constrainsts are created to prevent corrupting data by inserting or updating your tables.



In your case, you are trying to insert data which is not existing. You are trying to insert into Currency_rate values ID's of Currency which are not existing in Currency table. So this is a whole goal of constraints - prevent corruption of data.



Just for demonstration purposes I've created Country table:



Create table Country (
ID int primary key identity(1,1),
CountryName nvarchar(50)
)


Then your first step would be:



INSERT INTO dbo.Country
(
--ID - this column value is auto-generated
CountryName
)
VALUES
(
-- ID - int
N'India' -- CountryName - nvarchar
)
, (N'Canada')
, (N'South America')


The second step will be:



INSERT INTO dbo.Currency
(
--ID - this column value is auto-generated
Code,
Name,
Is_Active,
Is_Base_Currency,
Country_id
)
VALUES
(
-- ID - int
N'Code1', -- Code - nvarchar
N'India Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
1 -- Country_id - int
)
, (
N'Code2', -- Code - nvarchar
N'Canada Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
2 -- Country_id - int
)
, (
N'Code3', -- Code - nvarchar
N'South America Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
3 -- Country_id - int
)


And the final step is the following:



Insert into Currency_rate(Currency_id,Base_currency_id,Rate,Ts)
values(1,1,121212.212121,'2008-11-11 13:23:44.111'),
(2,2,232323.323232,'2009-11-11 13:23:44.222'),
(3,3,343434.434343,'2010-11-11 13:23:44.333')





share|improve this answer



















  • 1




    thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
    – elias hayar
    Nov 20 at 19:24













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Constrainsts are created to prevent corrupting data by inserting or updating your tables.



In your case, you are trying to insert data which is not existing. You are trying to insert into Currency_rate values ID's of Currency which are not existing in Currency table. So this is a whole goal of constraints - prevent corruption of data.



Just for demonstration purposes I've created Country table:



Create table Country (
ID int primary key identity(1,1),
CountryName nvarchar(50)
)


Then your first step would be:



INSERT INTO dbo.Country
(
--ID - this column value is auto-generated
CountryName
)
VALUES
(
-- ID - int
N'India' -- CountryName - nvarchar
)
, (N'Canada')
, (N'South America')


The second step will be:



INSERT INTO dbo.Currency
(
--ID - this column value is auto-generated
Code,
Name,
Is_Active,
Is_Base_Currency,
Country_id
)
VALUES
(
-- ID - int
N'Code1', -- Code - nvarchar
N'India Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
1 -- Country_id - int
)
, (
N'Code2', -- Code - nvarchar
N'Canada Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
2 -- Country_id - int
)
, (
N'Code3', -- Code - nvarchar
N'South America Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
3 -- Country_id - int
)


And the final step is the following:



Insert into Currency_rate(Currency_id,Base_currency_id,Rate,Ts)
values(1,1,121212.212121,'2008-11-11 13:23:44.111'),
(2,2,232323.323232,'2009-11-11 13:23:44.222'),
(3,3,343434.434343,'2010-11-11 13:23:44.333')





share|improve this answer














Constrainsts are created to prevent corrupting data by inserting or updating your tables.



In your case, you are trying to insert data which is not existing. You are trying to insert into Currency_rate values ID's of Currency which are not existing in Currency table. So this is a whole goal of constraints - prevent corruption of data.



Just for demonstration purposes I've created Country table:



Create table Country (
ID int primary key identity(1,1),
CountryName nvarchar(50)
)


Then your first step would be:



INSERT INTO dbo.Country
(
--ID - this column value is auto-generated
CountryName
)
VALUES
(
-- ID - int
N'India' -- CountryName - nvarchar
)
, (N'Canada')
, (N'South America')


The second step will be:



INSERT INTO dbo.Currency
(
--ID - this column value is auto-generated
Code,
Name,
Is_Active,
Is_Base_Currency,
Country_id
)
VALUES
(
-- ID - int
N'Code1', -- Code - nvarchar
N'India Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
1 -- Country_id - int
)
, (
N'Code2', -- Code - nvarchar
N'Canada Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
2 -- Country_id - int
)
, (
N'Code3', -- Code - nvarchar
N'South America Currency', -- Name - nvarchar
0, -- Is_Active - bit
0, -- Is_Base_Currency - bit
3 -- Country_id - int
)


And the final step is the following:



Insert into Currency_rate(Currency_id,Base_currency_id,Rate,Ts)
values(1,1,121212.212121,'2008-11-11 13:23:44.111'),
(2,2,232323.323232,'2009-11-11 13:23:44.222'),
(3,3,343434.434343,'2010-11-11 13:23:44.333')






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 7:05

























answered Nov 19 at 19:41









StepUp

7,10274473




7,10274473








  • 1




    thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
    – elias hayar
    Nov 20 at 19:24














  • 1




    thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
    – elias hayar
    Nov 20 at 19:24








1




1




thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
– elias hayar
Nov 20 at 19:24




thank you for the reply mate , well i had already inserted data into the tables, what I tried is to delete the table currency_rate and re-creating it then inserting data ...That worked for me ... my problem was that i executed a full transact sql code without noticing order of the statements , Again Thanks A Lot
– elias hayar
Nov 20 at 19:24


















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%2f53380709%2fcant-insert-data-for-2-foreign-key-for-the-same-primary-key-in-sql-server%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy