ALTER TABLE error

*爱你&永不变心* 提交于 2019-12-11 06:19:18

问题


Can someone explain to me why I am receiving the following error?

I want to rename the column "exerciseID" to "ID" in a mysql table using the following syntax.

ALTER TABLE  `exercises` CHANGE  `exerciseID`  `ID` INT( 11 ) NOT NULL AUTO_INCREMENT

However I receive the following error:

MySQL said: 

#1025 - Error on rename of './balance/#sql-de_110e' to './balance/exercises' (errno: 150)

Any suggestions would be much appreciated


回答1:


I would check to see if you have any foreign key references to that column. If so, you may need to remove the foreign relationships that you have defined for that column, then rename, then place your foreign key relationships back in place with the new column name.

I think MySQL is getting hung up on the fact that when you rename, the FK relationships are no longer valid and it is throwing an error.

EDIT: Confirmed FK Rename in MySQL

You will need to do something like this:

alter table yourTable drop foreign key yourID



回答2:


i think it might be a multi step process.

  1. add a new column,
  2. copy the data over from the original column
  3. drop the old column



回答3:


just having a quick search in google, and it looks like you are referencing the column in a foreign key - which is preventing the rename.

afraid i'm not sure how you'd resolve the problem, as I haven't used foreign keys in MySQL all that much



来源:https://stackoverflow.com/questions/2920946/alter-table-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!