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

发布于 2024-09-29 18:29:41 字数 492 浏览 0 评论 0原文

我正在尝试导入 .sql 文件,但在创建表时失败。

这是失败的查询:

CREATE TABLE `data` (
`id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`value` varchar(15) NOT NULL,
UNIQUE KEY `id` (`id`,`name`),
CONSTRAINT `data_ibfk_1` FOREIGN KEY (`id`) REFERENCES `keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;    

我从同一个数据库导出 .sql,我删除了所有表,现在我尝试导入它,为什么失败?

MySQL:无法创建表“./dbname/data.frm”(错误号:150)

I am trying to import a .sql file and its failing on creating tables.

Here's the query that fails:

CREATE TABLE `data` (
`id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`value` varchar(15) NOT NULL,
UNIQUE KEY `id` (`id`,`name`),
CONSTRAINT `data_ibfk_1` FOREIGN KEY (`id`) REFERENCES `keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;    

I exported the .sql from the same database, I dropped all the tables and now im trying to import it, why is it failing?

MySQL: Can't create table './dbname/data.frm' (errno: 150)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

滥情空心 2024-10-06 18:29:41

来自 MySQL - 外键约束文档

如果重新创建已删除的表,它必须具有符合引用它的外键约束的定义。如前所述,它必须具有正确的列名称和类型,并且必须在引用的键上具有索引。 如果不满足这些条件,MySQL 将返回错误 1005 并在错误消息中引用错误 150,这意味着外键约束未正确形成。同样,如果ALTER TABLE 由于错误 150 而失败,这意味着更改的表的外键定义格式不正确。

From the MySQL - FOREIGN KEY Constraints Documentation:

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 correct column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns Error 1005 and refers to Error 150 in the error message, which means that a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails due to Error 150, this means that a foreign key definition would be incorrectly formed for the altered table.

阪姬 2024-10-06 18:29:41

错误 150 表示您的外键有问题。外表上的键可能不是完全相同的类型?

Error 150 means you have a problem with your foreign key. Possibly the key on the foreign table isn't the exact same type?

吃颗糖壮壮胆 2024-10-06 18:29:41

您可以通过运行 SHOW ENGINE INNODB STATUS; 来获取实际的错误消息,然后在输出中查找 LATEST FOREIGN KEY ERROR

来源:另一位用户在类似问题中的回答

You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.

Source: answer from another user in a similar question

厌倦 2024-10-06 18:29:41

数据类型必须完全匹配。如果您正在处理 varchar 类型,则表必须使用相同的排序规则。

Data types must match exactly. If you are dealing with varchar types, the tables must use the same collation.

缘字诀 2024-10-06 18:29:41

我认为所有这些答案虽然正确,但都误导了这个问题。

如果您要使用外键恢复转储文件,则实际答案是在开始恢复之前:

SET FOREIGN_KEY_CHECKS=0;

因为恢复自然会在外部表存在之前创建一些约束。

I think all these answers while correct are misleading to the question.

The actual answer is this before you start a restore, if you're restoring a dump file with foreign keys:

SET FOREIGN_KEY_CHECKS=0;

because naturally the restore will be creating some constraints before the foreign table even exists.

前事休说 2024-10-06 18:29:41

在某些情况下,如果相关表之间存在不同的引擎,您可能会遇到此错误消息。例如,一个表可能使用 InnoDB,而另一个表使用 MyISAM。两者必须相同

In some cases, you may encounter this error message if there are different engines between the relating tables. For example, a table may be using InnoDB while the other uses MyISAM. Both need to be same

人海汹涌 2024-10-06 18:29:41

错误号150表示外键约束失败。您可能在外键所依赖的表(表 keywords)之前创建此表。首先创建该表,它应该可以正常工作。

如果没有,请删除外键语句并在创建表后添加它 - 您将收到有关特定约束失败的更有意义的错误消息。

Error no. 150 means a foreign key constraint failure. You are probably creating this table before the table the foreign key depends on (table keywords). Create that table first and it should work fine.

If it doesn't, remove the foreign key statement and add it after the table is created - you will get a more meaningful error message about the specific constraint failure.

夜声 2024-10-06 18:29:41

有很多事情可能导致 errno 150,因此对于搜索该主题的人来说,这里是我认为接近详尽的列表(来源 Errno 150的原因):

对于errno 150或errno 121,只需在SHOW ENGINE INNODB STATUS中输入即可,有一个名为“LATEST FOREIGN KEY ERROR”的部分”。在此之下,它会给您一个非常有用的错误消息,通常会立即告诉您出了什么问题。您需要超级权限才能运行它,因此如果您没有超级权限,则只需测试以下场景。

1) 数据类型不匹配:列的类型必须相同

2) 父列未索引(或索引顺序错误)

