简体   繁体   中英

Single Foreign Key for multiple look up tables

I'm building a simple application that would record several widget selections within a form (drop-down list and multiple select for this example) and save it within a log table in a small database.

http://i.stack.imgur.com/THpfs.png

This schema represents a selection of a Fruit from a drop down, and 1-n Fruit Type Selection from a multi-select:

Fruit FruitId Name 1 Apple 2 Orange 3 Melon

Orange OrangeId Name PLU1 PLU2 1 Navel 123 321 2 Blood 213 412 3 Cara 512 433
LogFruitSelection LogId FruitId FruitSelection 1 2 2 1 2 3 2 1 1 2 1 2 2 1 3 2 1 4

The user is restricted to select only one fruit which will give them an option to select multiple types of that fruit, 1 type of fruit per submission. The selections will be held within a selection table which will reference a LogId, a FruitId, and a SelectionId which references a lookup table.

I am lost as to how to create a schema that would let me use a selection table with a single foreign key that would reference several look up tables based on another columns value.

Hope this is what you want

Fruit

 FruitId     Name
    1           Apple
    2           Orange
    3           Melon

Fruit Type

 TypeId     Name     PLU1     PLU2  FruitId
 1            Navel    123      321  2
 2            Blood    213      412  2
 3            Cara     512      433  2

LogFruitSelection

 LogId     FruitId     FruitSelection
 1         2           2
 1         2           3
 2         1           1
 2         1           2
 2         1           3
 2         1           4

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