外键引用新架构

时间:2018-01-19 00:34:44

标签: sql oracle

我想将一些表移动到新架构。当我移动它时,我在外键引用时遇到错误。我也试过给予权限。 Accounts是地址表所在的旧架构,我将地址移动到名为address的新架构。我得到错误表或视图不存在:

CREATE TABLE ACCOUNTS.PARTY_ADDRESS
(
 PARTY_ID NUMBER(18,0),
 ADDRESS_ID NUMBER(18,0),
 CONSTRAINT PARTY_ADDRESS_PK PRIMARY KEY (PARTY_ID, ADDRESS_ID),
 constraint PARTY_ADDRESS_party_fk foreign key (PARTY_ID)
 references PARTY(id),
 constraint PARTY_ADDRESS_ADDRESS_UNQ unique (ADDRESS_ID)
);

ALTER TABLE ACCOUNTS.PARTY_ADDRESS ADD CONSTRAINT PARTY_ADDRESS_address_fk FOREIGN KEY (ADDRESS_ID) REFERENCES ADDRESS.ADDRESS(id);

我还在地址中给予了补助金:

GRANT ALL ON ADDRESS.ADDRESS TO ACCOUNTS;

1 个答案:

答案 0 :(得分:1)

表特权REFERENCES是模式能够为另一个模式中的表创建引用约束所必需的。 GRANT ALL应该有效。在我的测试案例中,它确实有效,例如:

架构1:

create table schema1.t1 (id number primary key);

Table T1 created.

架构2:

create table schema2.t2 (id number primary key, fk number);

Table T2 created.

alter table schema2.t2 add constraint fktest
  foreign key (fk) references schema1.t1 (id);

ORA-00942: table or view does not exist

架构1:

grant references on schema1.t1 to schema2;

Grant succeeded.

架构2:

alter table schema2.t2 add constraint fktest
  foreign key (fk) references schema1.t1 (id);

Table T2 altered.

架构1:

revoke references on schema1.t1 from schema2;

ORA-01981: CASCADE CONSTRAINTS must be specified to perform this revoke

revoke references on schema1.t1 from schema2 cascade constraints;

Revoke succeeded.

如果你GRANT ALL

,它也有效

架构1:

grant all on schema1.t1 to schema2;

Grant succeeded.

架构2:

alter table schema2.t2 add constraint fktest
  foreign key (fk) references schema1.t1 (id);

Table T2 altered.