How to relate tables in MySQL to more than one entity?

2

How can I relate a table (entity) to more than one entity in the MySQL database? following the following example ...

I have the tables: fornecedores , clientes and telefones .

  • fornecedores , may have more than one phone.
  • clientes , can also have more than one phone.

To solve this problem, I created a N:N relationship, where fornecedor can have multiple telefones , but a phone does not belong to several fornecedores . This is equal to clientes , where a client can have multiple telefones , but a phone does not belong to several clientes .

With this relationship N: N, we created an intermediate table between fornecedores and telefones , and also between clientes and telefones .

How could I solve this problem, without having to have a telephone table to relate to fornecedores , and another phone table to relate to clientes ? without being N:N ?

    
asked by anonymous 26.11.2015 / 02:11

3 answers

1

I do not think you're right about the N:N relationship that you described. As far as I know, the relationship between:

  • clients and phones: one-to-many
  • providers and phones: one-to-many
  • phones and customers: many-to-one
  • phones and providers: many-to-one

Also, from the pragmatic point of view, I think that creating a table such as telefones_clientes and telefones_forncedores is not a good idea, since redundancy is at first unnecessary. That said, I think a more sensible and practical approach would be something like:

Ofcourse,thereisnotaforeignkeyexplicitlyandformallydefinedinthephonestable.However,theconceptstillremains,sincethetelefones.quemfieldshouldpointtoaprimarykeyofthesupplierstableorclientstable.

FORNECEDORES|+-------------+|CLIENTES|------------+|TELEFONES|+-------------+id|____+-------------+___|id|------------+\|id|/+-------------+nome|\___+-------------+___/|nome|------------+|quem|+-------------++-------------+|is_cliente|--écliente(1)oufornecedor(2)?--+-------------+|telefone|+-------------+

Withtheaboveschemeyouhavetelefones.quemreferencingaclientes.idorforncedores.id.ThisevenfacilitatesaSQLqueryforonlycustomerorvendorphonesthroughthetelefones.is_clientefield.

Insummaryyouwouldhave:

Pros

  • Flexibility(easytochangeandmanage)
  • Justatablewithphones,lessJOINS,easiertoquery(inthiscase);
  • Cleanandmeaningfullayoutandstructure;

Cons:

  • Thereisnoexplicitrestrictiononrelationalintegrity;
  • ForeignKeyoptions/actionssuchasONDELETECASCADEisnotavailable;
  • Onceyoudeleteacustomerorvendorrecord,youwillhavetomanuallydeleteyourreferencesinthetelefonestableifthisisimportant;

AfamousPython"mantra" ( Zen of Python ) comes in handy here:
  

... practicality beats purity .

    
26.11.2015 / 02:53
1

There's no reason not to have two tables, is your customer your supplier, or vice versa? I think not, and even if that happens I do not think it would happen more than 10 times ...

Starting from this principle then the ideal is to have two tables, even to organize the access plans in the database.

Solutions like:

TelefoneRelacionado
ID_TELEFONE   
ID_CLIENTEouFORNECEDOR

or

TelefoneRelacionado  
ID_TELEFONE  
ID_CLIENTE (pode null) 
ID_FORNECEDOR (pode null)

Besides complicating the maintenance life can leave the database crazy in the access plans, in case the example one will not have foreign key linking the tables.

Another point is that you will not replicate data using two tables, you will not have a single phone number written in the two tables, because of the fact that you did not have many customers that are suppliers and vice versa.

That would be my answer to your question. The above two solutions are ways of doing this, they are not elegant and I dare say they are bad. (my point of view only)

If the system is so large as to cause such a concern then it is worth thinking about how much it is running and full of data, how SQLs will be done.

    
26.11.2015 / 03:16
1

You are doing the wrong thing, the correct thing is for you to relate the person / user (or your entity) to the phones, and your entity relates to his profile, even if he already has an entity, you create a control or customer). This way you can dynamically include vendor, distributor, reseller, client, vendor and so on ...

  • entity-> 1: N (one-to-many) telephones;
  • entity-> profile 1: N (one-to-many);
  • profile-> entity N: N (many-to-many);
  • PS: I do not recommend using automatic exclusion rules for foreign keys, as this can affect data loss, my suggestion is, do it by code, it's safer, bank, passing the responsibility to the system.

        
    26.11.2015 / 03:45