MySQL Shell API 9.0.0
Unified development interface for MySQL Products
Methods | List of all members
TableDelete Class Reference

Operation to delete data from a table. More...

Methods

TableDelete delete ()
 Initializes the deletion operation. More...
 
TableDelete where (String expression)
 Sets the search condition to filter the records to be deleted from the Table. More...
 
TableDelete orderBy (List sortCriteria)
 Sets the order in which the records will be deleted. More...
 
TableDelete limit (Integer numberOfRows)
 Sets the maximum number of rows to be deleted by the operation. More...
 
TableDelete bind (String name, Value value)
 Binds a value to a specific placeholder used on this operation. More...
 
Result execute ()
 Executes the delete operation with all the configured options. More...
 

Detailed Description

Operation to delete data from a table.

Handler for Delete operation on Tables.

Member Function Documentation

◆ delete()

TableDelete delete ( )

Initializes the deletion operation.

Returns
This TableDelete object.

This function is called automatically when Table.delete() is called.

The actual deletion of the records will occur only when the execute() method is called.

Method Chaining

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ where()

TableDelete where ( String  expression)

Sets the search condition to filter the records to be deleted from the Table.

Parameters
expressionA condition to filter the records to be deleted.
Returns
This TableDelete object.

If used, only those rows satisfying the expression will be deleted

The expression supports Parameter Binding.

Method Chaining

This function can be invoked only once after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ orderBy()

TableDelete orderBy ( List  sortCriteria)

Sets the order in which the records will be deleted.

Returns
This TableDelete object.

If used, the TableDelete operation will delete the records in the order established by the sort criteria.

Every defined sort criterion follows the format:

name [ ASC | DESC ]

ASC is used by default if the sort order is not specified.

Method Chaining

This function can be invoked only once after:

See also
Usage examples at execute().

◆ limit()

TableDelete limit ( Integer  numberOfRows)

Sets the maximum number of rows to be deleted by the operation.

Parameters
numberOfRowsThe maximum number of rows to be deleted.
Returns
This TableDelete object.

If used, the operation will delete only numberOfRows rows.

Method Chaining

This function can be invoked only once after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ bind()

TableDelete bind ( String  name,
Value  value 
)

Binds a value to a specific placeholder used on this operation.

Parameters
nameThe name of the placeholder to which the value will be bound.
valueThe value to be bound on the placeholder.
Returns
This TableDelete object.

Binds the given value to the placeholder with the specified name.

An error will be raised if the placeholder indicated by name does not exist.

This function must be called once for each used placeholder or an error will be raised when the execute method is called.

Method Chaining

This function can be invoked multiple times right before calling execute().

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ execute()

Result execute ( )

Executes the delete operation with all the configured options.

Returns
A Result object.

Method Chaining

This function can be invoked after any other function on this class.

Examples

Deleting records with a condition

var result = table.delete().where('age = 15').execute();
print('Affected Rows:', result.affectedItemsCount, '\n');
var records = table.select().execute().fetchAll();
print('Records Left:', records.length, '\n');

Deleting records with a condition and parameter binding

var result = table.delete().where('gender = :heorshe').limit(2).bind('heorshe', 'male').execute();
print('Affected Rows:', result.affectedItemsCount, '\n');
var records = table.select().execute().fetchAll();
print('Records Left:', records.length, '\n');

Deleting all records using a view

var result = view.delete().execute();
print('Affected Rows:', result.affectedItemsCount, '\n');
// Deletion is of course reflected on the target table
var records = table.select().execute().fetchAll();
print('Records Left:', records.length, '\n');

Deleting records with a limit

var result = table.delete().limit(2).execute();
print('Affected Rows:', result.affectedItemsCount, '\n');
var records = table.select().execute().fetchAll();
print('Records Left:', records.length, '\n');