简体   繁体   English

当两个表在Oracle中都有两个主键时,如何从另一个表插入表?

[英]How to insert into a table from another table when both have two primary keys in Oracle?

I have an identical table of applies on two databases. 我在两个数据库上有相同的表。 I have a link in one of databases to another one. 我有一个数据库链接到另一个数据库。 I have filled up most of my data, except Applies table (So there is no error with insertion or connection): 我已经填满了我的大部分数据,但“适用”表除外(因此插入或连接没有错误):

The command I run is: 我运行的命令是:

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);

The error I receive is: 我收到的错误是:

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

I tried to disable PK temporary on both sides: 我试图在两侧禁用PK临时:

 alter table applies disable constraint applies_pkey;
 Table altered.

But still I get same error. 但是我仍然遇到同样的错误。 Appreciate if anyone give me a solution: 感谢有人给我解决方案:

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 
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);

and a table of Position: 和位置表:

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 
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 ) );

And here is table of applicant: 这是申请人表:

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 ) );

So far I have found one solution which is: 到目前为止,我已经找到了一种解决方案:

/* 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;

I think you should go with index rebuild first. 我认为您应该首先进行索引重建。

alter index BKG988.APPLICANT_PKEY rebuild online; 在线更改索引BKG988.APPLICANT_PKEY;

Then go with your insertion. 然后插入。 If possible don't disable primary key. 如果可能,请不要禁用主键。 Or if you are not able to rebuild due to already having unique ids in the column then please remove first. 或者,如果由于列中已有唯一的ID而无法重建,请先删除。

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

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