SQLServer 表注释和字段注释的一些基本操作

查看字段注释和表的信息

  • 参考文章
  • https://www.cnblogs.com/qy1234/p/9044275.html

information_schema:系统视图

sys.extended_properties:系统视图

表或表字段等的注释,是数据库对象的扩展属性。在MSSQL中,支持把一些注释性的内容放到数据库或数据库对象中,增强可读性,有助于日后的管理和维护工作。扩展属性的内容可以通过SSMS添加、修改或删除,也可以通过系统视图查询,通过执行相关的存储过程来维护。

  1. 查询一个表的所有列名
select t.column_name from information_schema.columns t where t.table_name='sys_menu';
  1. 查询一个表的所有列名,数据类型
select column_name,data_type from information_schema.columns 
where table_name = ''
  1. 查询一个表的所有字段的注释
select value from sys.extended_properties where major_id = object_id ('');
  1. 查询一个表的所有信息
select * FROM information_schema.columns 
where table_name = ''
  1. 查询一个表的所有列名,字段的注释
SELECT
A.name AS table_name,
B.name AS column_name,
C.value AS column_description
FROM sys.tables A
INNER JOIN sys.columns B ON B.object_id = A.object_id
LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id
WHERE A.name = ''
  1. 查询某个表的列名称、说明、备注、类型等
SELECT 
    表名       = case when a.colorder=1 then d.name else '' end,
    表说明     = case when a.colorder=1 then isnull(f.value,'') else '' end,
    字段序号   = a.colorder,
    字段名     = a.name,
    标识       = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
    主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
                     SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
    类型       = b.name,
    占用字节数 = a.length,
    长度       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
    小数位数   = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
    允许空     = case when a.isnullable=1 then '√'else '' end,
    默认值     = isnull(e.text,''),
    字段说明   = isnull(g.[value],'')
FROM 
    syscolumns a
left join 
    systypes b 
on 
    a.xusertype=b.xusertype
inner join 
    sysobjects d 
on 
    a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
left join 
    syscomments e 
on 
    a.cdefault=e.id
left join 
sys.extended_properties   g 
on 
    a.id=G.major_id and a.colid=g.minor_id  
left join
sys.extended_properties f
on 
    d.id=f.major_id and f.minor_id=0
where 
    d.name=''    --如果只查询指定表,加上此where条件,tablename是要查询的表名;去除where条件查询所有的表信息
order by 
    a.id,a.colorder

字段注释 增删改查

  • 参考文章
  • https://blog.csdn.net/huapenguag/article/details/80471309
  • https://blog.csdn.net/luming666/article/details/78538986
  1. 为字段添加描述信息
EXECUTE sp_addextendedproperty N'MS_Description', '需要注释的内容', N'user', N'dbo', N'table', N'', N'column', N'';
  1. 为字段修改描述信息
execute sp_updateextendedproperty 'MS_Description', '','user','dbo','table','','column','';
  1. 删除字段描述信息
execute sp_dropextendedproperty 'MS_Description','user','dbo','table','','column','';  

增删改查 表的注释

  • 参考文章
  • https://blog.csdn.net/huapenguag/article/details/80471309
  1. 添加表注释
EXECUTE sp_addextendedproperty 'MS_Description','注释','user','dbo','table','',NULL,NULL;
  1. 删除表注释
execute sp_dropextendedproperty 'MS_Description','user','dbo','table','
',null,null;
  1. 修改表注释
execute sp_updateextendedproperty 'MS_Description','修改注释','user','dbo','table','
',null,null;

查看表的注释

  • https://blog.csdn.net/qq_35023116/article/details/81178214
  • 查询所有表及注释
SELECT DISTINCT
	d.name,
	f.value 
FROM
	syscolumns a
	LEFT JOIN systypes b ON a.xusertype= b.xusertype
	INNER JOIN sysobjects d ON a.id= d.id 
	AND d.xtype= 'U' 
	AND d.name<> 'dtproperties'
	LEFT JOIN syscomments e ON a.cdefault= e.id
	LEFT JOIN sys.extended_properties g ON a.id= G.major_id 
	AND a.colid= g.minor_id
	LEFT JOIN sys.extended_properties f ON d.id= f.major_id 
	AND f.minor_id= 0
  • 查询所有表及注释(只显示有注释的表)
  • 参考文章
  • https://www.cnblogs.com/pang572936554/p/9700280.html
SELECT
A.name ,C.value
FROM sys.tables A

LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id
WHERE C.minor_id=0 
group by A.name ,C.value

你可能感兴趣的:(数据库)