外键问题:ERROR 1005(HY000):无法创建表(错误号:150)

时间:2013-11-02 15:51:46

标签: mysql sql foreign-keys

我收到此错误:ERROR 1005 (HY000): Can't create table (errno: 150); 我知道它与外键有关但我已经检查过它们是否有相同的信息类型等我找不到问题。视频,导演,角色和成员创造;其余的没有。

CREATE TABLE IF NOT EXISTS actor (
  actorNo VARCHAR(15) NOT NULL,
  actorName VARCHAR(40) NOT NULL,
  PRIMARY KEY (actorNo)
) ENGINE=InnoDB;


CREATE TABLE IF NOT EXISTS staff (
  StaffNo VARCHAR (15) NOT NULL,
  name VARCHAR(40) NOT NULL,
  position VARCHAR(40) ,
  salary FLOAT(5,2) ,
  branchNo VARCHAR(15) NOT NULL,
FOREIGN KEY (branchNo) REFERENCES branch(branchNo),
  UNIQUE KEY (name,branchNo)
) ENGINE=InnoDB;

SHOW TABLES;

CREATE TABLE IF NOT EXISTS branch (
  branchNo VARCHAR(15) NOT NULL,
  Street VARCHAR (40) ,
  City VARCHAR (20) ,
  State VARCHAR (40) ,
  zipCode VARCHAR (6) NOT NULL,
  StaffNo VARCHAR (15) NOT NULL,
  PRIMARY KEY (branchNo),
  UNIQUE KEY (zipCode), FOREIGN KEY (StaffNo) REFERENCES staff (StaffNo) ON DELETE NO ACTION ON UPDATE 
CASCADE) ENGINE=InnoDB; 

CREATE TABLE IF NOT EXISTS director (
  directorNo VARCHAR(15) NOT NULL,
  directorName VARCHAR(40) NOT NULL,
  PRIMARY KEY (directorNo)
) ENGINE=InnoDB;   

CREATE TABLE IF NOT EXISTS member (
  fName VARCHAR(20) ,
  lName VARCHAR(20) ,
  address VARCHAR (100) ,
  memberNo VARCHAR(15) NOT NULL,
  PRIMARY KEY (memberNo),
  UNIQUE KEY `CCfnmLnmAddr` (fName,lName,address)
) ENGINE=InnoDB;  

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;

CREATE TABLE IF NOT EXISTS videoforrent (
  videoNo INT(15) NOT NULL,
  available CHAR(1) NOT NULL,
  catalogNo VARCHAR (15) ,
  branchNo  VARCHAR(15) NOT NULL,
  PRIMARY KEY (videoNo),
  UNIQUE KEY CCAvailBrnchNoCatNo (available, branchNo,catalogNo), FOREIGN KEY (catalogNo) REFERENCES video (catalogNo) ON DELETE CASCADE ON UPDATE 
CASCADE, FOREIGN KEY (branchNo) REFERENCES branch (branchNo) ON DELETE CASCADE ON UPDATE 
CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS video (
  catalogNo VARCHAR(10) ,
  title VARCHAR(100) NOT NULL,
  category VARCHAR(30) ,
  dailyRental FLOAT(4,2) ,
  price FLOAT(5,2) ,
  directorNo VARCHAR(10) NOT NULL,
  PRIMARY KEY (catalogNo),
  UNIQUE KEY `CCTitleDrNo` (title,directorNo)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS rentalagreement (
  rentalNo VARCHAR(15) NOT NULL,
  dateOut DATE NOT NULL,
  dateReturn DATE ,
  memberNo VARCHAR(15) NOT NULL,
  videoNo INT(15) NOT NULL,
  PRIMARY KEY (rentalNo),
  UNIQUE KEY `CCdtOutMembrNoVidNo` (dateOut,memberNo,videoNo),
  FOREIGN KEY (videoNo) REFERENCES videoforrent (videoNo) ON DELETE SET NULL ON UPDATE 
CASCADE, FOREIGN KEY (memberNo) REFERENCES member (memberNo) ON DELETE NO ACTION ON UPDATE 
CASCADE) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS role (
    actorNo VARCHAR(15) NOT NULL,
  catalogNo INT(11) NOT NULL,
  charachter VARCHAR(40),
  PRIMARY KEY (actorNo, catalogNo)
) ENGINE=InnoDB;

2 个答案:

答案 0 :(得分:3)

您有FK定义将尝试在非空列上设置空值。因此,FK声明是不可能的。

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;

您可以设置“在DELETE CASCADE上”或允许这些字段为空。

您可能在其他表上遇到相同的错误。我刚刚发现了第一个错误:)

答案 1 :(得分:2)

解决:

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.