mysql无法添加外键?

发布于 2024-11-03 11:05:32 字数 1134 浏览 6 评论 0原文

我使用MySQL Workbench在表中添加外键,但是发生了一些奇怪的错误,这是SQL语句:

ALTER TABLE `tansung`.`Declaration` ADD COLUMN `goodsId` INT(11) NOT NULL  AFTER `declarationId` , 
    ADD CONSTRAINT `goodsId`
        FOREIGN KEY (`goodsId` )
        REFERENCES `tansung`.`Goods` (`goodsId` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
    , ADD INDEX `goodsId` (`goodsId` ASC) ;

当我单击应用时,惊喜出现了!

ERROR 1005: Can't create table 'tansung.#sql-1b10_1' (errno: 150)

SQL Statement:

ALTER TABLE `tansung`.`Declaration` ADD COLUMN `goodsId` INT(11) NOT NULL  AFTER `declarationId` , 
    ADD CONSTRAINT `goodsId`
        FOREIGN KEY (`goodsId` )
        REFERENCES `tansung`.`Goods` (`goodsId` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
    , ADD INDEX `goodsId` (`goodsId` ASC)


ERROR: Error when running failback script. Details follow.


ERROR 1050: Table 'Declaration' already exists

SQL Statement:

CREATE TABLE `Declaration` (
    `declarationId` int(11) NOT NULL,
    PRIMARY KEY (`declarationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我找不到任何逻辑错误,甚至无法理解错误,请给我帮助。

I used MySQL workbench to add a foreign key in a table, but some strange error happened, this is the SQL statement:

ALTER TABLE `tansung`.`Declaration` ADD COLUMN `goodsId` INT(11) NOT NULL  AFTER `declarationId` , 
    ADD CONSTRAINT `goodsId`
        FOREIGN KEY (`goodsId` )
        REFERENCES `tansung`.`Goods` (`goodsId` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
    , ADD INDEX `goodsId` (`goodsId` ASC) ;

When i click apply, the surprise comes out!

ERROR 1005: Can't create table 'tansung.#sql-1b10_1' (errno: 150)

SQL Statement:

ALTER TABLE `tansung`.`Declaration` ADD COLUMN `goodsId` INT(11) NOT NULL  AFTER `declarationId` , 
    ADD CONSTRAINT `goodsId`
        FOREIGN KEY (`goodsId` )
        REFERENCES `tansung`.`Goods` (`goodsId` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
    , ADD INDEX `goodsId` (`goodsId` ASC)


ERROR: Error when running failback script. Details follow.


ERROR 1050: Table 'Declaration' already exists

SQL Statement:

CREATE TABLE `Declaration` (
    `declarationId` int(11) NOT NULL,
    PRIMARY KEY (`declarationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I can't find out any mistake in logic, even can't understand the error, please give me a help.

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

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

发布评论

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

评论(10

旧街凉风 2024-11-10 11:05:32

整个数据库中的所有外键名称必须是唯一的。如果您已经有一个名为“goodsId”的外键,即使在另一个表上,您也会收到此错误。

如果相关列没有完全相同的类型(例如 INT)和约束(UNIQUE 等),您将收到该错误。

All foreign key names throughout the database must be unique. If you already have a foreign key named 'goodsId', even on another table, you will receive this error.

If the related columns do not have exactly the same type (e.g. INT) and constraints (UNIQUE and such), you will receive that error.

独行侠 2024-11-10 11:05:32

发生这种情况的原因有很多。以下是一些常见原因。您也可以说语法错误,因此会引发此类错误。

  1. 如果FK(外键)表引擎使用MyISAM,PK(主键)表引擎使用InnoDB。 MyISAM 不支持外键约束。因此,您可能需要将链接表转换为 InnoDB。

  2. 整个数据库中的所有外键名称必须是唯一的。如果您已经有同名的外键约束,即使在另一个表上,您也会收到此错误。

  3. 如果相关列没有完全相同的数据类型(例如 INT)和约束(UNIQUE 等),您将收到该错误。

It can happen because of many reasons. Following are some of the common reasons. You can also say syntactical errors, because of which these kinds of error are thrown.

  1. If the FK (Foreign Key) table Engine is using MyISAM and PK (Primary Key) table Engine is using InnoDB. MyISAM does not support foreign key constraints. So, you might want to converting your linking table to InnoDB.

  2. All foreign key names throughout the database must be unique. If you already have a foreign key constraint with the same name, even on another table, you will receive this error.

  3. If the related columns do not have exactly the same data typetype (e.g. INT) and constraints (UNIQUE and such), you will receive that error.

眼睛会笑 2024-11-10 11:05:32

当使用 MyISAM 存储链接到的表(在您的情况下为 Goods)并且存储您要添加索引的表(在您的情况下为声明)时,我收到此错误使用InnoDB。

您可以从数据库目录中的文件看出这一点。 MyISAM 表将具有如下文件:

table_name.frm
table_name.MYD
table_name.MYI

InnoDB 表将仅具有:

table_name.frm

MyISAM 不支持外键约束。我建议将您的 Goods 表转换为 InnoDB(不过,请先查看文档并执行一些基础研究):

ALTER TABLE Goods ENGINE=INNODB;

进行此更改后,我的 ADD INDEX 操作成功完成。

I'm getting this error when the table being linked to (in your case, Goods) is stored using MyISAM, and the table you're adding the index to (in your case, Declarations) is stored using InnoDB.

You can tell this from the files in the database directory. MyISAM tables will have files like:

table_name.frm
table_name.MYD
table_name.MYI

The InnoDB table will just have:

table_name.frm

MyISAM does not support foreign key constraints. I would suggest converting your Goods table to InnoDB (though, have a look at the documentation first and do some basic research):

ALTER TABLE Goods ENGINE=INNODB;

After making this change, my ADD INDEX operation completed successfully.

音盲 2024-11-10 11:05:32

就像其他人所说,首先确保两列的类型相同并且数据库支持它。之后,确保保存其他表键的列有效。
我遇到了一个问题,我将约束添加到其中包含数据的现有列,并且该数据与另一个表中的任何主键都不匹配,因此尝试创建关系将失败。修复它涉及更新所有列,以确保我的列数据与我试图设置的约束相匹配。

Like the others have said, first make sure the types of the two columns are the same and the database supports it. After that, make sure that the columns that hold the keys to the other tables are valid.
I had a problem where I was adding the constraint to an existing column with data in it, and that data didn't match any of the primary keys in the other table so the attempt to create the relationship would fail. Fixing it involved updating all the columns to make sure my column data matched up with the constraint I was trying to make.

翻了热茶 2024-11-10 11:05:32

我发现,当尝试在 phpMyAdmin 中执行此操作时,名称中带有连字符的表将只允许一个 FK,然后会出现错误。我不知道为什么,但这很容易解决,我只是重新制作了

 CREATE TABLE `something_new` LIKE `something-old`;
 DROP TABLE `something-old`;

YMMV。

I discovered that when trying to do this in phpMyAdmin that tables that had a hyphen in the name would only allow one FK and then give errors. I have no idea why but it was easy enough to work around I simply remade the

 CREATE TABLE `something_new` LIKE `something-old`;
 DROP TABLE `something-old`;

YMMV.

满天都是小星星 2024-11-10 11:05:32

Goods.goodsIdDeclarations.goodsId 的类型定义必须相同,否则您将得到 errno: 150

确保它们的数据类型相同,在 Declarations 表中看起来是 goodsId INT(11) NOT NULLGoodsCREATE TABLE 语句是什么?

The type definitions of Goods.goodsId and Declarations.goodsId must be identical, or you will get the errno: 150.

Make sure they are both the same data type, which looks to be goodsId INT(11) NOT NULL in the Declarations table. What is the CREATE TABLE statement for Goods?

尐籹人 2024-11-10 11:05:32

我也有同样的问题。子表中似乎有一些父表中不存在的数据。您可以执行外连接来查看差异,并且可以为不匹配的行分配有效的 id 或将其删除:

DELETE FROM books
WHERE NOT EXISTS (
    SELECT * FROM users
    WHERE books.user_id = users.id
)

I had the same problem. It seems that there was some data in the child table that was not present in the parent table. You can do an outer join to see the differences and you can assign a valid id for non-matching rows or delete them:

DELETE FROM books
WHERE NOT EXISTS (
    SELECT * FROM users
    WHERE books.user_id = users.id
)
病毒体 2024-11-10 11:05:32

Errno 150 有很多原因。如果您有超级权限,您应该尝试使用

SHOW ENGINE INNODB STATUS

,这会告诉您原因是什么。如果您没有超级权限,则只需检查所有可能的原因即可。您可以在此处找到如何使用 SHOW INNODB STATUS 以及所有原因的列表:

MySQL 外键错误和 Errno 150

Errno 150 has a lot of causes. If you have SUPER privileges, you should try using

SHOW ENGINE INNODB STATUS

and that will tell you what the cause was. If you don't have SUPER privileges, you need to just go through all the possible causes. You can find how to use the SHOW INNODB STATUS and a list of all the causes here:

MySQL Foreign Key Errors and Errno 150

北风几吹夏 2024-11-10 11:05:32

当我收到该错误时,是因为我试图更新已包含数据的表,但数据不符合 FK 限制

When I got that error it was becuase I was trying to update a table that already had data int it and the data didn't meet the FK restrictions.

妄司 2024-11-10 11:05:32

第四个可能的问题(对于 abhijitcaps 提出的三个问题)是您没有将引用的列设为主键。

A fourth possible problem (to the three proposed by abhijitcaps) is that you didn't make the column you are referencing to a primary key.

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