sopcce


--1、修改字段名:
      alter table 表名 rename column A to B
--2、修改字段类型:
      alter table 表名 alter column 字段名 type not null
-- 修改字段类型长度
      ALTER TABLE userinfo
      ALTER COLUMN name varchar(100);
--3、修改字段默认值
      alter table 表名 add default (0) for 字段名 with values
   --修改字段类型
      ALTER TABLE userinfo ALTER COLUMN age int;
  --如果字段有默认值,则需要先删除字段的约束,在添加新的默认值,
      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
   -- 修改字段不允许 NULL 值
      ALTER TABLE userinfo ALTER COLUMN age float NOT NULL;

   -- 添加主键
      ALTER TABLE userinfo ADD CONSTRAINT id_name PRIMARY KEY(ID);
   -- 修改字段名 (执行后会有提示:注意: 更改对象名的任一部分都可能会破坏脚本和存储过程。)
      EXEC sp_rename "userinfo.age","userage","COLUMN";
 
   

    
--4、增加字段:
  alter table 表名 add 字段名 type not null default 0
--5、删除字段:
  alter table 表名 drop column 字段名;
--6、删除表
      DROP TABLE userinfo;





分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-28
  • 2021-11-28
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-12-08
猜你喜欢
  • 2021-09-07
  • 2021-06-21
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2021-08-18
  • 2021-07-02
相关资源
相似解决方案