3 Replies - 1455 Views - Last Post: 15 May 2012 - 05:45 AM

#1 Skod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-February 12

INSERT statement with FK INSERT if not exist

Posted 14 May 2012 - 01:07 AM

Hello

I'm trying to make an insert statement, that inserts into a table with foreign keys, and if a foreign from another table doesn't exists it creates it and inserts the new ID. It's hard to explain I hope you understand what I mean.
I've seen this piece of SQL somewhere but i can't seem to find it or write it myself, can anyone please help?

Any help will be appreciated.

Sincerely,
Daniel

Is This A Good Question/Topic? 0
  • +

Replies To: INSERT statement with FK INSERT if not exist

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,148
  • Joined: 12-June 08

Re: INSERT statement with FK INSERT if not exist

Posted 14 May 2012 - 07:16 AM

It's nothing more than a multi-part sql procedure using a boolean variable and if statement. Create boolean variable.. have it be set true or false if a select statement querying the foreign key table contains the pertinent data. If it does not exist then do an insert... else just exit the procedure or do what ever you want to do if it exists.
Was This Post Helpful? 0
  • +
  • -

#3 Skod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-February 12

Re: INSERT statement with FK INSERT if not exist

Posted 15 May 2012 - 02:02 AM

I'm still a bit new to SQL, could you please give an example of what you mean?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,148
  • Joined: 12-June 08

Re: INSERT statement with FK INSERT if not exist

Posted 15 May 2012 - 05:45 AM

Alternatively you could read up on the syntax and usage... select statements, user variables, and if-syntax!

https://dev.mysql.co.../en/select.html
https://dev.mysql.co...-variables.html
https://dev.mysql.co...-statement.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1