如何在Oracle中获取主键列?


107

我需要获取主键列的名称。

在输入中,我只有表名。

Answers:


189
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'TABLE_NAME'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;

由于Oracle将表名存储为大写,因此请确保“ TABLE_NAME”为大写。


谢谢,里奇。还有一个问题:如何编码“ P”?我需要使用“立即执行”执行此操作。再次感谢。
Kirill A.

4
我想您会像这样“ P”将两个单引号括起来
Greg Reynolds 2012年

或者,您可以使用来引用它q
Burhan Ali 2014年

和constraint_type ='R'作为外键
Abinash Bishoyi 2015年

在表前没有架构前缀时为我工作!谢谢!
FearlessFuture

19

与“理智”的答案相同,但更为简洁。

  1. 仅查询用户约束

    SELECT column_name FROM all_cons_columns WHERE constraint_name = (
      SELECT constraint_name FROM user_constraints 
      WHERE UPPER(table_name) = UPPER('tableName') AND CONSTRAINT_TYPE = 'P'
    );
  2. 查询所有约束

    SELECT column_name FROM all_cons_columns WHERE constraint_name = (
      SELECT constraint_name FROM all_constraints 
      WHERE UPPER(table_name) = UPPER('tableName') AND CONSTRAINT_TYPE = 'P'
    );

@FearlessFuture对我来说效果很好。您能否将您的问题描述得更具表现力?
我的名字是

我没有从该查询中得到任何结果,但是从查询中得到了可接受答案的结果。
FearlessFuture

2
@FearlessFuture我假设您要搜索的约束不是用户约束。替换user_constraintsall_constraints
我的名字是

如果您的表在两个或多个模式中具有相同的名称,则会引起麻烦-需要在连接中也包含所有者: SELECT owner, column_name, position FROM all_cons_columns WHERE (owner, constraint_name) in (SELECT owner, constraint_name FROM all_constraints WHERE UPPER(table_name) = UPPER('&tableName') AND CONSTRAINT_TYPE = 'P') order by owner, position;
Mark Stewart

1
@MarkStewart我同意第二个查询将不起作用。但是您的解决方案效率低下。尝试:“ SELECT cols.column_name AS KEY_STREAM,'='AS KEY_CONDITION,cols.column_name作为KEY_LOOKUP,” AS KEY_STREAM2 FROM user_constraints cons,user_cons_columns cols WHERE cons.constraint_name = cols.constraint_name和contable.int = ='<TABLE_NAME>'和cons.table_name = cols.table_name;”
Alexander Heim

2
Select constraint_name,constraint_type from user_constraints where table_name** **= TABLE_NAME ;

(这将列出主键,然后)

Select column_name,position from user_cons_cloumns where constraint_name=’PK_XYZ’; 

(这将为您提供该列,此处PK_XYZ是主键名称)


1

尝试此代码在这里,我创建了一个表来获取oracle中的主键列,该表称为test,然后进行查询

create table test
(
id int,
name varchar2(20),
city varchar2(20),
phone int,
constraint pk_id_name_city primary key (id,name,city)
);

SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cols.table_name = 'TEST' AND cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner  ORDER BY cols.table_name, cols.position;

0

将以下脚本另存为findPK.sql。

set verify off
accept TABLE_NAME char prompt 'Table name>'

SELECT cols.column_name
FROM all_constraints cons NATURAL JOIN all_cons_columns cols
WHERE cons.constraint_type = 'P' AND table_name = UPPER('&TABLE_NAME');

然后可以使用

@findPK
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.