2014-01-21 43 views
3

比方說,我有一個具有這些表重組數據庫/表

Car --> Has Dealerships --> Has Demographic --> Has Revenue 

後來後啓動客戶說,他們希望汽車具有模型類型和每個模型應該有一個經銷商

數據庫所以我的結構變成

Car --> Has Model --> Has Dealerships --> has Demographic --> Has Revenue 

我該如何遷移到第二個數據庫結構?我需要切斷CarDealership之間的外鍵才能使其工作。我只需要拍攝舊數據庫的快照並在知道如何處理它的上下文中編寫包裝器?

+1

你需要做到這一點在實際系統上,不會丟失數據,或者是這個東西,可以在沒有人注視你做些什麼呢?在修正外鍵時,使用臨時列和/或表來保存舊的和新的ID是很常見的。 – HABO

+3

爲什麼在這個世界上有4個upvotes?在OP方面做了什麼嘗試? – Kermit

+0

如果' - >'應該表示一對多關係,那麼編寫'Model - > Has Cars'可能會更準確。 –

回答

1

我的理解是應該是Model --> Has Cars,而不是Car --> Has Model。這在我看來更加一致地使用了-->這個名稱。

所以,正確的最終關係鏈可能會是這個樣子:

 
Model --> Has Cars 
     --> Has Dealerships --> Has Demographic --> Has Revenue 

我想象的動作順序如下:

  1. 創建和填充Models表。您的架構在這一點上:

     
    Model 
    
    Car --> Has Dealerships --> Has Demographic --> Has Revenue 
    
  2. 添加ModelIDCars,並根據您的業務需求的正確Models引用來填充它。它會給你以下結果:

     
    Model --> Has Cars --> Has Dealerships --> Has Demographic --> Has Revenue 
    
  3. 添加ModelID參考Dealerships,通過使用目前的關係加入到DealershipsCars填充它。你最終將不得不DealershipsModels,直接之間並通過Cars雙重關係,是這樣的:

     
    Model  -->  Has Dealerships --> Has Demographic --> Has Revenue 
        \    /
        \-> Has Cars -/ 
    
  4. Cars --> Dealership外鍵,刪除Dealerships.CarID參考列。這將是你最後的結果:

     
    Model --> Has Dealerships --> Has Demographic --> Has Revenue 
        \ 
        \--> Has Cars