Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > change column name in MySQL

change column name in MySQL
Thread Tools
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jun 17, 2002, 10:43 PM
 
Hi

I am trying to change the name of a MySQL table column that I created. I incorrectly spelled the name and now need to change it from primeryid to primaryid.

Here is the table:

+-----------+------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------------------+----------------+
| primeryid | int(5) | | PRI | NULL | auto_increment |
| email | char(100) | | | | |
| username | char(100) | | | | |
| mimetype | char(1) | | | | |
| password | char(16) | | | | |
| lastlogin | datetime | | | 0000-00-00 00:00:00 | |
| admin | tinyint(4) | | | 0 | |
+-----------+------------+------+-----+---------------------+----------------+

I know that you use the ALTER TABLE Syntax I however get an error when I use the following.

mysql> alter table account
-> change primeryid primaryid int(5) auto_increment null primary key;

The error is ERROR 1068: Multiple primary key defined. All help welcome.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jun 18, 2002, 02:22 AM
 
I use phpMyAdmin for stuff like this. The error that your getting is saying that a different column is defined as the primary (for indexing), and you can only have a single primary defined.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
dogwood  (op)
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jun 18, 2002, 09:06 AM
 
I cannot install myPHPadmin on my ISP server, so I need to know the syntax for SQL via the MySQL client in the terminal.
     
Fresh-Faced Recruit
Join Date: May 2002
Location: Ft Lauderdale
Status: Offline
Reply With Quote
Jun 21, 2002, 11:36 AM
 
I'm not sure if this will work, try doing exactly what you did only leave primary key out. then do it again with primary key.

-> change primeryid primaryid int(5) auto_increment null;
-> change primaryid primaryid int(5) auto_increment null primary key;
     
Forum Regular
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jun 21, 2002, 11:48 AM
 
Just drop the column and add it again.

alter table account drop primeryid;
alter table account add primaryid int(5) not null primary key auto_increment first;

The first keyword at the end of the SQL statement will put the primaryid as the first column in the table - otherwise the primaryid will be the last column.
Is this rock and roll, or
a form of state control?
     
Dedicated MacNNer
Join Date: Aug 1999
Status: Offline
Reply With Quote
Jun 21, 2002, 12:34 PM
 
Have you tried it without the primary key reference:

alter table account change primeryid primaryid int(5) auto_increment null;

?? That should work, and then you could add the primary key later, if it was dropped, which I am not sure it would be.
     
dogwood  (op)
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jun 24, 2002, 07:18 PM
 
This I found to be finally the solution:-

mysql> alter table account
-> change primeryid primaryid int(5);

It only seems to work if you include the the_old_name the_new_name and the int(5) in the statement, and exclude the rest.

This keeps the data in the column intact and changes the column name only. If I was to drop the column so I could add it later I would also lose all the data in the column.
     
Senior User
Join Date: Sep 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 26, 2002, 06:27 AM
 
off topic question.....

I have a column that is defined as a varchar(20) and now needs to be
wider (say, 30 or so). Can I alter table similar to this w/o dropping the
column (and losing the data) ????
     
Fresh-Faced Recruit
Join Date: May 2002
Location: Ft Lauderdale
Status: Offline
Reply With Quote
Jun 26, 2002, 10:55 AM
 
ALTER TABLE tablename MODIFY fieldname varchar(30);

Use MODIFY to change the definition of a field or CHANGE to change definition and name of a field.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2