gpt4 book ai didi

MySQL 5.5 添加外键失败,出现错误 [HY000][150] 和 [HY000][1005]

转载 作者:IT老高 更新时间:2023-10-29 00:13:25 26 4
gpt4 key购买 nike

我试过像这样添加外键...

ALTER TABLE OrderLineItem
ADD CONSTRAINT
FK_OrderLineItem_ShippingType_name FOREIGN KEY
(shippingType)
REFERENCES ShippingType(name);

或者在 Mysql 5.5 中像这样...

alter table OrderLineItem add foreign key 
FK_OrderLineItem_ShippingType (shippingType) references ShippingType(name);

每次我看到以下错误。

[2011-11-18 15:07:04] [HY000][150] Create table 'realtorprint_dev_dev/#sql-7d0_80' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.

[2011-11-18 15:07:04] [HY000][1005] Can't create table 'realtorprint_dev_dev.#sql-7d0_80' (errno: 150)

OrderLineItem.shippingType 和 ShippingType.name 的类型都不是 null 的 varchar(50)。 ShippingType.name是ShippingType的primaryKey。

这是在 ShippingType 和 OrderLineItem 上显示创建表的结果...

CREATE TABLE `shippingtype` (
`name` varchar(50) CHARACTER SET latin1 NOT NULL DEFAULT '',
`description` varchar(255) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `orderlineitem` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`description` varchar(255) CHARACTER SET latin1 NOT NULL,
`lineNumber` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`quantityMultiplier` int(11) NOT NULL,
`unitPrice` decimal(10,2) NOT NULL,
`order_id` bigint(20) NOT NULL,
`productDefinition_id` bigint(20) NOT NULL,
`mlsId` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`printProviderUnitCost` decimal(10,2) NOT NULL,
`shippingType` varchar(50) NOT NULL,
`address` varchar(255) DEFAULT NULL,
`zipPostal` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`stateProvince` varchar(255) NOT NULL,
`country` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_OrderLineItem_productDefinition_id` (`productDefinition_id`),
KEY `idx_OrderLineItem_order_id` (`order_id`),
CONSTRAINT `FK_OrderLineItem_order_id` FOREIGN KEY (`order_id`) REFERENCES `userorder` (`id`),
CONSTRAINT `FK_OrderLineItem_productDefinition_id` FOREIGN KEY (`productDefinition_id`) REFERENCES `productdefinition` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10029 DEFAULT CHARSET=utf8;

最佳答案

当列类型不完全匹配时,Mysql 可能会给出这个错误 - 检查排序规则/大小等。

关于MySQL 5.5 添加外键失败,出现错误 [HY000][150] 和 [HY000][1005],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189779/

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