我有一个奇怪的错误

时间:2012-10-12 01:39:02

标签: sql database

我相信它与我的外键有关,但我真的不明白什么是错的..这是我得到的错误:

第1行的错误1005(HY000):无法创建表'cse156.Accounts'(错误号: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);

1 个答案:

答案 0 :(得分:2)

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

AccountTypes FK定义中的表名AccountType而不是Accounts不正确:

 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)的数据类型AccountIDAccounts.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`)
 );