You’re probably familiar with the ON DELETE CASCADE option in a foreign key. For example, if I have DEPT and EMP as per below
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from emp order by deptno, empno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
14 rows selected.
And I add a foreign key from EMP back to DEPT with the ON DELETE CASCADE option.
SQL> alter table emp add constraint emp_fk
2 foreign key ( deptno)
3 references dept (deptno)
4 on delete cascade;
Table altered.
Then as you would expect, when I go ahead and delete department 10, all the employees for department 10 also vanish
SQL> delete from dept where deptno = 10;
1 row deleted.
SQL> select * from emp order by deptno, empno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
11 rows selected.
But did you know that we also have an alternative to ON DELETE CASCADE and that is ON DELETE SET NULL. With this option, deleting the parent row will not delete the children rows, it will simply erase their reference back to the parent.
SQL> alter table emp add constraint emp_fk
2 foreign key ( deptno)
3 references dept (deptno)
4 on delete set null;
Table altered.
SQL> delete from dept where deptno = 20;
1 row deleted.
SQL> select * from emp order by deptno, empno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7369 SMITH CLERK 7902 17-DEC-80 800 <null>
7566 JONES MANAGER 7839 02-APR-81 2975 <null>
7788 SCOTT ANALYST 7566 09-DEC-82 3000 <null>
7876 ADAMS CLERK 7788 12-JAN-83 1100 <null>
7902 FORD ANALYST 7566 03-DEC-81 3000 <null>
11 rows selected.
SQL>
As a rule of thumb, I generally prefer not to use either. I would rather my code explicitly manages this and the referential integrity constraint is used to guarantee the correctness of my data.
Ho Ho Ho! Merry Christmas.




Leave a Reply