zoukankan      html  css  js  c++  java
  • SQL SERVER之查询外键及索引

    --查询表或数据库中的所有外键
    
    select
    A.name as 约束名,
    object_name(b.parent_object_id) as 外健表,
    c.name as 外键列,
    object_name(b.referenced_object_id) as 主健表,
    D.name as 主键列
    from sys.foreign_keys A
    inner join sys.foreign_key_columns B on A.object_id=b.constraint_object_id
    inner join sys.columns C on B.parent_object_id=C.object_id and B.parent_column_id=C.column_id 
    inner join sys.columns D on B.referenced_object_id=d.object_id and B.referenced_column_id=D.column_id 
    where object_name(B.referenced_object_id)='TB_TestConfigTemplate';--主键表
    
    -- 查询一个表或数据库中的索引及索引列
    
    SELECT  indexname = a.name , tablename = c. name , indexcolumns = d .name , a .indid
    FROM    sysindexes a JOIN sysindexkeys b ON a .id = b .id  AND a .indid = b.indid
            JOIN sysobjects c ON b .id = c .id
            JOIN syscolumns d ON b .id = d .id  AND b .colid = d .colid
    WHERE   a .indid NOT IN ( 0 , 255 )  --indid=1代表聚集索引 indid>1代表非聚集索引
    -- and   c.xtype='U'   and   c.status>0 -- 查所有用户表
    --AND c .name = 'DatabaseLog' --查指定表
    ORDER BY c. name ,
            a.name ,
            d.name
  • 相关阅读:
    JavaScript函数
    JavaScript数组知识点
    面向对象之继承及属性查找顺序
    面向对象二
    面向对象
    正则表达式补充
    垃圾回收机制、标记删除及分代回收
    hashlib、hmac、subprocess、configparser模块
    模块、起别名、from导入
    递归、匿名函数、内置函数
  • 原文地址:https://www.cnblogs.com/lfxiao/p/6760507.html
Copyright © 2011-2022 走看看