473,408 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
4 6661
Atli
5,058 Expert 4TB
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
5,821 Expert 4TB
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
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
5,058 Expert 4TB
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

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

Similar topics

2
by: Victor M. | last post by:
Hello everyone. I am a newbie to Oracle 8 so please be patient and thanks for your time. The problem is the above mentioned error. I have two databases successully created using SQL*Plus 8.03....
10
by: ree32 | last post by:
I am inserting a record into a table that automatically generates unique ids (i.e. Primary Key). Is there anyway to return this id. As I am using this on ASP.net page and I really need the ID to...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
2
by: Rune Froysa | last post by:
I have one table with columns that are used as foreign-keys from several other tables. Sometimes deletion from this table takes +5 seconds for a single row. Are there any utilities that can be...
1
by: Philippe Lang | last post by:
Hello, What is the best method to debug a database that *sometimes* gets locked in the INSERT mode? ('ps -afx | grep postgres' shows me a process that is freezed in INSERT mode.) Thanks ...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
1
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D,...
1
by: vinceboy | last post by:
Hey..guys! I have INSERTstatement here which have two foreign keys that need to refer to other tables.How can I achieve it? $sql = sprintf("INSERT INTO `reserve` (`reserve_datetime`, `seat_qty`,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.