在SQL Server中用新的Int FK替换varchar FK

时间:2019-05-03 12:52:48

标签: sql-server sql-update foreign-keys

我有两个表,我们将它们称为Event和EventResponse

  • Event具有PK eventGuid varchar(36)和其他几列
  • EventResponse具有FK eventGuid varchar(36)和其他列

我可以轻松地将INT identity列添加到Event中,

ALTER TABLE dbo.[Event]
   ADD eventId INT IDENTITY;

然后在EventResponse表中添加一列就可以了。

如何更新响应表中的所有eventId?

使用整洁的查询是否有可能?还是必须遍历?

最终状态应为:

  • Event具有PK eventId, eventGuid varchar(36),其他列
  • EventResponse具有FK eventId, eventGuid varchar(36)和其他列

2 个答案:

答案 0 :(得分:1)

尝试一下:

15.61 ns(R)     15.56 ns(U)     0.04 ns(S)      (2050002iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<3})
16.29 ns(R)     16.27 ns(U)     0.01 ns(S)      (1964070iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<4})
16.31 ns(R)     16.29 ns(U)     0.00 ns(S)      (1962244iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<5})
18.17 ns(R)     18.15 ns(U)     0.00 ns(S)      (1761118iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<6})
16.42 ns(R)     16.41 ns(U)     0.00 ns(S)      (1949061iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<7})
15.97 ns(R)     15.96 ns(U)     0.00 ns(S)      (2003412iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<8})
16.14 ns(R)     16.14 ns(U)     0.00 ns(S)      (1982292iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<9})
16.80 ns(R)     16.79 ns(U)     0.00 ns(S)      (1905223iters; 32 ms)   (malloc_free_)(&($_sz){1ULL<<10})
42.19 ns(R)     42.17 ns(U)     0.00 ns(S)      (758535iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<11})
42.90 ns(R)     42.88 ns(U)     0.00 ns(S)      (746074iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<12})
42.85 ns(R)     42.84 ns(U)     0.00 ns(S)      (746926iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<13})
42.32 ns(R)     42.18 ns(U)     0.00 ns(S)      (756378iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<14})
42.59 ns(R)     42.55 ns(U)     0.00 ns(S)      (751520iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<15})
41.98 ns(R)     41.97 ns(U)     0.00 ns(S)      (762451iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<16})
42.74 ns(R)     42.72 ns(U)     0.00 ns(S)      (748953iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<17})
42.32 ns(R)     42.31 ns(U)     0.00 ns(S)      (756267iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<18})
41.99 ns(R)     41.98 ns(U)     0.00 ns(S)      (762255iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<19})
42.31 ns(R)     42.30 ns(U)     0.00 ns(S)      (756442iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<20})
51.03 ns(R)     50.17 ns(U)     0.00 ns(S)      (627259iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<21})
44.93 ns(R)     44.91 ns(U)     0.00 ns(S)      (712362iters; 32 ms)    (malloc_free_)(&($_sz){1ULL<<23})
6674.43 ns(R)   677.29 ns(U)    5813.42 ns(S)   (4797iters; 32 ms)      (malloc_free_)(&($_sz){1ULL<<25})

答案 1 :(得分:1)

不需要循环。生活的话语:)

这应该就是您所需要的。使用现有的外键更新新键的值,然后删除旧的关系并添加新的关系。

UPDATE er
  SET er.eventID = e.eventID
FROM
  dbo.EventResponse AS er
  JOIN
  dbo.Event as e
    ON er.eventGuid = e.eventGuid;

GO 

ALTER TABLE dbo.EventResponse
DROP CONSTRAINT <FK_Name> 

GO

ALTER TABLE dbo.EventResponse
ADD CONSTRAINT <FK_Name> FOREIGN KEY (eventId)
  REFERENCES dbo.Event(eventId)
  [ON DELETE CASCADE
  ON UPDATE CASCADE]

GO

然后,如果您想在数据库中节省一些空间,则可能是这样:

ALTER TABLE dbo.Event
DROP COLUMN eventGuid;

GO

ALTER TABLE dbo.EventResponse
DROP COLUMN eventGuid;

GO