开发者

Information schema and Primary Keys

How do I just print out a 'primary key' for the column with the primary key?

I get 'primary key' for all the columns if the table has a primary key, instead of the one column with the primary key and the other columns as blank in 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


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 


As an addition, consider looking at:

sp_helptext N'sp_help'

Sometimes learning from the MS team is a great thing. :-)


a very small add to the first answer. It's possible to view in same time the MS_Description (or other extend properties)

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 


I've found this code below to be the simplest method to find the primary key for a table. It only requires a left outer join between the information_schema.columns table and the information_schema.key_column_usage table.

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>';

Note: This code was built using SQL Server 2017 Express (14.0.1000) and after I created the primary key, I was able to see those values in the KEY_COLUMN_USAGE table. I also saw a record in the CONSTRAINT_TABLE_USAGE which identified the constraint_name, but I didn't find that to be a requirement in order to get the join into which revealed the primary key.

Final note: I'm just getting back into SQL server on a personal project after nearly a decade away from the DB (using MySQL), and so I'm nowhere near an expert on the subject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