外键问题:ERROR 1005 (HY000): Can't create table (errno: 150)

Foreign key issue:ERROR 1005 (HY000): Can#39;t create table (errno: 150)(外键问题:ERROR 1005 (HY000): Cant create table (errno: 150))
本文介绍了外键问题:ERROR 1005 (HY000): Can't create table (errno: 150)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:ERROR 1005 (HY000): Can't create table (errno: 150);我知道它与外键有关,但我检查了它们是否具有相同的信息类型等,但我找不到问题.影片、导演演员、角色及成员创作;其余的则没有.

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 have the same info types etc and I can't find the problem. The video, director actor, role and member create; the rest do not.

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;

推荐答案

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

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;

您可以设置on DELETE CASCADE"或允许这些字段为空.

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

这篇关于外键问题:ERROR 1005 (HY000): Can't create table (errno: 150)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)