我在 mysql 数据库中有一个 2 个表:
Personnel:
personnel_id (PK)
name
age
qualification:
qualification_id (PK)
personnel_id (FK)
clg name
schl name
如何重命名人员表(personnel_id)的PK?由于它充当资格表的 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
或者,您可以
- 删除“资格”表的外键
- 重命名“人员”表中的主键
- 重新定义外键
关于python - MySQL : rename primary key column which is foregin key to another table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47989980/