MySQL执行SQL报错1005_SQL_解决由于操作不当出现的ERROR 1005错误,在使用MySQL的时候,在操作不 - phpStudy...

解决由于操作不当出现的ERROR 1005错误

在使用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

本文作者:相关阅读:

PHP教程:strtok()函数实际应用

sql2005 存储过程分页示例代码

arrayToJson将数组转化为json格式的js代码

学堂:当前Linux系统运行的各种参数

在ASP里面创建GUID

来自国外的一款Js图片浏览效果

css 表单效果

将Oracle中的数据更改后出错的解决方法

在DIV容器中使用浮动元素的方法

fedora8 下mysql 安装的安装方法

Vista系统下硬盘IDE模式安装后开启AHCI的方法

Linux应用问答系列之硬件FAQ

Linux和Windows文件路径的解决办法

仿迅雷焦点广告效果(JQuery版)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值