简体   繁体   English

如何知道外键在Oracle SQL Developer中引用到哪些列?

[英]How to know to which columns a foreign key is referencing in Oracle SQL Developer?

I can check what the columns in a foreign key are that are referencing but how do I know which columns this foreign key is referencing in the other table? 我可以检查外键中正在引用的列,但是我如何知道该外键在另一个表中引用的列呢? Since, it doesn't have to be the primary keys of a table. 既然如此,它不必是表的主键。 Is there an easy way to do this on Oracle SQL Developer without executing any query? 有没有一种简单的方法可以在Oracle SQL Developer上执行此操作而不执行任何查询?

You could join the user_cons_columns and user_constraints views to get the metadata information for the primary key and foreign key references. 您可以加入user_cons_columnsuser_constraints视图以获取主键和外键引用的元数据信息。

For example, 例如,

SQL> COLUMN table_name format a10
SQL> COLUMN column_name format a11
SQL> COLUMN constraint_name format a20
SQL> COLUMN owner format a10
SQL> COLUMN ref_table_name format a15
SQL> COLUMN ref_pk format a10
SQL> SELECT a.table_name,
  2    a.column_name,
  3    a.constraint_name,
  4    b.owner,
  5    c_pk.table_name ref_table_name,
  6    c_pk.constraint_name ref_pk
  7  FROM user_cons_columns a
  8  JOIN user_constraints b
  9  ON a.owner            = b.owner
 10  AND a.constraint_name = b.constraint_name
 11  JOIN user_constraints c_pk
 12  ON b.r_owner            = c_pk.owner
 13  AND b.r_constraint_name = c_pk.constraint_name
 14  WHERE b.constraint_type = 'R';

TABLE_NAME COLUMN_NAME CONSTRAINT_NAME      OWNER      REF_TABLE_NAME  REF_PK
---------- ----------- -------------------- ---------- --------------- ----------
EMP        DEPTNO      FK_DEPTNO            SCOTT      DEPT            PK_DEPT

SQL>

You could also use DBMS_METADATA.GET_DDL to generate the DDL for the table. 您也可以使用DBMS_METADATA.GET_DDL为表生成DDL。 It will have complete table information. 它将具有完整的表信息。

For example, 例如,

SQL> set long 200000 pages 0 lines 131
SQL> column txt format a121 word_wrapped
SQL> select dbms_metadata.get_ddl('TABLE', 'EMP') from dual;

  CREATE TABLE "SCOTT"."EMP"
   (    "EMPNO" NUMBER(4,0),
        "ENAME" VARCHAR2(10),
        "JOB" VARCHAR2(9),
        "MGR" NUMBER(4,0),
        "HIREDATE" DATE,
        "SAL" NUMBER(7,2),
        "COMM" NUMBER(7,2),
        "DEPTNO" NUMBER(2,0),
         CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"  ENABLE,
         CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

Without executing any query, Yes. 不执行任何查询,是。

Open the table editor. 打开表格编辑器。

在此处输入图片说明

If you don't see the Model page, upgrade to version 4.1 如果没有看到“模型”页面,请升级到版本4.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM