Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Forgive me a little new at databases and I came across something while working with Workbench that I have a question on.

In a very simple testing database, with three tables: Company, Department, Employee

Company          Department      Employee
 - id (PK)(AI)   - id (PK)(AI)    - id (PK)(AI)
 - Name          - Name           - Name

I created a 1 to many relationship between Company and Department and since Employees can only have one Department and One company, I created a 1 to many relationship between Company and Employee and Department and Employee.

When I did this, Workbench created a third Relationship column in Employee Called Company_Department_id and all of my columns that have FK all have now been check as PK as well, tables now looks like this:

    Company       Department             Employee
    - id (PK)(AI) - id (PK)(AI)           - id (PK)(AI)
    - Name        - Name                  - Name
                  - Company_id (PK)(FK)   - Company_id (PK)(FK)
                                          - Department_id (PK)(FK)
                                          - Company_Department_id (PK)(FK)

Questions

  1. Why does workbench automatically create the Company_Department_id table? I feel like I should be able to delete it, but I just want to make sure I'm not missing something

  2. How come Workbench automatically tagged each related column as a primary key? Is it because they are primary keys in their own tables? Should I keep them as primary keys?

share|improve this question
    
not sure why this was downvoted. It seems to be a valid question about workbench or mysql. –  kdub May 22 '13 at 0:46
add comment

1 Answer

up vote 1 down vote accepted

It looks like you've used the option to create an Identifying relationship (this is the one with the unbroken line in the toolbox).

An Identifying relationship assumes that the primary key in the referenced table should be part of primary key of the referencing table.

Identifying relationship

Because Company_id is now part of Department's Primary Key, it must also be part of the foreign key of Employee, hence your problem #1.

What you're probably wanting to do is create a Non-identifying relationship. This can be done by either selecting the dashed line from the toolbox, or unticking "Identifying relationship" in the Foreign Key tab of the Relationship properties.

A Non-identifying relationship is a classic foreign key constraint, and simply ensures that any value in the referencing table exists in the referenced table.

Non-identifying relationship

Technically this should resolve your problems, but there are still potential issues with the underlying database design.

For example, there's nothing to stop multiple companies existing with the same name, or multiple departments with the same name, even within the same company.

This could be solved by adding unique key constraints, but another way to tackle the same problem would be to use the Company and Department names as primary keys.

If you were doing this, you'd actually want to use an Identifying relationship, so that the Company name becomes an integral part of the Department.

(You may wish to read up on database theory and database normalisation, as it will help you avoid a lot of traps that you're likely to come up against when building a database)

Another Identifying Relationship

I'm not suggesting that this is a better database design, simply that this is one where an Identifying relationship is valid / useful.

share|improve this answer
    
Thanks a lot for the feedback, that was very helpful and I appreciate it. –  kdub May 22 '13 at 3:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.