Sql server中如何刪除有默認(rèn)值的列
刪除列操作是在使用Sql server數(shù)據(jù)庫(kù)中比較常見(jiàn)的操作,但是如果刪除的列有默認(rèn)值,那應(yīng)該如何操作呢?下面就將為您介紹Sql server中刪除有默認(rèn)值的列的方法。
Access數(shù)據(jù)庫(kù)里可以直接用alter table article drop [列名]來(lái)刪隊(duì)列,但在Sql server數(shù)據(jù)庫(kù),如果這個(gè)列有默認(rèn)值,這樣刪除列會(huì)報(bào)錯(cuò),這時(shí)要?jiǎng)h除列的默認(rèn)值。
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 '+@name) 
alter table [表名] drop column [列名]
其它: 
  刪除索引時(shí)Access為:drop index indexName on tableName 
        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)
 
 
 
 
 














 
 
 