繁体   English   中英

MySQL:重命名主键列,它是另一个表的原键

[英]MySQL : rename primary key column which is foregin key to another table

我在mysql数据库中有2个表:

Personnel:
   personnel_id (PK)
   name
   age


qualification:
  qualification_id (PK)
  personnel_id (FK)
  clg name
  schl name

如何重命名人员表PK(personnel_id)? 由于它充当资格表的Fk,而我将其重命名为person_id时,我面临的问题是:

无法更改列'personnel_id':用于表'mobilemeta.qualification的外键约束'qualification_ibfk_1'中

 A possible way that I know is:
     Drop FK, Rename Column, Add FK

但是我正在使用python处理这些数据库操作 因此,不可能找到它作为外键引用到的表,有什么方法可以动态处理它?

有什么办法吗?

您可以按照下面的步骤进行操作。

选项1 :

SET FOREIGN_KEY_CHECKS=0;
<Your first alter command to rename the primary key in "Personnel" table>;
<Your second alter command to change the foreign key in "qualification" table>;
SET FOREIGN_KEY_CHECKS=1;

选项2

或者,您可以

  1. 删除“资格”表的外键
  2. 重命名“人员”表中的主键
  3. 重新定义外键

使用alter命令更改列名,不需要更改外键或主键。 只需更改名称即可使用alter命令ALTER TABLE table_name修改列名称旧名称新名称数据类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM