字段操作
- 修改列名
EXEC sp_rename '表名.[字段旧名]', '字段新名' , 'COLUMN';
- 修改类型:
alter table 表名 alter column 字段名 类型 not null
- 修改默认值
alter table 表名 add default (0) for 字段名 with values
- 4、增加:
alter table 表名 add 字段名 类型 not null default 0
- 5、删除:
alter table 表名 drop column 字段名;
约束
alter table 表名 drop constraint 约束名
alter table 表名 add constraint pk_s primary key (id)
如果字段有默认值,则需要先删除字段的约束,在添加新的默认值,
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='字段名'
ID自增长:
ALTER TABLE [表名] ADD ID INT IDENTITY(1,1)
ID加时间自动生成编号:
select ROW_NUMBER() OVER(ORDER BY GETDATE()) as xh from test