天天看點

sql server删除有預設值的列

MSSQL資料庫裡可以直接用

alter table article drop [列名]

  來删隊列

  但在Sql server資料庫,如果這個列有預設值,這樣删除列會報錯,這時要删除列的預設值

declare @name varchar(20)

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%'

exec('alter table article drop constraint '[email protected])

alter table [表名] drop column [列名]

删除索引時sql 為:drop index tableName.indexName

  mssql給表添加主索引:alter table tablename add constraint [DF_tablename] default (1) for column

  建帶主索引表:create table tablename (id int identity(1,1) not null constraint PK_tablename primary key, column1 nvarchar(250) null)