2012-05-30 58 views
3

我曾嘗試將一列添加到名爲discountcode的UserOrder表中。這是一個可空的外鍵嘗試在MySQL中添加外鍵約束時出現錯誤1050

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 

這裏的 「秀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' 

這裏是展示創建表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' 
+0

'SHOW ENGINE INNODB STATUS'命令可以解釋這種情況。 – Devart

+0

我已經試過了。 Nothing – benstpierre

回答

1

the manual中所述:

  • InnoDB需要對外鍵和引用鍵進行索引,以便外鍵檢查可以很快並且不需要表掃描。在引用表中,必須有一個索引,其中外鍵列以相同順序排列爲第一列。如果索引表不存在,則會在引用表上自動創建這樣的索引。如果您創建另一個可用於強制實施外鍵約束的索引,則此索引可能會在稍後悄悄丟棄。 index_name,如果給出,如前所述使用。

是否定義上引用的關鍵DiscountCode.code的指數?

+0

DiscountCode.code是一個主鍵...不是已經有索引本身了嗎? – benstpierre

+1

@本州:是的,它會...你可以編輯你的問題,包括'SHOW CREATE TABLE UserOrder'和'SHOW CREATE TABLE DiscountCode'的輸出嗎? – eggyal

相關問題