Doubt about relationship between tables in mysql

Asked

Viewed 420 times

5

I have the following tables:

  1. Table: Customers
  2. Table: Interests of the client
  3. Table: User

inserir a descrição da imagem aqui

In the client interests table, I need to enter the user id and the client id as foreign keys, the user id I’ll get from a Session.

I need to know, if when created the relationship with the respective indexes, the client id is dynamically inserted in the table client interests as foreign key?

  • If you don’t create one trigger or procedure in the database, no. The constraint serves as a limiter of that field to accept only data contained in the column of the table to which it is being referenced.

  • 2

    That’s why I use frameworks like Laravel 4. You’ll probably have to take a walk to do that, buddy. You’ll probably have to do a TRIGGER

1 answer

1

Your modeling is not very good.

There is no connection between client and user, as you will know which client is only by user id.

If each client can have more than one user, then create a Mxn table between the user and client tables, user_client and place only the id_user in the table of interests, so you will be able to consult later which client is linked to the interest by making an INNER JOIN.

Now if each client has only one user I don’t see reasons for two different tables, create only one in this case.

  • I reviewed my modeling, really was without much logic, the fields of the table interesses_client I migrated to the table clients, leaving only the table clients and users and creating a foreign key in the table clients for the user id.

  • Very good, good modeling is important for the system.

Browser other questions tagged

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