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

2021-09-26 21:37:52 字數 4156 閱讀 5044

information_schema:系統檢視

sys.extended_properties:系統檢視

表或表字段等的注釋,是資料庫物件的擴充套件屬性。在mssql中,支援把一些注釋性的內容放到資料庫或資料庫物件中,增強可讀性,有助於日後的管理和維護工作。擴充套件屬性的內容可以通過ssms新增、修改或刪除,也可以通過系統檢視查詢,通過執行相關的儲存過程來維護。

查詢乙個表的所有列名

select t.column_name from information_schema.columns t where t.table_name='sys_menu';
查詢乙個表的所有列名,資料型別

select column_name,data_type from information_schema.columns 

where table_name = ''

查詢乙個表的所有欄位的注釋

select value from sys.extended_properties where major_id = object_id ('');
查詢乙個表的所有資訊

select * from information_schema.columns 

where table_name = ''

查詢乙個表的所有列名,欄位的注釋

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 = ''

查詢某個表的列名稱、說明、備註、型別等

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

為字段新增描述資訊

execute sp_addextendedproperty n'ms_description', '需要注釋的內容', n'user', n'dbo', n'table', n'', n'column', n'';
為字段修改描述資訊

execute sp_updateextendedproperty 'ms_description', '','user','dbo','table','','column','';
刪除字段描述資訊

execute sp_dropextendedproperty 'ms_description','user','dbo','table','','column','';
新增表注釋

execute sp_addextendedproperty 'ms_description','注釋','user','dbo','table','',null,null;
刪除表注釋

execute sp_dropextendedproperty 'ms_description','user','dbo','table','',null,null;
修改表注釋

execute sp_updateextendedproperty 'ms_description','修改注釋','user','dbo','table','',null,null;
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

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

sql server 新增表注釋 字段注釋

為字段新增注釋 格式如右 execute sp addextendedproperty ms description 字段備註資訊 user dbo table 字段所屬的表名 column 新增注釋的欄位名 execute sp addextendedproperty ms description...

SqlServer查詢表的字段和注釋

select case when a.colorder 1 then d.name else null end 表名,a.colorder 字段序號,a.name 欄位名,case when columnproperty a.id,a.name,isidentity 1 then else end ...

mysql 表注釋和字段注釋

1 建立表的時候寫注釋 create table test1 field name int comment 欄位的注釋 comment 表的注釋 2 修改表的注釋 alter table test1 comment 修改後的表的注釋 3 修改欄位的注釋 alter table test1 modif...