简体   繁体   中英

Creating a relationship between two tables without using a Primary Key

I'm looking to create a relationship in SSMS between the Menu table and the WinePrice table. I've attempted to do this via a link table (MenuContents). However I can't figure out the relationship between MenuContentsId in the MenuContents table (as it won't be unique) and MenuContentsId in the Menu table. I've left my other tables out of the picture to keep things clearer.

Menu:                  WinePrice:            MenuContents:

MenuId(PK)             WinePriceId(PK)       MenuContentsId
PubId                  WineId                WinePriceId
MenuContentsId         Size
MenuName               Price 

The idea is that a menu can contain many variations of the same wine (based on its price and size), each identified by WinePriceId- which relates to a specific wine in another unshown table. I can't make MenuContentsId a PK because many MenuContentsIds will have many WinePriceIds.

To me it looks like you want one menu to have many wines. Many wines can exist in many menus.

The link table is correct, all you need to do is to make the PK of MenuContents a composite of both fields. That way, any MenucContents::MenuId field may appear many times as long as MenuContents::WindPriceId is different.

And it means that the same WindPriceId can appear against multiple MenuIds.

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