0

我有一张桌子

1.

CREATE TABLE `Parent` (
`ID` bigint(19) NOT NULL DEFAULT '0',
 PRIMARY KEY (`ID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.

CREATE TABLE `Child_1` (
`ParentID` bigint(19) NOT NULL DEFAULT '0',
`ID` bigint(19) NOT NULL DEFAULT '0',
PRIMARY KEY (`ParentID`,`ID`),
KEY `Child_1_FK1_IDX` (`ParentID`,`ID`),
CONSTRAINT `Child_1_FK1` FOREIGN KEY (`ParentID`, `ID`) REFERENCES `Child_2` (`ParentID`,   `ID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.

CREATE TABLE `Child_2` (
`ParentID` bigint(19) NOT NULL DEFAULT '0',
`ID` bigint(19) NOT NULL DEFAULT '0',
PRIMARY KEY (`ParentID`,`ID`),
KEY `Child_2_FK1_IDX` (`ParentID`),
CONSTRAINT `Child_2_FK1` FOREIGN KEY (`ParentID`) REFERENCES `Parent` (`ParentID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

不幸的是,我删除了第二个表约束。

CONSTRAINT `Child_1_FK1` FOREIGN KEY (`ParentID`, `ID`) REFERENCES `Child_2` (`ParentID`,   `ID`) ON DELETE CASCADE

我怎样才能恢复呢?

4

2 回答 2

0

试试这个(未测试):

ALTER TABLE Child_1
ADD FOREIGN KEY (`ParentID`, `ID`) 
REFERENCES `Child_2` (`ParentID`,   `ID`) 
ON DELETE CASCADE

更新:

要获取违反约束的记录,请运行以下命令:

select child1.ID from 
   Child_1 child1 left join Child_2 child2 on child1.ParentID=child2.ParentID and  child1.ID=child2.ID
where child2.ID is null and child2.ParentID is null;
于 2013-08-20T05:25:48.747 回答
0

最好是转储。冷启动数据库。

于 2013-10-31T02:50:58.203 回答