Look buddy, I’m seeing a mistake that I consider to be the preponderant of why your code is giving problems.
Your modeling is incorrect.
Which led me to this conclusion: you’re working with text fields - mixing letter and number - in an ID column.
Databases handle numbers better, avoid using anything other than this for the ID field.
If as a consequence you still need to use the column Contactoid likewise, this implies that you are incorrectly modelling the bank.
Come on. Blza, we have customers, we have stores. Shops have address, customers have address, blza, address remains the same for the two, a table Addresses, and in the customers and in the stores we have a reference column (Foreign key) for her.
What you don’t understand is the following: a store has N customers, a customer can be "customer" of more than one store - M stores); so in the store-to-customer relationship, we have an N to M (or M to N).
What is the implication of this? Simple, when we have a relation N to M, we have something we call an associative entity, which is nothing more than another table. She has a reference ID for both the customer and the store.
From what I understand, in this modeling, this associative entity is the contact. (store customer contact).
That implies two things:
- If the customer can give 2 different addresses to two stores, I advise to take the customer’s address column, so a store will only know his address when he becomes her contact.
- If this does not matter for your modeling, that is, once the customer updates the address in any store, it counts for all, you leave the reference the address in the customer table.
Same thing goes for the phone. Only the store has a phone - this is on the shop table itself - and the phone number can be on both the contact table and the client table, in the same way that I just explained.
The phone does not need to have an extra table, even if you put there also mobile. It is too much work for little return.
The address you may not even create an extra table, it will save you many joins, nowadays it doesn’t even have much importance vc have a 100% normalized model, maybe if you do meaningful analysis with address make some difference, but again it is too much work for little return.
the prefix is just to differentiate, the field
ContatoId
references both to the tablecliente
how much for tableloja
, imagine the scenario that I have registered in the stores table:idLoja | 1
and in the contact table insert this same idcontatoid | 1
, when registering a customer,clienteId | 1
when entering the contact table would already have another record withid 1
, I don’t know if it’s clear– Hebert Lima