国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

在 SQL Server 2005 中查詢表結(jié)構(gòu)及索引

2024-08-31 00:49:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在 sql server 2005 中查詢表結(jié)構(gòu)及索引
-- 1. 表結(jié)構(gòu)信息查詢
-- ========================================================================
-- 表結(jié)構(gòu)信息查詢

-- 鄒建 2005.08(引用請(qǐng)保留此信息)
-- ========================================================================
select
    tablename=case when c.column_id=1 then o.name else n'' end,
    tabledesc=isnull(case when c.column_id=1 then ptb.[value] end,n''),
    column_id=c.column_id,
    columnname=c.name,
    primarykey=isnull(idx.primarykey,n''),
    [identity]=case when c.is_identity=1 then n'√'else n'' end,
    computed=case when c.is_computed=1 then n'√'else n'' end,
    type=t.name,
    length=c.max_length,
    precision=c.precision,
    scale=c.scale,
    nullable=case when c.is_nullable=1 then n'√'else n'' end,
    [default]=isnull(d.definition,n''),
    columndesc=isnull(pfd.[value],n''),
    indexname=isnull(idx.indexname,n''),
    indexsort=isnull(idx.sort,n''),
    create_date=o.create_date,
    modify_date=o.modify_date
from sys.columns c
    inner join sys.objects o
        on c.[object_id]=o.[object_id]
            and o.type='u'
            and o.is_ms_shipped=0
    inner join sys.types t
        on c.user_type_id=t.user_type_id
    left join sys.default_constraints d
        on c.[object_id]=d.parent_object_id
            and c.column_id=d.parent_column_id
            and c.default_object_id=d.[object_id]
    left join sys.extended_properties pfd
        on pfd.class=1
            and c.[object_id]=pfd.major_id
            and c.column_id=pfd.minor_id
--             and pfd.name='caption'  -- 字段說(shuō)明對(duì)應(yīng)的描述名稱(一個(gè)字段可以添加多個(gè)不同name的描述)
    left join sys.extended_properties ptb
        on ptb.class=1
            and ptb.minor_id=0
            and c.[object_id]=ptb.major_id
--             and pfd.name='caption'  -- 表說(shuō)明對(duì)應(yīng)的描述名稱(一個(gè)表可以添加多個(gè)不同name的描述)

    left join                       -- 索引及主鍵信息
    (
        select
            idxc.[object_id],
            idxc.column_id,
            sort=case indexkey_property(idxc.[object_id],idxc.index_id,idxc.index_column_id,'isdescending')
                when 1 then 'desc' when 0 then 'asc' else '' end,
            primarykey=case when idx.is_primary_key=1 then n'√'else n'' end,
            indexname=idx.name
        from sys.indexes idx
        inner join sys.index_columns idxc
            on idx.[object_id]=idxc.[object_id]
                and idx.index_id=idxc.index_id
        left join sys.key_constraints kc
            on idx.[object_id]=kc.[parent_object_id]
                and idx.index_id=kc.unique_index_id
        inner join  -- 對(duì)于一個(gè)列包含多個(gè)索引的情況,只顯示第1個(gè)索引信息
        (
            select [object_id], column_id, index_id=min(index_id)
            from sys.index_columns
            group by [object_id], column_id
        ) idxcuq
            on idxc.[object_id]=idxcuq.[object_id]
                and idxc.column_id=idxcuq.column_id
                and idxc.index_id=idxcuq.index_id
    ) idx
        on c.[object_id]=idx.[object_id]
            and c.column_id=idx.column_id

-- where o.name=n'要查詢的表'       -- 如果只查詢指定表,加上此條件
order by o.name,c.column_id

-- 2. 索引及主鍵信息
-- ========================================================================
-- 索引及主鍵信息

-- 鄒建 2005.08(引用請(qǐng)保留此信息)
-- ========================================================================
select
    tableid=o.[object_id],
    tablename=o.name,
    indexid=isnull(kc.[object_id],idx.index_id),
    indexname=idx.name,
    indextype=isnull(kc.type_desc,'index'),
    index_column_id=idxc.index_column_id,
    columnid=c.column_id,
    columnname=c.name,
    sort=case indexkey_property(idxc.[object_id],idxc.index_id,idxc.index_column_id,'isdescending')
        when 1 then 'desc' when 0 then 'asc' else '' end,
    primarykey=case when idx.is_primary_key=1 then n'√'else n'' end,
    [uqique]=case when idx.is_unique=1 then n'√'else n'' end,
    ignore_dup_key=case when idx.ignore_dup_key=1 then n'√'else n'' end,
    disabled=case when idx.is_disabled=1 then n'√'else n'' end,
    fill_factor=idx.fill_factor,
    padded=case when idx.is_padded=1 then n'√'else n'' end
from sys.indexes idx
    inner join sys.index_columns idxc
        on idx.[object_id]=idxc.[object_id]
            and idx.index_id=idxc.index_id
    left join sys.key_constraints kc
        on idx.[object_id]=kc.[parent_object_id]
            and idx.index_id=kc.unique_index_id
    inner join sys.objects o
        on o.[object_id]=idx.[object_id]
    inner join sys.columns c
        on o.[object_id]=c.[object_id]
            and o.type='u'
            and o.is_ms_shipped=0
            and idxc.column_id=c.column_id
--    inner join  -- 對(duì)于一個(gè)列包含多個(gè)索引的情況,只顯示第1個(gè)索引信息
--    (
--        select [object_id], column_id, index_id=min(index_id)
--        from sys.index_columns
--        group by [object_id], column_id
--    ) idxcuq
--        on idxc.[object_id]=idxcuq.[object_id]
--            and idxc.column_id=idxcuq.column_id
--      

 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芒康县| 义马市| 沧源| 称多县| 孝感市| 长武县| 临城县| 吴桥县| 涟水县| 岐山县| 青海省| 嘉义市| 陆丰市| 吉安市| 屏南县| 尉犁县| 海丰县| 宁海县| 包头市| 德钦县| 平顺县| 龙海市| 津市市| 台北市| 沧源| 呼图壁县| 宜川县| 达日县| 天水市| 静安区| 星座| 湘西| 枣阳市| 鄂托克前旗| 萨嘎县| 仙游县| 缙云县| 三门县| 溧水县| 大埔区| 广元市|