小编典典

错误1005(HY000):无法创建表(错误号:150)

mysql

尝试在mysql中创建表时出现错误。

有解决的技巧吗?

create table stock_in(
    ind int not null auto_increment,
    itemcode varchar(10) not null,
    quantity int not null,
    description text not null,
    sales_ref int not null default -1,
    return_outwards_ref int not null default -1,
    stock_in_receipt_ref int not null default -1,
    date text not null,
    time text not null,
    username text not null,
    foreign key (sales_ref) references sales (receiptno),
    foreign key (return_outwards_ref) references returnoutwards(ind),
    primary key (ind)
);

错误:

ERROR 1005 (HY000): Can't create table 'posinventory.stock_in' (errno: 150)

阅读 543

收藏
2020-05-17

共1个答案

小编典典

查看有关外键约束的MySQL手册:

如果重新创建已删除的表,则该表必须具有符合引用该表的外键约束的定义。如前所述,它必须具有正确的列名和类型,并且必须在引用的键上具有索引。如果不满足这些条件,MySQL将返回错误号1005,并在错误消息中引用错误150。

一些想法:

  • 最好删除表,并使用格式正确的语法对其进行新建。
  • 确保添加ENGINE=InnoDB;CREATE TABLE-命令。
  • 确保在您的MySQL服务器上启用了InnoDB。要验证这一点,请尝试以下命令:SHOW VARIABLES LIKE 'have_innodb';-如果返回YES,则启用InnoDB。
  • 检查命令中表名和字段名的大小写。
  • 不仅要检查要创建的表,还要检查外键引用的表。
  • 确保引用的表已正确索引。
2020-05-17