Mysql create constraint foreign key faild.trouble shooting method share

mysql> create table tb_test (id int(10) not null auto_increment primary key,action_id int(10) not null,error_code int(10) not null default 0,desc_key varchar(64) not null default ‘audit.log.default‘,INDEX(action_id),constraint `FK_ACTIONID` foreign key(`action_id`) references `tb_tab1` (`action_id`));
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql>

定位方法如下:

SHOW ENGINE INNODB STATUS:

LATEST FOREIGN KEY ERROR
------------------------
2016-01-29 08:45:15 7f5b11b09700 Error in foreign key constraint of table auditdb/tb_test:
foreign key(`action_id`) references `tb_tab1` (`action_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.

很详细

时间: 2024-08-01 22:46:04

Mysql create constraint foreign key faild.trouble shooting method share的相关文章

MYSQL外键(Foreign Key)的使用

转载自:http://www.cppblog.com/wolf/articles/69089.html#Post 原文实在太精辟又形象,忍不住转载过来留下笔记,像作者致敬 在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持):2.外键列必须建立了索引,MySQL 4.1.2以后的版本在建立外键时会自动创建索引,但如果在较早的版本则需要显示建立: 3.外

mysql 外键(FOREIGN KEY)使用介绍

一.基本概念 1.MySQL中"键"和"索引"的定义相同,所以外键和主键一样也是索引的一种.不同的是MySQL会自动为所有表的主键进行索引,但是外键字段必须由用户进行明确的索引.用于外键关系的字段必须在所有的参照表中进行明确地索引,InnoDB不能自动地创建索引. 2.外键可以是一对一的,一个表的记录只能与另一个表的一条记录连接,或者是一对多的,一个表的记录与另一个表的多条记录连接. 3.如果需要更好的性能,并且不需要完整性检查,可以选择使用MyISAM表类型,如果

MySql中的FOREIGN KEY

在mysql数据库编程中经常会出现几个表中存在某些相关属性,通常情况下我们可以通过使用外键约束来解决这个问题.但在删除表和修改表的结构时会出现一些小问题. 在删除表的时候Cannot delete or update a parent row: a foreign key constraint fails (...) 这是因为外键约束导致不能对表直接的进行相关操作. 取消外键约束  SET FOREIGN_KEY_CHECKS=0; 设置外键约束 SET FOREIGN_KEY_CKECKS=1

MySQL数据库之-foreign key 外键(一对多、多对多、一对一)、修改表、复制表

今日重点:外键 一对多 多对多      一对一 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 一.引言: 我们在同一数据库创建的表时候,很多时候会出现相同数据的冗余问题,也就是说几个id

MySQL Foreign Key

ntroduction to MySQL foreign key A foreign key is a field in a table that matches another field of another table. A foreign key places constraints on data in the related tables, which enables MySQL to maintain referential integrity. Let’s take a look

MySQL foreign key - How to define a foreign key in MySQL

table of contents Creating a MySQL foreign key MySQL foreign key support in database engines MySQL foreign keys and ON UPDATE and ON DELETE MySQL foreign key FAQ: How do I define a foreign key in MySQL? Answer: Here's a quick example of how I typical

SQL - Create Foreign Key Relationships

/* To create a foreign key in a new table */ CREATE TABLE Sales.TempSalesReason ( TempID int NOT NULL, Name nvarchar(50), CONSTRAINT PK_TempSales PRIMARY KEY NONCLUSTERED (TempID), CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID) REFERENCES S

Database Primary key and Foreign key [From Internet]

Database Primary key and Foreign key --Create Referenced Table CREATE TABLE Department ( DeptID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, Address varchar(100) NULL ) --Create Referencing Table CREATE TABLE Employee ( EmpID int

MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sas_basic的操作能复制到该从库上. 2. 整个架构是1主2从,且都是选择性复制,上面这个从库是直接复制card,upay,deal,monitor,collect这几个数据库的数据,而另外一个从库则是忽略上述库,如下所示: 怀疑是在上述schema下,执行了DROP TABLE IF EXIST