我可以通过Oracle ALL_TAB_COLS视图查看列是否是主键吗?

时间:2011-08-24 04:04:16

标签: oracle

我可以通过Oracle的ALL_TAB_COLS视图查看列是主键还是外键?

select * 
from all_tab_cols 
where table_name = 'ALL_TAB_COLS'
;

1   SYS ALL_TAB_COLS    OWNER   VARCHAR2            30          N   1       <Long>                                  CHAR_CS 30  NO  NO      30  B   NO  YES NO  NO  1   1
2   SYS ALL_TAB_COLS    TABLE_NAME  VARCHAR2            30          N   2       <Long>                                  CHAR_CS 30  NO  NO      30  B   NO  YES NO  NO  2   2
3   SYS ALL_TAB_COLS    COLUMN_NAME VARCHAR2            30          N   3       <Long>                                  CHAR_CS 30  NO  NO      30  B   NO  YES NO  NO  3   3
4   SYS ALL_TAB_COLS    DATA_TYPE   VARCHAR2            106         Y   4       <Long>                                  CHAR_CS 106 NO  NO      106 B   NO  YES NO  NO  4   4
5   SYS ALL_TAB_COLS    DATA_TYPE_MOD   VARCHAR2            3           Y   5       <Long>                                  CHAR_CS 3   NO  NO      3   B   NO  YES NO  NO  5   5
6   SYS ALL_TAB_COLS    DATA_TYPE_OWNER VARCHAR2            30          Y   6       <Long>                                  CHAR_CS 30  NO  NO      30  B   NO  YES NO  NO  6   6
7   SYS ALL_TAB_COLS    DATA_LENGTH NUMBER          22          N   7       <Long>                                          NO  NO      0       NO  YES NO  NO  7   7
8   SYS ALL_TAB_COLS    DATA_PRECISION  NUMBER          22          Y   8       <Long>                                          NO  NO      0       NO  YES NO  NO  8   8
9   SYS ALL_TAB_COLS    DATA_SCALE  NUMBER          22          Y   9       <Long>                                          NO  NO      0       NO  YES NO  NO  9   9
10  SYS ALL_TAB_COLS    NULLABLE    VARCHAR2            1           Y   10      <Long>                                  CHAR_CS 1   NO  NO      1   B   NO  YES NO  NO  10  10
11  SYS ALL_TAB_COLS    COLUMN_ID   NUMBER          22          Y   11      <Long>                                          NO  NO      0       NO  YES NO  NO  11  11
12  SYS ALL_TAB_COLS    DEFAULT_LENGTH  NUMBER          22          Y   12      <Long>                                          NO  NO      0       NO  YES NO  NO  12  12
13  SYS ALL_TAB_COLS    DATA_DEFAULT    LONG            0           Y   13      <Long>                                  CHAR_CS 0   NO  NO      0       NO  YES NO  NO  13  13
14  SYS ALL_TAB_COLS    NUM_DISTINCT    NUMBER          22          Y   14      <Long>                                          NO  NO      0       NO  YES NO  NO  14  14
15  SYS ALL_TAB_COLS    LOW_VALUE   RAW         32          Y   15      <Long>                                          NO  NO      0       NO  YES NO  NO  15  15
16  SYS ALL_TAB_COLS    HIGH_VALUE  RAW         32          Y   16      <Long>                                          NO  NO      0       NO  YES NO  NO  16  16
17  SYS ALL_TAB_COLS    DENSITY NUMBER          22          Y   17      <Long>                                          NO  NO      0       NO  YES NO  NO  17  17
18  SYS ALL_TAB_COLS    NUM_NULLS   NUMBER          22          Y   18      <Long>                                          NO  NO      0       NO  YES NO  NO  18  18
19  SYS ALL_TAB_COLS    NUM_BUCKETS NUMBER          22          Y   19      <Long>                                          NO  NO      0       NO  YES NO  NO  19  19

3 个答案:

答案 0 :(得分:6)

否 - 您不能,因为此信息不属于该视图,请参阅http://download.oracle.com/docs/cd/B13789_01/server.101/b10755/statviews_1179.htm

要查找表格的主键列,请参阅http://www.techonthenet.com/oracle/questions/find_pkeys.php - 信息位于视图all_constraints + all_cons_columns中。

要查找外键信息,请尝试:

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

答案 1 :(得分:2)

以下Oracle DB查询将列出当前架构中存在的所有此类表名称,这些表名称具有声明为主键/复合键的给定列(YOUR_COLUMN_NAME):

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

答案 2 :(得分:0)

我这样做是为了确定列是否是主键的一部分(假设主键没有给出精美的名称):

select a.*, b.CONSTRAINT_NAME
from ALL_TAB_COLS a
left join 
  (select CONSTRAINT_NAME,table_name,column_name from ALL_CONS_COLUMNS 
  where OWNER= '<USER_YOU_WANT>'
  and (CONSTRAINT_NAME like '%PK'
  or CONSTRAINT_NAME like 'PK%'
  or CONSTRAINT_NAME like '%PK%')
  and CONSTRAINT_NAME not like 'BIN%') B on a.COLUMN_NAME=B.COLUMN_NAME and a.TABLE_NAME=B.TABLE_NAM 
WHERE a.OWNER='<USER_YOU_WANT>'
;