zoukankan      html  css  js  c++  java
  • Sql Server 2008 增加字段、修改字段、修改类型、修改默认值

    1、修改字段名:

      alter table 表名 rename column A to B

           -- Sql Server 2016 修改SQL(执行后会有提示:注意: 更改对象名的任一部分都可能会破坏脚本和存储过程。)

      exec sp_rename "[dbo].[TBL_00001].id","sex是变更后的名称","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 default (0) for 字段名 with values

    4、增加字段:

      alter table 表名 add 字段名 type not null default 0

    5、删除字段:

      alter table 表名 drop column 字段名;

  • 相关阅读:
    bzoj3211 花神游历各国
    bzoj1010
    day8
    day6&7
    bzoj1087
    noi同步赛day1
    bzoj1076
    day3&4&5
    bzoj1079 dp
    cholesky分解
  • 原文地址:https://www.cnblogs.com/pangpanghuan/p/6432331.html
Copyright © 2011-2022 走看看