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

时间:2021-12-09 00:59:19

I get an error when I try to create a table in mysql.

当我尝试在mysql中创建表时,会出现错误。

Any tips on resolving it?

有什么解决方法吗?

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)
);

The Error:

错误:

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

5 个解决方案

#1


27  

Check out the MySQL manual about foreign key constrains:

查看MySQL手册关于外键限制:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

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

A few ideas:

几个想法:

  • Better drop the tables and create it new with a well formed syntax.
  • 最好删除表并使用格式良好的语法创建新的表。
  • Make sure to add ENGINE=InnoDB; to your CREATE TABLE - command.
  • 确保添加ENGINE=InnoDB;到您的创建表-命令。
  • Make sure InnoDB is enabled on your MySQL server. To verify this, try this command: SHOW VARIABLES LIKE 'have_innodb'; - if it returns a YES, then InnoDB is enabled.
  • 确保在MySQL服务器上启用了InnoDB。要验证这一点,请尝试以下命令:显示'have_innodb'等变量;-如果它返回YES,则启用InnoDB。
  • Check your command for upper- and lowercases in table- and fieldnames.
  • 检查表和字段名中上部和小写的命令。
  • Check this not only one the table you want to create, but also on the tables the foreign keys are referring to.
  • 检查这不仅是您想要创建的表,还包括外键所指的表。
  • Make sure your referred tables are properly indexed.
  • 确保引用的表被正确索引。

#2


4  

Had exactly the same problem. The source of it is in types for foreign key and reference.

有完全相同的问题。它的来源是用于外键和引用的类型。

Foreign key:

外键:

fer_id SMALLINT NOT NULL

and the origin field (refernce to which we provide):

和产地来源(我们提供的参考):

id INT(11) UNSIGNED NOT NULL

I've just make the fer_id INT(11) UNSIGNED as well. Magically works!

我还将fer_id INT(11)设为无符号。神奇的作品!

#3


1  

One of the reason would be the foreign key column data type and length should be same as the referring table key column. For example the referring table column is mediumint(11) and the foreign key column is int(11) means error will occur while creating a table with foreign key.

原因之一是外键列数据类型和长度应该与引用表键列相同。例如,引用表列是mediumint(11),外键列是int(11),表示在创建带有外键的表时将发生错误。

#4


1  

On top of other things, you need to make sure both tables/fields match CHARSET settings. A UTF type field can be substantially different size than a 'latin1'. SHOW CREATE TABLE on both tables will show you if there is a mismatch

最重要的是,您需要确保两个表/字段都匹配字符集设置。UTF类型字段的大小与“latin1”字段的大小有很大的不同。在两个表上显示CREATE TABLE将显示是否存在不匹配。

#5


1  

If you restore your database backup you may temporary disable foreign keys check by inserting these strings in the begining of .sql file:

如果您恢复数据库备份,您可以临时禁用外键检查,在.sql文件的开头插入这些字符串:

/*!40030 SET NAMES UTF8 */;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

#1


27  

Check out the MySQL manual about foreign key constrains:

查看MySQL手册关于外键限制:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

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

A few ideas:

几个想法:

  • Better drop the tables and create it new with a well formed syntax.
  • 最好删除表并使用格式良好的语法创建新的表。
  • Make sure to add ENGINE=InnoDB; to your CREATE TABLE - command.
  • 确保添加ENGINE=InnoDB;到您的创建表-命令。
  • Make sure InnoDB is enabled on your MySQL server. To verify this, try this command: SHOW VARIABLES LIKE 'have_innodb'; - if it returns a YES, then InnoDB is enabled.
  • 确保在MySQL服务器上启用了InnoDB。要验证这一点,请尝试以下命令:显示'have_innodb'等变量;-如果它返回YES,则启用InnoDB。
  • Check your command for upper- and lowercases in table- and fieldnames.
  • 检查表和字段名中上部和小写的命令。
  • Check this not only one the table you want to create, but also on the tables the foreign keys are referring to.
  • 检查这不仅是您想要创建的表,还包括外键所指的表。
  • Make sure your referred tables are properly indexed.
  • 确保引用的表被正确索引。

#2


4  

Had exactly the same problem. The source of it is in types for foreign key and reference.

有完全相同的问题。它的来源是用于外键和引用的类型。

Foreign key:

外键:

fer_id SMALLINT NOT NULL

and the origin field (refernce to which we provide):

和产地来源(我们提供的参考):

id INT(11) UNSIGNED NOT NULL

I've just make the fer_id INT(11) UNSIGNED as well. Magically works!

我还将fer_id INT(11)设为无符号。神奇的作品!

#3


1  

One of the reason would be the foreign key column data type and length should be same as the referring table key column. For example the referring table column is mediumint(11) and the foreign key column is int(11) means error will occur while creating a table with foreign key.

原因之一是外键列数据类型和长度应该与引用表键列相同。例如,引用表列是mediumint(11),外键列是int(11),表示在创建带有外键的表时将发生错误。

#4


1  

On top of other things, you need to make sure both tables/fields match CHARSET settings. A UTF type field can be substantially different size than a 'latin1'. SHOW CREATE TABLE on both tables will show you if there is a mismatch

最重要的是,您需要确保两个表/字段都匹配字符集设置。UTF类型字段的大小与“latin1”字段的大小有很大的不同。在两个表上显示CREATE TABLE将显示是否存在不匹配。

#5


1  

If you restore your database backup you may temporary disable foreign keys check by inserting these strings in the begining of .sql file:

如果您恢复数据库备份,您可以临时禁用外键检查,在.sql文件的开头插入这些字符串:

/*!40030 SET NAMES UTF8 */;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;