Sql Server-フィールドの追加、フィールドの変更、タイプの変更、デフォルト値ALTERの変更

812 ワード

Sql Server追加フィールド、修正フィールド、修正タイプ、修正デフォルト
一.列名の変更
EXEC sp_テーブル名[フィールドの古い名前]','フィールドの新しい名前','COLUMN';
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 constraint pk_s primary key (id)
4、フィールドを増やす:
alter tableテーブル名addフィールド名type not null default 0
5、フィールドを削除する
alter tableテーブル名drop columnフィールド名;