How to manage associated data in combination

Asked 2 years ago, Updated 2 years ago, 113 views

Suppose you have a table with about 30 columns, and you have a huge number of records every day.
We initially fix 30 columns, but we may increase them to 35.

AAA,BBB,FFF

AAA, CCC, DDD

BBB, CCC, EEE

If you have data like this,

When AAA fills in the input form to find other relevant data in the combination,
BBB, CCC, DDD, FFF
appears as relevant data and

When CCC fills out the input form to find other relevant data in the combination,
AAA, BBB, DDD, and EEE are
Displayed as Related Data


if you want to create a mechanism called Data storage, search thinking,
The RDB's concept of table design is
What is the rule?

Thank you for your cooperation.

sql database

2022-09-30 21:11

1 Answers

You may have mistaken the intent of the question, but
How about expressing the relationship between associative sources (e.g., 'AAA') and associative destinations (e.g., 'BBB', 'CCC', 'DDD', 'FFF') in the relevant table?
Below, the relevant directions are described as unidirectional (even if BBB is present in AAA's relevant data, AAA is not necessarily present in BBB's relevant data).
(In case of bidirectional, a little consideration is required when registering associations (Record registration should be denied only if the key_content_id and value_content_id are reversed.)

ER diagram

content:

+------------+
| id | data |
+----+------+
|  1 | AAA |
|  2 | BBB |
|  3 | CCC |
|  4 | DDD |
|  5 | EEE|
|  6|FFF|
+----+------+

association (part of):

+------------------------------------------------------------------------------
| id | key_content_id | value_content_id |
+----+----------------+------------------+
|  1 |              1 |                2 |
|  2 |              1 |                3 |
|  3 |              1 |                4 |
|  4 |              1 |                6 |
|  5 |              3 |                1 |
|  6 |              3 |                2 |
|  7 |              3 |                4 |
|  8 |              3 |                5 |
+----+----------------+------------------+

If you want to register the following new records with the content:

+------------+
| id | data |
+----+------+
|  7 | GGG|
+----+------+

For example, if 'AAA' associates with 'GGG' and 'BBB' and 'CCC' associates with 'GGG', the data to be newly registered in the association table is as follows:

+------------------------------------------------------------------------------
| id | key_content_id | value_content_id |
+----+----------------+------------------+
|  9 |              1 |                7 |
| 10 |              7 |                3 |
| 11 |              7 |                3 |
+----+----------------+------------------+

If you look at the relationship, you can draw the association from the association source.
SQL:

 select v.data from content vwhere id in(
    select value_content_id from association 
    inner join(
        select id from content where data = 'AAA'
    )k
    on association.key_content_id=k.id
);

k (meaning key), v (meaning value) is the alias in the content table that appears in the FROM clause.In the sentence, it appears twice in a different sense (as an associative source, as an associative destination), so it is named differently.

If you look at what you really want to do in the diagram, you will see the following:

Processing Concepts

If you put this in a sentence, you can get the data you want in the next step.

Let's take a look at each one below.

Get the ID of 'AAA' from the content to get the association registered association

The following parts of the SQL mentioned above are affected:

select id from content where data='AAA'

Since we actually put another name for it, k, think of it as a table like this:

k table

A table with one record and a column called id.

Retrieve the associated ID of 'AAA' from association using the obtained ID

Of the previous SQL, the following parts are affected:

select value_content_id from association 
inner join
on association.key_content_id=k.id

The table with association and k inner join looks like this:

association and k join table

Only records with ID 1 of 'AAA' in key_content_id exist.
In other words, the value_content_id in this table is the associated ID of 'AAA'.

Obtain the entity of associative destination from content using the associative destination ID obtained

select v.data from content vwhere id in (2,3,4,6)

Retrieves the data for the id specified from content.This is the association.


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.