1005错误解决办法

来源:互联网 发布:sql 最大值 最小值 编辑:程序博客网 时间:2024/06/17 20:11

1005错误解决办法:

use test;
create table number(nid bigint(20) not null primary key, homeNumber varchar(20),workNumber varchar(20),mobileNumber varchar(20),linkmanNumber varchar(20), foreign key(nid) references customer(id_no));
因为id_no的类型是bigint(20),所以引用它做外键的表的键必须同类型,如果是int就不行

示例二:

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