如何重命名 MySQL 中的主键列?
如何重命名 MySQL 中的主键列?
How do I rename a primary key column in MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何重命名 MySQL 中的主键列?
How do I rename a primary key column in MySQL?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
这与更改任何其他列没有什么不同 -
这会将表
pkey
中的列keyfield
更改为keyfield2
- 您必须提供像往常一样,之后定义。it's no different than altering any other column --
this changes the column
keyfield
in tablepkey
to be calledkeyfield2
-- you have to supply the definition afterwards, as usual.省略 alter 语句的 PRIMARY KEY 部分。主键将自动更新。
Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically.
也许您有外键约束。您可以通过
SETforeign_key_constraints=0
禁用这些功能,但您必须记住事后更新数据库。Maybe you have a foreign key constraint in place. You can disable those by
SET foreign_key_constraints=0
but you have to remember to update the database afterwards.可能是一种不好的做法。但是您可以将整个数据库导出到 sql 文本文件。找到并替换你要重命名的PK,然后通过sql恢复数据库。
Possible a bad practice work around. But you could export your entire db to an sql text file. Find and replace the PK you want to rename, and then restore the database over sql.
如果您正在使用 InnoDB,那么我认为您无法重命名主键,至少在它们被外键引用时您不能。您需要转储数据库,重命名转储文件中的列和引用键,然后重新加载数据库。
If you are working with InnoDB then I think you cannot rename primary keys, at least you can't if they are referenced by foreign keys. You need to dump the database, rename the columns and referencing keys in the dump file, then reload the database.
如果其他表在你的表上有外键,你不能直接使用alter table重命名该列,它会抛出以下错误:[HY000][1025] Error on rename of xxx to yyy (errno: 150)
您必须:
中 在 Intellij 中重命名表时,它会生成删除并添加外键的代码。
If others tables have a foreign key on your table, you cannot directly rename the column using alter table, it will throw the following error: [HY000][1025] Error on rename of xxx to yyy (errno: 150)
You must :
When renaming a table in Intellij, it generates you the code do drop and add the foreign key.