• SQL删除约束


     1 1)禁止所有表约束的SQL
     2 select 'alter table '+name+' nocheck constraint all' from sysobjects where type='U'
     3 
     4 2)删除所有表数据的SQL
     5 select 'TRUNCATE TABLE '+name from sysobjects where type='U'
     6 
     7 3)恢复所有表约束的SQL
     8 select 'alter table '+name+' check constraint all' from sysobjects where type='U'
     9 
    10 4)删除某字段的约束
    11 declare @name varchar(100)
    12 --DF为约束名称前缀
    13 select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='字段名' and b.name like 'DF%'
    14 --删除约束
    15 alter table 表名 drop constraint @name
    16 --为字段添加新默认值和约束
    17 ALTER TABLE 表名 ADD CONSTRAINT @name  DEFAULT (0) FOR [字段名]对字段约束进行更改
    18 --删除约束
    19 ALTER TABLE tablename
    20 Drop CONSTRAINT 约束名
    21 --修改表中已经存在的列的属性(不包括约束,但可以为主键或递增或唯一)
    22 ALTER TABLE tablename 
    23 alter column 列名 int not null
    24 --添加列的约束
    25 ALTER TABLE tablename
    26 ADD CONSTRAINT DF_tablename_列名 DEFAULT(0) FOR 列名
    27 --添加范围约束
    28 alter table  tablename  add  check(性别 in ('M','F'))
    好的代码就和美食一样,都是需要时间烹饪出来的!
  • 相关阅读:
    [剑指offer] 赋值运算符重载
    [hihoCoder] 股票价格
    [LintCode] Kth Smallest Number in Sorted Matrix
    [LeetCode] Subarray Sum Equals K | 前缀和+哈希表
    [LeetCode] Add and Search Word
    [LeetCode] Implement Trie
    Tableau基础练习(三)
    Tableau基础练习(二)
    Tableau基础练习(一)
    SpringMVC用户请求下载文件
  • 原文地址:https://www.cnblogs.com/slmdr9/p/5329532.html
Copyright © 2020-2023  润新知