Foreign key issue:ERROR 1005 (HY000): Can't create table (errno: 150)

后端 未结 2 1104
难免孤独
难免孤独 2021-01-26 10:49

I\'m getting this error: ERROR 1005 (HY000): Can\'t create table (errno: 150); I know it has something to do with the foreign keys but I have checked to see if they

相关标签:
2条回答
  • 2021-01-26 10:58

    You have FK definitions that will try to set a null value on non null columns. Therefore that FK declaration is impossible.

    CREATE TABLE IF NOT EXISTS registration (
      branchNo VARCHAR (15) NOT NULL,
      memberNo VARCHAR (15) **NOT NULL**,
      StaffNo VARCHAR (15) **NOT NULL**,
      dateJoined date NOT NULL,
      PRIMARY KEY (branchNo, memberNo), 
    FOREIGN KEY (memberNo) REFERENCES member (memberNo) ON DELETE **SET NULL** ON UPDATE 
    CASCADE, 
    FOREIGN KEY (StaffNo) REFERENCES staff (StaffNo) ON DELETE **SET NULL** ON UPDATE 
    CASCADE
    ) ENGINE=InnoDB;
    

    you can either set "on DELETE CASCADE" or allow those fields to be null.

    You might have the same error on other tables. I just stopped at the first error I found :)

    0 讨论(0)
  • 2021-01-26 10:59

    Solved:

    Check to make sure Primary_Key and Foreign_Key are exact match with data types.
    
    If one is signed another one unsigned, it will be failed.
    Good practice is to make sure both are unsigned INT.
    
    0 讨论(0)
提交回复
热议问题