Two n x m relationships with the same table in mysql
- by Christian
I want to create a database in which there's an n x m relationship between the table drug and the table article and an n x m relationship between the table target and the table article.
I get the error: Cannot delete or update a parent row: a foreign key constraint fails
What do I have to change in my code?
DROP TABLE IF EXISTS `textmine`.`article`;
CREATE TABLE  `textmine`.`article` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Pubmed ID',
  `abstract` blob NOT NULL,
  `authors` blob NOT NULL,
  `journal` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `textmine`.`drugs`;
CREATE TABLE  `textmine`.`drugs` (
  `id` int(10) unsigned NOT NULL COMMENT 'This ID is taken from the biosemantics dictionary',
  `primaryName` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `textmine`.`targets`;
CREATE TABLE  `textmine`.`targets` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `primaryName` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `textmine`.`containstarget`;
CREATE TABLE  `textmine`.`containstarget` (
  `targetid` int(10) unsigned NOT NULL,
  `articleid` int(10) unsigned NOT NULL,
  KEY `target` (`targetid`),
  KEY `article` (`articleid`),
  CONSTRAINT `article` FOREIGN KEY (`articleid`) REFERENCES `article` (`id`),
  CONSTRAINT `target` FOREIGN KEY (`targetid`) REFERENCES `targets` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `textmine`.`contiansdrug`;
CREATE TABLE  `textmine`.`contiansdrug` (
  `drugid` int(10) unsigned NOT NULL,
  `articleid` int(10) unsigned NOT NULL,
  KEY `drug` (`drugid`),
  KEY `article` (`articleid`),
  CONSTRAINT `article` FOREIGN KEY (`articleid`) REFERENCES `article` (`id`),
  CONSTRAINT `drug` FOREIGN KEY (`drugid`) REFERENCES `drugs` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;