MySQL默默地拒绝创建外键约束

时间:2016-11-11 19:23:23

标签: mysql

我在MySQL中创建一个非常简单的数据库(下面的例子)。正确创建表,并正确创建主键约束,并正确创建外键约束的索引,但不创建外键约束。没有 - 重复 - 没有与DDL执行相关的错误消息。我已经盯着这么久,我再也看不到了。

我已经验证过的事情:

  1. MySQL v5.1.73
  2. 我对数据库有*权限
  3. 默认引擎是MyISAM
  4. 创建主键约束。
  5. 创建外键索引。
  6. 有人可以看到错误,或建议前进的方向吗?

    SET FOREIGN_KEY_CHECKS=0;
    
    DROP TABLE IF EXISTS Sample CASCADE;
    DROP TABLE IF EXISTS LocationCode CASCADE;
    DROP TABLE IF EXISTS SampleTypeCode CASCADE;
    
    CREATE TABLE Sample (
        sample VARCHAR(50) NOT NULL,
        location VARCHAR(50),
        sampleType VARCHAR(50),
        PRIMARY KEY (sample)
    ) ENGINE = MyISAM DEFAULT CHARACTER SET = latin1;
    
    
    CREATE TABLE LocationCode (
        code VARCHAR(50) NOT NULL,
        text VARCHAR(50),
        sequence INTEGER,
        PRIMARY KEY (code)
    ) ENGINE = MyISAM DEFAULT CHARACTER SET = latin1;
    
    
    CREATE TABLE SampleTypeCode (
        code VARCHAR(50) NOT NULL,
        text TEXT,
        sequence INTEGER,
        PRIMARY KEY (code)
    ) ENGINE = MyISAM DEFAULT CHARACTER SET = latin1;
    
    SET FOREIGN_KEY_CHECKS=1;
    
    ALTER TABLE Sample ADD CONSTRAINT
        FOREIGN KEY (location) REFERENCES LocationCode (code);
    
    ALTER TABLE Sample ADD CONSTRAINT
        FOREIGN KEY (sampleType) REFERENCES SampleTypeCode (code);
    

    当我从MySQL获得DDL时,这就是它产生的,所以它让我相信它根本没有创建约束。

    ALTER TABLE Sample DROP PRIMARY KEY;
    ALTER TABLE Sample DROP INDEX location;
    ALTER TABLE Sample DROP INDEX sampleType;
    
    DROP TABLE Sample;
    
    CREATE TABLE Sample (
      sample VARCHAR(50) NOT NULL,
      location VARCHAR(50),
      sampleType VARCHAR(50),
      PRIMARY KEY (sample)
    ) ENGINE=MyISAM;
    
    CREATE INDEX location ON Sample (location ASC);
    CREATE INDEX sampleType ON Sample (sampleType ASC);
    ALTER TABLE Sample ADD PRIMARY KEY (sample);
    

0 个答案:

没有答案