使用show engine innodb status\G 查看数据库状态的时候,发现以下报错信息:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2017-09-16 16:22:38 7ff485c25700 Error in foreign key constraint of table abce/#sql-1f3_284b3:
foreign key (sappres_id) references s_app_res (id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
------------------------

可以看出,创建外键索引失败,在父表和子表之间约束不匹配。但是只能看出被参照的表是s_app_res,对应的字段是id。但是没有显示参照表的名字,只是显示参照表中的列名。

使用以下语句将参照表的信息找出来。其中,table_schema字段为db的名称(所属的数据库),字段table_name为表的名称。

mysql> select * from information_schema.columns where column_name='sappres_id';
+---------------+--------------+-------------------+-------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+-------------+------------+-------+---------------------------------+----------------+
| table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length | character_octet_length | numeric_precision | numeric_scale | datetime_precision | character_set_name | collation_name | column_type | column_key | extra | privileges | column_comment |
+---------------+--------------+-------------------+-------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+-------------+------------+-------+---------------------------------+----------------+
| def | abce | s_sync_res | sappres_id | 15 | null | yes | int | null | null | 10 | 0 | null | null | null | int(11) | | | select,insert,update,references | |
+---------------+--------------+-------------------+-------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+-------------+------------+-------+---------------------------------+----------------+
1 row in set (0.04 sec) mysql>

  

然后对比表s_app_res、s_sync_res的定义语句:
s_app_res.id是表的主键,但是字段类型定义格式为:

 `id` bigint(20) not null

s_sync_res.sappres_id 的定义格式为:

`sappres_id` int(11) DEFAULT NULL

  

原因找到,修改这两个对应字段的定义即可。

MySQL -- 外键创建失败的更多相关文章

  1. mysql外键创建失败原因

    引用:http://blog.csdn.net/wangpeng047/article/details/19624351 首先,如果和外键相关的几张表中已经插入了数据,可能导致外键插入的失败 在MyS ...

  2. mysql外键创建不成功/失效

    当前mysql版本:SELECT VERSION();结果为:5.5.40. 在复习mysql外键约束时创建表格:stu与grade,目标:grade的id随着student的id级联更新,且限制删除 ...

  3. MySQL外键的作用和创建

    MySQL外键的作用: 保持数据一致性,完整性,主要目的是控制存储在外键表中的数据.使两张表形成关联,外键只能引用外表中列的值! 我们来建两个表 CREATE TABLE `example1` ( ` ...

  4. Mysql 外键设置

    MySql外键设置详解 (1) 外键的使用: 外键的作用,主要有两个:    一个是让数据库自己通过外键来保证数据的完整性和一致性    一个就是能够增加ER图的可读性    有些人认为外键的建立会给 ...

  5. mysql --->mysql 外键总结

    mysql 外键总结 1.设置外键MySQL ERROR 1005 错误 MySQL ERROR 1005 (主要是约束不一样导致的)例如: 1.两表外键的引用类型不一样,如主键是int外键是char ...

  6. mysql 外键约束备注

    梳理mysql外键约束的知识点. 1.mysql外键约束只对InnoDb引擎有效: 2.创建外键约束如下: DROP TABLE IF EXISTS t_demo_product; CREATE TA ...

  7. MySQL外键之级联

    简介 MySQL外键起到约束作用,在数据库层面保证数据的完整性.例如使用外键的CASCADE类型,当子表(例如user_info)关联父表(例如user)时,父表更新或删除时,子表会更新或删除记录,这 ...

  8. MySQL外键约束On Delete、On Update各取值的含义

    主键.外键和索引的区别?   主键 外键 索引 定义: 唯一标识一条记录,不能有重复的,不允许为空 表的外键是另一表的主键, 外键可以有重复的, 可以是空值 主索引(由关键字PRIMARY定义的索引) ...

  9. mysql 外键和子查询,视图

    1.mysql 外键约束 建表时生成外键   foreing key ('sid') references' student'('id'); 建表后添加外键  alter table' course ...

随机推荐

  1. Android Volley 库的使用

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库的使用 参考资料 Android 关于网络操作一般都会介绍 HttpClient 以及 Ht ...

  2. AS-->创建项目(慢)和打开项目(慢)等需要注意的问题

    Android Studio 创建新项目的时候,会在进度条的界面滞留很久. 有时候一天都卡在这. 打开其它项目有些时候,也会出现这种情况. 主要的原因就是, 此过程正在 下载对应版本的 gradle. ...

  3. mycat分库分表 mod-long

    转载自:http://blog.csdn.net/sunlihuo/article/details/54574903 下面是配置文件 schema.xml: <?xml version=&quo ...

  4. iOS 获取设备型号 ip6更新

    //获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...

  5. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  6. ZH奶酪:Python按行读取文件

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...

  7. 常用API文档

    Python requtets PyQuery Pascal Delphi PHP ThinkPHP5.0完全开发手册 ThinkPHP3.2.3快速入门

  8. 自己定义RatingBar,能依据设置改变样式

    项目在我的GITHUB上  mirsfang的GitHub 一个简单的自己定义View  ,为了一个 比較奇葩的需求而搞出来的.他的功能就是能让你自己设置图片和图片的大小以及星星的数量,是一个组合型的 ...

  9. AndroidAnnotations库的使用

    AndroidAnnotations(Code Diet) android高速开发框架简单介绍: 项目地址:https://github.com/excilys/androidannotations ...

  10. 〖Linux〗noip免费域名申请,及更新域名的API

    1. 登录 http://www.noip.com2. 选择 Hosts/Redirects -- Add A Host3. 填写 期望的域名即可(如下图) 4. 更新域名的API: wget -q ...