zoukankan      html  css  js  c++  java
  • 查询表内属于主键的列SQLServer2005

    查询主键的列,将用到sys.indexes和sys.index_columns两个视图。
    sys.indexes主要保存索引和主键等值,并保存有判断是否为主键的列。
    sys.index_columns主要用保存sys.indexes视图内对应的主键等列的index_id的信息。

    具体实现为:

    select t4.name as columnname from --t4.name 列名
    sys.tables t1                                  --t1 存储对应数据库里所有用户表

    left outer join
    sys.indexes t2                                --t2 存储对应数据库里所有索引和主键
    on
    t1.object_id = t2.object_id              --object_id 表对象相应的编号

    left outer join
    sys.index_columns t3                     --index_columns 表索引及主键相应的列信息
    on
    t2.object_id = t3.object_id
    and t2.index_id = t3.index_id

    left outer join
    sys.columns t4                              --t4 存储数据库内所有用户表的列值信息
    on
    t3.object_id = t4.object_id
    and t3.column_id = t4.column_id

    where
    t1.name = 'M_Class'                      -- t1.name 表名
    and t2.is_primary_key = 1             -- t2.is_primary_key 是否为主键

    order by t4.column_id                   -- 按照列编号进行输出

  • 相关阅读:
    linux查看java jdk安装路径和设置环境变量
    linq where in 排序
    Console程序后台运行
    winform中文本框,软键盘跟随
    winform 应用log4net做日志记录到mysql
    c# 单实例运行
    Sql Server数据库监听 c#代码
    winform程序开机自动启动
    c# 连接mysql配置config,不用装net connector
    winform 不规则窗体无锯齿demo
  • 原文地址:https://www.cnblogs.com/si812cn/p/1168667.html
Copyright © 2011-2022 走看看