Foreign key not working in MySQL: Why can I INSERT a value that's not in the foreign column?

前端 未结 10 1039
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 02:38

I\'ve created a table in MySQL:

CREATE TABLE actions ( A_id int NOT NULL AUTO_INCREMENT,
type ENUM(\'rate\',\'report\',\'submit\',\'edit\',\'delete\') NOT NU         


        
10条回答
  •  春和景丽
    2020-12-01 03:03

    For those who still have problems with mysql ignoring foreign keys constraints and for those who the answers above or in any other related question didn't solve teir puzzle, here is what I found to be the issue.

    If you declare your foreign keys as such

    id  INTEGER UNSIGNED    REFERENCES    A_Table(id)
    

    Then the foreign key seems to be ignored, to enforce the constraint without (apparently) having to use any of the SET commands, use the following declaration.

    id  INTEGER UNSIGNED,
    CONSTRAINT fk_id  FOREIGN KEY (id)  REFERENCES A_Table(id)
    

    This way solved the problem for me. Not sure why, as many say the first declaration is only a shorthand to the second variant.

提交回复
热议问题