Question

The problem is there are relationships which are so huge that after normalizing they have like a 20 primary keys (composite keys) which are really foreign keys.

These have to be declared as primary keys to identify the relationship uniquely. Is this correct?

Was it helpful?

Solution

First, do not use composite keys ever. They are a bad technique. They are slow and are a nightmare to maintain when they change.

If you need uniqueness over two or more fields, you do not need a primary key, you need a unique index. Make the PK of the table a surrogate key (preferably int).

If you are trying to create tables with a one-to-one relationship, it is acceptable to use the PK of the parent table as the PK of the Child table and set a PK_FK relationship between the tables; however it would be unusual to need 20 separate one-to-one tables.

OTHER TIPS

If you say that you have "foreign keys that really are foreign keys but that need to be declared as primary keys", then you actually indicate that you lack the competence and the skill and the authority to be doing database design.

Foreign keys and "primary" keys are quite distinct concepts, which are quite impossible to confuse with one another by anyone who is even just remotely knowledgeable in the field of database design.

Maybe you could give it a second try to explain what it actually is that you mean.

It sounds like your database is huge and has a lot of relationships; one thing you can do to simplify the primary key situation is to define a single column as the primary key for each table, and use an automatically incremented int or guid datatype. That way you can ensure uniqueness and your foreign keys are at least independent of your primary key.

I'm having trouble visualizing a design with 20 tables that have a relationship to one table.

I can't tell without looking at your data design, but it sounds like you designed a hierarchical database, rather than a relational database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top