1

我相信这与我的外键有关,但我真的不明白出了什么问题。这是我得到的错误:

第 1 行的错误 1005 (HY000):无法创建表 'cse156.Accounts' (errno: 150)

 CREATE TABLE `AccountType`(
   `AccountType` varchar(255)NOT NULL,
   `Label` varchar(255) NOT NULL,
   `BaseAPR` float(10) NOT NULL DEFAULT'0.0',
   `BaseFee` float(10) NOT NULL DEFAULT '0.0',
   PRIMARY KEY (`AccountType`)
 );

CREATE TABLE `Accounts`(
   `AccountID` int(10) NOT NULL DEFAULT '0',
   `AccountType` varchar(255) NOT NULL,
   `Balance` float(10) Default '0',
   `DateofCreation` varchar(255),
   `AprAdjustment` float(10) NOT NULL DEFAULT'0.0',
   `FeeAdjustment` float(10) NOT NULL DEFAULT'0.0',
   PRIMARY KEY (`AccountID`),
   FOREIGN KEY (`AccountType`) REFERENCES AccountTypes(`AccountType`)
 );

 INSERT INTO `Accounts` VALUES (867001,'RIRA08',543.23,'2008/03/01',0.0,0.0),(530900,'RIRA08',123.00,'2008/04/05',0.125,3.50),(321455,'GCD1009',1232123.12,'2002/01/05',-1.25,0.0),(392108,'RPSA',450.00,'1994/06/09',0.0,15.00),(32948,'RPSA',25.00,'1997/08/03',0.25,2.50),(90490001,'GCD1009',1000000.50,'2005/03/06',1.25,0.50);



 INSERT INTO `AccountType` VALUES('RIRA08','Roth IRA',2.05,10.00),('GCD1009','Golden Years         Certificate of Deposit',5.05,0.00),('MIRA09','Roth IRA',3.2,0.00),('RPSA','Savings    Advantage',1.85,0.00);

 DROP TABLE IF EXISTS `Customers`;

 CREATE TABLE `Customers` (
   `CustomerID` int(11) NOT NULL DEFAULT '0',
   `CustomerFirstName` varchar(255) NOT NULL,
   `CustomerLastName` varchar(255) NOT NULL,
   `Address` varchar(255) NOT NULL,
   `Email` varchar(255) NOT NULL,
   `Flag` varchar(1) NOT NULL,
   PRIMARY KEY (`CustomerID`),
   FOREIGN KEY (`AccountID`) REFERENCES Accounts(`AccountID`)
 );

 INSERT INTO Customers VALUES (829304,'Anthony','Rizzo','123 A Street,Omaha, NE, 68116','rizzo@cubs.com,arizzo@iowacubs.com','S'),(423904,'Starlin','Castro','456 B Ave., Chicago, IL, 67777','scastro@gmail.com','P'),(423431,'Darwin','Barney','7G North,Le City,ON,E5F456','dbar@yahoo.com,barn@mlb.com,db01@unl.edu','S');

 DROP TABLE IF EXISTS `CustomerAccounts`;

 CREATE TABLE `CustomerAccounts` (
   `CustomerID` int(11) NOT NULL DEFAULT '0',
   `AccountID` int(11)  DEFAULT '0',
   FOREIGN KEY (`AccountID`) REFERENCES Accounts(`AccountID`),
   FOREIGN KEY (`CustomerID`) REFERENCES Customers(`CustomerID`)
 );

 INSERT INTO `CustomerAccounts`VALUES(829304,0), (423904,867001),(423904,530900),(423431,90490001),(423431,32948),(423431,392108);
4

1 回答 1

2

Errno 150 是一个FOREIGN KEY错误,通常是由于数据类型不匹配或缺少列。在您的情况下,这是表名中的拼写错误。

您的表名不正确AccountTypes,而不是AccountTypeAccountsFK 定义中:

 FOREIGN KEY (`AccountType`) REFERENCES AccountTypes(`AccountType`)
 -------------------------------------------------^^^  Oops, should be AccountType

再往下一点,该Customers表接下来将失败,因为没有AccountID列:

 CREATE TABLE `Customers` (
   `CustomerID` int(11) NOT NULL DEFAULT '0',
   `CustomerFirstName` varchar(255) NOT NULL,
    /* Add the AccountID column for your FK definition */
   `AccountID` INT(10),
   `CustomerLastName` varchar(255) NOT NULL,
   `Address` varchar(255) NOT NULL,
   `Email` varchar(255) NOT NULL,
   `Flag` varchar(1) NOT NULL,
   PRIMARY KEY (`CustomerID`),
   FOREIGN KEY (`AccountID`) REFERENCES Accounts(`AccountID`)
 );

再进一步,您会在这里遇到问题,因为数据类型INT(11)AccountID匹配Accounts.AccountID,即INT(10)

 CREATE TABLE `CustomerAccounts` (
   `CustomerID` int(11) NOT NULL DEFAULT '0',
   /* Make this INT(10) to match the referenced column */
   `AccountID` int(10)  DEFAULT '0',
   FOREIGN KEY (`AccountID`) REFERENCES Accounts(`AccountID`),
   FOREIGN KEY (`CustomerID`) REFERENCES Customers(`CustomerID`)
 );
于 2012-10-12T01:47:20.853 回答