3) 列排序规则不匹配

4) 在 NOT NULL 列上使用 SET NULL

5) 表排序规则不匹配:即使列排序规则匹配,在某些 MySQL 版本上这也可能是一个问题。

6) 父列实际上并不存在于父表中。检查拼写(可能还有列开头或结尾处的空格)

7) 其中一列上的索引之一不完整,或者该列对于完整索引来说太长。请注意,MySQL(除非您调整它)的最大单列键长度为 767 字节(这对应于 varchar(255) UTF 列)

如果您收到 errno 121,则有几个原因:

1)约束您选择的名称已被使用

2) 在某些系统上,如果您的语句和表名称存在大小写差异。如果您从一台服务器转到另一台具有不同案例处理规则的服务器,这可能会给您带来麻烦。

There are quite a few things that can cause errno 150, so for people searching this topic, here is what I think is a close to exhaustive list (source Causes of Errno 150):

For errno 150 or errno 121, simply typing in SHOW ENGINE INNODB STATUS, there is a section called "LATEST FOREIGN KEY ERROR". Under that it will give you a very helpful error message, which typically will tell you right away what is the matter. You need SUPER privileges to run it, so if you don't have that, you'll just have to test out the following scenarios.

1) Data Types Don't Match: The types of the columns have to be the same

2) Parent Columns Not Indexed (Or Indexed in Wrong Order)

3) Column Collations Don't Match

4) Using SET NULL on a NOT NULL Column

5) Table Collations Don't Match: even if the column collations match, on some MySQL versions this can be a problem.

6) Parent Column Doesn't Actually Exist In Parent Table. Check spelling (and perhaps a space at the beginning or end of column)

7) One of the indexes on one of the columns is incomplete, or the column is too long for a complete index. Note that MySQL (unless you tweak it) has a maximum single column key length of 767 bytes (this corresponds to a varchar(255) UTF column)

In case you get an errno 121, here are a couple of causes:

1) The constraint name you chose is already taken

2) On some systems if there is a case difference in your statement and table names. This can bite you if you go from one server to another that have different case handling rules.

葬心 2024-10-06 18:29:41

有时MySQL非常愚蠢-我可以理解外键的原因..但就我而言,我刚刚删除了整个数据库,但我仍然收到错误...为什么?我的意思是,不再有数据库了...并且我正在使用的sql用户无法访问服务器上的任何其他数据库...我的意思是,服务器对于当前用户来说是“空的”,我仍然得到这个错误?抱歉,但我猜MySQL在骗我...但我可以处理它:)只需在你该死的语句周围添加这两行SQL:

SET FOREIGN_KEY_CHECKS = 0;
# some code that gives you errno: 150
SET FOREIGN_KEY_CHECKS = 1;

现在应该执行sql...如果你真的有外键问题,它会通过您将再次启用检查的行向您显示 - 这将失败..但我的服务器很安静:)

Sometimes MySQL is just super stupid - i can understand the reason cause of foreign-keys.. but in my case, i have just dropped the whole database, and i still get the error... why? i mean, there is no database anymore... and the sql-user i'm using has no access to any other db's on the server... i mean, the server is "empty" for the current user and i still get this error? Sorry but i guess MySQL is lying to me... but i can deal with it :) Just add these two lines of SQL around your fucky statement:

SET FOREIGN_KEY_CHECKS = 0;
# some code that gives you errno: 150
SET FOREIGN_KEY_CHECKS = 1;

Now the sql should be executed... If you really have a foreign-key problem, it would show up to you by the line where you will enable the checks again - this will fail then.. but my server is just quiet :)

无力看清 2024-10-06 18:29:41

