【问题标题】:How to modify column of one table to AUTO_INCREMENT which is foreign key for other table?如何将一个表的列修改为其他表的外键AUTO_INCREMENT?
【发布时间】:2021-06-26 12:50:50
【问题描述】:

SQL 查询:复制

ALTER TABLE categories MODIFY id int(11) AUTO_INCREMENT

MySQL 说:文档

#1833 - 无法更改列“id”:用于表“techblog.posts”的外键约束“posts_ibfk_1”

【问题讨论】:

    标签: mysql xampp


    【解决方案1】:

    您需要删除外键。在你的情况下:

    alter table posts drop constraint posts_ibfk_1;
    

    然后你可以更改列并重新添加外键:

    ALTER TABLE categories MODIFY id int(11) AUTO_INCREMENT;
    
    alter table posts add constraint foreign key (id) references categories (id);
    

    Here 是一个 dbfiddle。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2019-05-19
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      相关资源
      最近更新 更多