简体   繁体   中英

SQL Conditional relationship between 1 parent 3 child tables

I am trying to develope a db with Sql Server Management Studio.

Grandparent table
Resource
ResourceID_PK   int
Project     nvarchar(5)
Model       nvarchr(15)

Parent table
Machine
MachineID_PK    int
Model_UK        nvarchar(15)
Type
Brand
EmptyWeight

Child tables
DumpTruck   .       Excavator       Loader
DumpTruckID_PK      ExcavatorID_PK  Loader_ID       int
Model_UK            Model_UK        Model_UK        nvarchar(15)
Capacity            Capacity        Capacity

Model column is unique for whole db. I need to reach Capacity column from Grandparent table. Model column of each Child tables are unique since all machinery will have different Model like “CAT966”.

I need to use Grandparent Model column to read one of the Child table, which has the same Model.

I develope ac# project which inputs Model of the machine and will return capacity from the corresponding child table.

To join the tables, you will need ResourceID_PK in the Parent table and MachineID_PK in the Child tables. Then you can join the tables and reach the Capacity column from the Grandparent table.

EDIT

  1. Add ResourceID_PK to Machine (the Parent table)
  2. Add MachineID_PK to the Child tables

To get the Capacity based on Model, use this query:

Select Capacity
From DumpTruck c
Join Machine p on c.DumpTruckID_PK=p.MachineID_pk
Join Resource gp on gp.ResourceID_PK=p.ResourceID_PK
where gp.Model='X'

(A small warning, I have not tested this query.)

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