n8w Posted June 27, 2009 Share Posted June 27, 2009 I forgot to put a primary key column in one of my tables so when I tried to insert one called portfolio_id (int) auto_increment it won't let me do it because it says there is duplicate values is there an easy way to insert a primary key column with auto increment? If not .. I figure I need to insert a column and update the values with a php script and then make it the primary key once the values are unique? which everything goes smoothly up until the number 127 .. then it puts the value 127 for everything after ... 127 $sql = 'SELECT * FROM portfolio_n8w LIMIT 500'; $retid = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($retid)){ $counter++; $title = $row["title"]; $sql2 = 'UPDATE portfolio_n8w SET' . ' portfolio_id = "'.$counter.'"' . ' WHERE' . ' title = "'.$title.' " LIMIT 1'; $retid2 = mysql_query($sql2) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/163922-forgot-to-insert-primary-key-colum/ Share on other sites More sharing options...
n8w Posted June 27, 2009 Author Share Posted June 27, 2009 problem solved .. here is a super easy way to add one ALTER TABLE portfolio_n8w ADD portfolio_id INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE Quote Link to comment https://forums.phpfreaks.com/topic/163922-forgot-to-insert-primary-key-colum/#findComment-864833 Share on other sites More sharing options...
fenway Posted June 28, 2009 Share Posted June 28, 2009 problem solved .. here is a super easy way to add one ALTER TABLE portfolio_n8w ADD portfolio_id INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE Why not PRIMARY KEY? Quote Link to comment https://forums.phpfreaks.com/topic/163922-forgot-to-insert-primary-key-colum/#findComment-865051 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.