Question

I trying to make database diagram with MySQL workbench. Something like this :

users
--FK user_logins
--FK user_tabs
----FK tab_favorites
--FK user_settings
...

But when I make some relations in diagram, MySQL Workbench makes additional column for each table as foreign key. So if I have 20 tables related to users table, it will make 20 additional cols in users table.

I am n00b in DBs, but this looks bad. Is better to make users.user_id FK to all other tables? And if is, how to make it with MySQL workbench diagram tool?

Was it helpful?

Solution 2

This is because you are defining the relation the wrong way around.

I take it that a user may be related to several (e.g.) logins, so the foreign key column must be in the login table.

Please note, the term "foreign key" is the name of the column that references a foreign table (formally, the referenced column must be the primary key). So for instance:

        User         Login
        -------      -------
        id (PK)      id (PK)
        name         login
        ...          user_id (FK to user.id)     <== this is the foreign key
        -------      ...
                     -------

OTHER TIPS

In order to re-use existing columns use the last toolbutton for creating an 1:n relationship. Then pick the source and target columns as you need.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top