2013-01-16 55 views
3

我有名爲add_diseasespecificity_of_gender的表格。我想將specificity_of_gender表的主鍵設置爲add_disease表中的外鍵。我的代碼如下圖所示:我無法在表格中創建外鍵。我該怎麼辦?

-- 
-- Table structure for table `add_disease` 
-- 
CREATE TABLE IF NOT EXISTS `add_disease` (
    `Disease_Id` int(100) NOT NULL AUTO_INCREMENT, 
    `Disease_Name` varchar(100) NOT NULL, 
    `Gender_Id` int(100) NOT NULL, 
    `Age_Id` int(100) NOT NULL, 
    `Notion_Id` int(100) NOT NULL, 
    `Type_Id` int(100) NOT NULL, 
    `Stage_Id` int(100) NOT NULL, 
    `Scope_Id` int(100) NOT NULL, 
    `Symptoms` text NOT NULL, 
    `Description` text NOT NULL, 
    `Image` int(100) NOT NULL, 
    PRIMARY KEY (`Disease_Id`), 
KEY ` Gender_Id ` (`Gender_Id `), 
KEY ` Age_Id ` (`Age_Id `), 
KEY ` Notion_Id ` (`Notion_Id `), 
KEY ` Type_Id ` (`Type_Id `), 
KEY ` Stage_Id ` (`Stage_Id `), 
KEY ` Scope_Id ` (`Scope_Id `) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 
-- 
-- Dumping data for table `add_disease` 
--** 

而且

-- 
-- Table structure for table `specificity_of_gender` 
-- 

CREATE TABLE IF NOT EXISTS `specificity_of_gender` (
    `Gender_Id` int(100) NOT NULL AUTO_INCREMENT, 
    `Gender_Name` varchar(100) NOT NULL, 
    PRIMARY KEY (`Gender_Id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 

-- 
-- Dumping data for table `specificity_of_gender` 
-- 

INSERT INTO `specificity_of_gender` (`Gender_Id`, `Gender_Name`) VALUES 
(**1, 'Male'), 
(2, 'Female'), 
(3, 'Others'); 

和MySQL說

Error 

SQL query: 
-- 
-- Database: `online_medical_service` 
-- 
-- -------------------------------------------------------- 
-- 
-- Table structure for table `add_disease` 
-- 
CREATE TABLE IF NOT EXISTS `add_disease` (
`Disease_Id` int(100) NOT NULL AUTO_INCREMENT , 
`Disease_Name` varchar(100) NOT NULL , 
`Gender_Id` int(100) NOT NULL , 
`Age_Id` int(100) NOT NULL , 
`Notion_Id` int(100) NOT NULL , 
`Type_Id` int(100) NOT NULL , 
`Stage_Id` int(100) NOT NULL , 
`Scope_Id` int(100) NOT NULL , 
`Symptoms` text NOT NULL , 
`Description` text NOT NULL , 
`Image` int(100) NOT NULL , 
PRIMARY KEY (`Disease_Id`) , 
KEY ` Gender_Id ` (`Gender_Id `) , 
KEY ` Age_Id ` (`Age_Id `) , 
KEY ` Notion_Id ` (`Notion_Id `) , 
KEY ` Type_Id ` (`Type_Id `) , 
KEY ` Stage_Id ` (`Stage_Id `) , 
KEY ` Scope_Id ` (`Scope_Id `) 
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =1; 

MySQL表示:

#1072 - Key column 'Gender_Id ' doesn't exist in table 

請誰能告訴我該怎麼辦? ?請幫我這個... :(

+0

鏈接將來,請附上您的代碼示例代碼塊(我提交了一個編輯已經這樣做了)。這極大地增強您的問題的可讀性,以便其他用戶可以更好地幫助您解決您的問題。 –

回答

3

的問題是,你對column nameindex name多餘的空格,當你定義一個索引,

PRIMARY KEY (`Disease_Id`), 
KEY `bGender_Idb` (`Gender_Id `), 
KEY ` Age_Id ` (`Age_Id `), 
KEY ` Notion_Id ` (`Notion_Id `), 
KEY ` Type_Id ` (`Type_Id `), 
KEY ` Stage_Id ` (`Stage_Id `), 
KEY ` Scope_Id ` (`Scope_Id `) 

所以基本上,

`Gender_Id ` is not equal to `Gender_Id` 
     ^see extra spaces from here 

並且您已將其寫入所有列:Gender_Id,Age_Id等。

你應該刪除多餘的尾部空格,它會起作用。

下面是固定DDL

+0

非常感謝你...我會刪除額外的空間,如果我面臨任何問題,我會讓你知道... :) –

+1

噢再次謝謝你... :) 它的工作原理... :D –

+0

不客氣':D' –

相關問題