MySQL Shell API 8.4.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 (str expression)
 Sets the search condition to filter the records to be deleted from the Table. More...
 
TableDelete order_by (list sortCriteria)
 Sets the order in which the records will be deleted. More...
 
TableDelete limit (int numberOfRows)
 Sets the maximum number of rows to be deleted by the operation. More...
 
TableDelete bind (str 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 ( str  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().

◆ order_by()

TableDelete order_by ( 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 ( int  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 ( str  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

result = table.delete().where('age = 15').execute()
print('Affected Rows:', result.affected_items_count, '\n')
records = table.select().execute().fetch_all()
print('Records Left:', len(records), '\n')

Deleting records with a condition and parameter binding

result = table.delete().where('gender = :heorshe').limit(2).bind('heorshe', 'male').execute()
print('Affected Rows:', result.affected_items_count, '\n')
records = table.select().execute().fetch_all()
print('Records Left:', len(records), '\n')

Deleting all records using a view

view = schema.get_table('view1')
result = view.delete().execute()
print('Affected Rows:', result.affected_items_count, '\n')
# Deletion is of course reflected on the target table
records = table.select().execute().fetch_all()
print('Records Left:', len(records), '\n')

Deleting records with a limit

result = table.delete().limit(2).execute()
print('Affected Rows:', result.affected_items_count, '\n')
records = table.select().execute().fetch_all()
print('Records Left:', len(records), '\n')