Jump to content


Photo

command to add column to a table


  • Please log in to reply
3 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 05 February 2006 - 05:14 PM

Hi,
i am fairly new to mysql. I need to add a new column to a table and it works fine when using the following command:

alter table table name add column name int(10) first;

however i wish to also make it the new primary key of the table and also to assign it an auto increment value. How do i go about doing this?? Thanks.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 February 2006 - 06:08 PM

Try the following:

alter table table name add column name int(10) unsigned first not null auto_increment, add primary key (first);

Just make sure you remove any existing primary key index first.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 05 February 2006 - 07:48 PM

I have just tried that command and unfortunately it comes up with error in your sql syntax. anything else i can try??

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 February 2006 - 09:00 PM

Post the "SHOW CREATE TABLE" for the table in question -- it's possible that you already have a primary key, and need to drop it first.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users