erron Posted July 17, 2006 Share Posted July 17, 2006 hi, no matter how much i google search this, i cant find the answer.how do i set the primary key and auto_increment on an already existing field in a table.For the history, im copying 35 tables in a database with different table names using:$table = "CREATE TABLE IF NOT EXISTS ".$username."_banner SELECT * FROM default_banner";mysql_query($table);but this does not copy over the primary key and auto_increment settings. Quote Link to comment https://forums.phpfreaks.com/topic/14802-set-primary-key-in-existing-table-and-field/ Share on other sites More sharing options...
realjumper Posted July 17, 2006 Share Posted July 17, 2006 Below are examples for specifying a primary key by altering a table:MySQL: ALTER TABLE Customer ADD PRIMARY KEY (SID);Oracle: ALTER TABLE Customer ADD PRIMARY KEY (SID);SQL Server: ALTER TABLE Customer ADD PRIMARY KEY (SID);Note: Before using the ALTER TABLE command to add a primary key, you'll need to make sure that the field is defined as 'NOT NULL' -- in other words, NULL cannot be an accepted value for that field. (taken from http://www.1keydata.com/sql/sql-primary-key.html) Quote Link to comment https://forums.phpfreaks.com/topic/14802-set-primary-key-in-existing-table-and-field/#findComment-59135 Share on other sites More sharing options...
erron Posted July 17, 2006 Author Share Posted July 17, 2006 Thankyou for that,after a lot experimenting and questions, the following works a charm:$fix = "alter table `".$username."_core_acl_aro` change `aro_id` `aro_id` int (11) NOT NULL AUTO_INCREMENT , add primary key (`aro_id`)";mysql_query($fix);Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/14802-set-primary-key-in-existing-table-and-field/#findComment-59171 Share on other sites More sharing options...
realjumper Posted July 17, 2006 Share Posted July 17, 2006 No worries....googled it in 1 minute!!! Quote Link to comment https://forums.phpfreaks.com/topic/14802-set-primary-key-in-existing-table-and-field/#findComment-59174 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.