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

前端 未结 10 1041
被撕碎了的回忆
被撕碎了的回忆 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 02:45

    I know this thread was opened long time ago, but I am posting this message for future users who will look for the answer. I was having the same problem with foreign key in mysql. The following thing worked for me.

    Parent table:

    CREATE TABLE NameSubject (
      Autonumber INT NOT NULL AUTO_INCREMENT,
      NameorSubject nvarchar(255),
      PRIMARY KEY (Autonumber)
     ) ENGINE=InnoDB;
    

    Child Table:

    CREATE TABLE Volumes (
      Autonumber INT NOT NULL,
      Volume INT,
      Pages nvarchar(50),
      Reel int,
      Illustrations bit,
      SSMA_TimeStamp timestamp,
      Foreign KEY (Autonumber) references NameSubject(Autonumber)
      ON  update cascade 
    )engine=innodb;
    

    "ON update cascade" did the magic for me.

    I hope this works for others. Best of luck.

提交回复
热议问题