`

MySQL Error Number 1005 Can’t create table

阅读更多
This is a very common error when create a mysql table , even some people has take it as a mysql bug and commit it to the official , I came up with this bothered problem these days also, after a crazy Google work, the source of the problem became clear , the following lines is a very good article that give that "bug" a conclusion , and I'd like to translate it into simple Chinese in case you'll hit that problem again ,

Concerning the personal talent, some mistakes can not be avoid , if you found any, please feel free to contact me , or leave a message below, thanks in advance.
source url :

http://www.verysimple.com/blog/2006/10/22/mysql-error-number-1005-cant-create-table-mydbsql-328_45frm-errno-150/

Translation text :

当你试图在mysql中创建一个外键的时候,这个出错会经常发生,这是非常令人沮丧的。像这种不能创建一个.frm 文件的报错好像暗示着操作系统的文件的权限错误或者其它原因,但实际上,这些都不是的,事实上,这个mysql报错已经被报告是一个mysql本身的bug并出现在mysql 开发者列表当中很多年了,然而这似乎又是一种误导。

在很多实例中,这种错误的发生都是因为mysql一直以来都不能很好的支持的关系的问题, 更不幸的是它也并没有指明到底是哪一个问题会导致上面那种错误,下面我把导致这个可怕 的150错误的常见原因列出来了,并且我以可能性的大小作了排序,已知的原因:

1, 两个字段的类型或者大小不严格匹配,例如,如果一个是INT(10), 那么外键也必须设置成INT(10), 而不是 INT(11) 也不能是 TINYINT. 你得使用 SHOW 命令来查看字段的大小,因为一些查询浏览器有时候把 int(10) 和int(11) 都显示为integer。另外,你还必须确定两个字段是否一个为 SIGNED,而另一个又是UNSIGNED, 这两字段必须严格地一致匹配,更多关于signed 和 unsigned 的信息,请参阅:http://www.verysimple.com/blog/?p=57

2, 你试图引用的其中一个外键没有建立起索引,或者不是一个primary key , 如果其中一个不是primary key 的放,你必须为它创建一个索引。

3, 外键的名字是一个已经存在的一个键值了,这个时候,你应该检查你的数据库以确保外健名字是唯一的,或者你在键名后面加上几个随机的字符以测试是否是这个原因。

4, 其中一个或者两个表是MyISAM引擎的表,若想要使用外键约束,必须是InnoDB引擎,(实际上,如果两个表都是MyISAM 引擎的,这个错误根本不会发生,但也不会产生外键),你可以通过查询浏览器来设置表的引擎类型

5, 你可能设置了ON DELETE SET NULL, 但是相关的键的字段又设置成了NOTS NULL 值。你可能通过修改cascade 的属性值或者把字段属性设置成 allow null 来搞定这个bug.

6, 请确定你的Charset 和 Collate 选项在表级和字段级上的一致

7, 你可能设置为外键设置了一个默认值,如 default=0

8, 在这个关系里面,其中的一个字段是一个混合键值中的一个,它没有自己独立的索引,这时,你必须为它创建一个独立的索引。

9, ALTER 声明中有语法错误
分享到:
评论

相关推荐

    [php]mysql数据库操作——DB类

    public function create_database($data=''){$this->query("CREATE DATABASE {$data}");} // 查询服务器所有数据库 public function show_database(){ $this->query('SHOW DATABASES'); $db=array(); while...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    The specific example is parsing a MySQL error log file, organizing the data into unique dates and error messages, and counting the number of times each error message appeared on each date. The ...

    php.ini-development

    If PHP can't find an expected ; directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one ; of the...

    go系统编程(英文版)

    goroutines, by talking about how you can create goroutines and how you can synchronize them and wait for them to finish before ending a program. Then it talks about channels and pipelines, which help ...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    1. Create table命令 用于创建表。在创建表时,经常会创建该表的主键、外键、唯一约束、Check约束等  语法结构 create table 表名( [字段名] [类型] [约束] ……….. CONSTRAINT fk_column FOREIGN KEY(column1...

    Securing PHP Web Applications.pdf

    Hide the Version Number and Other Sensitive Information 151 Restrict Apache to Its Own Directory Structure 152 Disable Any Options You Don’t Explicitly Need 153 Install and Enable ModSecurity 154 ...

    VB编程资源大全(英文源码 数据库)

    When you click on a table, it lists all <END><br>38,3a.zip An Inventory System, you can add, edit, sort, find, delete, print, change your password and create a backup of your database<END><br>39,A...

    Delphi7.1 Update

    * TClientDataSet doesn‘t save data to file when FileName is set and there is no existing file on disk (Quality Central 2307). * Using the Delphi 7 version of midas.dll to open an XML file that was ...

    php_6_fast_and_easy_web_development.pdf

    Inserting, Updating, or Replacing Within a Table. . . . . . . . . . . 515 Deleting from a Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Selecting from a Table. . . . . . . ....

    WordPress 2.7 Cookbook.pdf

    Table of Contents xii There's more... 214 Inserting shortcodes into sidebar widgets 215 Displaying Adsense ads to search engines visitors only 215 Getting ready 216 How to do it 216 How it works ...

    Java学习笔记-个人整理的

    {12.13.2}create}{175}{subsection.12.13.2} {12.13.3}rownum}{175}{subsection.12.13.3} {12.13.4}update}{176}{subsection.12.13.4} {12.13.5}delete}{177}{subsection.12.13.5} {12.13.6}drop}{177}{...

Global site tag (gtag.js) - Google Analytics