문제

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?

도움이 되었습니까?

해결책 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
        -------      ...
                     -------

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top