Mamo Posted April 19, 2013 Share Posted April 19, 2013 hi i have a problem and that is i created a MYSQL table with a foreign key column referencing to a primary key column in another table that i have already inserted a values in it earlier. Now what i want to do is to insert all of the primary key's column values into the foreign key column . thank you for reading this. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/ Share on other sites More sharing options...
requinix Posted April 19, 2013 Share Posted April 19, 2013 Try an INSERT...SELECT statement. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425844 Share on other sites More sharing options...
Mamo Posted April 19, 2013 Author Share Posted April 19, 2013 Try an INSERT...SELECT statement. okay when i insert a new value to the primary key column , do i have to insert it to the foreign key to or it will be inserted automatically. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425858 Share on other sites More sharing options...
requinix Posted April 19, 2013 Share Posted April 19, 2013 Since you want to use existing data you have to say what that data is. Otherwise it'll give you new values. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425862 Share on other sites More sharing options...
Mamo Posted April 19, 2013 Author Share Posted April 19, 2013 I am sorry, but i didn't quite understand you. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425863 Share on other sites More sharing options...
jazzman1 Posted April 19, 2013 Share Posted April 19, 2013 She means something like that: INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 http://dev.mysql.com/doc/refman/5.0/en/insert-select.html Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425865 Share on other sites More sharing options...
Mamo Posted April 20, 2013 Author Share Posted April 20, 2013 thanks a lot, i understand now. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425887 Share on other sites More sharing options...
Mamo Posted April 20, 2013 Author Share Posted April 20, 2013 I am having another problem.... i have two tables.....table '1' and table '2' , in table '1' i made a "title" column and indexed it as UNIQUE, and in table '2' i made a "title-a" column which is foreign key to the "title" column in the other table and "answer" column, the two of the columns ("title-a" + "answer") forms the primary key for this table. Now, what i need is to INSERT values to the "answer" column WHERE the "title-a" column satisfies a specific condition. Thank you for reading this. Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425889 Share on other sites More sharing options...
Mamo Posted April 20, 2013 Author Share Posted April 20, 2013 i even tried to use an UPDATE statement instead of INSERT...like this $choose = "UPDATE answer SET answer-ch = '$answ' WHERE title-a = '$release' "; Quote Link to comment https://forums.phpfreaks.com/topic/277154-foreign-primary-keys-problem/#findComment-1425890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.