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

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

    As noted, your table have to be InnoDB for FK constraints to be enforced.

    I've only run into the 'Can't create table' in the case where I'm trying to create a foreign key constraint where my local column is a different type from the foreign column.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 02:48

    Just to save other's of the hours of headache I've been thru - as giraffa touches upon, ensure @FOREIGN_KEY_CHECKS is set to 1.

    SELECT @@FOREIGN_KEY_CHECKS

    SET FOREIGN_KEY_CHECKS=1

    0 讨论(0)
  • 2020-12-01 02:52

    I think some of the folks having this problem might be starting out with some of the sample databases provided on the ORACLE website for MYSQL (e.g. sakila DB). Don't forget to "turn the foreign key constraints back on" at the end of your script (e.g. at the beginning of sakila DB script they are turned OFF)

    SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
    SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
    SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
    

    create your tables here

    then don't forget this:

    SET SQL_MODE=@OLD_SQL_MODE;
    SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
    SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
    
    0 讨论(0)
  • 2020-12-01 02:54

    the problem is most likely that questions.p_id and users.p_id are not defined as INT NOT NULL. for foreign keys to work, the definition of the columns on both side of the foreign key must match exactly, with the exception of auto_increment and default.

    0 讨论(0)
  • 2020-12-01 03:00

    I found the following article. I don't have time to test it out, currently, but it may be helpful:

    http://forums.mysql.com/read.php?22,19755,43805

    The author,Edwin Dando, says:

    both tables must be INNODB. The foreign key field must have an index on it. The foeign key field and the field being referenced must be of the same type (I only use integer) and, after hours of pain, they must be UNSIGNED.

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