gpt4 book ai didi

MySQL 错误号 : 150 "Foreign key constraint is incorrectly formed"

转载 作者:可可西里 更新时间:2023-11-01 07:24:42 24 4
gpt4 key购买 nike

有谁知道为什么我会收到以下错误消息?

errno: 150 "Foreign key constraint is incorrectly formed"

CREATE TABLE meter (
`code` CHAR(5) NOT NULL,
`type` VARCHAR(30) NOT NULL,
description VARCHAR(30) NULL,
location_code CHAR(3) NOT NULL,
CONSTRAINT pri_meter
PRIMARY KEY (`code`),
CONSTRAINT for_meter
FOREIGN KEY (location_code) REFERENCES location (`code`));

CREATE TABLE location(
`code` CHAR(3) NOT NULL,
company VARCHAR(30),
`type` VARCHAR(30),
CONSTRAINT pri_location
PRIMARY KEY (`code`));

CREATE TABLE reading(
meter_code CHAR(5) NOT NULL,
`when` DATETIME NOT NULL,
display DECIMAL(9,3) NOT NULL,
estimate BIT NOT NULL,
CONSTRAINT pri_reading
PRIMARY KEY (`when`, meter_code),
CONSTRAINT for_reading
FOREIGN KEY (meter_code) REFERENCES meter (`code`));

CREATE INDEX index_meter ON meter (location_code);
CREATE INDEX index_reading ON reading (meter_code);

最佳答案

首先创建location 表。

当被引用的表还不存在时,您不能引用表列。

在您的示例中,您在创建 meter 表时引用了表 location

关于MySQL 错误号 : 150 "Foreign key constraint is incorrectly formed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45556758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com