sql server 语句获取表的描述,主键等等
Posted Sealee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 语句获取表的描述,主键等等相关的知识,希望对你有一定的参考价值。
sql语句添加表,字段的描述
描述类型全部设置为:MS_Description
--添加表的描述 --格式如右:execute sp_addextendedproperty \'MS_Description\',\'字段备注信息\',\'user\',\'dbo\',\'table\',\'字段所属的表名\',\'column\',\'添加注释的字段名\'; --添加表描述 EXECUTE sp_addextendedproperty \'C\', \'课程表\', \'user\', \'dbo\', \'table\', \'Course\',NULL, NULL; --添加字段描述 EXECUTE sp_addextendedproperty \'MS_Description\', \'课程ID\', \'user\', \'dbo\',\'table\', \'Course\', \'column\', \'CourseId\';
或者:右键表(列)的属性,找到扩展属性
①获取表名
--查询所有的表名称 SELECT name FROM sys.objects obj WHERE obj.type=\'U\'; -- xtype:类型可以区分表,视图,存储过程,函数等等
②获取表的描述 sys.extended_properties 这张表存的扩展描述
SELECT obj.name 表名 , ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sys.objects obj ON ds.major_id = obj.object_id WHERE ds.minor_id = 0 AND obj.name = \'Course\';
最后整理:
--最后整理 SELECT obj.name, se.value FROM sys.objects obj LEFT JOIN [sys].[extended_properties] se ON obj.object_id = se.major_id AND se.minor_id = 0 WHERE obj.type = \'U\';
③获取表的主键信息
--表 sys.indexes SELECT * FROM sys.indexes WHERE is_primary_key = 1 AND object_id =Object_Id(\'Course\')
--最后整理 SELECT obj.name, CAST(CASE WHEN ( SELECT COUNT(1) FROM sys.indexes WHERE is_primary_key = 1 AND object_id =obj.object_id ) >= 1 THEN 1 ELSE 0 END AS BIT) AS HasPrimaryKey FROM sys.objects obj WHERE obj.type = \'U\';
④获取主键的类型和名称
--得到表的主键列id SELECT sc.column_id FROM sys.indexes si INNER JOIN sys.index_columns sc ON si.index_id = sc.index_id AND si.object_id = sc.object_id WHERE si.is_primary_key = 1 AND si.object_id = OBJECT_ID(\'Course\');
--获取主键类型和名称 SELECT TOP 1 colm.name AS TablePrimarkeyType, systype.name AS TablePrimarkeyName FROM sys.columns colm INNER JOIN sys.types systype ON colm.system_type_id = systype.system_type_id WHERE colm.object_id = OBJECT_ID(\'Course\') AND colm.column_id IN ( SELECT ic.column_id FROM sys.indexes idx INNER JOIN sys.index_columns ic ON idx.index_id = ic.index_id AND idx.object_id = ic.object_id WHERE idx.object_id = OBJECT_ID(\'Course\') AND idx.is_primary_key = 1 );
--整理 SELECT obj.name, t.TablePrimarkeyType, t.TablePrimarkeyName FROM sys.objects obj OUTER APPLY ( SELECT TOP 1 colm.name AS TablePrimarkeyType, systype.name AS TablePrimarkeyName FROM sys.columns colm INNER JOIN sys.types systype ON colm.system_type_id = systype.system_type_id WHERE colm.object_id = obj.object_id AND colm.column_id IN ( SELECT ic.column_id FROM sys.indexes idx INNER JOIN sys.index_columns ic ON idx.index_id = ic.index_id AND idx.object_id = ic.object_id WHERE idx.object_id = obj.object_id AND idx.is_primary_key = 1 ) ) t WHERE obj.type = \'U\';
⑤最终结果:
--获取表的结构 表名,拥有着,行数,是否拥有主键,表描述,表的主键,表的主键名称 SELECT obj.name Tablename, schem.name Schemname, idx.rows, CAST(CASE WHEN ( SELECT COUNT(1) FROM sys.indexes WHERE object_id = obj.object_id AND is_primary_key = 1 ) >= 1 THEN 1 ELSE 0 END AS BIT) HasPrimaryKey, b.value TableDesc, t.* FROM sys.objects obj INNER JOIN sysindexes idx --行数 ON obj.object_id = idx.id AND idx.indid <= 1 INNER JOIN sys.schemas schem --架构 ON obj.schema_id = schem.schema_id LEFT JOIN sys.extended_properties b --描述 ON obj.object_id = b.major_id AND b.minor_id = 0 AND b.name=\'MS_Description\' OUTER APPLY --主键名称和类型 ( SELECT TOP 1 colm.name AS TablePrimarkeyType, systype.name AS TablePrimarkeyName FROM sys.columns colm INNER JOIN sys.types systype ON colm.system_type_id = systype.system_type_id WHERE colm.object_id = obj.object_id AND colm.column_id IN ( SELECT ic.column_id FROM sys.indexes idx INNER JOIN sys.index_columns ic ON idx.index_id = ic.index_id AND idx.object_id = ic.object_id WHERE idx.object_id = obj.object_id AND idx.is_primary_key = 1 ) ) t WHERE obj.type = \'U\' -- AND obj.name LIKE \'My_%\' ORDER BY obj.name;
以上是关于sql server 语句获取表的描述,主键等等的主要内容,如果未能解决你的问题,请参考以下文章