Sql 在Oracle中,当两个表都有两个主键时,如何从另一个表插入到一个表中?
我在两个数据库上有一个相同的应用程序表。我在一个数据库中有一个链接到另一个数据库。我已经填写了除Applies表之外的大部分数据(因此插入或连接没有错误): 我运行的命令是:Sql 在Oracle中,当两个表都有两个主键时,如何从另一个表插入到一个表中?,sql,oracle,insert,connection,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
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 ) );
到目前为止,我找到了一个解决方案:
/* 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;
我认为你应该先用索引重建 修改索引BKG988.1,申请人在线重建 然后继续插入。如果可能,不要禁用主键。或者,如果由于列中已有唯一ID而无法重建,请先删除