MySQL Error Number 1005 Can’t create table(Errno:150)

时间:2021-08-17 07:17:56

mysql数据库1005错误解决方法

MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

MySQL Error Number 1005
Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

If you get this error while trying to create a foreign key, it can be pretty frustrating. The error about not being able to create a .frm file seems like it would be some kind of OS file permission error or something but this is not the case. This error has been reported as a bug on the MySQL developer list for ages, but it is actually just a misleading error message.

In every case this is due to something about the relationship that MySQL doesn’t like. Unfortunately it doesn’t specify what the exact issue is.

First Steps:

If you have admin permission on the server, you may want to start by running the MySQL command “SHOW INNODB STATUS” (or MySQL 5.5 “SHOW ENGINE INNODB STATUS”) immediately after receiving the error. This command displays log info and error details. (Thanks Jonathan for the tip)

If your script runs fine on one server, but gives an error when you try to run it on a different server, then there is a good chance that #6 is the problem.  Different versions of MySQL have different default charset setting and you may have unknowingly assigned different charsets on the different servers.

Known Causes:

Below is a running list of known causes that people have reported for the dreaded errno 150:

  1. The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
  2. One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
  3. The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this. (Thanks to Niels for this tip)
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
  5. You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL.  You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
  6. Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns. (Thanks to FRR for this tip)
  7. You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
  8. One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint. (Thanks to Alex for this tip)
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)
  10. The name of your foreign key exceeds the max length of 64 chars.  (Thanks to Nyleta for the tip)

The MySQL documentation includes a page explaining requirements for foreign keys. Though they don’t specifically indicate it, these are all potential causes of errno 150. If you still haven’t solved your problem you may want to check there for deeper technical explainations.

If you run into this error and find that it’s caused by something else, please leave a comment and I’ll add it to the list.

MySQL Error Number 1005 Can’t create table(Errno:150)的更多相关文章

  1. django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")

    自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键, 今天,按以前的思路写完外键之后, migrate命令报错: 1005 - Can't create tab ...

  2. MySQL ERROR 1005: Can't create table (errno: 150)的错误解决办法

    在mysql 中建立引用约束的时候会出现MySQL ERROR 1005: Can't create table (errno: 150)的错误信息结果是不能建立 引用约束. 出现问题的大致情况 1. ...

  3. 【MySQL】ERROR 1005: Can't create table (errno: 150)的错误解决办法

    在mysql 中建立引用约束的时候会出现MySQL ERROR 1005: Can't create table (errno: 150)的错误信息结果是不能建立 引用约束. 出现问题的大致情况 1. ...

  4. 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can’t create table (errno: 121)

    ERROR 1005: Can't create table (errno: 121) errno 121 means a duplicate key error. Probably the tabl ...

  5. hive基本的操作语句(实例简单易懂,create table XX as select XX)

    hive建表语句DML:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Cr ...

  6. JS组件系列——表格组件神器:bootstrap table(三:终结篇,最后的干货福利)

    前言:前面介绍了两篇关于bootstrap table的基础用法,这章我们继续来看看它比较常用的一些功能,来个终结篇吧,毛爷爷告诉我们做事要有始有终~~bootstrap table这东西要想所有功能 ...

  7. JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)

    前言:上篇 JS组件系列——表格组件神器:bootstrap table 简单介绍了下Bootstrap Table的基础用法,没想到讨论还挺热烈的.有园友在评论中提到了父子表的用法,今天就结合Boo ...

  8. 错误代码: 1005 Can't create table 'hibernate.bill' (errno: 150)

    主要问题以及解决办法是: 1,MySQL支持外键约束,并提供与其它DB相同的功能,但表(外键表和外键主表)类型必须为 InnoDB,外键表和外键主表的类型都要是innoDB 建表约束语句: user表 ...

  9. MYSQL Statement violates GTID consistency: CREATE TABLE ... SELECT. 错误代码: 1786 问题

    1.在MYSQL中,执行建表语句时CREATE TABLE  aaaa  AS SELECT * FROM menu;  报: 错误代码: 1786Statement violates GTID co ...

随机推荐

  1. lombok介绍

    Lombok是一种JavaArchive(JAR)文件,可用来消除Java代码的冗长.在写代码时,可以通过这个插件消除各种getter和setter,toString等常用方法. lombok 注解: ...

  2. css3 自定义动画(1)

    <style> /*@-webkit-keyframes 动画名称 {} 用时:-webkit-animation:时间 动画名称; */ /* @-webkit-keyframes mo ...

  3. &lbrack;Asp&period;net MVC&rsqb;Asp&period;net MVC5系列——添加数据

    目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...

  4. Spring&plus;Struts2&sol;Hibernate 学习笔记

    ============Spring与Struts2整合============ (1)拷JAR包(Spring.Struts2) (2)配置org.springframework.web.conte ...

  5. Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)

    Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以&qu ...

  6. Android编译中m、mm、mmm的区别

    准备工作 在AndroidSource Code中有envsetup.sh档案,当执行过此档案后,可以大幅将build的过程简单化.自动化 此档案在src(android source code 位置 ...

  7. URAL1204&period; Idempotents(扩展欧几里得)

    1204 大体推推 会出来这个式子 x(x-1) = k*n;n = p*q ;x(x-1)%(p*q)==0; 因为p,q都为素数 那也就是说x和x-1中必定包含这两个数 而且一个里面只能有一个 不 ...

  8. Laravel自学第一课:laravel下载与安装

    本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...

  9. &lbrack;转&rsqb;python类方法

    Python定义类-方法 公有方法.私有方法.类方法.静态方法

  10. MongoDB学习--安装与管理

    一.简介 MongoDB是一种强大.灵活,且易于扩展的通用型数据库.他能扩展出非常多的功能.如二级索引(secondary index).范围查询(range query).排序.聚合(aggrega ...