MySQL SQL查询以查找表的主键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12379221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:48:09  来源:igfitidea点击:

SQL query to find Primary Key of a table?

mysqlsqlsql-serveroracle

提问by sky scraper

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

如何使用查询查找表的主键是哪一列?

采纳答案by Jo?o Silva

For Oracle, you can look it up in the ALL_CONSTRAINTStable:

对于 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 的回答

回答by ajon

This is a duplicate question:

这是一个重复的问题

credit to Lukmdo for this answer:

这个答案归功于 Lukmdo:

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

回答by Romuald Brunet

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;

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

警告具有两列的主键将用逗号 ( ,)分隔它们

回答by user1577417

Try this query in SQL server:

在 SQL 服务器中试试这个查询:

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'

回答by BhandariS

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

回答by Brian Wangombe

In mySQL

在我的SQL

SHOW COLUMNS FROM `table_name`;

This show the details of the columns ie field, data-type, key etc.

这显示了列的详细信息,即字段、数据类型、键等。

回答by Peter Lionhart

LEFT joining table_constraints seems to take more time on mine. i use Information_schema.COLUMNS

LEFT 加入 table_constraints 似乎需要更多时间在我身上。我使用 Information_schema.COLUMNS

SELECT TABLE_SCHEMA , TABLE_NAME , COLUMN_NAME FROM Information_schema.Columns WHERE COLUMN_KEY = "PRI" AND TABLE_SCHEMA = DATABASE()

SELECT TABLE_SCHEMA , TABLE_NAME , COLUMN_NAME FROM Information_schema.Columns WHERE COLUMN_KEY = "PRI" AND TABLE_SCHEMA = DATABASE()