MySQL FOREIGN KEY 约束报错:Can't create table 'outlook.orders' (errno: 150)


首先在我本机,和一台linux服务器上执行了一下同样的SQL都是 Can't create table 'outlook.orders' (errno: 150) 这个错误

软件版本: 5.5.25a - MySQL Community Serve (本机用了XAMPP,linux服务器是编译版本) 均启用innoDB。

我用

CREATE TABLE Persons (
    Id_P SERIAL PRIMARY KEY NOT NULL,
    Name varchar(255)
)

创建了Persons表
在 "Orders" 表创建时为 "Id_P" 列创建 FOREIGN KEY到Persons的Id_P:

CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (O_Id),
FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
)

在看书的时候遇到了这个问题,原文使用的是postgreSQL,我换成了MySQL。对SQL语句还不慎了解,不知道上面的语法是否有错误?,但执行时未发现syntax报错,只有在使用FOREIGN KEY语句的时候无法通过,报错Can't create table 'outlook.orders' (errno: 150)问题:(

谢谢:)

sql 数据库 mysql

啊叻掉下来了 10 years, 6 months ago
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
Id_P SERIAL,
PRIMARY KEY (O_Id),
FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
);

Id_P的类型必须跟Persons中的一致。

podbots answered 10 years, 6 months ago

Your Answer