数据库/表的重组

时间:2014-01-21 13:56:09

标签: sql sql-server

假设我有一个包含这些表的数据库

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

在推出后,客户说他们希望汽车有型号,每个型号都应该有经销商

所以我的结构变成了

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

我如何迁移到第二个数据库结构?我需要在CarDealership之间切断外键才能使用。我是否只是拍摄旧数据库的快照,并在知道如何处理它的上下文中编写一个包装器?

1 个答案:

答案 0 :(得分: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. ModelID添加Cars列,并根据您的业务需求使用正确的Models引用填充该列。它会给你以下结果:

    Model --> Has Cars --> Has Dealerships --> Has Demographic --> Has Revenue
    
  3. ModelID添加Dealerships引用,通过使用当前关系将Dealerships加入Cars来填充该引用。您最终会在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