SQLServer禁用、启用外键约束
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
---启用or禁用指定表所有外键约束 alter table PUB_STRU NOCHECK constraint all ; alter table PUB_STRU CHECK constraint all ; ---生成启用or禁用指定表外键约束的sql select 'ALTER TABLE ' + b. name + ' NOCHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; select 'ALTER TABLE ' + b. name + ' CHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; --生成的sql如下 ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK2; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK2; --查看约束状态(查询字典表 sys.foreign_keys,该字典表开始出现于sqlserver2005及以上版本): select name , is_disabled from sys.foreign_keys order by name ; --其中:name : 外键约束名称 is_disabled : 是否已禁用 |
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
--删除外键 alter table AdItem drop constraint AdOrder_AdItem_FK1 --增加外键 alter table AdItem add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo) --单个表的一个外键 alter table Student nocheck constraint FK__Student__SchoolN__4222D4EF alter table Student check constraint FK__Student__SchoolN__4222D4EF --单个表的所有外键 alter table Student nocheck constraint all alter table Student check constraint all --某个数据库的所有表 EXEC sp_MSforeachtable @command1= 'alter table ? NOCHECK constraint all; EXEC sp_MSforeachtable @command1=' alter table ? CHECK constraint all ; |
SQLServer禁用、启用外键约束
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
---启用or禁用指定表所有外键约束 alter table PUB_STRU NOCHECK constraint all ; alter table PUB_STRU CHECK constraint all ; ---生成启用or禁用指定表外键约束的sql select 'ALTER TABLE ' + b. name + ' NOCHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; select 'ALTER TABLE ' + b. name + ' CHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; --生成的sql如下 ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK2; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK2; --查看约束状态(查询字典表 sys.foreign_keys,该字典表开始出现于sqlserver2005及以上版本): select name , is_disabled from sys.foreign_keys order by name ; --其中:name : 外键约束名称 is_disabled : 是否已禁用 |
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
--删除外键 alter table AdItem drop constraint AdOrder_AdItem_FK1 --增加外键 alter table AdItem add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo) --单个表的一个外键 alter table Student nocheck constraint FK__Student__SchoolN__4222D4EF alter table Student check constraint FK__Student__SchoolN__4222D4EF --单个表的所有外键 alter table Student nocheck constraint all alter table Student check constraint all --某个数据库的所有表 EXEC sp_MSforeachtable @command1= 'alter table ? NOCHECK constraint all; EXEC sp_MSforeachtable @command1=' alter table ? CHECK constraint all ; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!