我在制作 TAG table 时收到错误代码 1822

Im getting an error code 1822 when i making the TAG table

create database priceTag;
use priceTag;

CREATE TABLE `ProductNumber` (
  `Sku` INT auto_increment not null,
  `Model` VARCHAR(100),
  PRIMARY KEY (`Sku`)
);

ALTER TABLE ProductNumber AUTO_INCREMENT=60000;


CREATE TABLE `Manufacture` (
  `Manufacture` VARCHAR(100),
  `Model` VARCHAR(100),
  `Category` VARCHAR(100),
  PRIMARY KEY (`Model`)
);

CREATE TABLE `OpenBox` (
  `Condtion` VARCHAR(100),
  `LP` INT auto_increment not null,
  `MissingItems` VARCHAR(100),
  `Model_` VARCHAR(100),
  FOREIGN KEY (`Model_`) REFERENCES `Manufacture`(`Model`),
  PRIMARY KEY (`LP`)
);

ALTER TABLE OpenBox AUTO_INCREMENT=200000000;

CREATE TABLE `TAG` (
  `SKU*` INT,
  `Model*` VARCHAR(100),
  `PRICE*` DECIMAL(10,2),
  `LP*` INT,
  `condtion*` VARCHAR(100), 
  FOREIGN KEY (`SKU*`) REFERENCES `ProductNumber`(`Sku`),
  FOREIGN KEY (`Model*`) REFERENCES `Manufacture`(`Model`),
  FOREIGN KEY (`LP*`) REFERENCES `OpenBox`(`LP`),
  FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)
);

CREATE TABLE `Inventory` (
  `INV` int,
  `Sku!` int,
  `Model!` VARCHAR(100),
  FOREIGN KEY (`Sku!`) REFERENCES `ProductNumber`(`Sku`),
  FOREIGN KEY (`Model!`) REFERENCES `Manufacture`(`Model`)
);

您在 FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)(即 OpenBox.condtion)中引用的列未编入索引。

添加需要的唯一索引创建然后创建 TAG table.

DEMO

我测试了你的代码,然后 运行 SHOW ENGINE INNODB STATUS 获得了关于错误的更多详细信息。


LATEST FOREIGN KEY ERROR

2021-12-02 09:47:16 0x700007565000 Error in foreign key constraint of table test2/tag: FOREIGN KEY (condtion*) REFERENCES OpenBox(condtion) ):

Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint.

它抱怨这个外键:

FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)

OpenBox.condtion 列不是主键或唯一键。外键必须引用父键 table.

您的 TAGS table 中已经有另一个外键引用 OpenBox table。您是否打算将相应行的 condtion 列复制到 TAGS table?这不是外键的用途。