2012-10-12 50 views
1

我相信这是与我的外键,但我真的不明白什么是错。这是我的错误:我有一个奇怪的错误

ERROR 1005(HY000)在1号线:无法创建表 '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','[email protected],[email protected]','S'),(423904,'Starlin','Castro','456 B Ave., Chicago, IL, 67777','[email protected]','P'),(423431,'Darwin','Barney','7G North,Le City,ON,E5F456','[email protected],[email protected],[email protected]','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); 
+2

“Accounts”表在哪里?即使它在两个FK中被引用,你也没有表“Accounts”。此外,即使您试图在其上添加FK约束,您在“客户”表中也没有列'AccountID'。 –

+0

@MichaelBerkowski这是我的不好,我忘了复制那些代码,但问题依然存在 –

回答

2

errno的150是FOREIGN KEY错误,通常是由于数据类型不匹配或丢失柱。就你而言,它是表名中的拼写错误。

你必须在Accounts FK定义不正确的表名AccountTypes而不是AccountType

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`) 
); 

稍微还有一点,您会遇到问题,因为的的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`) 
);