简体   繁体   中英

Creating a Composite Foreign key, based on the SAME primary key in another table

first of all, i'm not even sure i worded the title correctly, so let me explain in detail what exactly i'm trying to do.

i'm trying to link 2 tables together (outfits and Items) in SQL Server; the primary keys are Outfit_ID and Item_ID (an auto incrementing int) respectively.

An outfit is made up of a combination of items; therefore i want the Primary key ( Outfits_ID ) to be ( Item_ID,Item_ID,Item_ID ) or how many ever items it takes to make up the outfit.

Is this even possible, and if so how do i go about it?

For a composite key, you would need another table Outfit_Item and it would have Outfit_ID and Item_ID. You can Add as many records for each. Outfit 1 can have multiple items and one item can have multiple outfits. You can restrict these using relationships.

If am item can be in more than one outfit, then you can set up a many to many relationship by adding a table in between the two tables:

Outfit ---> OutfitItem ----> Item

Each OutfitItem record would contain a composite key containing one outfit_id and one Item_id.

Outfit would have a one to many relationship with OutfitItem and Item would have a one to many relationship with OutfitItem.

This will allow multiple items per outfit and multiple outfits for a single item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM