错误代码:1215。无法添加新的外键约束

时间:2016-06-21 22:36:08

标签: mysql database

这是我正在使用的查询:

alter table publication1 add foreign key (Venuekey) references publicationvenue.venueKey

以下是此查询的两个表:

发布表:

CREATE TABLE `publication1` 
(

 `keypub` varchar(200) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,

 `title` varchar(700) DEFAULT NULL,

 `booktitle` varchar(250) DEFAULT NULL,

 `journal` varchar(250) DEFAULT NULL,

 `volume` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

 `month` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

  `year` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

  `cdrom` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `ee` varchar(500) CHARACTER SET latin1 DEFAULT NULL,

  `publisher` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `pages` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

  `number` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

  `url` varchar(500) CHARACTER SET latin1 DEFAULT NULL,

  `cite` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `address` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `note` varchar(500) CHARACTER SET latin1 DEFAULT NULL,

  `isbn` varchar(50) CHARACTER SET latin1 DEFAULT NULL,

  `series` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `school` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `chapter` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `crossref` varchar(250) CHARACTER SET latin1 DEFAULT NULL,

  `type` varchar(30) CHARACTER SET latin1 DEFAULT NULL,

  `venueClean` varchar(250) DEFAULT NULL,

  `venuekey` varchar(200) DEFAULT NULL,

  PRIMARY KEY (`keypub`),

  KEY `venue_index` (`venueClean`)

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='InnoDB free: 49152 kB;  

  InnoDB free: 5424128 kB';

第二个表格是 PublicationVenue

  CREATE TABLE `publicationvenue` (

  `venuekey` varchar(200) NOT NULL,

  `VenueName` varchar(1000) DEFAULT NULL,

  PRIMARY KEY (`venuekey`)

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我已经为表和列设置了相同的默认字符集并完成了所有类似的事情,但仍然无法解决此查询请帮忙吗?

1 个答案:

答案 0 :(得分:1)

外键语法错误,请按以下步骤使用:

 ALTER TABLE publication1 ADD foreign key (Venuekey) 
     references publicationvenue(venueKey);