SQL query to find Primary Key of a table?

前端 未结 8 446
耶瑟儿~
耶瑟儿~ 2021-02-04 01:30

How can I find which column is the primary key of a table by using a query?

8条回答
  •  臣服心动
    2021-02-04 01:38

    For MSSQL: SELECT DISTINCT C.TABLE_NAME,C.COLUMN_NAME,C.DATA_TYPE FROM sys.indexes I INNER JOIN sys.index_columns IC ON I.OBJECT_ID = IC.OBJECT_ID AND I.INDEX_ID = IC.INDEX_ID INNER JOIN information_schema.columns C ON COL_NAME(IC.OBJECT_ID,IC.COLUMN_ID) = C.COLUMN_NAME WHERE I.IS_PRIMARY_KEY = 1

提交回复
热议问题