views:

48

answers:

4

For example, I have a table that stores classes, and a table that stores class_attributes. class_attributes has a class_attribute_id and a class_id, while classes has a class_id.

I'd guess if a dataset is "a solely child of" or "belongs solely to" or "is solely owned by", then I need a FK to identify the parent. Without class_id in the class_attributes table I could never find out to which class this attribute belongs to.

Maybe there's an helpful answer matrix for this?

+1  A: 

I don't have an answer matrix, but just for clarification purposes, we're talking about Database Normalization:

http://en.wikipedia.org/wiki/Database%5Fnormalization

And to a certain extent Denormalization:

http://en.wikipedia.org/wiki/Denormalization

Kristopher Ives
+1  A: 

Wikipedia is helpful.

In the context of relational databases, a foreign key is a referential constraint between two tables.[1] The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must be the primary key or other candidate key in the referenced table.

(and it goes on into more and more detail)

If you want to enforce the constraint that each row in class_attributes applies to exactly one row of classes, you need a foreign key. If you don't care about enforcing this (ie, you're fine to have attributes for non-existent classes), you don't need an FK.

James Polley
A: 

Don't be concerned with the type of relationship -- it has more to do with the cardinality of the relationship.

If you have a one-to-many relationship, then you'd want to assign a Primary Key to the smaller of the tables, and store it as a Foreign Key in the larger table.

You'd also do it with one-to-one relationships, but some people argue that you should avoid them.

In the case of a many-to-many relationship, you'd want to make a join table, and then have each of the original tables have a foreign key to the join table.

Joe
+1  A: 

I would say, it's the other way around. First, you design what kind of objects you need to have. For those will create a table.

Part of this phase is designing the keys, that is the combinations of attributes (columns) that uniquely identify the object. You may or may not add an artificial key or surrogate key for convenience or performance reasons. From these keys, you typically elect one canonical key, the primary key, which you try to use consistently to identify objects in that table (you keep the other keys too, they serve to ensure unicity as a business rule, not so much for identificattion purposes.)

Then, you think what relationships exist between the objects. An object that is 'owned' by another object, or an object that refers to another object needs some way to identify its related object. In the corresponding table (child table) you add columns to make a foreign key to point to the primary key of the referenced table.

This takes care of all one to many relationships.

Sometimes, an object can be related multiple times to another object. For example, an order can be used to order multiple products, but a product can appear on multiple orders as well. For those relationships, you design a separate table (intersection table - in this example, order_items). This table will have a unique key created from two foreign keys: one pointing to the one parent (orders), one to the other parent (products). And again, you add the columns to the intersection table that you need to create those foreign keys.

So in short, you first design keys and foreign keys, only then you start adding columns to implement them.

Roland Bouman
You may find this post useful too http://stackoverflow.com/questions/1979522/how-to-fetch-an-object-graph-at-once/1979549#1979549
Roland Bouman