Sql – Database Design: Alternate to composite keys

data modelingdatabase-designinterbasesql

I am building a database system and having trouble with the design of one of my tables.

In this system there is a users table, an object table, an item table and cost table.

A unique record in the cost table is determine by the user, object, item and year. However, there can be multiple records that have the same year if the item is different.

The hierarchy goes user->object->item->year, multiple unique years per item, multiple unique items per object, multiple unique objects per user, multiple unique users.

What would be the best way to design the cost table?

I am thinking of including the userid, objectid and itemid as foreign keys and then using a composite key consisting of userid, objecid, itemid and costyear. I have heard that composite keys are bad design, but I am unsure how to structure this to get away from using a composite key. As you can tell my database building skills are a bit rusty.

Thanks!

P.S. If it matters, this is an interbase db.

Best Answer

To avoid the composite key, you just define a surrogate key. This holds an artificial value, for instance an auto counter.

You still can (and should) define a unique constraint on these columns.

Btw: its not only recommended not to use composite keys, it's also recommendable to use surrogate keys. In all your tables.