tags:

views:

50

answers:

2

I have a table. Call it TableA

this table will link to many tables and ideally be enforced by database relationships in (many-1)(TableA-TableB) (many-1)(TableA-TableC) ... etc

The solution i have is to put all the foreign keys of TableB, TableC, etc in TableA along with a "Type" field (which contains a word version of which relationship is to be enforced). however i think there must be a better way. What would you do?

I'd appreciate any advice in this and thanks.

A: 

If you want to add referential integrity enforced by the database server, each key must be represented by a unique column in TableA.

It's hard to give more advice than that without knowing more about what your design is attempting to do.

Jeff Siver
Why should the key columns be unique? Suppose TableA is "items" and TableB is "categories" - putting a unique constraint on the "category_id" field of TableA would disallow multiple items from belonging to the same category.
danben
@danben: FKeys are used to enforce a 1-to-many relationship. If you want a many-to-many relationship, they wont do. Also ideally, you would create a middle table that would define the many-to-many relation, each line of the middle table being a relation.
kurast
@kurast: the OP specifically states that he is implementing many-to-one relationships. I don't see the relevance of this comment.
danben
@danben: well, now i see that when you said "Why should the key columns be unique?" it is a rethorical question, but when i first read it, i thought it was a doubt...
kurast
@kurast: no, it was not rhetorical. I was asking Jeff S why he thought the key columns should be unique in this particular problem that we are discussing in this thread.
danben
@danben: I probably should've used distinct instead of unique. I'm not saying to use a unique constraint; that won't work. I'm saying that, in order to have referential integrity enforced by the database, each reference must be in it's own column.
Jeff Siver
@Jeff S - ah, ok. I think that that's what the OP has now, though.
danben
+1  A: 

This is a perfectly acceptable approach - foreign keys are indeed the correct way of modeling a many-to-one relationship.

Generally, you can't just say you want to make a solution "better"; rather, you should have a specific goal in mind. Faster, shorter implementation, less memory, whatever. Even better is if you have a specific use case you would like to optimize for.

Edit: your question is more clear now that you've edited it. If I understand correctly, you feel your current implementation is inefficient because one of your TableA items can be attached to at most one other item, be it from TableC, TableC, etc.

If that is correct, what I might do is implement the foreign key in Table A as both an ID and a table name, rather than having a new column for each new type of object you want to add to your system. Of course, this would prevent you from changing table names, so a more robust solution would be to have another table mapping unique ids to object types (stored as table names). Then the foreign key in Table A would be item_id and object_type_id, and you could retrieve the object by looking up object_type_id in the object_types table to get the table name.

danben
My present way is most definately inefficient. However i've thought about this and unless i'm mistaken, this technique breaks refenterial integrity to the objects that are supposed to contain the Item_id
Beta033
@Beta033: I suppose. What problems do you see arising from this solution?
danben