尝试在MySQL中添加外键约束时出现错误1050

时间:2012-05-30 05:59:28

标签: mysql foreign-keys mysql-error-1050

我尝试在名为discountcode的UserOrder表中添加一列。

这是一个可以为空的外键
alter table UserOrder add column discountCode varchar(100) null;
alter table UserOrder add foreign key FK_UserOrder_DiscountCode_code(`discountCode`) references DiscountCode(`code`);

错误发生在第二行。两个表都运行InnoDB。我在使用MySQL 5.5.11。

这是错误日志...

[2012-05-29 23:59:07] [42S01][1050] Table '.\realtorprint_dev_dev\userorder' already exists
[2012-05-29 23:59:07] [HY000][1025] Error on rename of '.\realtorprint_dev_dev\#sql-28a4_3' to '.\realtorprint_dev_dev\userorder' (errno: -1)
[2012-05-29 23:59:07] [42S01][1050] Table '.\realtorprint_dev_dev\userorder' already exists

这是“show create Table UserOrder”

'CREATE TABLE `userorder` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL,
  `paymentTxID` varchar(255) DEFAULT NULL,
  `shippedDate` datetime DEFAULT NULL,
  `shippingTrackingNumber` varchar(255) DEFAULT NULL,
  `taxType` varchar(255) NOT NULL,
  `taxValue` decimal(10,2) NOT NULL,
  `orderStatus` varchar(50) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `address` varchar(255) NOT NULL,
  `city` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `stateProvince` varchar(255) NOT NULL,
  `zipPostal` varchar(255) NOT NULL,
  `paymentType` varchar(255) NOT NULL,
  `backendUserId` bigint(20) DEFAULT NULL,
  `adjustedTotalPrice` decimal(10,2) DEFAULT NULL,
  `adjustedPrinterPrice` decimal(10,2) DEFAULT NULL,
  `adminNotes` varchar(2048) DEFAULT NULL,
  `printerBillStatus` varchar(40) NOT NULL,
  `userInvoiceStatus` varchar(40) NOT NULL,
  `expeditedAddressDescription` varchar(255) DEFAULT NULL,
  `shippingType` varchar(50) NOT NULL,
  `shippingCost` decimal(10,2) NOT NULL,
  `discountCode` varchar(100) DEFAULT NULL,
  `discountAmount` decimal(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_UserOrder_user_id` (`user_id`),
  KEY `idx_UserOrder_orderStatus_id` (`orderStatus`),
  KEY `FK_UserOrder_PaymentType` (`paymentType`),
  KEY `FK_UserOrder_PrinterBillStatus_Name` (`printerBillStatus`),
  KEY `FK_UserOrder_UserInvoiceStatus_Name` (`userInvoiceStatus`),
  KEY `FK_UserOrder_User` (`backendUserId`),
  KEY `FK_UserOrder_ShippingType_name` (`shippingType`),
  CONSTRAINT `FK_UserOrderBcknd_User` FOREIGN KEY (`backendUserId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_UserOrder_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `userorder_ibfk_1` FOREIGN KEY (`shippingType`) REFERENCES `shippingtype` (`name`),
  CONSTRAINT `UserOrder_ibfk_2` FOREIGN KEY (`paymentType`) REFERENCES `paymenttype` (`name`),
  CONSTRAINT `UserOrder_ibfk_6` FOREIGN KEY (`printerBillStatus`) REFERENCES `printerbillstatus` (`name`),
  CONSTRAINT `UserOrder_ibfk_7` FOREIGN KEY (`userInvoiceStatus`) REFERENCES `userinvoicestatus` (`name`),
  CONSTRAINT `UserOrder_ibfk_8` FOREIGN KEY (`orderStatus`) REFERENCES `orderstatus` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=21412 DEFAULT CHARSET=utf8'

这是show create table DiscountCode ...

'CREATE TABLE `discountcode` (
  `code` varchar(100) NOT NULL,
  `percent` int(11) NOT NULL,
  `created` datetime NOT NULL,
  `expires` datetime NOT NULL,
  `maxUses` int(11) NOT NULL,
  `useCount` int(11) NOT NULL,
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8'

1 个答案:

答案 0 :(得分:1)

the manual中所述:

  
      
  • InnoDB需要外键和引用键的索引,以便外键检查速度快,不需要表扫描。在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。如果引用表不存在,则会自动在引用表上创建此索引。如果您创建另一个可用于强制执行外键约束的索引,则可以稍后以静默方式删除此索引。 index_name ,如果给出,将按照前面的说明使用。
  •   

您是否在引用的密钥DiscountCode.code上定义了索引?