在Oracle中获取主键列的问题

时间:2015-08-05 06:00:57

标签: oracle

这是一个常见问题,我发现很少有与此相关的链接

Link1

link2

Link 3

在我的情况下,这些查询都没有返回值。可能的问题是什么?

    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.table_name LIKE '%WORKORDERSPEC%'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
AND cons.owner =  'DEV_WWM_WEBAPP_SIT1' 
ORDER BY cols.table_name, cols.position;

以下是创建表DDL

CREATE TABLE DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC
  (
    WORKORDERSPECID NUMBER NOT NULL ,
    ALNVALUE        VARCHAR2(4000 CHAR) ,
    ASSETATTRID     VARCHAR2(100 BYTE) NOT NULL ,
    WONUM           VARCHAR2(15 BYTE) NOT NULL ,
    CHANGEBY        VARCHAR2(30 BYTE) NOT NULL ,
    CHANGEDATE DATE NOT NULL ,
    CLASSSTRUCTUREID    VARCHAR2(20 BYTE) NOT NULL ,
    DISPLAYSEQUENCE     NUMBER NOT NULL ,
    LINKEDTOATTRIBUTE   VARCHAR2(100 BYTE) ,
    LINKEDTOSECTION     VARCHAR2(20 BYTE) ,
    MEASUREUNITID       VARCHAR2(25 BYTE) ,
    NUMVALUE            NUMBER(30, 10) ,
    ORGID               VARCHAR2(8 BYTE) NOT NULL ,
    SECTION             VARCHAR2(20 BYTE) ,
    SITEID              VARCHAR2(8 BYTE) NOT NULL ,
    REFOBJECTID         NUMBER NOT NULL ,
    REFOBJECTNAME       VARCHAR2(30 BYTE) ,
    CLASSSPECID         NUMBER ,
    MANDATORY           NUMBER NOT NULL ,
    TABLEVALUE          VARCHAR2(254 BYTE) ,
    ROWSTAMP            VARCHAR2(40 BYTE) NOT NULL ,
    GBSREQDCONDITIONNUM VARCHAR2(30 BYTE) ,
    GBSROCONDITIONNUM   VARCHAR2(30 BYTE) ,
    NBNVALUEREMOVED     NUMBER DEFAULT NULL NOT NULL ,
    NBNTARGETOBJECT     VARCHAR2(30 CHAR) ,
    NBNTARGETATTRIBUTE  VARCHAR2(50 CHAR) ,
    GBSSPECDATETIMEVALUE DATE ,
    GBSSPECDATEVALUE DATE
  )
  LOGGING TABLESPACE "MAXDATA_SIT1" PCTFREE 10 INITRANS 1 STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 BUFFER_POOL DEFAULT
  )
  CREATE UNIQUE INDEX DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC_NDX ON DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC
  (
    WORKORDERSPECID ASC
  )
  LOGGING TABLESPACE "MAXDATA_SIT1" PCTFREE 10 INITRANS 2 STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 BUFFER_POOL DEFAULT
  )
  CREATE UNIQUE INDEX DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC_NDX1 ON DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC
  (
    WONUM ASC,
    SITEID ASC,
    ASSETATTRID ASC,
    SECTION ASC
  )
  LOGGING TABLESPACE "MAXDATA_SIT1" PCTFREE 10 INITRANS 2 STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 BUFFER_POOL DEFAULT
  )
CREATE INDEX DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC_NDX2 ON DEV_WWM_WEBAPP_SIT1.WORKORDERSPEC
  (
    REFOBJECTID ASC
  )
  LOGGING TABLESPACE "MAXDATA_SIT1" PCTFREE 10 INITRANS 2 STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 BUFFER_POOL DEFAULT
  )

希望这有帮助

2 个答案:

答案 0 :(得分:3)

您的查询正在搜索主键约束:

WHERE cons.constraint_type = 'P'

但是您的DDL显示没有创建主键约束。您只显示了表和某些索引的DDL。

唯一索引不会创建主键约束 - 但是,主键约束(通常)会创建唯一索引。

答案 1 :(得分:0)

如果存在约束类型= to' P'也许这个查询会起作用。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE WORKORDERSPEC
  (
    WORKORDERSPECID NUMBER NOT NULL ,
    ALNVALUE        VARCHAR2(4000 CHAR) ,
    ASSETATTRID     VARCHAR2(100 BYTE) NOT NULL ,
    WONUM           VARCHAR2(15 BYTE) NOT NULL ,
    CHANGEBY        VARCHAR2(30 BYTE) NOT NULL ,
    CHANGEDATE DATE NOT NULL ,
    CLASSSTRUCTUREID    VARCHAR2(20 BYTE) NOT NULL ,
    DISPLAYSEQUENCE     NUMBER NOT NULL ,
    LINKEDTOATTRIBUTE   VARCHAR2(100 BYTE) ,
    LINKEDTOSECTION     VARCHAR2(20 BYTE) ,
    MEASUREUNITID       VARCHAR2(25 BYTE) ,
    NUMVALUE            NUMBER(30, 10) ,
    ORGID               VARCHAR2(8 BYTE) NOT NULL ,
    SECTION             VARCHAR2(20 BYTE) ,
    SITEID              VARCHAR2(8 BYTE) NOT NULL ,
    REFOBJECTID         NUMBER NOT NULL ,
    REFOBJECTNAME       VARCHAR2(30 BYTE) ,
    CLASSSPECID         NUMBER ,
    MANDATORY           NUMBER NOT NULL ,
    TABLEVALUE          VARCHAR2(254 BYTE) ,
    ROWSTAMP            VARCHAR2(40 BYTE) NOT NULL ,
    GBSREQDCONDITIONNUM VARCHAR2(30 BYTE) ,
    GBSROCONDITIONNUM   VARCHAR2(30 BYTE) ,
    NBNVALUEREMOVED     NUMBER DEFAULT NULL NOT NULL ,
    NBNTARGETOBJECT     VARCHAR2(30 CHAR) ,
    NBNTARGETATTRIBUTE  VARCHAR2(50 CHAR) ,
    GBSSPECDATETIMEVALUE DATE ,
    GBSSPECDATEVALUE DATE
  )
;
   CREATE UNIQUE INDEX WORKORDERSPEC_NDX ON WORKORDERSPEC
  (
    WORKORDERSPECID ASC
    )
;
    CREATE UNIQUE INDEX WORKORDERSPEC_NDX1 ON WORKORDERSPEC
  (
    WONUM ASC,
    SITEID ASC,
    ASSETATTRID ASC,
    SECTION ASC
  )
;
    CREATE INDEX WORKORDERSPEC_NDX2 ON WORKORDERSPEC
  (
    REFOBJECTID ASC
  )
;

查询1

select OWNER , CONSTRAINT_NAME , CONSTRAINT_TYPE ,  TABLE_NAME 
from user_constraints

<强> Results

|        OWNER | CONSTRAINT_NAME | CONSTRAINT_TYPE |    TABLE_NAME |
|--------------|-----------------|-----------------|---------------|
| USER_4_78AA2 |  SYS_C001048129 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048128 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048127 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048126 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048125 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048124 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048123 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048122 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048121 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048120 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048119 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048118 |               C | WORKORDERSPEC |
| USER_4_78AA2 |  SYS_C001048117 |               C | WORKORDERSPEC |