SQL Many-to-many Combination Table

a 夏天 提交于 2019-12-08 05:09:37

问题


I've had an interesting problem with a database design whose solution is not yet satisfactory.

I have a table of Stations with the following fields:
- key_station (Primary key)
- station_name
- info

Then I have a table of Positions:
- key_position (Primary key)
- position_number
- info
- key_station (Foreign key to Stations)

And table of Setups:
- key_setup (Primary key)
- setup_number
- info
- key_station (Foreign key to Stations)

Now, the Positions table lists all the slot numbers of a station, and the Setups table lists all the possible different setups of a station. What I need is a table that has a different part in each position depending on the setup. Some positions may be empty. For example...

Setup 1
Position1 - AA
Position2 - BB
Position3 - NULL

Setup 2
Position1 - NULL
Position2 - CC
Position3 - NULL

So I created a table name Settings, with these fields:
- key_setting (Primary key)
- key_position (Foreign key)
- key_setup (Foreign key)
- part

The problem here is that I have to ensure that Settings always has the combinations of the referenced tables. So when I insert a new position in the example, it has to insert 2 records in Settings (one per setup), or when I insert a new setup, it has to insert 3 records in Settings (one per position) with NULL parts that the user can fill later. Why? because the positions don't change in each setup, only their assigned parts.

What I currently do is that I insert and delete in both table Positions and Settings (or Setups and Settings) with two SQL statements. Unfortunately I've had issues of inserted positions or setups without their corresponding records in table Settings because I didn't take care of the atomicity of my operations.

So I want to know what is the most elegant and correct solution to a many-to-many relationship in which the intermediary table must have all the combinations of that relationship. What kind of normalization do I need? Or what kind of constraint can I add in SQL Server? Does this problem have a specific name so I can search more?


回答1:


  • Configure FKs with ON UPADE CASCADE ON DELETE CASCADE

  • After inserting new StationSlot or StationSetup run this


insert into StationConfiguration (StationID, StationSlotNo, StationSetupNo)
select
      a.StationID
    , b.StationSlotNo
    , a.StationSetupNo
from StationSetup as a
join StationSlot  as b on b.StationID = a.StationID
where not exists (
     select 1 
     from StationConfiguration as xx
     where xx.StationID      = a.StationID
       and xx.StationSlotNo  = b.StationSlotNo
       and xx.StationSetupNo = a.StationSetupNo
     )  
;   

  • This inserts any new combination with Part being NULL.



回答2:


In Setups table remove key_station and add key_position and remove Settings table altogether. That way you will know to which station a given row belongs by following the position reference and you will store the mappings between position and their values in that same table (Setups).



来源:https://stackoverflow.com/questions/11183847/sql-many-to-many-combination-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!