当前位置: 首页 > 图文教程 > 数据库 > MYSQL > MYSQL使用错误 MYSQL中ERROR 1005

MYSQL
让MYSQL彻底支持中文
数据安全之MySQL安全的二十三条军规
MySQL的一些安全注意点
mysql5存储过程编写实践
oracle移植到mysql注意事项
对抗MySQL数据库解密高手
最简便的备份MySQL数据库的方法
Linux操作系统下MySQL数据库的使用方法
Oracle与MySQL删除字段时对索引和约束的处理
Linux下C连接MySQL出现错误解决一例
Linux下MySQL整个数据库的备份与还原
MySQL数据库的授权原则
MySQL中数据表操作详解
在函数间不能传递32个以上参数的疑难问题
带你轻松接触MySQL数据库的出错代码列表
MySQL数据库备份恢复的两个实用方法
巧用JDBC实现对MySQL的“增删改查”
怎样在MySQL数据库中导出整个数据库
实例讲解MySQL数据库中文问题的解决方法
实例讲解如何使用C++操作MySQL数据库类

MYSQL使用错误 MYSQL中ERROR 1005


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 110 ::
收藏到网摘: n/a

在使用MYsql的时候,在操作不当时,很容易出现 ERROR 1005 (HY000): Can't create table 这类错误。很多站长朋友可能需要排查很久才会找到问题的原因其实很简单,希望这篇文章可以对站长朋友以及Mysql初学者一点帮助。

MYSQL官方提供的问题原因:

在信息中有一组【LATEST FOREIGN KEY ERROR】会有最近错误的详细描述和解决办法。

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.
(译:不能在“被reference的表”里找到包含“被reference字段”的索引,或者是两个关联字段类型不匹配)

以下介绍两个示例:

示例一:

create table booktype
(
   btid   int(5) unsigned zerofill auto_increment not null primary key,
   btname varchar(100) not null unique,
   btnote text
);

create table books
(
   bid int(5) unsigned zerofill auto_increment not null primary key,
   bname char(30) not null,
   isbn char(50) not null,
   author char(30) not null,
   press   text,
   summary text,
   bcount int not null default 0,
   btid   int,
   foreign key(btid) references booktype(btid)
);

出现的报错:

ERROR 1005 (HY000): Can't create table '.\bookdata\books.frm' (errno: 150)

主要问题以及解决办法是:

foreign key(btid) references booktype(btid) 中books表的 btid   是int和booktype表中的btid设置的关联字段类型不匹配,books表中btid改正成:btid   int(5) unsigned zerofill ,就不会报错了,创建表和修改表地时候常常一步小小就忘记了这个.

示例二:

MySQL里创建外键时(Alter table xxx add constraint fk_xxx foreign key),提示错误,但只提示很简单的信息:ERROR 1005 (HY000): Can't create table '.\env_mon\#sql-698_6.frm' (errno: 150)。根本起不到解决问题的作用。

(以下红色部分为已经修改)

drop table if exists products;
create table products(
 id  int  not null auto_increment,
 title  varchar(100) not null,
 description text  not null,
 image_url varchar(200) not null,
 price  decimal(10,2) not null,
 date_available  datetime not null,
 primary key(id)
)type=innodb;


drop table if exists line_items;

create table line_items(
 id  int  not null auto_increment,
 product_id int  not null,
 quantity int  not null default 0,
 unit_price decimal(10,2) not null,

 constraint fk_items_product foreign key (product_id) references producets(id),

index(product_id)
 primary key(id)
)type=innodb;

出现的报错:

ERROR 1005: Can't create table

主要问题以及解决办法是:

1,MySQL支持外键约束,并提供与其它DB相同的功能,但表类型必须为 InnoDB
2、建外键的表的那个列要加上index