Du kan ikke oprette en begrænsning for at kontrollere det med de eksisterende tabelstrukturer. En almindelig måde at gøre det på er sådan her:
create table loaner (
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
constraint loaner_uk unique (loan_id, loan_type)
);
create table office_worker (
worker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
worker_name varchar2(50),
constraint office_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint office_worker_loan_type_chk check (loan_type = 'OFFICE')
);
create table nonoffice_worker (
nonworker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
nonworker_name varchar2(50),
constraint nonoffice_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint nonoffice_worker_loan_type_chk check (loan_type = 'NONOFFICE')
);
Det vil sige:
- Opret en redundant UNIK begrænsning i (load_id, loan_type) i den første tabel.
- Tilføj lånetype til undertypetabellerne og baser fremmednøglen på (lån_id, låntype).
- Tilføj en kontrolbegrænsning til hver undertypetabel for at sikre, at den korrekte lånetype bruges.