我无法在表中创建外键。我该怎么办?

时间:2013-01-16 15:29:40

标签: mysql sql phpmyadmin foreign-keys

我有名为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 

请有人能告诉我该怎么办?请帮助我...... :(

1 个答案:

答案 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_IdAge_Id等。

你应该删除额外的尾随空格,它会起作用。

以下是固定DDL

的链接