SQL查询查找表的主键?

时间:2022-12-22 09:07:00

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

如何使用查询查找表的主键?

5 个解决方案

#1


11  

For Oracle, you can look it up in the ALL_CONSTRAINTS table:

对于Oracle,可以在ALL_CONSTRAINTS表中查找:

SELECT a.COLUMN_NAME
FROM all_cons_columns a INNER JOIN all_constraints c 
     ON a.constraint_name = c.constraint_name 
WHERE c.table_name = 'TBL'
  AND c.constraint_type = 'P';

DEMO.

演示。

For SQL Server, it was already answered here, and for MySQL check @ajon's answer.

对于SQL Server,这里已经回答了这个问题,对于MySQL检查@ajon的答案。

#2


17  

This is a duplicate question:

这是一个重复的问题:

credit to Lukmdo for this answer:

这要归功于卢卡多:

It might be not advised but works just fine:

这可能不被建议,但效果很好:

show index from TABLE where Key_name = 'PRIMARY' ;

The solid way is to use information_schema:

可靠的方法是使用information_schema:

SELECT k.COLUMN_NAME
FROM information_schema.table_constraints t
LEFT JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
    AND t.table_schema=DATABASE()
    AND t.table_name='owalog';

#3


7  

For MySQL:

MySQL:

SELECT GROUP_CONCAT(COLUMN_NAME), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  TABLE_SCHEMA = '**database name**'
  AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;

Warning a primary key with two columns will have them separated by a coma (,)

警告具有两列的主键将被昏迷(,)分隔

#4


5  

Try this query in SQL server:

在SQL server中尝试这个查询:

SELECT     X.NAME AS INDEXNAME,
           COL_NAME(IC.OBJECT_ID,IC.COLUMN_ID) AS COLUMNNAME
FROM       SYS.INDEXES  X 
INNER JOIN SYS.INDEX_COLUMNS  IC 
        ON X.OBJECT_ID = IC.OBJECT_ID
       AND X.INDEX_ID = IC.INDEX_ID
WHERE      X.IS_PRIMARY_KEY = 1
  AND      OBJECT_NAME(IC.OBJECT_ID)='YOUR_TABLE'

#5


2  

The following query gives the list of all the primary keys in the given database.

下面的查询给出给定数据库中所有主键的列表。

SELECT DISTINCT TABLE_NAME ,column_name
    FROM INFORMATION_SCHEMA.key_column_usage
    WHERE TABLE_SCHEMA IN ('*your_db_name*');

#1


11  

For Oracle, you can look it up in the ALL_CONSTRAINTS table:

对于Oracle,可以在ALL_CONSTRAINTS表中查找:

SELECT a.COLUMN_NAME
FROM all_cons_columns a INNER JOIN all_constraints c 
     ON a.constraint_name = c.constraint_name 
WHERE c.table_name = 'TBL'
  AND c.constraint_type = 'P';

DEMO.

演示。

For SQL Server, it was already answered here, and for MySQL check @ajon's answer.

对于SQL Server,这里已经回答了这个问题,对于MySQL检查@ajon的答案。

#2


17  

This is a duplicate question:

这是一个重复的问题:

credit to Lukmdo for this answer:

这要归功于卢卡多:

It might be not advised but works just fine:

这可能不被建议,但效果很好:

show index from TABLE where Key_name = 'PRIMARY' ;

The solid way is to use information_schema:

可靠的方法是使用information_schema:

SELECT k.COLUMN_NAME
FROM information_schema.table_constraints t
LEFT JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
    AND t.table_schema=DATABASE()
    AND t.table_name='owalog';

#3


7  

For MySQL:

MySQL:

SELECT GROUP_CONCAT(COLUMN_NAME), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  TABLE_SCHEMA = '**database name**'
  AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;

Warning a primary key with two columns will have them separated by a coma (,)

警告具有两列的主键将被昏迷(,)分隔

#4


5  

Try this query in SQL server:

在SQL server中尝试这个查询:

SELECT     X.NAME AS INDEXNAME,
           COL_NAME(IC.OBJECT_ID,IC.COLUMN_ID) AS COLUMNNAME
FROM       SYS.INDEXES  X 
INNER JOIN SYS.INDEX_COLUMNS  IC 
        ON X.OBJECT_ID = IC.OBJECT_ID
       AND X.INDEX_ID = IC.INDEX_ID
WHERE      X.IS_PRIMARY_KEY = 1
  AND      OBJECT_NAME(IC.OBJECT_ID)='YOUR_TABLE'

#5


2  

The following query gives the list of all the primary keys in the given database.

下面的查询给出给定数据库中所有主键的列表。

SELECT DISTINCT TABLE_NAME ,column_name
    FROM INFORMATION_SCHEMA.key_column_usage
    WHERE TABLE_SCHEMA IN ('*your_db_name*');