MySQL Shell API  8.0.18
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 this record deletion handler. More...
 
TableDelete where (String expression)
 Sets the search condition to filter the records to be deleted from the Table. More...
 
TableDelete orderBy (List sortExprStr)
 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 this record deletion handler.

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
expressionOptional 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  sortExprStr)

Sets the order in which the records will be deleted.

Returns
This TableDelete object.

If used the records will be deleted in the order established by the sort criteria.

The elements of sortExprStr list are strings defining the column name on which the sorting will be based.

The format is as follows: columnIdentifier [ ASC | DESC ]

If no order criteria is specified, ASC will be used by default.

Method Chaining

This function can be invoked only once after:

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

◆ 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:

This function can be called every time the statement is executed.

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 a value to a specific placeholder used on this operation.

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:

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.

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

//@ TableDelete: delete under 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');
//@ TableDelete: delete with 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');
//@ TableDelete: full delete with a view object
var view = schema.getTable('view1');
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');
//@ TableDelete: with limit 0
var result = table.insert({ name: 'adam', age: 15, gender: 'male' }).execute();
var result = table.insert({ name: 'brian', age: 14, gender: 'male' }).execute();
var result = table.insert({ name: 'alma', age: 13, gender: 'female' }).execute();
var result = table.insert({ name: 'carol', age: 14, gender: 'female' }).execute();
var result = table.insert({ name: 'donna', age: 16, gender: 'female' }).execute();
var records = table.select().execute().fetchAll();
print('Records Left:', records.length, '\n');
//@ TableDelete: with limit 1
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');
//@ TableDelete: with limit 2
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');
//@ TableDelete: with limit 3
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');