zoukankan      html  css  js  c++  java
  • 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;

     

    参考:
     Enable/Disable Constraint in SQLServer 
     sp_MSforeachtable使用方法

    --启用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;

     

    参考:
     Enable/Disable Constraint in SQLServer 
     sp_MSforeachtable使用方法

  • 相关阅读:
    Civil 3D 二次开发 创建Civil 3D 对象—— 01 —— 创建几何空间点
    Civil 3D 二次开发 创建Civil 3D 对象—— 00 ——
    Civil 3D 二次开发 创建AutoCAD对象—— 01 —— 创建直线
    Civil 3D 二次开发 新建CLR项目出现错误C2143
    Civil 3D 二次开发 创建AutoCAD对象—— 00 ——
    了解AutoCAD对象层次结构 —— 6 ——块表记录
    datepicker97使用
    使用angular 外接 templateUrl,使用ng-include
    angularJs 遮罩
    网上找的有关css兼容问题
  • 原文地址:https://www.cnblogs.com/dragon2017/p/9708289.html
Copyright © 2011-2022 走看看