|
| 1 | +## Deletes |
| 2 | + |
| 3 | +Create a delete object: |
| 4 | + |
| 5 | +```php |
| 6 | +use Odan\Database\Connection; |
| 7 | + |
| 8 | +$db = new Connection($dsn, $username, $password, $options); |
| 9 | +$delete = $db->delete(); |
| 10 | +``` |
| 11 | + |
| 12 | +The query builder may also be used to delete records from the |
| 13 | +table via the delete method. You may constrain delete |
| 14 | +statements by adding where clauses before calling the delete method: |
| 15 | + |
| 16 | + |
| 17 | +```php |
| 18 | +$db->delete()->from('users')->execute(); // DELETE FROM `users` |
| 19 | +$db->delete()->from('users')->where('votes', '>', 100)->execute(); // DELETE FROM `users` WHERE `votes` > '100' |
| 20 | +``` |
| 21 | + |
| 22 | +If you wish to truncate the entire table, which will remove |
| 23 | +all rows and reset the auto-incrementing ID to zero, |
| 24 | +you may use the truncate method: |
| 25 | + |
| 26 | +```php |
| 27 | +$db->delete()->from('users')->truncate()->execute(); // TRUNCATE TABLE `users`; |
| 28 | +``` |
| 29 | + |
| 30 | +### Order of Deletion |
| 31 | + |
| 32 | +If the DELETE statement includes an ORDER BY clause, rows are deleted in the |
| 33 | +order specified by the clause. This is useful primarily in conjunction with LIMIT. |
| 34 | + |
| 35 | +```php |
| 36 | +$db->delete()->from('some_logs') |
| 37 | + ->where('username', '=', 'jcole') |
| 38 | + ->orderBy('created_at') |
| 39 | + ->limit(1) |
| 40 | + ->execute(); |
| 41 | +``` |
| 42 | + |
| 43 | +ORDER BY also helps to delete rows in an order required to avoid referential integrity violations. |
| 44 | + |
| 45 | +### Delete Limit |
| 46 | + |
| 47 | +The LIMIT clause places a limit on the number of rows that can be deleted. |
| 48 | + |
| 49 | +```php |
| 50 | +$db->delete()->from('users')->limit(10)->execute(); |
| 51 | +``` |
| 52 | + |
| 53 | +### Delete Low Priority |
| 54 | + |
| 55 | +If you specify `LOW_PRIORITY`, the server delays execution of the |
| 56 | +DELETE until no other clients are reading from the table. |
| 57 | + |
| 58 | +This affects only storage engines that use only table-level |
| 59 | +locking (such as MyISAM, MEMORY, and MERGE). |
| 60 | + |
| 61 | +```php |
| 62 | +$db->delete()->from('users')->lowPriority()->execute(); |
| 63 | +``` |
| 64 | + |
| 65 | +### Delete and ignore errors |
| 66 | + |
| 67 | +The `IGNORE` modifier causes MySQL to ignore errors during the process of deleting rows. |
| 68 | + |
| 69 | +(Errors encountered during the parsing stage are processed in the usual manner.) |
| 70 | + |
| 71 | +Errors that are ignored due to the use of IGNORE are returned as warnings. |
| 72 | + |
| 73 | +```php |
| 74 | +$db->delete()->from('users')->ignore()->execute(); |
| 75 | +``` |
| 76 | + |
| 77 | +### Delete Quick modifier |
| 78 | + |
| 79 | +For MyISAM tables, if you use the QUICK modifier, the storage engine |
| 80 | +does not merge index leaves during delete, which may speed up some kinds of delete operations. |
| 81 | + |
| 82 | +```php |
| 83 | +$db->delete()->from('users')->quick()->execute(); |
| 84 | +``` |
0 commit comments