HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Both Primary and Foreign Key Needed on Table?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
neededprimaryforeignbothandtablekey

Problem

Someone recently asked this question:


There are two tables: Deal and DealCategories. One deal can have many
deal categories.


So the proper way should be to make a table called DealCategories with
the following structure:

DealCategoryId (PK)
 DealId (FK)
 DealCategoryId (FK)




cont ...

Can someone explain why DealCategories table has

DealCategoryId (FK)


on it?

Solution

That part did not make sense to me either. The OP (of that question) states:

"One deal can have many deal categories."

I assume that there can also be many deals in a deal category. So, the "proper" way to would be a DealCategory table:

DealCategory
--------------
DealCategoryId (PK)


and a DealInCategory many-to-many table, with:

DealInCategory
--------------
DealId (FK to Deal)
DealCategoryId (FK to DealCategory)
PK: (DealId, DealCategoryId)

Code Snippets

DealCategory
--------------
DealCategoryId (PK)
DealInCategory
--------------
DealId (FK to Deal)
DealCategoryId (FK to DealCategory)
PK: (DealId, DealCategoryId)

Context

StackExchange Database Administrators Q#17824, answer score: 4

Revisions (0)

No revisions yet.