CONSTRAINTS: ON DELETE CASCADE
La regola ON DELETE CASCADE è utilizzata a livello di Foreign Key.
Permette di eseguire l'istruzione DELETE di righe di una tabella padre referenziata senza dover cancellare prima le corrispondenti righe della tabella figlio.
Ogni DELETE delle righe della tabella padre cancella anche le corrispondenti righe della tabella figlio.
E s e m p i o
CREATE TABLE ESEMPIO1
(
IDESE1 number,
NOME varchar2(10 byte),
COGNOME varchar2(10 byte),
constraint ESE1_PK primary key (IDESE1)
);
198 Donald OConnell
199 Douglas Grant
200 Jennifer Whalen
201 Michael Hartstein
202 Pat Fay
203 Susan Mavris
204 Hermann Baer
205 Shelley Higgins
206 William Gietz
CREATE TABLE ESEMPIO2
(
IDESE2 number,
NOME varchar2(10 byte),
COGNOME varchar2(10 byte),
constraint ESE2_PK FOREIGN KEY (IDESE2) REFERENCES ESEMPIO1(IDESE1)
);
204 ANNA Baer
202 LISA Fay
206 GREC Gietz
199 PHIL Grant
201 DONALD Hartstein
205 Shelley Higgins
203 Susan Mavris
198 MARY OCONNELL
200 Jenni Whalen
DELETE FROM ESEMPIO1 where IDESE1 = 198;
Errore SQL: ORA-02292: integrity constraint (HR.ESE2_PK) violated - child record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign dependency.
*Action: delete dependencies first then parent or disable constraint.
Occorre eseguire il drop della Constraint e ricrearla.
ALTER TABLE ESEMPIO2 DROP CONSTRAINT ESE2_PK ;
Modifichiamo la tabella figlio aggiungendo la suddetta regola:
ALTER TABLE ESEMPIO2 ADD CONSTRAINT ESE2_PK FOREIGN KEY (IDESE2) REFERENCES ESEMPIO1(IDESE1) ON DELETE CASCADE;
Se rieseguo la delete seguente:
DELETE FROM ESEMPIO1 where IDESE1 = 198;
1 righe eliminato.
Anche in tabella ESEMPIO2 viene cancellata la riga con IDESE2 = 198.
Permette di eseguire l'istruzione DELETE di righe di una tabella padre referenziata senza dover cancellare prima le corrispondenti righe della tabella figlio.
Ogni DELETE delle righe della tabella padre cancella anche le corrispondenti righe della tabella figlio.
E s e m p i o
CREATE TABLE ESEMPIO1
(
IDESE1 number,
NOME varchar2(10 byte),
COGNOME varchar2(10 byte),
constraint ESE1_PK primary key (IDESE1)
);
198 Donald OConnell
199 Douglas Grant
200 Jennifer Whalen
201 Michael Hartstein
202 Pat Fay
203 Susan Mavris
204 Hermann Baer
205 Shelley Higgins
206 William Gietz
CREATE TABLE ESEMPIO2
(
IDESE2 number,
NOME varchar2(10 byte),
COGNOME varchar2(10 byte),
constraint ESE2_PK FOREIGN KEY (IDESE2) REFERENCES ESEMPIO1(IDESE1)
);
204 ANNA Baer
202 LISA Fay
206 GREC Gietz
199 PHIL Grant
201 DONALD Hartstein
205 Shelley Higgins
203 Susan Mavris
198 MARY OCONNELL
200 Jenni Whalen
DELETE FROM ESEMPIO1 where IDESE1 = 198;
Errore SQL: ORA-02292: integrity constraint (HR.ESE2_PK) violated - child record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign dependency.
*Action: delete dependencies first then parent or disable constraint.
Occorre eseguire il drop della Constraint e ricrearla.
ALTER TABLE ESEMPIO2 DROP CONSTRAINT ESE2_PK ;
Modifichiamo la tabella figlio aggiungendo la suddetta regola:
ALTER TABLE ESEMPIO2 ADD CONSTRAINT ESE2_PK FOREIGN KEY (IDESE2) REFERENCES ESEMPIO1(IDESE1) ON DELETE CASCADE;
Se rieseguo la delete seguente:
DELETE FROM ESEMPIO1 where IDESE1 = 198;
1 righe eliminato.
Anche in tabella ESEMPIO2 viene cancellata la riga con IDESE2 = 198.