Relationship between Similar Tables

Asked

Viewed 36 times

2

I have a Relational Database structure. I will illustrate more briefly my tables:

Tab_client with fields (ID, Name, ETC...)

Tab_company with the fields (ID, Name, ETC...)

Image tag with fields (ID, Date, ETC...)

I would like to create a relationship 1.N from both Tab_client and Tab_company with Tab_imagem

As far as I know, I have to add a foreign key to the Tab_image, right?

However, I need to create a related FK for both Tab_client and Tab_company, right?

It would look something like:

Tab_image with the fields (ID, Data, ETC..., fk_client, fk_company)

Only that way, every line would have a primary key of the user you entered, but only one of them will do it and not both...

I thought about the possibility of creating something:

Tab_image with the fields (ID, Data, ETC..., fk_users)

This way I understand that each line of Tab_image will save only the primary key of the user who entered it, but I know how I would inform it to the bank, because as far as I know the code would be something like this:

FOREIGN KEY(fk_users) REFERENCES NOME_DA_TABELA (CHAVE_PRIMARIA)

  • 1

    A foreign key uniquely identifies a line of one another table, thus it cannot reference more than one table. Perhaps the use of partitioning can help you (https://dev.mysql.com/doc/refman/8.0/en/partitioning.html) but there is not enough data for a conclusion.

  • I get it, thank you.

2 answers

2

From what I understand, both a client and a company can insert images in the table of images, but only one of them each time and not both at the same time, right?

This is a problem for the foreign key concept, as this concept assumes that there will always be the relationship between one table and the other in which the foreign key of one is equal to the primary key of the other, and this alternation of tables is not allowed.

This strongly suggests that your modeling is wrong and you will need to change it.

But that’s another problem, which involves studying data modeling and applying to your scenario.

  • Exactly @Piovezan, in fact I need to read a little more to redo the modeling of my bank, thank you.

2


Tab_image with the fields (ID, Data, ETC..., fk_client, fk_company)

Only that way, every line would have a primary key from the user that inserted, but only one of them will do this and not both...

That would be the simplest way, the table would have two FK (Foreign Keys), but which should be NULL, because it can have value of one or the other, and still, could have the two filled, this would be possible?

As for the part of "every line would have a primary key from the user who entered it" means that there is also an FK in this table with the correct users table?

Now the next part got confused: "i understand that each row of Tab_image will save only the primary key of the user who entered the same", needs to better explain the user’s relationship with the client and company Tables.

I am "assuming" that, a user inserts an image (table "Tab_image") and informs its relation, with the table "Tab_client" or "Tab_company", and your problem is if you link the image to both tables, you will not have the key of the user who made the two actions, is that it? It would be like this model:

inserir a descrição da imagem aqui

Sorry to ask this in the answer, but it would be bad to do this in the comments, if I’m wrong I edit the question.

But in this case or even for another problem, a solution that would solve these problems would be to make the relationship in new tables, for example:

inserir a descrição da imagem aqui

In this case, the relation between "image x client" and "image x company" are in separate tables, and it is not necessary to modify the table "Tab_imagem".
See that you still have the user in the "Tab_imagem". If you want to know the user who related to the other tables, just add a FK to "Tab_users" in "Tab_image cliente_and "Tab_image". That way, relationships are separated, and avoids having FK with value NULL in the table "Tab_image"

  • Thank you for your reply. "Tab_users" was just an example, it doesn’t exist. The goal is that both the client user and the company user can store data in the Image table. Since both are users, I was in doubt which one would be the FK table Image

  • I really liked your second example, opened a north for me to think about how to solve this, thank you.

  • 1

    do not forget to vote if the answer has helped or solved your problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.