By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

How to insert into two tables that have Foreign Keys Referencing each other.

P: 52
I have two tables table1 and table2
Table1 has a foreign key which references key of table2
Table2 also has a foreign key which references key of table1

Now when I try to insert into any of those 2 tables, violation of foreign key occurs.
How can I insert into both tables without violating foreign key

Is there any way to insert both tables simultaneously?

thanks
Sep 11 '07 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi.

I would have thought this could not happen.

How did you manage to create the first table with a Foreign Key referencing a second table when the second table didn't exists? That should have caused an error.
Sep 11 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Shreedhan.

The solution is to remove one of your foreign key constraints.

If that doesn't make sense for your database design, then you probably need to rethink your database design.
Sep 11 '07 #3

P: 52
Thanks for your reply

I am now removing the foreign key, because its not very significant.

And, Atli
I created first table without any foreign key
then I created the second one having foreign key referencing the first one
After that I altered the first table to add the foreign key referencing the second one.

Thanks
Sep 12 '07 #4

Atli
Expert 5K+
P: 5,058
I created first table without any foreign key
then I created the second one having foreign key referencing the first one
After that I altered the first table to add the foreign key referencing the second one.
Ahh ok. So you basically bypassed the built in protection against the very problem you encountered :)

Maybe it should be submitted as a MySQL bug...

Anyways, good luck with you project!

P.S. I changed the title of the thread to make it more descriptive.
Sep 12 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.