Removing Rows From a Table Using the SQL DELETE Statement
- Procedure
-
Formulate a SQL DELETE string for removing rows of data from a table.
- Call the
createStatement()method of theSessionobject. Pass the SQL DELETE string as an argument. - If the SQL DELETE string contains parameter markers, call the
Statementmethods to set the parameter values. - Run the DELETE statement by calling the
executeUpdate()method of theStatementobject. -
Check the result of the
executeUpdate()method to verify that the correct number of rows have been removed from the table. -
Set different parameter values and rerun the DELETE statement by repeating steps 3 to 5.
-
Close the
Statementobject.
Deleting large number of rows
To delete a large number of rows from a table, design a SQL DELETE statement as follows:
- Procedure
-
Design a range query that is bounded on both ends to select a subset of rows to be removed from the table.
-
Avoid unbounded range queries to reduce the number of rows scanned. For example, use a WHERE clause similar to the following:
WHERE key >= 0 AND key <=1000
WHERE key BETWEEN 0 AND 1000
-
Use parameters in the range query that results in a subset of rows being removed each time
executeUpdate()is invoked. For example,WHERE key BETWEEN ? AND ?
-
Design the WHERE clause so that an index is used for finding the rows and prevent full table scans.
-
Set the statement property
TIBDG_STATEMENT_PROPERTY_DOUBLE_UPDATE_TIMEOUTso theexecuteUpdaterequests do not time out.Note: The SQL LIMIT clause is not part of the syntax for SQL DELETE.