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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:36:21+00:00 2026-06-18T14:36:21+00:00

I have a built database with all tables set using INNODB and Foreign Keys

  • 0

I have a built database with all tables set using INNODB and Foreign Keys. Now I need to modify a single table and add a new column which references another table. My query is:

ALTER TABLE test ADD newcol INT NOT NULL;

ALTER TABLE test ADD CONSTRAINT fk_test
FOREIGN KEY (newcol) 
REFERENCES othertable(id);

I get the following error:

"#1452 - Cannot add or update a child row: a foreign key constraint fails."

All the other tables contain data, but I know that if I drop the test table and create it with foreign key it will work. If I drop test table it will delete a lot of records and I want to avoid copying data and re-inserting it.

Can anyone show me how to add a new column via a foreign key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:36:22+00:00Added an answer on June 18, 2026 at 2:36 pm

    That error message means you have values in newcol that do not exist in othertable id column. By adding a column of INT NOT NULL, you set all the initial values for that column to 0. If you do not have an id of 0 in othertable, you have a key mismatch

    Try this query:

    SELECT  newcol 
    FROM    test
    WHERE   newcol NOT IN (
        SELECT  id
        FROM    othertable
    )
    

    if you get any results returned, then you have key values in newcol that violate the FK restraints

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

Sidebar

Related Questions

I have built a local DVD Database using Codeigniter, With film names etc in.
I have built a persistence layer with clojure where database tables are read into
I have all my sites built on php and mysql stack using PDO. I
I have a database table with images that I need to display. In my
I have built a DataTable from my database. Then I am looping through the
Suppose you have a messaging system built in PHP with a MySQL database backend,
I have a number of scripts used to build a database. These need to
I have for some time build n-tier Applications using a database server as the
I have created SQL Server 2008 Database project using visual studio 2010. after build
We already have the database with real data inside. Now we want to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.