通常,外键与外键不匹配。主键导致
错误:150。

外键必须与主键具有相同的数据类型。此外,如果主键无符号,那么外键也必须是无符号

usually, the mismatch between foreign key & primary key causes the
error:150.

The foreign key must have the same datatype as the primary key. Also, if the primary key is unsigned then the foreign key must also be unsigned.

-黛色若梦 2024-10-06 18:29:41

我有同样的问题。它与表格的列排序规则字符集有关。
确保两个表中两列的字符集排序规则必须相同。如果你想在上面设置外键。
示例 - 如果您将外键放在引用用户表的 userID 列的 userImage 表的 userID 列上。则两列的排序规则必须相同,即 utf8_general_ci 和字符集 utf8表。通常,当您创建表时,mysql 从服务器设置中获取这两个配置。

I had same issue. It was related to table's column Collation and Character Set.
Make sure Character Set and Collation must be same for both columns on two tables. If you want to set a foreign key on that.
Example- If you put foreign key on userID column of userImage table referencing userID column of users table.Then Collation must be same that is utf8_general_ci and Character set utf8 for both columns of tables. Generally when you create a table mysql takes these two configuration from server settings.

并安 2024-10-06 18:29:41

改变表的引擎,只有innoDB支持外键

Change the engines of your tables, only innoDB supports foreign keys

有木有妳兜一样 2024-10-06 18:29:41

在浏览完上面的答案并进行一些实验后,这是解决 MySQL 中外键错误(1005 - 错误 150)的有效方法。

为了正确创建外键,MySQL 的所有要求是:

  • 所有引用的键必须具有 PRIMARY 或 UNIQUE 索引。
  • 再次引用列必须具有与引用列相同的数据类型。

满足这些要求,一切都会好起来的。

After cruising through the answers above, and experimenting a bit, this is an effective way to solve Foreign Key errors in MySQL (1005 - error 150).

For the foreign key to be properly created, all MySQL asks for is:

  • All referenced keys MUST have either PRIMARY or UNIQUE index.
  • Referencing Column again MUST have identical data type to the Referenced column.

Satisfy these requirements and all will be well.

隔纱相望 2024-10-06 18:29:41

我在将 Windows 应用程序移植到 Linux 时遇到了这个错误。在Windows中,数据库表名称不区分大小写,而在Linux中,数据库表名称区分大小写,可能是因为文件系统的差异。因此,在 Windows 上,表 Table1table1 相同,而在 REFERENCES 中,table1Table1 有效。在 Linux 上,当应用程序在创建数据库结构时使用 table1 而不是 Table1 时,我看到错误 #150;当我在 Table1 引用中正确设置字符大小写时,它也开始在 Linux 上运行。因此,如果没有其他帮助,请确保在 Linux 上的 REFERENCES 中表名中使用正确的字符大小写。

I experienced this error when have ported Windows application to Linux. In Windows, database table names are case-insensitive, and in Linux they are case-sensitive, probably because of file system difference. So, on Windows table Table1 is the same as table1, and in REFERENCES both table1 and Table1 works. On Linux, when application used table1 instead of Table1 when it created database structure I saw error #150; when I made correct character case in Table1 references, it started to work on Linux too. So, if nothing else helps, make you sure that in REFERENCES you use correct character case in table name when you on Linux.

半衬遮猫 2024-10-06 18:29:41

如果 PK 表是在一个 CHARSET 中创建的,然后在另一个 CHARSET 中创建 FK 表。那么您也可能会收到此错误...我也收到此错误,但在将字符集更改为 PK 字符集之后然后它就没有错误地执行了

create table users
(
------------
-------------
)DEFAULT CHARSET=latin1;


create table Emp
(
---------
---------
---------
FOREIGN KEY (userid) REFERENCES users(id) on update cascade on delete cascade)ENGINE=InnoDB, DEFAULT CHARSET=latin1;

If the PK table is created in one CHARSET and then you create FK table in another CHARSET..then also you might get this error...I too got this error but after changing the charset to PK charset then it got executed without errors

create table users
(
------------
-------------
)DEFAULT CHARSET=latin1;


