Deleting Rows with DELETE

The final thing we'll look at today is the DELETE statement. Quite simply, DELETE removes rows of data from a table.

Here is its syntax:

DELETE FROM table_name
 [WHERE where_definition]
 [ORDER BY column_list]
 [LIMIT num_rows]

You should see immediately several similarities with the SELECT query (which is why we're looking at it now!).

For a start, there's a WHERE clause, which restricts the delete action to just those rows meeting the condition. This works exactly the same as in a SELECT statement.

Occasionally, you may also use a LIMIT clause, and if so, you may also use an ORDER BY clause. (ORDER BY has no effect without LIMIT.) You may choose a limit of 1 for safety (so that you can't delete everything by mistake), ...

Get Sams Teach Yourself MySQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.