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 108221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:52:40+00:00 2026-05-11T01:52:40+00:00

I have a SQL table like so: Update: I’m changing the example table as

  • 0

I have a SQL table like so:

Update: I’m changing the example table as the existing hierarchical nature of the original data (State, Cities, Schools) is overshadowing the fact that a simple relationship is needed between the items.

entities id      name                1       Apple      2       Orange             3       Banana              4       Carrot                 5       Mushroom         

I want to define two-way relationships between these entities so a user viewing one entity can see a list of all related entities.

The relationships are defined by an end user.

What is the best way to represent these relationships in the database and subsequently query and update them?

One way as I see it…

My instinct says a relationship table like so:

entity_entity entity_id_a       entity_id_b 1                 2 5                 1 4                 1 5                 4 1                 3 

That being the case, given a supplied entity_id of 4, how would one get all related records, which would be 1 and 5?

Likewise a query of entity_id = 1 should return 2, 3, 4, and 5.

Thanks for your time and let me know if I can clarify the question at all.

  • 1 1 Answer
  • 2 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. 2026-05-11T01:52:40+00:00Added an answer on May 11, 2026 at 1:52 am

    Define a constraint: entity_id_a < entity_id_b.

    Create indexes:

    CREATE UNIQUE INDEX ix_a_b ON entity_entity(entity_id_a, entity_id_b); CREATE INDEX ix_b ON entity_entity(entity_id_b); 

    Second index doesn’t need to include entity_id_a as you will use it only to select all a‘s within one b. RANGE SCAN on ix_b will be faster than a SKIP SCAN on ix_a_b.

    Populate the table with your entities as follows:

    INSERT INTO entity_entity (entity_id_a, entity_id_b) VALUES (LEAST(@id1, @id2), GREATEST(@id1, @id2)) 

    Then select:

    SELECT entity_id_b FROM entity_entity WHERE entity_id_a = @id UNION ALL SELECT entity_id_a FROM entity_entity WHERE entity_id_b = @id 

    UNION ALL here lets you use above indexes and avoid extra sorting for uniqueness.

    All above is valid for a symmetric and anti-reflexive relationship. That means that:

    • If a is related to b, then b is related to a

    • a is never related to a

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

Sidebar

Ask A Question

Stats

  • Questions 155k
  • Answers 155k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you haven't committed changes then you could create a… May 12, 2026 at 10:42 am
  • Editorial Team
    Editorial Team added an answer What you need to do is to load the data… May 12, 2026 at 10:42 am
  • Editorial Team
    Editorial Team added an answer You should reference the codebehind class instead of the ascx… May 12, 2026 at 10:42 am

Related Questions

I have a SQL table like so: Update: I'm changing the example table as
I have data that looks like this: entities id name 1 Apple 2 Orange
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I have a table named visiting that looks like this: id | visitor_id |
I have a SQL Server query that I need to convert to MySQL. I've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.