443

How do I rename a column in table xyz? The columns are:

Manufacurerid, name, status, AI, PK, int

I want to rename to manufacturerid

I tried using PHPMyAdmin panel, but I get this error:

MySQL said: Documentation
#1025 - Error on rename of '.\shopping\#sql-c98_26' to '.\shopping\tblmanufacturer' (errno: 150)
jpmc26
  • 28,463
  • 14
  • 94
  • 146
Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
  • 5
    You're trying to change a table that has a foreign key reference in it. – mellowsoon Oct 23 '10 at 03:41
  • 3
    possible duplicate of [Renaming foreign-key columns in MySQL](http://stackoverflow.com/questions/2014498/renaming-foreign-key-columns-in-mysql) – OMG Ponies Oct 23 '10 at 03:44

9 Answers9

753

Lone Ranger is very close... in fact, you also need to specify the datatype of the renamed column. For example:

ALTER TABLE `xyz` CHANGE `manufacurerid` `manufacturerid` INT;

Remember :

  • Replace INT with whatever your column data type is (REQUIRED)
  • Tilde/ Backtick (`) is optional
c0degeas
  • 762
  • 9
  • 19
Matt Diamond
  • 11,646
  • 5
  • 29
  • 36
  • 12
    I think the column contains a foreign key to getting the error of the answer so you have to Drop foreign key, alter table, and add foreign key (it's better to backup first) and u can alter it by only changing the name in right click on the table - alter table – Chris Sim May 09 '14 at 10:06
  • 7
    Keep in mind that in this solution you lose all other column definitions such as nullability, default value, etc. (see: http://stackoverflow.com/questions/8553130/rename-a-column-in-mysql-table-without-having-to-repeat-its-type-definition). – Dejan Jul 13 '15 at 10:50
  • So basically, instead of `MODIFY column ` (for redefining a column), it's `CHANGE column new_column `? – mwfearnley Oct 13 '15 at 15:54
  • `SHOW CREATE TABLE table_name` can help you figure out current column definition – Mr. Deathless Jan 21 '16 at 18:32
  • @Dejan thanks and this is really annoying, why require a type when you can't specify any other modifiers? Either require a type + modifiers or don't require it at all. – JMac Apr 12 '16 at 05:12
  • @JMac It appears that at lest since MySQL 5.1 all modifiers are supported, the type is just the only one that is required, and it is always required (like when you create a table). – J.D. Dec 21 '16 at 19:43
44

The standard MySQL rename statement is:

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name 
CHANGE [COLUMN] old_col_name new_col_name column_definition 
[FIRST|AFTER col_name]

For this example:

ALTER TABLE xyz CHANGE manufacurerid manufacturerid datatype(length)

Reference: MYSQL 5.1 ALTER TABLE Syntax

informatik01
  • 16,038
  • 10
  • 74
  • 104
dongpf
  • 1,537
  • 16
  • 8
42

FOR MYSQL:

ALTER TABLE `table_name` CHANGE `old_name` `new_name` VARCHAR(255) NOT NULL;

FOR ORACLE:

ALTER TABLE `table_name` RENAME COLUMN `old_name` TO `new_name`;
T.Todua
  • 53,146
  • 19
  • 236
  • 237
mahbub_siddique
  • 1,755
  • 18
  • 22
14

EDIT

You can rename fields using:

ALTER TABLE xyz CHANGE manufacurerid manufacturerid INT

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

Community
  • 1
  • 1
Lone Ranger
  • 296
  • 1
  • 3
  • 1
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 – Bharanikumar Oct 23 '10 at 04:02
  • 17
    This would be useful, but it's not true to the documentation you cited: "When you use CHANGE or MODIFY, **column_definition must include the data type and all attributes that should apply to the new column** [...] Attributes present in the original definition but not specified for the new definition are not carried forward. " – artfulrobot Jan 19 '13 at 23:34
  • it has to be "ALTER TABLE xyz CHANGE manufacurerid manufacturerid datatype(length)" – Chris Sim May 09 '14 at 08:30
7

There is a syntax problem, because the right syntax to alter command is ALTER TABLE tablename CHANGE OldColumnName NewColunmName DATATYPE;

Torsten
  • 1,696
  • 2
  • 21
  • 42
Darshan
  • 2,094
  • 1
  • 15
  • 15
2

With MySQL 5.x you can use:

ALTER TABLE table_name
CHANGE COLUMN old_column_name new_column_name DATATYPE NULL DEFAULT NULL;
Morix Dev
  • 2,700
  • 1
  • 28
  • 49
MIKE KIM
  • 29
  • 1
1

Renaming a column in MySQL :

ALTER TABLE mytable CHANGE current_column_name new_column_name DATATYPE;
Hasib Kamal Chowdhury
  • 2,476
  • 26
  • 28
0

ALTER TABLE CHANGE ;

Example:

ALTER TABLE global_user CHANGE deviceToken deviceId VARCHAR(255) ;
vaquar khan
  • 10,864
  • 5
  • 72
  • 96
-5

SYNTAX

alter table table_name rename column old column name to new column name;

Example:

alter table library rename column cost to price;

Abinaya
  • 15
  • 1