MySQL: Insert if foreign key exists?

You could change to insert the result of a select, so something like: INSERT INTO test (id, value) SELECT foobar. Id, 20 FROM foobar WHERE id = 10.

Have to add limit 1, otherwise each insert could be multiple rows. – MJB Jun 25 '10 at 12:05 @MJB I'm assuming that id is the primary key on foobar, per the OP, so the LIMIT shouldn't be needed. – Rowland Shaw Jun 25 '10 at 12:10 If I do it like that, it's only possible to insert data into "test", which is already in "foobar", right?

But I have new data to add, which comes from the excel sheet. Can I somehow adjust this, so I can add new data to it? – user375700 Jun 25 '10 at 12:12 I mis-read.

I think you are right, and I retract my comment. – MJB Jun 25 '10 at 13:45 @snootles Have you the information to create the record in foobar? If so you could instead pre-create the values, perhaps using a query along the lines of INSERT INTO foobar (id,...) SELECT 10, ... FROM test LEFT JOIN foobar ON test.Id = foobar.

Id WHERE foobar. Id IS NULL – Rowland Shaw Jun 25 '10 at 18:05.

I have an excel sheet with around 2.000 rows that I want to insert into my database. The problem is that the table I want to insert the 2.000 rows into has a field that references to a foreign key in another table. Unfortunately a lot of queries fail, since the given foreign key does NOT exist.

I know that I can ignore foreign key checks, but this is not what I want. I don't want to ignore foreign key checks, I just want bad queries not to be executed. The first query fails, since TEST.id references to foobar.id and there is no foobar.id=10.

However, the second query would work, since foobar.id=20 exists, but the second query won't be executed, because the first one already failed. Is there any way I don't get an error on the first quiery and my other queries will still be executed? I could write a php script, but I'd prefer a MySQL solution here.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions