您的位置:首页 > 博客中心 > 数据库 >

创建与删除SQL约束或字段约束

时间:2022-03-14 21:44

 

技术分享1)禁止所有表约束的SQL
技术分享select ‘alter table ‘+name+‘ nocheck constraint all‘ from sysobjects where type=‘U‘
技术分享
技术分享2)删除所有表数据的SQL
技术分享select ‘TRUNCATE TABLE ‘+name from sysobjects where type=‘U‘
技术分享
技术分享3)恢复所有表约束的SQL
技术分享select ‘alter table ‘+name+‘ check constraint all‘ from sysobjects where type=‘U‘
技术分享
技术分享4)删除某字段的约束
技术分享declare @name varchar(100)
技术分享--DF为约束名称前缀
技术分享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%‘
技术分享--删除约束
技术分享alter table 表名 drop constraint @name
技术分享--为字段添加新默认值和约束
技术分享ALTER TABLE 表名 ADD CONSTRAINT @name  DEFAULT (0) FOR [字段名]

 

技术分享--删除约束
技术分享ALTER TABLE tablename
技术分享Drop CONSTRAINT 约束名
技术分享--修改表中已经存在的列的属性(不包括约束,但可以为主键或递增或唯一)
技术分享ALTER TABLE tablename 
技术分享alter column 列名 int not null
技术分享--添加列的约束
技术分享ALTER TABLE tablename
技术分享ADD CONSTRAINT DF_tablename_列名 DEFAULT(0) FOR 列名
技术分享--添加范围约束
技术分享alter table  tablename  add  check(性别 in (‘M‘,‘F‘))

热门排行

今日推荐

热门手游