重命名 MySQL 中的外键列
我们正在尝试重命名 MySQL(5.1.31,InnoDB)中作为另一个表的外键的列。
起初,我们尝试使用 Django-South,但遇到了一个已知问题:
http://south .aeracode.org/ticket/243
操作错误:(1025,“将'./xxx/#sql-bf_4d'重命名为'./xxx/cave_event'时出错(errno:150)”)
和
将“./xxx/#sql-bf_4b”重命名为“./xxx/cave_event”时出错(errno:150)
此错误 150 肯定与外键约束有关。请参阅例如
什么mysql错误1025(HY000):重命名“./foo”时出错(错误号:150)是什么意思?
http://www.xaprb.com/blog/2006/08/22/mysqls-error-1025-explained/
所以现在我们'正在尝试在原始 SQL 中进行重命名。
我们是否必须先删除外键,然后重命名,然后再次添加外键?
既然这看起来相当混乱和麻烦,有什么更好的方法呢?
We're trying to rename a column in MySQL (5.1.31, InnoDB) that is a foreign key to another table.
At first, we tried to use Django-South, but came up against a known issue:
http://south.aeracode.org/ticket/243
OperationalError: (1025, "Error on rename of './xxx/#sql-bf_4d' to './xxx/cave_event' (errno: 150)")
AND
Error on rename of './xxx/#sql-bf_4b' to './xxx/cave_event' (errno: 150)
This error 150 definitely pertains to foreign key constraints. See e.g.
What does mysql error 1025 (HY000): Error on rename of './foo' (errorno: 150) mean?
http://www.xaprb.com/blog/2006/08/22/mysqls-error-1025-explained/
So now we're trying to do the renaming in raw SQL.
Do we have to drop the foreign key first, then do the rename, and then add the foreign key back again?
What is a better way, since this seems pretty confusing and cumbersome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
AFAIK,删除约束,然后重命名,然后添加回约束是唯一的方法。先备份!
AFAIK, dropping the constraint, then rename, then add the constraint back is the only way. Backup first!
如果有人正在寻找语法,它会是这样的:
In case anyone is looking for the syntax it goes something like this:
这是常规键的 SQL 语法
here is the SQL syntax for regular keys
扩展@Dewey的答案,这里有一个小脚本,用于以有用的方式重命名由Hibernate生成的FK
(“FK__”+表名+“__”+引用的表名)
。一点输出:
Expanding on @Dewey's answer, here's a little script to rename FKs generated by Hibernate in a useful manner
("FK__" + table name + "__" + referenced table name)
.A bit of output:
以下查询将自动构建正确的语法。
只需执行返回的每一行,您的所有 FKEY 就会消失。
我将相反的部分(将它们添加回去)作为练习留给您。
The following query will build the correct syntax automatically.
Just execute each line returned and all your FKEYs will be gone.
I leave the reverse (adding them back) as an exercise for you.
现在不需要采取额外的行动。当使用
ALGORITHM = INPLACE
时,MySQL/MariaDB会自动修改外键约束。请参阅 https ://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html#online-ddl-column-operations“重命名列”
例如以下内容是充足的:
No extra actions are needed nowadays. When using
ALGORITHM = INPLACE
, MySQL/MariaDB automatically modifies the foreign key constraint.See https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html#online-ddl-column-operations "Renaming a column"
So for example the following would be sufficient:
下面的代码经过测试。
OldColumnConstrain
OldColumnName
INTEGER UNSIGNED NULL 的新外键列
NewColumnName
AFTER previousColumn
t1
约束新列newColumnConstrain
>重要
运行上述查询会将您的旧列重命名为新列(不完全是重命名,而是删除和添加列),但请注意该列数据将丢失,并且将被替换为
NULL
。你真的可以重命名你的fk吗?
我想不是。
请参阅此答案https://stackoverflow.com/a/2014519/5413283
The code below is tested.
OldColumnConstrain
OldColumnName
NewColumnName
with the datatypeINTEGER UNSIGNED NULL
AFTER previousColumn
newColumnConstrain
with the reference tablet1
Important
Running the above query will rename your Old column to a New column (not exactly renaming but deleting and adding a column), but WATCHOUT that column data will be lost and it will be replaced with
NULL
.Can you ACTUALLY rename your fk?
I guess not.
See this answer https://stackoverflow.com/a/2014519/5413283
如果您使用 GUI 工具,此任务会变得更简单。我尝试使用 IntelliJ IDEA 数据库工具 重命名 ID 列它就像一个魅力!重命名表或列时,我不必担心外键。
请参阅 IntelliJ IDEA 帮助 | 了解更多详细信息重命名项目。
This task becomes simpler if you use GUI tools. I tried to rename ID column using IntelliJ IDEA Database tool and it worked like a charm! I don't have to bother about foreign keys when renaming a table or column.
See more details in IntelliJ IDEA Help | Renaming items.