如何在 Oracle 中获取主键列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9016578/
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
How to get primary key column in Oracle?
提问by Kirill A.
I need to get the name of the primary key column.
我需要获取主键列的名称。
In the input, I only have the table name.
在输入中,我只有表名。
回答by Richie
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;
Make sure that 'TABLE_NAME' is in upper case since Oracle stores table names in upper case.
确保 'TABLE_NAME' 是大写的,因为 Oracle 以大写形式存储表名。
回答by My-Name-Is
Same as the answer from 'Richie' but a bit more concise.
与'Richie'的答案相同,但更简洁一些。
Query for user constraints only
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' );
Query for all constraints
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' );
仅查询用户约束
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' );
查询所有约束
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' );
回答by Vipin Tiwari
Select constraint_name,constraint_type from user_constraints where table_name** **= ‘TABLE_NAME' ;
(This will list the primary key and then)
(这将列出主键,然后)
Select column_name,position from user_cons_cloumns where constraint_name='PK_XYZ';
(This will give you the column, here PK_XYZ is the primay key name)
(这会给你列,这里 PK_XYZ 是主键名)
回答by Kobir
Try This Code Here I created a table for get primary key column in oracle which is called test and then query
试试这个代码在这里我创建了一个表,用于在 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;
回答by Bugalugs Nash
Save the following script as something like findPK.sql.
将以下脚本另存为 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');
It can then be called using
然后可以使用调用它
@findPK