码迷,mamicode.com
首页 > 数据库 > 详细

sql server 语句获取表的描述,主键等等

时间:2019-01-23 01:33:16      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:join   http   add   server   case   ble   LEDE   分表   rop   

sql语句添加表,字段的描述

--添加表的描述
--格式如右: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  sysobjects WHERE  xtype = u; -- xtype:类型可以区分表,视图,存储过程,函数等等

技术分享图片

 

②获取表的描述  sys.extended_properties  这张表存的扩展描述

--查询表的描述
SELECT  tbs.name 表名 ,
        ds.value 描述
FROM    sys.extended_properties ds
        LEFT JOIN sysobjects tbs ON ds.major_id = tbs.id
WHERE   ds.minor_id = 0
        AND tbs.name = Course;

技术分享图片

最后整理:

--表和描述
SELECT sb.name,
       se.value
FROM sysobjects sb
    LEFT JOIN [sys].[extended_properties] se
        ON sb.id = se.major_id
           AND se.minor_id = 0
WHERE sb.xtype = U;

技术分享图片

 

③获取表的主键信息

--获取主键字段  sysindexkeys
SELECT   * FROM SysColumns WHERE id=Object_Id(Course) and colid=(select TOP 1 keyno from sysindexkeys WHERE id=Object_Id(Course));

--表 sys.indexes
SELECT * FROM sys.indexes  WHERE is_primary_key = 1   AND object_id =Object_Id(Course)

技术分享图片

--最后整理
SELECT sb.name,
       CAST(CASE
                WHEN
                (
                    SELECT COUNT(1) FROM sysindexkeys sk WHERE sk.id = sb.id
                ) >= 1 THEN
                    1
                ELSE
                    0
            END AS BIT) AS HasPrimaryKey
FROM sysobjects sb
WHERE sb.type = U;

技术分享图片

 

 ④获取主键的类型和名称

第3步的上面进行扩展

SELECT sc.name [Name],st.name [Type]   FROM SysColumns sc   LEFT JOIN systypes st
        ON sc.xtype = st.xtype WHERE id=Object_Id(Course) AND  
colid=(select TOP 1 keyno from sysindexkeys WHERE id=Object_Id(Course));

技术分享图片

 --整理
SELECT sb.name,
       sc.name [ColumnName],
       st.name [ColumnType]
FROM sysobjects sb
    LEFT JOIN syscolumns sc
        ON sc.id = sb.id
           AND sc.colid =
           (
               SELECT TOP 1 keyno FROM sysindexkeys WHERE id = sb.id
           )
    LEFT JOIN systypes st
        ON sc.xtype = st.xtype
WHERE sb.type = U;

 

 或者: 

----查询该表中的所有列的类型和名称以及id
SELECT 
    b.name AS [Type],
    a.name AS [Name],    
    a.colid
FROM syscolumns a
    INNER JOIN systypes b
        ON a.xtype = b.xtype 
WHERE id = OBJECT_ID(Course) AND b.name!=sysname; 

技术分享图片

--得到表的主键列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 sb.name,
       t.Name,
       t.Type
FROM sysobjects sb
    OUTER APPLY
(
    SELECT b.name AS [Type],
           a.name AS [Name]
    FROM syscolumns a
        INNER JOIN systypes b
            ON a.xtype = b.xtype
    WHERE id = sb.id
          AND a.colid IN (   --得到主键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 = sb.id
                         )
) t
WHERE sb.type = U;

 

 ⑤最终结果:

--查询表名,行数,表注释,主键名,主键类型,是否有主键  可以根据自己要的进行更改
SELECT sb.name Tablename,
       idx.rows,
       se.value TableDesc,
       sc.name TablePrimarkeyName,
       st.name TablePrimarkeyType,
       CAST(CASE
                WHEN
                (
                    SELECT COUNT(1) FROM sysindexkeys sk WHERE sk.id = sb.id
                ) >= 1 THEN
                    1
                ELSE
                    0
            END AS BIT) AS HasPrimaryKey
FROM sysobjects sb
    LEFT JOIN sys.extended_properties se
        ON sb.id = se.major_id
           AND se.minor_id = 0
    LEFT JOIN syscolumns sc
        ON sc.id = sb.id
           AND sc.colid =
           (
               SELECT TOP 1 keyno FROM sysindexkeys WHERE id = sb.id
           )
    LEFT JOIN systypes st
        ON sc.xtype = st.xtype
    INNER JOIN dbo.sysindexes idx
        ON sb.id = idx.id
           AND idx.indid <= 1
WHERE sb.xtype = U;

技术分享图片 

 

sql server 语句获取表的描述,主键等等

标签:join   http   add   server   case   ble   LEDE   分表   rop   

原文地址:https://www.cnblogs.com/Sea1ee/p/10306786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!