MySQL -- 外键创建失败
使用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 -- 外键创建失败的更多相关文章
- mysql外键创建失败原因
引用:http://blog.csdn.net/wangpeng047/article/details/19624351 首先,如果和外键相关的几张表中已经插入了数据,可能导致外键插入的失败 在MyS ...
- mysql外键创建不成功/失效
当前mysql版本:SELECT VERSION();结果为:5.5.40. 在复习mysql外键约束时创建表格:stu与grade,目标:grade的id随着student的id级联更新,且限制删除 ...
- MySQL外键的作用和创建
MySQL外键的作用: 保持数据一致性,完整性,主要目的是控制存储在外键表中的数据.使两张表形成关联,外键只能引用外表中列的值! 我们来建两个表 CREATE TABLE `example1` ( ` ...
- Mysql 外键设置
MySql外键设置详解 (1) 外键的使用: 外键的作用,主要有两个: 一个是让数据库自己通过外键来保证数据的完整性和一致性 一个就是能够增加ER图的可读性 有些人认为外键的建立会给 ...
- mysql --->mysql 外键总结
mysql 外键总结 1.设置外键MySQL ERROR 1005 错误 MySQL ERROR 1005 (主要是约束不一样导致的)例如: 1.两表外键的引用类型不一样,如主键是int外键是char ...
- mysql 外键约束备注
梳理mysql外键约束的知识点. 1.mysql外键约束只对InnoDb引擎有效: 2.创建外键约束如下: DROP TABLE IF EXISTS t_demo_product; CREATE TA ...
- MySQL外键之级联
简介 MySQL外键起到约束作用,在数据库层面保证数据的完整性.例如使用外键的CASCADE类型,当子表(例如user_info)关联父表(例如user)时,父表更新或删除时,子表会更新或删除记录,这 ...
- MySQL外键约束On Delete、On Update各取值的含义
主键.外键和索引的区别? 主键 外键 索引 定义: 唯一标识一条记录,不能有重复的,不允许为空 表的外键是另一表的主键, 外键可以有重复的, 可以是空值 主索引(由关键字PRIMARY定义的索引) ...
- mysql 外键和子查询,视图
1.mysql 外键约束 建表时生成外键 foreing key ('sid') references' student'('id'); 建表后添加外键 alter table' course ...
随机推荐
- 一个小栗子聊聊JAVA泛型基础
背景 周五本该是愉快的,可是今天花了一个早上查问题,为什么要花一个早上?我把原因总结为两点: 日志信息严重丢失,茫茫代码毫无头绪. 对泛型的认识不够,导致代码出现了BUG. 第一个原因可以通过以后编码 ...
- SuperMap入门3——Hello World
Hello World程序很重要,对于入门来说,它可以检测我们的环境.配置是否正确,感受程序的易用性等. 添加工具 由于我是使用的VS2017+ SuperMap iObject绿色免安装版,所以新建 ...
- 微信小程序 - 自定义tabbar(组件)
配置项(关于使用组件) index.wxml <!-- tabBar:tabBar配置 activeIndex: 激活页面下标 slots: 多插槽配置(需与页面一致) --> <t ...
- Session Alerts
To create alerts for specific sessions, add rules using FiddlerScript. For example: Play a sound whe ...
- time series review
https://pdfs.semanticscholar.org/fc9c/1a94a15dd44c8c61b3e4841ecb9505f8cd37.pdf https://edoc.hu-berli ...
- Windows Python+Eclipse环境配置
参考这几篇博客: 1:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html 2:http://www.cnblogs.com/min ...
- 请用js去除字符串空格?
方法一:使用replace正则匹配的方法 去除所有空格: str = str.replace(/\s*/g,""); 去除两头空格: str = str.replace(/^\s* ...
- sell 创建项目
1.数据操作软件 2.创建项目 3.mevan 镜像
- window批处理杀死指定端口进程
@echo off setlocal enabledelayedexpansion set /p port=请输入端口号: for /f "tokens=1-5" %%a in ( ...
- Java ThreadLocal (Java代码实战-006)
ThreadLocal解决什么问题 由于 ThreadLocal 支持范型,如 ThreadLocal< StringBuilder >,为表述方便,后文用 变量 代表 ThreadLoc ...