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

前端 未结 10 1045
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  Happy的楠姐
    2020-12-01 03:01

    This answer would have saved me a lot of time if I'd seen it first. Try the following three steps, which I've ordered by frequency of newbie mistakes:

    (1) Change the table to be InnodDB by appending "ENGINE=InnoDB" to your "CREATE TABLE" statements.

    Other engines, which may be the default, do not support foreign key constraints, but neither do they throw an error or warning telling you they're not supported.

    (2) Make sure foreign key constraints are in fact being checked by executing "SET foreign_key_checks = 'ON'"

    (3) Append "ON UPDATE CASCADE" to your foreign key declaration.

    Note: Make sure that cascading is the behavior you want. There are other options...

提交回复
热议问题