create table Emp
(
---------
---------
---------
FOREIGN KEY (userid) REFERENCES users(id) on update cascade on delete cascade)ENGINE=InnoDB, DEFAULT CHARSET=latin1;
何以笙箫默 2024-10-06 18:29:41

如果两个表都有引用,例如,一个表是 Student,另一个表是 Education,并且我们希望 Education 表具有 Student 表的外键引用,则可能会发生此错误。在这种情况下,两个表的列数据类型应该相同,否则会生成错误。

This error can occur if two tables have a reference, for example, one table is Student and another table is Education, and we want the Education table to have a foreign key reference of Student table. In this instance the column data type for both tables should be same, otherwise it will generate an error.

乜一 2024-10-06 18:29:41

在大多数情况下,问题是由于 ENGINE 差异造成的。如果父表是由 InnoDB 创建的,那么引用的表应该由 MyISAM 和 InnoDB 创建。反之亦然

In most of the cases the problem is because of the ENGINE dIfference .If the parent is created by InnoDB then the referenced tables supposed to be created by MyISAM & vice versa

夜吻♂芭芘 2024-10-06 18:29:41

就我而言。我遇到了引擎和字符集问题,因为我的托管服务器更改了设置,并且我的新表是 MyISAM,但我的旧表是 InnoDB。只是我变了。

In my case. I had problems with engine and charset because my Hosting server change settings and my new tables was MyISAM but my old tables are InnoDB. Just i changed.

莫多说 2024-10-06 18:29:41

请确保您的主键列和引用列具有相同的数据类型和属性(无符号、二进制、无符号零填充等)。

Please make sure both your primary key column and referenced column have the same data types and attributes (unsigned, binary, unsigned zerofill etc).

£噩梦荏苒 2024-10-06 18:29:41

真正的边缘情况是您使用 MySQL 工具(在我的例子中为 Sequel Pro)来重命名数据库。然后创建一个同名的数据库。

这使外键约束保持在相同的数据库名称上,因此重命名的数据库(例如 my_db_renamed)在新创建的数据库(my_db)中具有外键约束。

不确定这是否是 Sequel Pro 中的错误,或者某些用例是否需要此行为,但这花费了我一个早上的大部分时间:/

A real edge case is where you have used an MySQL tool, (Sequel Pro in my case) to rename a database. Then created a database with the same name.

This kept foreign key constraints to the same database name, so the renamed database (e.g. my_db_renamed) had foreign key constraints in the newly created database (my_db)

Not sure if this is a bug in Sequel Pro, or if some use case requires this behaviour, but it cost me best part of a morning :/

恏ㄋ傷疤忘ㄋ疼 2024-10-06 18:29:41

您从子表引用的父表的列必须是唯一的。如果不是,则导致错误 150。

The column of PARENT table to which you are referring to from child table has to be unique. If it is not, cause an error no 150.

云巢 2024-10-06 18:29:41

我有同样的错误。在我的例子中,错误的原因是我在约束中有一个 ON DELETE SET NULL 语句,而我在其定义中放置约束的字段有一个 NOT NULL 语句。在字段中允许 NULL 解决了这个问题。

I had the same error. In my case the reason for the error was that I had a ON DELETE SET NULL statement in the constraint while the field on which I put the constraint in its definition had a NOT NULL statement. Allowing NULL in the field solved the problem.

起风了 2024-10-06 18:29:41

我在从文本文件创建数据库时遇到了此类问题。

mysql -uroot -padmin < E:\important\sampdb\createdb.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_absence.sql

mysql -uroot -padmin sampdb < E:\important\sampdb\insert_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\insert_absence.sql

mysql -uroot -padmin sampdb < E:\important\sampdb\load_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\load_absence.sql 

我刚刚在Create.bat中编写了以上几行并运行bat文件。

我的错误在于我的sql文件中的执行顺序。我尝试创建带有主键和外键的表。当它运行时,它将搜索参考表,但表不存在。
所以它会返回这类错误。

如果您使用外键创建表,请检查引用
桌子是否存在。还要检查参考文献的名称
表和字段。

I faced this kind of issue while creating DB from the textfile.

mysql -uroot -padmin < E:\important\sampdb\createdb.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_absence.sql

mysql -uroot -padmin sampdb < E:\important\sampdb\insert_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\insert_absence.sql

