· 8 years ago · Jul 19, 2018, 04:26 PM
1ALTER TABLE dbo.MyReallyBigTable
2ADD MyThingId INT NOT NULL
3 DEFAULT 0;
4
5ALTER TABLE dbo.MyReallyBigTable
6ADD MyThingId INT NOT NULL
7 DEFAULT 0
8 REFERENCES dbo.MyThing(MyThingId);
9
10drop table if exists MyReallyBigTable
11drop table if exists MyThing
12go
13select o.* into dbo.MyReallyBigTable
14from sys.objects o, sys.columns c
15
16go
17create table MyThing(MyThingId int primary key )
18insert into MyThing(MyThingId) values (0)
19
20set statistics io on
21go
22ALTER TABLE dbo.MyReallyBigTable
23ADD MyThingId INT NOT NULL
24 DEFAULT 0
25 --REFERENCES dbo.MyThing(MyThingId);
26go
27alter table MyReallyBigTable
28with nocheck
29add constraint fk_MyReallyBigTable_MyThing
30foreign key (MyThingId) references MyThing(MyThingId)
31set statistics io off
32
33select name, is_not_trusted
34from sys.foreign_keys
35go
36alter table MyReallyBigTable with check check constraint fk_MyReallyBigTable_MyThing
37go
38select name, is_not_trusted
39from sys.foreign_keys