Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:31:12+00:00 2026-05-31T16:31:12+00:00

Im trying to create relationships between four tables in MySql: mainnodes (ID) subnodes (ID)

  • 0

Im trying to create relationships between four tables in 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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:31:13+00:00Added an answer on May 31, 2026 at 4:31 pm

    The problem is your second NODEID CONSTRAINT is overwriting the first.

    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:

    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:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create relationships between different persons but couldn't find a way
I have two tables that I'm trying to create a relationship between so I
I am trying to create a many to many relationship between 2 tables. I've
I'm trying to create a very simple relationship between two objects. Can anybody explain
I'm trying to create a custom delete rule on a relationship between a parent
I am trying to create User, Group and Membership tables as follows. A User
Can I create relationships between my object stores in IndexedDB? For example, I have
I have 2 tables in my database that I'm trying to create Linq2Sql entities
I am trying to create an intermediate model between two models that I'd like
I am trying to create a many-many relationship between the max(id) in table1 with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.