我有两个表,table1是一个列ID的父表,table2是一个列IDFromTable1(不是实际的名称),当我把一个FK IDFromTable1到ID在table1,我得到的错误外键约束是不正确形成的错误。我想删除表2记录,如果表1记录被删除。谢谢你的帮助

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

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


当前回答

谢谢S Doerin:

“只是为了完成。 如果你有一个VARCHAR(..)外键,并且被引用的表的字符集与引用它的表的字符集不同,也可能会出现这个错误。 例如,Latin1表中的VARCHAR(50)与UTF8表中的VARCHAR(50)不同。

我解决了这个问题,改变了表格的字符类型。 创建的是latin1,正确的是utf8。

添加下一行。 默认字符集= utf8;

其他回答

试着运行以下命令:

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;

我在使用Alter table在两个表之间添加外键时遇到了问题,帮助我的是确保我试图添加外键关系的每一列都被索引。在PHP myAdmin中这样做: 转到表并单击结构选项卡。 点击索引选项可以索引所需的列,如下图所示:

一旦我索引了我试图用外键引用的两列,我就能够成功地使用alter表并创建外键关系。你会看到列的索引如下面的截图:

注意zip_code是如何在两个表中显示的。

甚至我在mysql和libase上也遇到了同样的问题。 这就是问题所在: 要引用其他表的列的表在数据类型或数据类型大小方面都不同。

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.

对于任何面临这个问题的人,跑吧 显示引擎innodb状态 详细信息请参见LATEST外键错误部分。

虽然其他的答案都很有帮助,但我也想分享一下我的经验。

当我删除了一个表,它的id已经在其他表(带数据)中被引用为外键时,我遇到了这个问题,并试图用一些额外的列重新创建/导入表。

用于重新创建的查询(在phpMyAdmin中生成)如下所示:

CREATE TABLE `the_table` (
  `id` int(11) NOT NULL,            /* No PRIMARY KEY index */  
  `name` varchar(255) NOT NULL,
  `name_fa` varchar(255) NOT NULL,
  `name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

... /* SOME DATA DUMP OPERATION */

ALTER TABLE `the_table`
  ADD PRIMARY KEY (`id`), /* PRIMARY KEY INDEX */
  ADD UNIQUE KEY `uk_acu_donor_name` (`name`);

正如您可能注意到的,PRIMARY KEY索引是在创建(和插入数据)之后设置的,这导致了问题。

解决方案

解决方案是在表定义查询中为被引用为外键的id添加PRIMARY KEY索引,同时从设置索引的ALTER table部分中删除它:

CREATE TABLE `the_table` (
  `id` int(11) NOT NULL PRIMARY KEY,            /* <<== PRIMARY KEY INDEX ON CREATION */  
  `name` varchar(255) NOT NULL,
  `name_fa` varchar(255) NOT NULL,
  `name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;