mysql添加外键失败解决方案

mysql重启命令:

[root@wshCentOS centOS7Share]# service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[root@wshCentOS centOS7Share]# systemctl start mysqld.service
[root@wshCentOS centOS7Share]# mysql -uroot -proot123

 

mysql运行sql文件:source /mnt/hgfs/XXX.sql;

初始化sql运行导致的外键关联失败:

:35:31.428 ERROR jdbc.sqlonly 4. Statement.executeUpdate(
    alter table1
        add index FK_atx28tyc1c6j00d5mi7e5xt9d (key_id),
        add constraint FK_atx28tyc1c6j00d5mi7e5xt9d
        foreign key (key_id)
        references table2 (id))
java.sql.SQLException: Cannot add foreign key constraint

查看表字段属性:

 

两个字段类型长度不同会导致外键关联失败,使用如下语句修改:
alter table 表名称 modify 字段名称 字段类型 [是否允许非空];

posted @ 2017-12-27 18:19  无才可去补苍天  阅读(532)  评论(0编辑  收藏  举报