Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 4: DB2 Data Types and SQL Basics


DELETE

The DELETE statement removes rows from a table. Like UPDATE, it is controlled by the WHERE clause - and without WHERE it removes every row.

DELETE syntax

  • Basic shape: DELETE FROM table WHERE condition.
  • DELETE removes whole rows. To clear single columns, use UPDATE and set them to NULL.
  • Deleted rows can be recovered only from a backup or image copy. There is no undo after COMMIT.
  • Always check SQLCODE after DELETE, then COMMIT to make the removal permanent.

DELETE vs DROP vs TRUNCATE thinking

  • DELETE removes rows but keeps the table structure. DROP removes the table itself.
  • DELETE without WHERE empties the table row by row and can be rolled back before COMMIT.
  • DELETE fires no warnings on large row counts. Count with SELECT first when deleting many rows.
  • Child rows with a RESTRICT rule block the DELETE of their parent row (SQLCODE -532).

Example

  • Deleting one employee row safely:-
EXEC SQL SELECT COUNT(*) INTO :WS-CNT FROM EMP WHERE EMPNO = '000100' END-EXEC. EXEC SQL DELETE FROM EMP WHERE EMPNO = '000100' END-EXEC. IF SQLCODE = 0 EXEC SQL COMMIT WORK END-EXEC ELSE EXEC SQL ROLLBACK WORK END-EXEC END-IF.





© copyright mainframebug.com
Privacy Policy
MainframeBug Assistant