信息架构和主键

时间:2011-06-27 19:44:33

标签: sql-server sql-server-2005 primary-key constraints information-schema

如何使用主键打印出列的“主键”?

如果表中有主键,而不是带有主键的列和其他列在keyType中为空,则为所有列获取“主键”。

   SELECT c.TABLE_NAME, 
          c.COLUMN_NAME, 
          c.DATA_TYPE, 
          c.Column_default, 
          c.character_maximum_length, 
          c.numeric_precision, 
          c.is_nullable,
          CASE 
            WHEN u.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 'primary key'
            ELSE '' 
          END AS KeyType
     FROM INFORMATION_SCHEMA.COLUMNS as c
LEFT JOIN information_schema.table_constraints as u ON c.table_name = u.table_name
 ORDER BY table_name

4 个答案:

答案 0 :(得分:30)

SELECT  c.TABLE_NAME, c.COLUMN_NAME,c.DATA_TYPE, c.Column_default, c.character_maximum_length, c.numeric_precision, c.is_nullable
             ,CASE WHEN pk.COLUMN_NAME IS NOT NULL THEN 'PRIMARY KEY' ELSE '' END AS KeyType
FROM INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN (
            SELECT ku.TABLE_CATALOG,ku.TABLE_SCHEMA,ku.TABLE_NAME,ku.COLUMN_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
            INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ku
                ON tc.CONSTRAINT_TYPE = 'PRIMARY KEY' 
                AND tc.CONSTRAINT_NAME = ku.CONSTRAINT_NAME
         )   pk 
ON  c.TABLE_CATALOG = pk.TABLE_CATALOG
            AND c.TABLE_SCHEMA = pk.TABLE_SCHEMA
            AND c.TABLE_NAME = pk.TABLE_NAME
            AND c.COLUMN_NAME = pk.COLUMN_NAME
ORDER BY c.TABLE_SCHEMA,c.TABLE_NAME, c.ORDINAL_POSITION 

答案 1 :(得分:0)

第一个答案的一个非常小的补充。可以同时查看MS_Description(或其他扩展属性)

SELECT  c.TABLE_NAME, c.COLUMN_NAME,c.DATA_TYPE, c.Column_default,  c.character_maximum_length, c.numeric_precision, c.is_nullable
             ,CASE WHEN pk.COLUMN_NAME IS NOT NULL THEN 'PRIMARY KEY' ELSE '' END AS KeyType, c.ORDINAL_POSITION 
             ,convert(varchar(8000), ex.value) as coln
   FROM INFORMATION_SCHEMA.COLUMNS c
   LEFT JOIN (
       SELECT ku.TABLE_CATALOG,ku.TABLE_SCHEMA,ku.TABLE_NAME,ku.COLUMN_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
            INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ku
                ON tc.CONSTRAINT_TYPE = 'PRIMARY KEY' 
                AND tc.CONSTRAINT_NAME = ku.CONSTRAINT_NAME
         )   pk 
    ON  c.TABLE_CATALOG = pk.TABLE_CATALOG
            AND c.TABLE_SCHEMA = pk.TABLE_SCHEMA
            AND c.TABLE_NAME = pk.TABLE_NAME
            AND c.COLUMN_NAME = pk.COLUMN_NAME
    outer apply ::fn_listextendedproperty('MS_Description', 'schema', 'dbo', 'table', c.TABLE_NAME, 'column', c.COLUMN_NAME) ex
   ORDER BY c.TABLE_SCHEMA,c.TABLE_NAME, c.ORDINAL_POSITION 

答案 2 :(得分:0)

我发现以下代码是查找表主键的最简单方法。只需在 information_schema.columns 表和 information_schema.key_column_usage 表之间进行左外部联接即可。

Select 
  col.column_name, 
  col.data_type, 
  col.ordinal_position, 
  col.is_nullable, 
  case when ky.COLUMN_NAME is null then 0 else 1 end as primary_key_flag
from <your-catalog>.Information_Schema.columns col
  left outer join <your-catalog>.INFORMATION_SCHEMA.KEY_COLUMN_USAGE ky
    on ky.COLUMN_NAME = col.COLUMN_NAME and ky.TABLE_NAME = col.TABLE_NAME 
      and ky.TABLE_CATALOG = col.TABLE_CATALOG and ky.table_name = col.table_name
where 
  col.TABLE_NAME = '<your-table-name>' 
  and col.table_catalog = '<your-catalog>';

注意:该代码是使用SQL Server 2017 Express(14.0.1000)构建的,创建主键后,我便可以在 KEY_COLUMN_USAGE 中看到这些值。 >表格。我还在 CONSTRAINT_TABLE_USAGE 中看到了一条记录,该记录标识了 constraint_name ,但是我并没有发现要成为显示主键的连接的必要条件

最后的提示:在距离数据库(使用MySQL)近十年后,我刚刚回到一个个人项目的SQL Server,所以我离专家还很近主题。

答案 3 :(得分:-1)

另外,请考虑一下:

sp_helptext N'sp_help'

有时从MS团队学习是件好事。 : - )