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

Boo*_*oon 16 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)
);
Run Code Online (Sandbox Code Playgroud)

错误:

ERROR 1005 (HY000): Can't create table 'posinventory.stock_in' (errno: 150)
Run Code Online (Sandbox Code Playgroud)

Bjo*_*ern 29

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

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

一些想法:

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

  • 我想补充一点,如果你用FK引用的任何表都不是同一个引擎,你会收到消息; 好吧,肯定如果他们不是InnoDB. (2认同)

小智 5

有完全一样的问题。它的来源是外键和引用的类型。

外键:

fer_id SMALLINT NOT NULL
Run Code Online (Sandbox Code Playgroud)

和原点字段(我们提供的参考):

id INT(11) UNSIGNED NOT NULL
Run Code Online (Sandbox Code Playgroud)

我也将fer_id INT(11)也设为未签名。神奇的作品!