如何重命名 MySQL 中的主键列?

发布于 2024-08-30 02:23:53 字数 25 浏览 3 评论 0原文

如何重命名 MySQL 中的主键列?

How do I rename a primary key column in MySQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

森林很绿却致人迷途 2024-09-06 02:23:53

这与更改任何其他列没有什么不同 -

ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT 

这会将表 pkey 中的列 keyfield 更改为 keyfield2 - 您必须提供像往常一样,之后定义。

it's no different than altering any other column --

ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT 

this changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual.

纵情客 2024-09-06 02:23:53

省略 alter 语句的 PRIMARY KEY 部分。主键将自动更新。

Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically.

情绪操控生活 2024-09-06 02:23:53

也许您有外键约束。您可以通过 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.

最好是你 2024-09-06 02:23:53

可能是一种不好的做法。但是您可以将整个数据库导出到 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.

梦明 2024-09-06 02:23:53

如果您正在使用 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.

╄→承喏 2024-09-06 02:23:53

如果其他表在你的表上有外键,你不能直接使用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 :

  • drop foreign keys from others tables pointing to the primary key you want to rename
  • rename the primary key
  • add the foreign column to other tables

When renaming a table in Intellij, it generates you the code do drop and add the foreign key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文