2017-01-20 1 views
0

Здравствуйте мои извинения за вопрос помощи, но я имею немного вопроса в базе данных при добавлении ссылкиНужна помощь в создании внешних ключей, ошибке SQL 1215

так я получил таблицу

CREATE TABLE `kooien` (
`kooiid` INT(11) NOT NULL AUTO_INCREMENT, 
`kooidlistppg` INT(11) NULL, 
`quarantaine` BIT(1) NOT NULL, 
`idvogelsoort` INT(11) NULL DEFAULT NULL, 
`idvogelondersoort` INT(11) NULL DEFAULT NULL, 
`vasteoflossekooie` BIT(1) NOT NULL, 
`bezetofniet` BIT(1) NOT NULL, 
`idlistsponsor` INT(11) NULL DEFAULT NULL, 
PRIMARY KEY (`kooiid`), 
INDEX `idvogelsoort` (`idvogelsoort`), 
INDEX `idvogelondersoort` (`idvogelondersoort`), 
INDEX `kooien_ibfk_4_idx` (`idlistsponsor`), 
CONSTRAINT `kooien_ibfk_2` FOREIGN KEY (`idvogelsoort`) REFERENCES `vogelsoort` (`id`), 
CONSTRAINT `kooien_ibfk_3` FOREIGN KEY (`idvogelondersoort`) REFERENCES `ondersoort` (`id`), 
CONSTRAINT `kooien_ibfk_4` FOREIGN KEY (`idlistsponsor`) REFERENCES `personen` (`id`), 
CONSTRAINT `FK_kooien_kooientoppg` FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`)) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB 

, который появляется как

kooiid|kooidlistppg|quarantaine|idvogelsoort|idvogelondersoort|Vasteoflossekooien|bezetofniet|lijstlist sponsor 

Теперь моя проблема kooidlistppg делает ссылки на kooidlistppg (ID) я сделать с этим запросом

ALTER TABLE `kooien` 
DROP INDEX `FK_kooien_kooientoppg`, 
ADD CONSTRAINT `FK_kooien_kooientoppg` FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`); 
/* SQL Fout (1215): Cannot add foreign key constraint */ 

плохо добавить создать запрос для таблицы

CREATE TABLE `kooientoppg` (
`id` INT(11) NOT NULL, 
`idpapegaaien` INT(11) NULL DEFAULT NULL 
) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB 

пожалуйста, знайте, что его отсутствием первичного ключа является преобразование списка в базу данных MySQL

ответ

0

Как объяснен в MySQL manual, подробнее из FOREIGN KEY ошибок с InnoDB можно проверить с помощью следующей команды SQL:

SHOW ENGINE INNODB STATUS 

Например:

------------------------ 
LATEST FOREIGN KEY ERROR 
------------------------ 
2017-01-22 23:43:46 0x7fc9fc0f1700 Error in foreign key constraint of table test/kooien: 
FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`)) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB: 
Cannot find an index in the referenced table where the 
referenced columns appear as the first columns, or column types 
in the table and the referenced table do not match for constraint. 
Note that the internal storage type of ENUM and SET changed in 
tables created with >= InnoDB-4.1.12, and such columns in old tables 
cannot be referenced by such columns in new tables. 
Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html for correct foreign key definition. 

Насколько я могу судить, добавление индекса к соответствующему столбцу поможет:

ALTER TABLE kooientoppg ADD INDEX id_idx (id);