errno: 150 "外键约束格式不正确 - mysql
Posted
技术标签:
【中文标题】errno: 150 "外键约束格式不正确 - mysql【英文标题】:errno: 150 "Foreign key constraint is incorrectly formed - mysql 【发布时间】:2018-09-04 21:46:13 【问题描述】:我正在尝试为学校项目创建数据库。我收到标题中指定的错误,不知道如何解决它。我在 mysql 方面不是很有经验,但显然错误是模糊的,可能是很多事情。当我尝试输入显示表时会发生这种情况。
`CREATE TABLE Complex(
name varchar(50) not null,
streetNum integer,
streetName varchar(50),
city varchar(50),
province varchar(50),
postalCode char(6),
numTheatres integer,
primary key(name)
);
CREATE TABLE Theatre(
complexName varchar(50) not null,
theatreNum integer not null,
maxSeats integer,
screenSize varchar(6),
foreign key (complexName) references Complex(name),
primary key (complexName, theatreNum)
);
CREATE TABLE Supplier(
compName varchar(50) not null,
streetName varchar(50),
streetNum varchar(50),
city varchar(50),
province varchar(50),
postalCode char(6),
phone char(10),
contactFName varchar(10),
contactLName varchar(10),
primary key (compName)
);
CREATE TABLE Movie(
title varchar(100) not null,
runningTime integer,
rating varchar(4),
synopsis varchar(500),
director varchar(100),
prodComp varchar(100),
supplierName varchar(100),
startDate date,
endDate date,
foreign key (supplierName) references Supplier(compName),
primary key (title)
);
Create Table Actor(
fName varchar(50) not null,
lName varchar(50) not null,
primary key (fName,lName)
);
create table Stars
(
fName varchar(100) not null references Actor(fName),
lName varchar(100) not null references Actor(lName),
title varchar(100) not null references Movie(title),
primary key (fName, lName, title)
);
CREATE TABLE Account(
accountNum integer not null AUTO_INCREMENT,
password varchar(50) not null,
fName varchar(50),
lName varchar(50),
phone char(10),
email varchar(50),
creditCard varchar(15),
cardExpiry char(4),
primary key (accountNum)
);
CREATE TABLE Showing(
complexName varchar(50) not null,
title varchar(100) not null,
theatreNum integer not null,
startTime time not null,
seatsAvailable integer,
foreign key (complexName) references Theatre(complexName),
foreign key (title) references Movie(title),
foreign key (theatreNum) references Theatre(theatreNum),
primary key (startTime)
);
CREATE TABLE Review(
title varchar(100) not null,
ID integer not null,
score integer,
primary key (ID),
foreign key (title) references Movie(title)
);
CREATE TABLE Reserved(
accountNum integer not null,
complexName varchar(50) not null,
theatreNum integer not null,
movieTitle varchar(50) not null,
startTime time not null,
ticketsNum integer,
foreign key (accountNum) references Account(accountNum),
foreign key (complexName) references Showing(compName),
foreign key (theatreNum) references Showing(theatreNum),
foreign key (movieTitle) references Showing(movieTitle),
foreign key (startTime) references Showing(startTime),
primary key (accountNum, complexName, theatreNum, movieTitle, startTime)
)
;`
【问题讨论】:
简化您的问题。我们不需要(或想要)所有代码,只需要重现问题的重要部分。 (即删除不需要获取错误的列和表。) MySQL 错误几乎总是表明错误发生在哪里;为什么要从您的问题中省略这些信息? 看我给你同学的答案。 ***.com/a/49483909/1797579 然后告诉他,在 *** 上接受问题的答案是正确的,此时我们可以将您作为副本关闭。 MySQL "Foreign key constraint is incorretly formed"的可能重复 【参考方案1】:这是初学者对外键最常见的误解之一。
CREATE TABLE Theatre(
complexName varchar(50) not null,
theatreNum integer not null,
primary key (complexName, theatreNum)
CREATE TABLE Showing(
complexName varchar(50) not null,
theatreNum integer not null,
foreign key (complexName) references Theatre(complexName),
foreign key (theatreNum) references Theatre(theatreNum),
...
当您引用具有多列主键的表时,您的外键也必须是多列的。不要声明两个外键约束。使用与其引用的主键相同的列声明一个外键约束。
CREATE TABLE Showing(
complexName varchar(50) not null,
theatreNum integer not null,
foreign key (complexName, theatreNum) references Theatre(complexName, theatreNum),
...
有关外键的更多提示,例如需要为真的清单,请参阅此问题的最佳答案:MySQL Creating tables with Foreign Keys giving errno: 150
【讨论】:
以上是关于errno: 150 "外键约束格式不正确 - mysql的主要内容,如果未能解决你的问题,请参考以下文章
laravel 8 (errno: 150 "外键约束格式不正确")
一般错误:1005 无法创建表 errno:150“外键约束格式不正确”)
Laravel 迁移:“外键约束格式不正确”(errno 150)
MySQL + Rails:errno:150“外键约束格式不正确”