简体   繁体   English

在MySql表之间创建关系

[英]Creating Relationships between MySql tables

Im trying to create relationships between four tables in MySql: 我试图在MySql的四个表之间创建关系:

mainnodes (ID)
subnodes (ID)
tagrelationship (NODEID & TAGID)
tag (ID)

The table 'tagrelationship' references 'mainnodes' so when I try an insert, I can choose records from the 'mainnodes' table, However I want to be able to choose from the 'subnodes' table aswell. 表“ tagrelationship”引用了“ mainnodes”,因此当我尝试插入时,我可以从“ mainnodes”表中选择记录,但是我也希望能够从“ subnodes”表中进行选择。

I have tried setting up the table structure for 'tagrelationship' like so: 我试图像这样为“标签关系”设置表结构:

CREATE  TABLE IF NOT EXISTS `database`.`tagrelationship` (
`NODEID` INT(11) NOT NULL ,
 `TAGID` INT(11) NOT NULL ,
PRIMARY KEY (`TAGID`, `NODEID`) ,
INDEX `TAGS_TAGRELATIONSHIP` (`TAGID` ASC) ,
INDEX `SUB_TAGRELATIONSHIP` (`NODEID` ASC) ,
CONSTRAINT `TAGS_AGRELATIONSHIP`
  FOREIGN KEY (`TAGID` )
  REFERENCES `database`.`tags` (`ID` )
  ON DELETE CASCADE,
CONSTRAINT `MAINNODES_CMSTAGRELATIONSHIP`
  FOREIGN KEY (`NODEID` )
  REFERENCES `database`.`mainnodes` (`ID` )
  ON DELETE CASCADE,
CONSTRAINT `SUBNODES_CMSTAGRELATIONSHIP`
  FOREIGN KEY (`NODEID` )
  REFERENCES `database`.`subnodes` (`ID` )
  ON DELETE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

This executes fine but I am only able to select records from the 'subnodes' table not both. 这执行得很好,但是我只能从“子节点”表中选择记录,而不能同时选择两者。

How am I able to achieve this? 我怎样才能做到这一点?

Thanks 谢谢

The problem is your second NODEID CONSTRAINT is overwriting the first. 问题是您的第二个NODEID CONSTRAINT正在覆盖第一个。

This is a polymorphic relationship you are looking to create, so one possible solution that still takes advantage of the database's foreign key constraints is to use a polymorphic "supertable" for both mainnodes and subnodes , called something like nodes : 这是您要创建的多态关系,因此仍然可以利用数据库的外键约束的一种可能的解决方案是对mainnodes subnodessubnodes都使用多态“超表”,称为nodes

CREATE  TABLE IF NOT EXISTS `database`.`nodes` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`))

Then have each of your "subtables" reference this with a foreign key constraint: 然后让您的每个“子表”都使用外键约束对此进行引用:

CREATE  TABLE IF NOT EXISTS `database`.`mainnodes` (
...
`NODEID` INT(11) NOT NULL,
CONSTRAINT `MAINNODE_NODE_RELATIONSHIP`
  FOREIGN KEY (`NODEID` )
  REFERENCES `database`.`nodes` (`ID` )
  ON DELETE CASCADE,
...)

CREATE  TABLE IF NOT EXISTS `database`.`subnodes` (
...
`NODEID` INT(11) NOT NULL,
CONSTRAINT `SUBNODE_NODE_RELATIONSHIP`
  FOREIGN KEY (`NODEID` )
  REFERENCES `database`.`nodes` (`ID` )
  ON DELETE CASCADE,
...)

Finally, your tagrelationship table can just reference the supertable, nodes : 最后,您的tagrelationship表可以仅引用tagrelationshipnodes

CREATE  TABLE IF NOT EXISTS `database`.`tagrelationship` (
...
CONSTRAINT `TAGS_AGRELATIONSHIP`
  FOREIGN KEY (`TAGID` )
  REFERENCES `database`.`tags` (`ID` )
  ON DELETE CASCADE,
CONSTRAINT `NODES_CMSTAGRELATIONSHIP`
  FOREIGN KEY (`NODEID` )
  REFERENCES `database`.`nodes` (`ID` )
  ON DELETE CASCADE,
...)

A simple but less robust solution is to simply remove the last two constraints about what NODEID can reference, and use your app code to enforce the constraint. 一个简单但不那么可靠的解决方案是简单地删除关于NODEID可以引用的内容的最后两个约束,并使用您的应用程序代码来实施约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM