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

时间:2014-06-09 08:56:15

标签: sql oracle insert connection

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

我运行的命令是:

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

更改索引BKG988.APPLICANT_PKEY在线重建;

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