获取 table 的列名 vs 确定列是否为主键 (sql)

get column names of table vs determine column is primary key or not (sql)

我想获取 table 的所有列与确定字段为主键的额外列或 not.i 使用此代码:

SELECT c.column_id     ID,       c.name                   ItemName,
    CASE
        WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
        ELSE 0
   END                      ISPK
   FROM   sys.tables tb
   JOIN sys.columns c
        ON  tb.name = 'register'
        AND tb.[object_id] = c.[object_id]
   JOIN sys.types tp
        ON  c.user_type_id = tp.user_type_id
   LEFT JOIN sys.computed_columns cc
        ON  c.[object_id] = cc.[object_id]
        AND c.column_id = cc.column_id
  LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
        ON  c.name = k.COLUMN_NAME
  LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tblCon
        ON  tblCon.TABLE_NAME = 'register'
        AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
        AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
        AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME

在此示例中 table名称为 "register"。

此代码是正确的,但对于主键字段 return 两条记录:1

有什么问题?

这应该有效

SELECT c.column_id  ID, c.name   ItemName,
    CASE
        WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
        ELSE 0
   END   ISPK
   FROM   sys.tables tb
   JOIN sys.columns c
        ON  tb.name = 'register'
        AND tb.[object_id] = c.[object_id]
   JOIN sys.types tp
        ON  c.user_type_id = tp.user_type_id
   LEFT JOIN sys.computed_columns cc
        ON  c.[object_id] = cc.[object_id]
        AND c.column_id = cc.column_id
  LEFT JOIN (select * from  INFORMATION_SCHEMA.KEY_COLUMN_USAGE where  CONSTRAINT_NAME like 'PK_%' and TABLE_NAME='register') AS K
        ON  c.name = k.COLUMN_NAME
  LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS  AS tblCon
        ON  tblCon.TABLE_NAME = 'register'
        AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
        AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
        AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME

从 Sagar 那里得到答案并进行了大量清理。有两个额外的连接,我将子查询更改为左连接并修改了一些连接条件,因此 table 名称只需输入一次。

SELECT c.column_id AS ID
    , c.name AS ItemName
    , CASE
        WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
        ELSE 0
        END AS ISPK
FROM sys.tables tb
JOIN sys.columns c ON 
    tb.object_id = c.object_id
    AND tb.[object_id] = c.[object_id]
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K on 
    CONSTRAINT_NAME like 'PK_%' 
    and TABLE_NAME = tb.name
    AND c.name = k.COLUMN_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS  AS tblCon ON 
    tblCon.TABLE_NAME = tb.name
    AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
    AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
    AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME
where tb.name = 'register'

添加了另外一列作为详细信息。

SELECT c.column_id ID,  c.name   ItemName,
    CASE
        WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
        ELSE 0
   END   ISPK,
      ISNULL(tblCon.CONSTRAINT_TYPE,'') as Detail

   FROM   sys.tables tb
   JOIN sys.columns c
        ON  tb.name = 'register'
        AND tb.[object_id] = c.[object_id]
   JOIN sys.types tp
        ON  c.user_type_id = tp.user_type_id
   LEFT JOIN sys.computed_columns cc
        ON  c.[object_id] = cc.[object_id]
        AND c.column_id = cc.column_id
  LEFT JOIN (select * from  INFORMATION_SCHEMA.KEY_COLUMN_USAGE where  (CONSTRAINT_NAME like 'PK_%' or CONSTRAINT_NAME like 'FK_%')  and TABLE_NAME='register') AS K
        ON  c.name = k.COLUMN_NAME
  LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS  AS tblCon
        ON  tblCon.TABLE_NAME = 'register'
        AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
        AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
        AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME