Создание ограничения внешнего ключа с Oracle - PullRequest
0 голосов
/ 13 января 2019

Я пытаюсь создать ограничение между таблицей mail.id и таблицей disposition.mailid, вот мой синтаксис

alter table disposition add foreign key (mailid) references mail(id) on cascade delete;

проблема в том, что я получаю эту ошибку

alter table disposition add foreign key (mailid) references mail(id) on cascade delete
ORA-00905: missing keyword

Time: 0,002s

или есть простой способ установить ограничение между двумя таблицами в navicat?

1 Ответ

0 голосов
/ 13 января 2019

Решение: {1} Используйте ключевое слово CONSTRAINT, {2} присвойте имени ограничение, {3} напишите: ON DELETE CASCADE. Пример (Oracle 12c):

create table mail ( id number primary key ) ;
-- Table MAIL created.

create table disposition ( id number primary key, mailid number ) ;
-- Table DISPOSITION created.

alter table disposition 
add constraint disposition_fk 
foreign key (mailid) references mail(id) on delete cascade ;
-- Table DISPOSITION altered.

Тестирование

-- table MAIL: insert 5 ids 
insert into mail ( id )
select level from dual connect by level <= 5 ;

-- table DISPOSITION: mail ids 1-5 okay, 6 etc not accepted
begin
  insert into disposition ( id, mailid ) values ( 101, 1 ) ;
  insert into disposition ( id, mailid ) values ( 102, 2 ) ;
  insert into disposition ( id, mailid ) values ( 103, 3 ) ;
  insert into disposition ( id, mailid ) values ( 104, 4 ) ;
  insert into disposition ( id, mailid ) values ( 105, 5 ) ;
end ;
/
-- PL/SQL procedure successfully completed.

insert into disposition ( id, mailid ) values ( 106, 6 ) ;
-- ORA-02291: integrity constraint (...DISPOSITION_FK) violated - parent key not found
...