错误代码:1215:无法添加外键约束

时间:2016-03-22 04:28:48

标签: mysql sql constraints

所以,我在MySQL中创建了以下表格。

    CREATE TABLE `StockTransaction` (
    `TransactionID` int(11),
    `Fee` decimal(6,2),
    `DateTime` datetime DEFAULT NULL,
    `PricePerShare` decimal(6,2),
    PRIMARY KEY (`TransactionID`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# Dump of table StockOrder
# ------------------------------------------------------------

DROP TABLE IF EXISTS `StockOrder`;

CREATE TABLE `StockOrder` (
  `OrderID` int(11) NOT NULL,
  `OrderStockSymbol` char(6) NOT NULL,
  `OrderType` enum('buy','sell') DEFAULT NULL,
  `NumOfShares` int(11) DEFAULT NULL,
  `OrderCustomerAccNum` int(11) unsigned NOT NULL,
  `DateOfOrder` datetime DEFAULT NULL,
  `PriceType` enum('Market','MarketOnClose','TrailingStop','HiddenStop') NOT NULL,
  `OrderEmployeeID` int(11) unsigned DEFAULT NULL,
  `OrderCustomerID` int(11) unsigned NOT NULL,
  `OrderTransactionID` int(11) unsigned,
  `Type` char(30) NOT NULL DEFAULT 'Default',
  PRIMARY KEY (`OrderID`,`Type`),
  CONSTRAINT `stockorder_ibfk_1` FOREIGN KEY (`OrderStockSymbol`) REFERENCES `Stock` (`StockSymbol`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_2` FOREIGN KEY (`OrderCustomerAccNum`) REFERENCES `Account` (`AccountNumber`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_3` FOREIGN KEY (`OrderCustomerID`) REFERENCES `Customer` (`SSN`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_4` FOREIGN KEY (`OrderEmployeeID`) REFERENCES `Employee` (`EmployeeSSN`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_5` FOREIGN KEY (`OrderTransactionID`) REFERENCES `StockTransaction` (`TransactionID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

无论出于何种原因,如果我注释掉约束的创建' stockorder_ibfk_5',这段代码有效,但对于我的生活,我无法弄清楚原因。到底发生了什么,以及为什么我无法创建StockTransaction的外键。TransactionID

2 个答案:

答案 0 :(得分:1)

类型不匹配。 StockOrder.OrderTransactionID 未签名StockTransaction.TransactionID 已签名。让他们签名或签名,你应该闪亮。

答案 1 :(得分:1)

在哪个字段上创建外键和引用的字段模式应该完全相同,其中一个是有符号的(默认),另一个是无符号的。所以你的架构应该如下 -

CREATE TABLE `StockTransaction` (
    `TransactionID` int(11) unsigned,
    `Fee` decimal(6,2),
    `DateTime` datetime DEFAULT NULL,
    `PricePerShare` decimal(6,2),
    PRIMARY KEY (`TransactionID`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# Dump of table StockOrder
# ------------------------------------------------------------

DROP TABLE IF EXISTS `StockOrder`;

CREATE TABLE `StockOrder` (
  `OrderID` int(11) NOT NULL,
  `OrderStockSymbol` char(6) NOT NULL,
  `OrderType` enum('buy','sell') DEFAULT NULL,
  `NumOfShares` int(11) DEFAULT NULL,
  `OrderCustomerAccNum` int(11) unsigned NOT NULL,
  `DateOfOrder` datetime DEFAULT NULL,
  `PriceType` enum('Market','MarketOnClose','TrailingStop','HiddenStop') NOT NULL,
  `OrderEmployeeID` int(11) unsigned DEFAULT NULL,
  `OrderCustomerID` int(11) unsigned NOT NULL,
  `OrderTransactionID` int(11) unsigned,
  `Type` char(30) NOT NULL DEFAULT 'Default',
  PRIMARY KEY (`OrderID`,`Type`),
  CONSTRAINT `stockorder_ibfk_1` FOREIGN KEY (`OrderStockSymbol`) REFERENCES `Stock` (`StockSymbol`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_2` FOREIGN KEY (`OrderCustomerAccNum`) REFERENCES `Account` (`AccountNumber`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_3` FOREIGN KEY (`OrderCustomerID`) REFERENCES `Customer` (`SSN`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_4` FOREIGN KEY (`OrderEmployeeID`) REFERENCES `Employee` (`EmployeeSSN`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `stockorder_ibfk_5` FOREIGN KEY (`OrderTransactionID`) REFERENCES `StockTransaction` (`TransactionID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

注意:此外我认为您希望将两个表中的主键保持为auto_increment,但这是根据您的要求而不是必需的。