2012-10-12 97 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`) 
);