sqlserver给表添加新字段、给表和字段添加备注、更新备注及查询备注(sql语句)

2022-10-22,,,,

先给大家介绍下sqlserver给表添加字段、给表和字段添加备注更新备注及查询备注,代码如下所示:

-- 添加新字段及字段备注的语法
use my_slaughterproduct--数据库
alter table my_sp_packagingweight--表名  
add fsummary--字段名  
int--类型 
default ((0))  --默认值
go
--给字段添加注释
exec sp_addextendedproperty n'ms_description', n'是否称重汇总,0未汇总 1已汇总'-- 注释
, n'schema', n'dbo',n'table', n'my_sp_packagingweight'--表名
, n'column', n'fsummary';--字段名
--为表添加主键语法
 alter table 表名 add  primary key(字段名)
 alter table my_sp_fproductsection add  primary key(fid)
--去掉表中无用字段
alter table t_business_asrs_inbill_feedback drop column bar_code

alter table t_business_asrs_outbill_feedback drop column bar_code
--sqlserver给表和字段添加备注并更新备注以及查询备注
--新增:
exec sp_addextendedproperty n'ms_description', n'表备注内容', n'schema', n'dbo',n'table', n'表名';
--修改:
exec sp_updateextendedproperty n'ms_description', n'表备注内容', n'schema', n'dbo',n'table', n'表名';
--字段添加和修改备注:
--新增:
exec sp_addextendedproperty n'ms_description', n'字段备答注内容', n'schema', n'dbo',n'table',`` n'表名',n'column', n'字段名';
--修改:
exec sp_updateextendedproperty n'ms_description', n'字段备注内容', n'schema', n'dbo',n'table', n'表名',n'column', n'字段名';

------2022-5.24更新-------------

--1、修改字段名:
alter table 表名 rename column a to b

--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 字段名;

--6、修改字段类型长度
alter table userinfor 
alter column name varchar(100);

--7、修改字段类型
alter table userinfo alter column age float;

--8、修改字段不允许nul值
alter table userinfo alter column age float not null;

--9、添加主键
alter table userinfo add constraint id_name primary key(id);

--10、修改字段名(执行后会有提示:注意:更改对象名的任一部分都可能会破坏脚本和存储过程。)
exec sp_ rename "userinfo. age","useage","column";
--11、添加字段名
alter table userinfo add gender bit default 0

sqlserver新增带备注字段

alter table meetingtype add isshowmeetlisteners bit not null default(1)
exec sys.sp_addextendedproperty @name=n'ms_description', @value=n'是否显示会议听众' , @level0type=n'schema',@level0name=n'dbo',
@level1type=n'table',@level1name=n'meetingtype', @level2type=n'column',@level2name=n'isshowmeetlisteners'

到此这篇关于sqlserver给表添加新字段、给表和字段添加备注、更新备注以及查询备注的文章就介绍到这了,更多相关sqlserver给表添加新字段内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《sqlserver给表添加新字段、给表和字段添加备注、更新备注及查询备注(sql语句).doc》

下载本文的Word格式文档,以方便收藏与打印。