我使用SqlCompare生成了一些Server(2008)升级脚本。主要目标是添加一个空列。但是,生成的脚本希望在其他两个表中删除外键,并在add列之前删除目标表的主键。然后它重新添加它们。这有必要吗?做这件事的理由是什么?
发布于 2013-08-21 14:46:34
不没有必要。请参阅下面的工作示例:
use TestDatabase;
go
-- create the parent table (houses the PK)
create table dbo.ParentTable
(
ParentId int identity(1, 1) not null
constraint PK_ParentTable_ParentId primary key clustered,
some_int int not null
);
go
-- insert some dummy data
insert into dbo.ParentTable(some_int)
values (5), (4), (3);
go
-- create the child table (houses the FK)
create table dbo.ChildTable
(
ChildId int identity(1, 1) not null,
ParentId int not null
constraint FK_ChildTable_ParentId foreign key references dbo.ParentTable(ParentId)
);
go
-- insert some dummy data
insert into dbo.ChildTable(ParentId)
values (1), (3);
go
-- view the contents of each table
select *
from dbo.ParentTable;
select *
from dbo.ChildTable;
-- add a nullable int column
alter table dbo.ParentTable
add another_col int null;
go
-- view the new layout
select *
from dbo.ParentTable;
select *
from dbo.ChildTable;正如您可以从代码中看到的,我通过添加一个列来修改dbo.ParentTable。这是一个成功的操作,主键约束仍然存在。
至于为什么你的第三方软件这样做,我们可以猜测一整天。但最有可能的是,他们这样做是为了处理某个角落的情况,而无需首先测试当前的操作是否属于这种情况。
https://dba.stackexchange.com/questions/48491
复制相似问题