MySQL : ERROR 1215 (HY000): Cannot add foreign key constraint

后端 未结 16 1784
误落风尘
误落风尘 2020-11-30 01:37

I have read Database system concepts, 6th edition, Silberschatz. I\'m going to implement the university database system shown in chapter 2 on OS X

相关标签:
16条回答
  • 2020-11-30 02:01

    In my case charset, datatype every thing was correct. After investigation I found that in parent table there was no index on foreign key column. Once added problem got solved.

    0 讨论(0)
  • 2020-11-30 02:02

    Even if this is not directly linked precisely to your situation, it may help further readers to note that you can get exactly the same error output when you type show engine innodb mstatus if you do not respect the order of creating the database tables; meaning you must not add a foreign constraint referencing a table that does not exist yet. The reference table must exist prior to the table which points to it.

    This is also true when the table creation order is respected but not the columns involved in the foreign key constraint.

    0 讨论(0)
  • 2020-11-30 02:04

    I don't meet the problem as you. But I get the same ERROR Message. So I mark it down here for others' convience.

    Check the charset of two table if the column type is char or varchar. I use a charset=gbk, but I create a new table whose default charset=utf8. So the charset is not the same.

    ERROR 1215 (HY000): Cannot add foreign key constraint
    

    To solve it is to use the same charset. For example utf8.

    0 讨论(0)
  • 2020-11-30 02:05
    foreign key (dept_name) references department
    

    This syntax is not valid for MySQL. It should instead be:

    foreign key (dept_name) references department(dept_name)
    

    MySQL requires dept_name to be used twice. Once to define the foreign column, and once to define the primary column.

    13.1.17.2. Using FOREIGN KEY Constraints

    ... [the] essential syntax for a foreign key constraint definition in a CREATE TABLE or ALTER TABLE statement looks like this:

    [CONSTRAINT [symbol]] FOREIGN KEY
        [index_name] (index_col_name, ...)
        REFERENCES tbl_name (index_col_name, ...)
        [ON DELETE reference_option]
        [ON UPDATE reference_option]
    
    reference_option:
        RESTRICT | CASCADE | SET NULL | NO ACTION
    
    0 讨论(0)
  • 2020-11-30 02:07

    Just add 'unsigned' for the FOREIGN constraint

    `FK` int(11) unsigned DEFAULT NULL,
    
    0 讨论(0)
  • 2020-11-30 02:10

    ERROR 1215 (HY000): Cannot add foreign key constraint

    It is also worth noting that you get this error when the type of the column that is a foreign key in another able doesn't explicitly match the column in the correct table.

    For example:

    alter table schoolPersons
             add index FKEF5AB5E532C8FBFA (student_id),
             add constraint FKEF5AB5E532C8FBFA
             foreign key (student_id)
             references student (id);
    ERROR 1215 (HY000): Cannot add foreign key constraint
    

    This was because the student_id field was defined as:

    mysql> desc schoolPersons;
    +--------------------+------------+------+-----+---------+----------------+
    | Field              | Type       | Null | Key | Default | Extra          |
    +--------------------+------------+------+-----+---------+----------------+
    | student_id         | bigint(20) | YES  |     | NULL    |                |
    

    while the id field in the student table was defined as:

    mysql> desc persons;
    +--------------+----------------------+------+-----+-------------------+-----------------+
    | Field        | Type                 | Null | Key | Default           | Extra           |
    +--------------+----------------------+------+-----+-------------------+-----------------+
    | id           | int(10) unsigned     | NO   | PRI | NULL              | auto_increment  |
    

    The bigint(20) (generated from Java long by hibernate) is not compatible with int(10) unsigned (Java int).

    0 讨论(0)
提交回复
热议问题