Example: DELETE rules

 

Suppose that deleting a department from the DEPARTMENT table sets WORKDEPT in the EMPLOYEE table to null for every employee assigned to that department.

Consider the following DELETE statement:

   DELETE FROM CORPDATA.DEPARTMENT            WHERE DEPTNO = 'E11'

Given the tables and the data in the DB2 Universal Database for iSeries sample tables, one row is deleted from table DEPARTMENT, and table EMPLOYEE is updated to set the value of WORKDEPT to its default wherever the value was 'E11'. A question mark ('?') in the following sample data reflects the null value. The results appear as follows:

Table 1. DEPARTMENT table. Contents of the table after the DELETE statement is complete.
DEPTNO DEPTNAME MGRNO ADMRDEPT
A00 SPIFFY COMPUTER SERVICE DIV. 000010 A00
B01 PLANNING 000020 A00
C01 INFORMATION CENTER 000030 A00
D01 DEVELOPMENT CENTER ? A00
D11 MANUFACTURING SYSTEMS 000060 D01
D21 ADMINISTRATION SYSTEMS 000070 D01
E01 SUPPORT SERVICES 000050 A00
E21 SOFTWARE SUPPORT 000100 E01
F22 BRANCH OFFICE F2 ? E01
G22 BRANCH OFFICE G2 ? E01
H22 BRANCH OFFICE H2 ? E01
I22 BRANCH OFFICE I2 ? E01
J22 BRANCH OFFICE J2 ? E01

Note that there were no cascaded delete operations in the DEPARTMENT table because no department reported to department 'E11'.

Below are the snapshots of one affected portion of the EMPLOYEE table before and after the DELETE statement is completed.

Table 2. Partial EMPLOYEE table. Partial contents before the DELETE statement.
EMPNO FIRSTNME MI LASTNAME WORKDEPT PHONENO HIREDATE
000230 JAMES J JEFFERSON D21 2094 1966-11-21
000240 SALVATORE M MARINO D21 3780 1979-12-05
000250 DANIEL S SMITH D21 0961 1960-10-30
000260 SYBIL P JOHNSON D21 8953 1975-09-11
000270 MARIA L PEREZ D21 9001 1980-09-30
000280 ETHEL R SCHNEIDER E11 0997 1967-03-24
000290 JOHN R PARKER E11 4502 1980-05-30
000300 PHILIP X SMITH E11 2095 1972-06-19
000310 MAUDE F SETRIGHT E11 3332 1964-09-12
000320 RAMLAL V MEHTA E21 9990 1965-07-07
000330 WING   LEE E21 2103 1976-02-23
000340 JASON R GOUNOT E21 5696 1947-05-05

Table 3. Partial EMPLOYEE table. Partial contents after the DELETE statement.
EMPNO FIRSTNME MI LASTNAME WORKDEPT PHONENO HIREDATE
000230 JAMES J JEFFERSON D21 2094 1966-11-21
000240 SALVATORE M MARINO D21 3780 1979-12-05
000250 DANIEL S SMITH D21 0961 1960-10-30
000260 SYBIL P JOHNSON D21 8953 1975-09-11
000270 MARIA L PEREZ D21 9001 1980-09-30
000280 ETHEL R SCHNEIDER ? 0997 1967-03-24
000290 JOHN R PARKER ? 4502 1980-05-30
000300 PHILIP X SMITH ? 2095 1972-06-19
000310 MAUDE F SETRIGHT ? 3332 1964-09-12
000320 RAMLAL V MEHTA E21 9990 1965-07-07
000330 WING   LEE E21 2103 1976-02-23
000340 JASON R GOUNOT E21 5696 1947-05-05

 

Parent topic:

Removing rows from tables with referential constraints

 

Related reference


DB2 Universal Database for iSeries sample tables