0

I have 3 tables is shown this tables:
table A

 A(
        code1
        .
        .
        .
    )

table B

B(
    code2
    .
    .
    .
)

table C

C(
    code3
    .
    .
    .
)

I want to be unique between code1,code2,code3 of this tables.
How do i recieve to my goal in oracle?
Does any syntax of oracle exist about this problem?

reza ramezani matin
  • 1,384
  • 2
  • 18
  • 41

3 Answers3

1

Based on materialized view.

P.s.
The code is not validated.
I currently have access only to oracle XE 11gR2, so I'm not able to use the materialized view log feature.


create table A   (code int primary key);
create table B   (code int primary key);
create table C   (code int primary key);

create materialized view log on a with primary key;
create materialized view log on b with primary key;
create materialized view log on c with primary key;

create materialized view ABC_MV
refresh fast
as
          select code from A
union all select code from B
union all select code from C
;

alter table ABC_MV add unique (code);
David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88
  • This is a clever solution. Do you know if it actually works in practice? Materialized views have different refresh options. It would seem that the constraint (if allowed) would break the materialized view but still allow the data to go into the underlying tables. – Gordon Linoff Nov 19 '16 at 14:54
  • @GordonLinoff, clever solution, but not mine. I saw it few weeks ago on SO but since it was a different use case involving a single table, I answered and didn't flag it as duplicate. The refresh on demand indeed causing the MV to break, but this is not surprising. I assume that refresh on commit will do the job but as I said I currently can't test it. – David דודו Markovitz Nov 19 '16 at 16:07
0

You can do that with trigger.

CREATE OR REPLACE TRIGGER table1_check
BEFORE INSERT OR UPDATE ON table1
FOR EACH ROW
BEGIN
    IF EXISTS (
        SELECT attribute FROM table2
        WHERE attribute = :NEW.attribute
    ) THEN
        RAISE_APPLICATION_ERROR(-20001,
        'Already exist in table2');
    END IF;
END;
/

You can modify it to check in more tables as well.

quantummind
  • 2,086
  • 1
  • 14
  • 20
-1

Using a 4th table


create table ABC (tab char(1) not null ,code int primary key, unique (tab,code));

create table A   (tab char(1) as ('A') virtual not null,code int primary key,foreign key (tab,code) references ABC(tab,code));
create table B   (tab char(1) as ('B') virtual not null,code int primary key,foreign key (tab,code) references ABC(tab,code));
create table C   (tab char(1) as ('C') virtual not null,code int primary key,foreign key (tab,code) references ABC(tab,code));

insert into ABC (tab,code) values ('A',1);
insert into A (code) values (1);
insert into A (code) values (1);

[Code: 1, SQL State: 23000] ORA-00001: unique constraint (SYS.SYS_C0012834) violated

insert into B (code) values (1);

[Code: 2291, SQL State: 23000] ORA-02291: integrity constraint (SYS.SYS_C0012839) violated - parent key not found

David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88