mysql外键约束格式错误

mbyulnm0  于 2021-06-09  发布在  Mysql
关注(0)|答案(30)|浏览(349)

我有两张table, table1 是具有列的父表 ID 以及 table2 有一列 IDFromTable1 (不是真实的名字)当我放上一个fk的时候 IDFromTable1IDtable1 我得到了错误 Foreign key constraint is incorrectly formed error . 我想删除表2记录,如果 table1 记录被删除。谢谢你的帮助

ALTER TABLE `table2`  
   ADD CONSTRAINT `FK1` 
      FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`) 
      ON UPDATE CASCADE 
      ON DELETE CASCADE;

如果需要其他信息,请告诉我。我是mysql新手

1wnzp6jl

1wnzp6jl1#

这是个老主题,但我发现了一些东西。在构建mysql工作台时,它还获取另一个表的关系。只需留下与你有关的支柱。清除其他自动添加的列。这对我有用。

esyap4oy

esyap4oy2#

检查表引擎,两个表必须是同一个引擎,这对我帮助很大。

ycggw6v2

ycggw6v23#

尝试运行以下命令:

show create table Parent

//and check if type for both tables are the same, like myISAM or innoDB, etc
//Other aspects to check with this error message: the columns used as foreign 
keys must be indexed, they must be of the same type 
(if i.e one is of type smallint(5) and the other of type smallint(6), 
it won't work), and, if they are integers, they should be unsigned.

//or check for charsets
show variables like "character_set_database";
show variables like "collation_database";

//edited: try something like this
ALTER TABLE table2
ADD CONSTRAINT fk_IdTable2
FOREIGN KEY (Table1_Id)
REFERENCES Table1(Table1_Id)
ON UPDATE CASCADE 
ON DELETE CASCADE;
xytpbqjk

xytpbqjk4#

我面临这个问题当您将主键放在不同的数据类型中时出现错误,例如:
表1:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_name');
        });

表2:

Schema::create('brands', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('brand_name');
        });

第二个表的id的数据类型必须是增量

nfs0ujit

nfs0ujit5#

当使用创建父表时,我遇到了相同的问题 MyISAM 引擎。这是个愚蠢的错误,我纠正了:

ALTER TABLE parent_table ENGINE=InnoDB;
6tr1vspr

6tr1vspr6#

面对这个问题的人,快跑吧 SHOW ENGINE INNODB STATUS 有关详细信息,请参阅最新的外键错误部分。

rkue9o1l

rkue9o1l7#

定义外键的语法是非常宽容的,但是对于任何一个在这个问题上犯错误的人来说,外键必须是“同一类型”这一事实甚至适用于排序规则,而不仅仅是数据类型、长度和位签名。
不是说你会在你的模型中混合排序规则(你会吗?),而是如果你这样做了,确保你的主键和外键字段在phpmyadmin或HeidiSQL或你使用的任何东西中是相同的排序规则类型。
希望这能节省你四个小时的试验和错误,它花费了我。

2nc8po8w

2nc8po8w8#

我也有同样的问题,但解决了。
只需确保“table1”中的列“id”具有唯一索引!
当然,这两个表中'id'和'idfromtable1'列的类型、长度必须相同。但你已经知道了。

juud5qan

juud5qan9#

确保列是相同的(类型相同),如果引用列不是 primary_key ,确保是 INDEXED .

n8ghc7c1

n8ghc7c110#

我为此输了好几个小时!
一张table上的pk是 utf8 另一方面是 utf8_unicode_ci !

nukf8bse

nukf8bse11#

或者可以使用dbdesigner4,它有一个图形界面来创建数据库并使用fk链接它们。右键单击表并选择“copy table sql create”创建代码。

dfty9e19

dfty9e1912#

甚至我在mysql和liquibase上也遇到了同样的问题。这就是问题所在:要从中引用另一个表的列的表在数据类型或数据类型大小方面是不同的。

Error appears in below scenario:
Scenario 1:
Table A has column id, type=bigint
Table B column referenced_id type varchar(this column gets the value from the id column of Table A.)
Liquibase changeset for table B:

    <changeset id="XXXXXXXXXXX-1" author="xyz">
            <column name="referenced_id"**type="varchar"**>
        </column>
            </changeset>
    <changeSet id="XXXXXXXXXXX-2" author="xyz">
                <addForeignKeyConstraint constraintName="FK_table_A"
                    referencedTableName="A"**baseColumnNames="referenced_id**"
                    referencedColumnNames="id" baseTableName="B" />
    </changeSet>

Table A changeSet:

    <changeSet id="YYYYYYYYYY" author="xyz">
     <column**name="id"****type="bigint"**autoIncrement="${autoIncrement}">
                    <constraints primaryKey="true" nullable="false"/>
                </column>
    </changeSet>

Solution: 
correct the type of table B to bigint because the referenced table has type bigint.

Scenrario 2:
The type might be correct but the size might not.
e.g. :
Table B : referenced column type="varchar 50"
Table A : base column type ="varchar 255"

Solution change the size of referenced column to that of base table's column size.
e0bqpujr

e0bqpujr13#

检查您是否以正确的大小写指定了表名(如果数据库中的表名区分大小写)。在我的情况下,我必须改变

CONSTRAINT `FK_PURCHASE_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON UPDATE CASCADE ON DELETE CASCADE

CONSTRAINT `FK_PURCHASE_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `CUSTOMER` (`id`) ON UPDATE CASCADE ON DELETE CASCADE

注意 customer 更改为 CUSTOMER .

kxkpmulp

kxkpmulp14#

mysql错误文本没有太大帮助,在我的例子中,列有“notnull”约束,所以“on delete set null”是不允许的

wljmcqd8

wljmcqd815#

如果一切都好的话,就加上 ->unsigned(); 结束时 foregin key .
如果不起作用,请检查这两个字段的数据类型。它们一定是一样的。

相关问题