mysql -uroot -padmin sampdb < E:\important\sampdb\load_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\load_absence.sql 

I just wrote the above lines in Create.batand run the bat file.

My mistake is in the sequence order of execution in my sql files. I tried to create table with primary key and also foreign key. While its running it will search for the reference table but tables are not there.
So it will return those kind of error.

If you creating tables with foreign key then check the reference
tables were present or not. And also check the name of the reference
tables and fields.

↘紸啶 2024-10-06 18:29:41

我有一个类似的问题,但我的问题是因为我正在向包含 data 的现有表添加一个新字段,并且新字段引用父表中的另一个字段,并且还具有 NOT NULL 的定义并且没有任何默认值。 - 我发现事情不起作用的原因是因为

  1. 我的新字段需要在应用约束之前使用每条记录上父表中的值自动填充空白字段。每次应用约束时,都需要保持表数据的完整性不变。实施约束(外键),但有一些数据库记录没有父表中的值,这意味着数据已损坏,因此 MySQL 永远不会强制执行您的约束

重要的是要记住,在正常情况下,如果您提前规划数据库,并在数据插入之前实施约束,可以避免这种特殊情况 避免

这种问题的更简单方法是

  • 保存数据库表数据
  • 截断表数据(和表工件,即索引等)
  • 应用约束
  • 导入您的数据

我希望这对某人有帮助

I had a similar problem but mine was because i was adding a new field to an existing table that had data , and the new field was referencing another field from the parent table and also had the Defination of NOT NULL and without any DEFAULT VALUES. - I found out the reason things were not working was because

  1. My new field needed to autofill the blank fields with a value from the parent table on each record, before the constraint could be applied. Every time the constraint is applied it needs to leave the Integrity of the table data intact. Implementing the Constraint (Foreign Key) yet there were some database records that did not have the values from the parent table would mean the data is corrupt so MySQL would NEVER ENFORCE YOUR CONSTRAINT

It is important to remember that under normal circumstances if you planned your database well ahead of time, and implemented constraints before data insertion this particular scenario would be avoided

The easier Approach to avoid this gotcha is to

  • Save your database tables data
  • Truncate the table data (and table artifacts i.e indexes etc)
  • Apply the Constraints
  • Import Your Data

I Hope this helps someone

韵柒 2024-10-06 18:29:41

创建没有外键的表,然后单独设置外键。

Create the table without foreign key, then set the foreign key separately.

凉月流沐 2024-10-06 18:29:41

也许会帮助?主键列的定义应该与外键列的定义完全相同。

Perhaps this will help? The definition of the primary key column should be exactly the same as the foreign key column.

揪着可爱 2024-10-06 18:29:41

确保所有表都支持外键 - InnoDB引擎

Make sure that the all tables can support foreign key - InnoDB engine

风吹雪碎 2024-10-06 18:29:41

当使用单个表转储 Django mysql 数据库时,我遇到了类似的问题。我能够通过将数据库转储到文本文件、使用 emacs 将有问题的表移动到文件末尾并将修改后的 sql 转储文件导入到新实例中来解决该问题。

HTH 乌韦

I had a similar problem when dumping a Django mysql database with a single table. I was able to fix the problem by dumping the database to a text file, moving the table in question to the end of the file using emacs and importing the modified sql dump file into the new instance.

HTH Uwe

流云如水 2024-10-06 18:29:41

我通过让变量接受 null 纠正了这个问题

ALTER TABLE `ajout_norme` 
CHANGE `type_norme_code` `type_norme_code` VARCHAR( 2 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL

I've corrected the problem by making the variable accept null

ALTER TABLE `ajout_norme` 
CHANGE `type_norme_code` `type_norme_code` VARCHAR( 2 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
尽揽少女心 2024-10-06 18:29:41

我在执行一系列MySQL命令时遇到了同样的问题。我的问题是在创建表期间引用尚未创建的其他表的外键时发生的。这是引用之前表存在的顺序。

解决办法:先创建父表,再创建有外键的子表。

I got the same problem when executing a series of MySQL commands. Mine occurs during creating a table when referencing a foreign key to other table which was not created yet. It's the sequence of table existence before referencing.

The solution: Create the parent tables first before creating a child table which has a foreign key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文