【问题标题】:I couldn't create a Foreign Key in a Table. What should I do?我无法在表中创建外键。我该怎么办?
【发布时间】:2012-12-30 23:29:50
【问题描述】:

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

谁能告诉我该怎么做??请帮我解决这个问题... :(

【问题讨论】:

  • 以后,请将您的代码附在代码示例块中(我已经提交了一个编辑这样做。)它大大提高了您问题的可读性,以便其他用户可以更好地帮助您解决您的问题。

标签: mysql sql phpmyadmin foreign-keys


【解决方案1】:

问题是在定义索引时,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的链接

【讨论】:

  • 非常感谢...我会删除多余的空格,如果遇到任何问题,我会告诉您...:)
猜你喜欢
  • 1970-01-01
  • 2016-06-20
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多