【问题标题】:How to insert into a table from another table when both have two primary keys in Oracle?当Oracle中都有两个主键时,如何从另一个表中插入一个表?
【发布时间】:2014-06-09 08:56:14
【问题描述】:

我在两个数据库上有一个相同的应用表。我在一个数据库中有一个链接到另一个数据库。我已经填写了大部分数据,除了应用表(因此插入或连接没有错误):

我运行的命令是:

CREATE SYNONYM APP FOR Applies@"DB.DATA-PC10";
insert into Applies select *  from APP where APP.a# in  ( select a# from Applicant) and APP.p# in  ( select p# from Position);

我收到的错误是:

ERROR at line 1:
ORA-01502: index 'BKG988.APPLICANT_PKEY' or partition of such index is in unusable state

我尝试在双方临时禁用PK:

 alter table applies disable constraint applies_pkey;
 Table altered.

但我仍然得到同样的错误。感谢有人给我解决方案:

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 
CREATE TABLE Applies(
a#      NUMBER(6)   NOT NULL, /* Applicant number       */
p#      NUMBER(8)   NOT NULL, /* Position number        */
appdate     DATE        NOT NULL, /* Application date       */
    CONSTRAINT Applies_pkey PRIMARY KEY ( a#, p# ), 
    CONSTRAINT Applies_fkey1 FOREIGN KEY ( a# )
                REFERENCES Applicant ( a# )
                ON DELETE CASCADE,
    CONSTRAINT Applies_fkey2 FOREIGN KEY ( p# )
                REFERENCES Position ( p# ) 
                ON DELETE CASCADE);

还有一个位置表:

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 
CREATE TABLE Position(
p#              NUMBER(8)       NOT NULL, /* Position number            */
ptitle          VARCHAR(30)     NOT NULL, /* Position title             */
employer    VARCHAR(100)    NOT NULL, /* Institution name           */
salary      NUMBER(9,2) NOT NULL, /* Salary         */
extras      VARCHAR(50)     , /* Extras         */
specification   LONG                , /* Specification      */
    CONSTRAINT Position_pkey PRIMARY KEY ( p# ),
    CONSTRAINT Position_fkey1 FOREIGN KEY ( ptitle )
                REFERENCES LPTitle ( title ) );

这是申请人的表格:

CREATE TABLE Applicant(
a#              NUMBER(6)       NOT NULL, /* Staff number               */
fname           VARCHAR(20)     NOT NULL, /* First name                 */
lname       VARCHAR(30) NOT NULL, /* Last name          */
address         VARCHAR(50)     NOT NULL, /* Street, home number, etc.  */
city        VARCHAR(30) NOT NULL, /* City           */
state       VARCHAR(20) NOT NULL, /* State          */
phone#      NUMBER(10)  NOT NULL, /* Phone number       */
fax#        NUMBER(10)      , /* Fax number         */
email       VARCHAR(50)     , /* E-mail address     */
acomment    LONG            ,  /* Interesting comments from interviews */
    CONSTRAINT Applicant_pkey PRIMARY KEY ( a# ),
    CONSTRAINT Applicant_fkey1 FOREIGN KEY ( state )
                REFERENCES LState ( state ) );

【问题讨论】:

    标签: sql oracle insert connection


    【解决方案1】:

    到目前为止,我找到了一种解决方案:

    /* Other option is to define the table DEFERRABLE  accrding to Q/A Tom in https://asktom.oracle.com/pls/asktom/f?p=100:11:0%3a%3a%3a%3aP11_QUESTION_ID:8806498660292*/
    Alter table applies disable constraint applies_pkey;
    Alter table applies disable constraint Applies_fkey1;
    Alter table applies disable constraint Applies_fkey2;
    
    CREATE SYNONYM APP FOR Applies@"DB.DATA-PC10";
    insert into Applies select *  from APP where APP.a# in  ( select a# from Applicant) and APP.p# in  ( select p# from Position);
    
    
    Alter table applies enable constraint applies_pkey;
    Alter table applies enable constraint Applies_fkey1;
    Alter table applies enable constraint Applies_fkey2;
    

    【讨论】:

      【解决方案2】:

      我认为您应该先进行索引重建。

      alter index BKG988.APPLICANT_PKEY 在线重建;

      然后继续您的插入。如果可能,不要禁用主键。或者,如果由于列中已有唯一 ID 而无法重建,请先删除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-06
        • 2016-06-11
        • 2023-03-17
        • 1970-01-01
        • 2013-12-14
        相关资源
        最近更新 更多