小编典典

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

sql

我收到此错误: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;

阅读 219

收藏
2021-04-15

共1个答案

小编典典

您有FK定义,它们将尝试在非null列上设置null值。因此,不可能进行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上”或允许这些字段为空。

您在其他表上可能有相同的错误。我只是停在发现的第一个错误:)

2021-04-15