Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值
参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html
初始化表:
--1、添加字段 --1.1、为null alter table DataTable add addField1 nvarchar(50) null alter table DataTable add addField3 nvarchar(50) null --1.2、不为null,有默认值 alter table DataTable add addField2 nvarchar(50) not null default \'test\' --2、删除字段 --2.1、 删除无默认值的字段 alter table DataTable drop column addField1; --2.2、删除有默认值的字段 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(\'DataTable\') and b.name=\'addField2\' alter table DataTable drop constraint 约束名 alter table DataTable drop column addField2; --3、修改字段类型 alter table DataTable alter column addField1 nvarchar(30) --4、修改字段名 exec sp_rename \'DataTable.[addField3]\', \'addField4\', \'COLUMN\' --5、修改字段默认值 --5.1、修改无默认值的字段 alter table DataTable add default (\'test\') for addField4 with values --5.2、修改有默认值的字段(同删除字段) 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(\'DataTable\') and b.name=\'addField4\' alter table DataTable drop constraint 约束名 alter table DataTable add default (\'test\') for addField4 with values