Table shared in database

-1

I'm new here. I have two tables that are not related to each other, but both need a third one. Let me explain with an example ...

TablaUsuarios (idUsuario, nombre, apellido)
TablaEmpresas (idempresa, nombre_e, rif)

For both tables I need to register mails; a user can have several emails ( 1: M ), and also a company ( 1: M ), so by normalization I separated this information into a table called Email that I structured in the following way

TablaEmail(idcorreo, direccion, propietario, idpropietario)

With the owner and owner-id columns as a single key, where the record was supposed to be as follows:

TablaUsuarios                                TablaEmpresas
idusuario | nombre | apellido                idempresa | nombre_e   | rif
    1     |  María | sosa                        1     | Empresa1   |123


TablaEmail
idemail | direccion       | propietario   |idpropietario
   1    | [email protected] | TablaUsuarios |   1        
   2    | [email protected]  | TablaEmpresas |   1

But when making a record of any of the tables requires me to make a record in both (Users-Companies) which should not be a rule, because they do not have any relationship between them.

If someone could help me, I would appreciate it. Greetings ..

    
asked by Virginia 13.11.2018 в 13:46
source

2 answers

1

As I understand you will relate the data of the post table with various tables with what should be like this:

  • Mailbox columns ( idcorreo , address, id owner)
  • TableUsers (userid, firstname, lastname, idcorreo )
  • Company Table (owner-id, company_name, rif, idcorreo )

I hope it serves you.

    
answered by 13.11.2018 в 13:52
1

The relationship is not difficult. In both the Empresa and the Usuario table you must have a correoId field.

This field correoId , is a foreign key to the table of Correos .

Then in the table of Correos for example the records may exist:

id  - correo
1   - [email protected]
2   - [email protected]
3   - [email protected]

And then in the Usuarios table

Pepe with idCorreo 1 and José with idCorreo 3

And in the table of Empresas

Existos SA with idCorreo 2

It could even be the case that Manuel, the owner of the company Exitos, uses the same mail as the company's, with which in Usuarios we would have, additionally:

Manuel with idCorreo 2 (Same idCorreo as Exists SA in the Empresas table)

The foreing key , also enforces integridad referencial , that is to say that the ids loaded in the tables of Empresas and Usuarios , must exist in the table of Correos .

    
answered by 13.11.2018 в 14:00