Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Ways to resolve the error - the insert statement conflicted with the foreign key constraint
Next
Answered
Helena Thomas
  • 33
Helena Thomas
Asked: May 12, 20222022-05-12T07:59:03+00:00 2022-05-12T07:59:03+00:00In: Programs

Ways to resolve the error – the insert statement conflicted with the foreign key constraint

  • 33

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: the insert statement conflicted with the foreign key constraint when I try to run the program below:

CREATE TABLE [dbo].[Person](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[PersonDetails](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PersonID] int NOT NULL,
[SurName] [varchar](50) NULL,
[Age] int
) ON [PRIMARY]
GO

Now let’s set the PersonID column in the PersonDetails table as the Foreign Key of the Primary Key in the Person table.

ALTER TABLE [dbo].[PersonDetails]  WITH CHECK ADD  CONSTRAINT [FK_PersonDetails_Person] FOREIGN KEY([PersonID])
REFERENCES [dbo].[Person] ([ID])
GO
ALTER TABLE [dbo].[PersonDetails] CHECK CONSTRAINT [FK_PersonDetails_Person]
GO
Then, when there is no record in the Primary table, try to add a record to the Foreign table.
USE [TestDB]
GO
INSERT INTO [dbo].[PersonDetails] ([PersonID],[SurName],[Age]) VALUES (1,'CAKIR',34)
GO

The error appears the system notifies as follows:

Msg 547, Level 16, State 0, Line 3

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_PersonDetails_Person”.

The conflict occurred in database “TestDB”, table “dbo.Person”, column ‘ID’.

The statement has been terminated.

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

conflicted with the foreign key constraint
  • 2 2 Answers
  • 64 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-30T12:26:30+00:00Added an answer on May 30, 2022 at 12:26 pm

    The cause: The Primary table does not have the corresponding ID value.

    Solution: Add a record to the primary table first.

    USE [TestDB]
    GO
    INSERT INTO [dbo].[Person] ([ID],[Name]) VALUES (1,'Nurullah')
    GO
    INSERT INTO [dbo].[PersonDetails] ([PersonID],[SurName],[Age]) VALUES (1,'CAKIR',34)
    GO
    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Ethan Durand
    2022-05-25T19:33:28+00:00Added an answer on May 25, 2022 at 7:33 pm

    This error message was sent to me when I tried to populate foreign key fields. This page was the first place I looked to find the solution. Although the page has the correct answer, I believe the information is not complete for those who aren’t as familiar with SQL. Although I’m fairly proficient at writing code, SQL queries and building tables are still new to me.

    The checked answer is correct, but:

    Mike M wrote-

    “The way a FK works is it cannot have a value in that column that is
    not also in the primary key column of the referenced table.”

    This answer does not include the following:

    You must build the table containing the Primary Key first.

    Another way is to say it:

    You must Insert Data into the parent table, containing the Primary
    Key, before attempting to insert data into the child table containing
    the Foreign Key.

    This is because many tutorials gloss over the fact that you will get an error if you try to do it yourself and don’t know how to order operations. This error will continue even if you have added the primary key data. Otherwise, the foreign key data in your child table must match the primary key field of the parent table.

    This is the final version. This should have made the check answer clearer. Some people may think that this is a straightforward task and that reading a book would have solved the question. But, the truth is that everyone learns differently.

    • 16
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.