1

我正在尝试使用以下方法向表 ag 添加外键约束:

alter table ag
add foreign key fk_ag_protein1 (protein_PID) references protein (PID);

但我收到以下错误消息:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`mux_new`.`#sql-884_3`, CONSTRAINT `#sql-884_3_ibfk_1` FOREIGN KEY (`protein_PID`)
REFERENCES `protein` (`PID`))"

为了获得更多详细信息,我检查了以下输出:

SHOW ENGINE INNODB STATUS\G

这是:

LATEST FOREIGN KEY ERROR
------------------------
TRANSACTION 193923, ACTIVE 0 sec inserting, thread declared inside InnoDB 4999
mysql tables in use 2, locked 2
5 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 3, OS thread handle 0x1714, query id 143 localhost ::1 root copy
 to tmp table
alter table ag
add foreign key fk_ag_protein1 (protein_PID) references protein (PID)
Foreign key constraint fails for table `mux_new`.`#sql-884_3`:
,
  CONSTRAINT `#sql-884_3_ibfk_1` FOREIGN KEY (`protein_PID`) REFERENCES `protein
 ` (`PID`)
Trying to add in child table, in index `fk_ag_protein1` tuple:
DATA TUPLE: 2 fields;
 0: len 4; hex 80000000; asc     ;;
 1: len 3; hex 002711; asc  ' ;;

But in parent table `mux_new`.`protein`, in index `PRIMARY`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 696e66696d756d00; asc infimum ;;

但我完全不明白这一点。表 ag 当前包含一些数据,但蛋白质没有。

关于我的问题可能是什么或我可以检查的事情的任何想法?

蛋白质表:

描述蛋白质输出:

+---------------------------+---------+------+-----+---------+-------+
| Field                     | Type    | Null | Key | Default | Extra |
+---------------------------+---------+------+-----+---------+-------+
| PID                       | int(11) | NO   | PRI | NULL    |       |
| uniprot_UniprotAC         | char(6) | YES  | MUL | NULL    |       |
| pubmedhits_id             | int(11) | YES  | MUL | NULL    |       |
| internallyDefinedNames_id | int(11) | YES  | MUL | NULL    |       |
| comment                   | int(11) | YES  | MUL | NULL    |       |
+---------------------------+---------+------+-----+---------+-------+

显示创建表蛋白质输出:

CREATE TABLE `protein` (
  `PID` int(11) NOT NULL,
  `uniprot_UniprotAC` char(6) COLLATE utf8_unicode_ci DEFAULT NULL,
  `pubmedhits_id` int(11) DEFAULT NULL,
  `internallyDefinedNames_id` int(11) DEFAULT NULL,
  `comment` int(11) DEFAULT NULL,
  PRIMARY KEY (`PID`),
  KEY `fk_protein_uniprot1_idx` (`uniprot_UniprotAC`),
  KEY `fk_protein_pubmedhits1_idx` (`pubmedhits_id`),
  KEY `fk_protein_internallyDefinedNames1_idx` (`internallyDefinedNames_id`),
  KEY `fk_protein_comments1_idx` (`comment`),
  CONSTRAINT `protein_ibfk_4` FOREIGN KEY (`uniprot_UniprotAC`) REFERENCES `uniprot` (`UniprotAC`),
  CONSTRAINT `protein_ibfk_1` FOREIGN KEY (`comment`) REFERENCES `comments` (`idcomments`),
  CONSTRAINT `protein_ibfk_2` FOREIGN KEY (`internallyDefinedNames_id`) REFERENCES `internallydefinednames` (`id`),
  CONSTRAINT `protein_ibfk_3` FOREIGN KEY (`pubmedhits_id`) REFERENCES `pubmedhits` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

农业表:

描述 ag:

+-------------+--------------------------------+------+-----+---------+-------+
| Field       | Type                           | Null | Key | Default | Extra |
+-------------+--------------------------------+------+-----+---------+-------+
| Article_AID | mediumint(5) unsigned zerofill | NO   | PRI | NULL    |       |
| Name        | varchar(200)                   | YES  |     | NULL    |       |
| Form        | varchar(150)                   | YES  |     | NULL    |       |
| Mw          | varchar(40)                    | YES  |     | NULL    |       |
| Source      | varchar(260)                   | YES  |     | NULL    |       |
| protein_PID | int(11)                        | YES  |     | NULL    |       |
+-------------+--------------------------------+------+-----+---------+-------+

显示创建表 ag:

`ag` (
  `Article_AID` mediumint(5) unsigned zerofill NOT NULL,
  `Name` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Form` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Mw` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Source` varchar(260) COLLATE utf8_unicode_ci DEFAULT NULL,
  `protein_PID` int(11) DEFAULT NULL,
   PRIMARY KEY (`Article_AID`),
   KEY `fk_ag_Article1_idx` (`Article_AID`),
  CONSTRAINT `fk_ag_Article1` FOREIGN KEY (`Article_AID`) REFERENCES `article` (`AID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
4

2 回答 2

3

我弄清楚了问题所在。在 ag 表中,protein_PID 列是在表中已有数据时添加的,并且在第一次创建时它被设置为不为空。然后所有设置为 0 的行的 protein_PID 作为默认值,由于蛋白质中没有数据,我无法添加外键。

于 2013-10-02T15:08:23.530 回答
0

使 PID 和 protein_PID 都不是 NULL 可以解决你的问题

于 2013-10-02T13:22:40.917 回答