在使用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
分享到:
相关推荐
erron:150相关介绍 当你试图在mysql中创建一个外键的时候,这个出错会经常发生,这是非常令人沮丧的。像这种不能创建一个.frm 文件的报错好像暗示着操作系统的文件的权限错误或者其它原因,但实际上,这些都不是的
mysql新手容易出错的操作 <br>
数据库代码 MySQL 5.0错误。如果您使用到智能交易中,指标或脚本将以 MySQL数据开启。这样您的智能交易可能有不正常工作或全部不工作的地方。通过数据库信息可以知道返回的MySQL数据错误。
MySQL的ERROR 150问题 MySQL的ERROR 150问题 MySQL的ERROR 150问题
Mysql 安装错误的详细解决方法一览表
。。。
NULL 博文链接:https://314858770.iteye.com/blog/686136
让人纠结了一把…… 代码如下:zhouqian@zhou:~$ mysqlERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)网上好多的解决办法,可是每个人的实际情况...
完整报错信息: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 解决: 1.打开在创建mysql容器时挂载的conf目录下的mysqld.cnf(如下文件) 我创建docker的姿势 ...
常见问题如果在OSX上收到错误“捕获的DBI :: InterfaceError异常'无法加载驱动程序(未初始化的常量MysqlError)'”,请尝试执行以下操作: export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH...
MysqlERROR1698(28000)解决,新装了mysql-server-5.7,登录为这一问题,普通用户不能进mysql,只有root用户才能进,并且不需要任何密码。 ~$ mysql -u root -p Enter password: ERROR 1698 (28000): Access denied ...
2、修正了同一IP24小时允许的最大上传附件数,超过时,上传图片直接返回错误的路径,uploadfile/ 3、修正了include/global.func.phpthumb函数问题 4、修正了整合后,utf8登陆乱码问题 5、修正了内容页面分页第一页为...
2、修正了同一IP24小时允许的最大上传附件数,超过时,上传图片直接返回错误的路径,uploadfile/ 3、修正了include/global.func.phpthumb函数问题 4、修正了整合后,utf8登陆乱码问题 5、修正了内容页面分页第一页为...