现在的位置: 首页 > 数据库 > 正文

关于MYSQL Error:ERROR 1005 (HY000) at line 3: Can’t create table ‘.\xx.frm’错误

2018年11月04日 数据库 ⁄ 共 2718字 ⁄ 字号 评论关闭

在使用mysql导入sql文件的时候出错了。

我机器上mysql重装了N次,数据文件已经达到了400MB之多,可能以前曾经导入过源表,再次导入的时候发现这个错误:

ERROR 1005 (HY000) at line 3: Can't create table '.\test\test.frm' (errno: 121)

google baidu了一番,果然和数据文件没有删干净有关。
我很NXX的直接把数据文件删了……结果再装MYSQL起不来了。
只好重新删干净mysql,再装一遍……

------------------------------------------------------------------------------------------------------------------------------

另有一种说法是主外键约束的问题,与我的问题不相符,资料先留着,备用:

附相关资料:
在mysql 中建立引用约束的时候会出现MySQL ERROR
1005:
Can't create table (errno: 150)的错误信息结果是不能建立 引用约束。

出现问题的大致情况

1、外键的引用类型不一样,主键是int外键是char

2、找不到主表中 引用的列

3、主键和外键的字符编码不一致,

进行mysql的数据备份,两种方法:
1)文件拷贝,这当然是最简单的了,把对应database的文件夹拷贝,然后拷贝文件ibdata1。
2)mysqldump

第2种方法当然更好了,比如我备份数据库mydb,步骤就是:

mysql/bin>mysqldump mydb > mydb.sql

恢复的时候首先在mysql创建对应的数据库;

create database mydb;

然后导入mydb.sql:

mysql/bin>mysql mydb < mydb.sql

结果今天出了问题,错误提示是:

ERROR 1005 (HY000) at line 12: Can't create table './mydb/foo.frm' (errno: 150)

这是因为表中有外键约束导致的。

……

对数据库不熟悉,结果google,baidu半天也没看到一个面向数据库菜鸟的指南,呵呵,现在解决了,给大家做个贡献:废话少说 :)解决办法是:

在恢复数据之前,

SET FOREIGN_KEY_CHECKS = 0;

恢复之后设置回来:

SET FOREIGN_KEY_CHECKS = 1

就Ok拉。

在使用MySQL的时候,在操作不当时,很容易出现 ERROR 1005 (HY000): Can't create table 这类错误。
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

抱歉!评论已关闭.