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

  • SEARCH
  • Home
  • 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 8700775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:14:47+00:00 2026-06-13T02:14:47+00:00

I m trying to add a foreign key to table table2 from table1 .

  • 0

I m trying to add a foreign key to table table2 from table1. But i got an error like this… what is the problem…help

Error

SQL query:

ALTER TABLE `table2` ADD FOREIGN KEY ( `bus_id` ) REFERENCES `db1`.`table1` (
`bus_id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`db1`.`#sql-664_e2`, CONSTRAINT `#sql-664_e2_ibfk_1` FOREIGN KEY (`bus_id`) REFERENCES `table1` (`bus_id`) ON DELETE CASCADE ON UPDATE CASCADE) 

Structure of table1

enter image description here

Structure of table2

enter image description here

  • 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-13T02:14:48+00:00Added an answer on June 13, 2026 at 2:14 am

    Your tables have some data. Check that all values stored in table2.bus_id are correct; all these values have to be in table1.bus_id.

    Run this query to view wrong bus_id values in table2:

    SELECT t2.bus_id AS inconsistent_bus_id FROM table2 t2
    LEFT JOIN table1 t1
      ON t1.bus_id = t2.bus_id
    WHERE t1.bus_id IS NULL
    

    Remove this records from table2, or add this bus_id to the table1. Then try to create foreign key again.

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

Sidebar

Related Questions

i'm trying to add a foreign key to my table, but when i try
I'm trying to add a foreign key to an existing table, and was having
I'm trying to add a foreign key reference to the aspnet_users table to my
I am trying to add a key from one table to another so that
While trying to add value into foreign key field, if same data doesn't exist
Im new to asp.net mvc. I'm trying add new model class but it got
I'm trying to create a foreign key on a table in MySQL and I'm
I'm trying to add a table from my database to my LINQ to SQL
I am trying to bind a ComboxBox to a foreign key table. I know
I'm having a problem with foreign key in mysql.I'm trying to insert a row

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.