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

蹲街弑〆低调 提交于 2019-12-02 11:37:37

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 :)

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!