views:

45

answers:

4

i have many tables : customers, prospects, friends..
They all have some Notes.

Question 1: Should i have one Notes Table shared with all the parent Tables.
OR
Should i have a NotesCustomer, NotesProspects, NotesFriends Tables ?

Question 2:
If the best solution is the first one then this general Notes table should then have a FK to the parent table but ALSO the type of the parent table ?

I'm using EntityFramework but this question is also general i guess...

Thanks Jon

A: 

the first option is best one Notes Table shared with all the others.

you can use two fields notes_obj_id and notes_obj_type for implementing table general Notes

ritesh choudhary
the 2 answers (Phil and ritesh)are exactly saying the opposite !!
John
Wait a day, and see what the upvotes say.
Philip Kelley
(Aug 30) I rest my case.
Philip Kelley
+5  A: 

The first option, a distinct Notes table for Prospects, Customers, and Friends, is likely a cleaner solution.

If you use a general notes table, you will need to have three nullable FK columns to determine which related table the FK belongs to, with only one of them populated in each row.

Generally speaking, the goal of a good relational model is NOT (or not necessarily) to store like data together, but to only store specific data once. So a good question to ask yourself when designing this would be "what am I gaining by storing all notes in the same table?"

Phil Sandler
+1 was going to advise the opposite, but "the goal of a good relational model is NOT (or not necessarily) to store like data together, but to only store specific data once" got me thinking
Nicolas78
+1. I did the single notes table type thing once, and I'd be slow to do it again. I think the biggest thing it saved me was creating fewer tables in my SQL script, which is not a big deal in the scheme of things, and it turned out to be a pain to work with.
joelt
yes, i agree. It is just that i was asking for confirmation from the community and i get 2 opposites answers...
John
One notes table is likely slower and harder to maintain data integrity on - I see no reason to ever use one unless you are querying all the notes to find certain information. In that case, you will want joining tables (one for each entity you want to join to) to join the notesid to the customerid, and notesid to the frinedid, etc.
HLGEM
A: 

I'd intuitively prefer the one table-approach, but it has its drawbacks, too. Pro one-table: Creating several tables with the same structure can be a pain if you ever need to change their structure. Also, you'll have to put in a variable for the table name whenever you query notes instead of the logically cleaner "type" parameter. However, Phil's arguments is interesting as well. You may also find that a single-table layout may end up with a database that can't be queried anymore easily using just SQL. If you're going to have a lot of data, having different tables will also give you a speed difference.

Hm. A really clean but also somewhat complicated solution would be to create a table NotableObject. Then give each customer, prospect, whatever a field "NotableObjectID" and link the notes to NotableObjects, not to customers or prospect. Of course this complicates matters if you want something like "give me all notes on customers" because you explicitly store only the information from customer to note and not reverse, but since, most of the time, you'll have a situation more like "give me all notes for THIS customer", you might be fine.

Nicolas78
A: 

You should have just one Notes table. The relationship flows from Notes to the other entities (it's a 0:M), so no need to have FK columns at the Notes table level. FK columns to the Prospect, Customer, Friend, on the Notes table only leads to a design where you will need to keep adding FK columns to the Notes table each time a new entity needs Notes (and this doesn't speed things up really).

E.g., if you want to get a list of all Prospect notes, simply query the Prospect table and use a join if you must get the notes detail:

select n.NoteId, n.NotesDetail from Prospect p inner join Notes n 
    on p.NoteId = n.NoteId

Your Notes table might look similar to this:

create table Notes
(
    NoteId int identity(1,1)
    ,NotesDetail varchar(max)
    // ... any other fields related to the Notes entity....
)

On the other tables, all you need is a field FK linking to NoteId on the Notes table.

code4life
Not if you need multiple notes per related entity. And if you want one note per entity, there is no reason to have a separate Notes table/tables in the first place.
Phil Sandler
@Phil, Agreed, you would need a join table if you have multiple notes per entity. I wouldn't agree about denormalizing a join table to actually contain the notes though.
code4life
I meant: if you only have 0-1 notes per entity, you would just store the note on the main entity table, and no additional tables would be needed. IOW, the Customer table would have a column called "Note". I'm not clear on what you mean by denormalizing the join table.
Phil Sandler