SQl Server 添加字段、修改字段

Sql Server 增加字段、修改字段、修改類型、修改默認值
1、修改字段名:


  alter table 表名 rename column A to B


2、修改字段類型:


  alter table 表名 alter column 字段名 type not null


3、修改字段默認值
  alter table 表名 add default (0) for 字段名 with values


  如果字段有默認值,則需要先刪除字段的約束,在添加新的默認值,


  select c.name from sysconstraints a 
  inner join syscolumns b on a.colid=b.colid 
  inner join sysobjects c on a.constid=c.id
  where a.id=object_id('表名') 
  and b.name='字段名'


  根據約束名稱刪除約束


  alter table 表名 drop constraint 約束名


  根據表名向字段中增加新的默認值


  alter table 表名 add default (0) for 字段名 with values


4、增加字段:


  alter table 表名 add 字段名 type not null default 0


5、刪除字段:


  alter table 表名 drop column 字段名;
發佈了32 篇原創文章 · 獲贊 40 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章