views:

56

answers:

4

I have a table called WorkItemNotes, and separate tables for Customers and Employees.

I am using MySQL WorkBench to map out the database tables and relationships and have a couple of questions.

Both Customers and Employees can add notes to the WorkItemNotes table, and the associated ID will be stored in the table:

| WorkItemNotes |
| guid/ID       |
| Notes         |
| Author        |

Am I better to have two tables for notes, one for each the Customer and the Employee? Or if I keep one table, how can I set up the relationship in WorkBench. Is it valid to attach a foreignkey to the Author field that is reference by both the Customer and Employee table?

+1  A: 

You can do something like this

| WorkItemNotes |
| guid/ID       |
| Notes         |
| Author        |
| AuthorType    |

add one more field in table as AuthorType will do work for you

  • AuthorType =1 Customer
  • AuthorType =2 Employee
Pranay Rana
+1  A: 

I would probably try to do this instead.

Have a generic users table with UserID as ID Maybe you can add a column called User Type which can be Employee / Customer / others

Link this user id to WorkItemNotes as a FK.

Have 2 separate Employee and Customer Tables Each of them has a FK reference to the UserID of users table with customer / employee specific information

InSane
+1  A: 

Have one table for your notes. The primary key in the notes table, say note_id, will be stored in your customers and employees table.

**Note**
note_id
notes
author

**Customers**
customers_id
note_id NULL
...

**Employees**
employees_id
note_id NULL

Have a FK between employees and notes, and customers and notes. This design only allows for 1 note. So if you need n notes, then you need a link table such as:

**Notes**
external_id
external_type
note_id

external_type=1 for customers, 2 for employees. the external_id is either the cusomter_id or the employee id.

This design allows for many different tables taking notes. If you only have two tables that can store notes, then simply use two note tables, but this design allows for many. However, because of the link table being able to join to any customer or employee table, you cannot use FK's.

Simon Hughes
+3  A: 

The cleanest solution is to model the existence of a Person as a fact, distinct from their status as a Customer or Employee, and tie the Notes to the Person relation:

Person { PersonID PK, ... }
Customer { PersonID PK FK(Person.PersonID), ... }
Employee { PersonID PK FK(Person.PersonID), ... }
WorkItemNotes { NotesID PK, Notes, AuthorID FK(Person.PersonID) }

This has the additional feature (or misfeature, depending on the business rules) that an employee can also be a customer.

Marcelo Cantos
so in this model, the shared attributes of all of the actors in in the Person field, while the function specific attributes are stored in the Customer or Employee table...
NinjaCat
and from this method, how would I distinguish from a customer or an employee? A separate field in the Customer and Employee table?
NinjaCat
You distinguish them by where they're found.
Marcelo Cantos
By that method, if I wanted to determine the "type", I would have to query each of the Customer and Employee tables to see if there was a match though - wouldn't that be non-optimal?
NinjaCat
Why do you want to know the type?
Marcelo Cantos
@NinjaCat - you could just have a PersonType field in person which would give you the type without even having to join to any child tables. PersonType is an attribute of the generic person and would fit in well here. Additionally this personType could itself be a FK from a MASTER person type table which may have all possible type of people in your application (customer / employee / maybe other types as well)
InSane
@In Sane: Usually, when someone wants to know the type, they don't actually need to know it, hence my question.
Marcelo Cantos
Ah, well if we offer a single form to sign on, if I knew the type we could point to the correct page (rather than having them tell us what role they are), is one example.
NinjaCat
@Marcelo - that may be the case - but there can be lots of scenarios where the business logic itself is dependant on the type of user / differnt views of pages need to be shown etc - so knowing the type could be a very valid requirement
InSane
so, PersonTable has a guid (PK).The PersonType has a name (PK).I make a 1:1 identifying relationship between them and I see that Workbench changed PersonTable to now have a persontype_name field as a PK. That doesn't seem right...
NinjaCat