
MySQL DELETE Record
To delete records from a MySQL table, you can use the DELETE
statement. Here’s the basic syntax for deleting records from a MySQL table:
DELETE FROM table_name
WHERE condition;
table_name
: The name of the table from which you want to delete records.condition
: An optional condition that specifies which records should be deleted. If you omit the condition, all records in the table will be deleted.
Here’s an example of how to use the DELETE
statement to delete records from a hypothetical “students” table:
DELETE FROM students
WHERE student_id = 3;
In this example:
- The
students
table is specified as the target table. - The
WHERE
clause specifies the condition for deletion. In this case, it deletes the record where thestudent_id
is equal to 3.
If you want to delete all records from a table, you can omit the WHERE
clause:
DELETE FROM students;
Be extremely cautious when using the DELETE
statement without a WHERE
clause, as it will delete all records from the specified table, and the data cannot be easily recovered.
You can also use more complex conditions to delete records based on multiple criteria:
DELETE FROM students
WHERE age < 18 AND grade = 'F';
This query will delete all records from the “students” table where the students are under 18 years old and have a grade of ‘F’.
Always be careful when using the DELETE
statement, especially in a production database, to ensure that you are only deleting the intended records. It’s a good practice to first run a SELECT
query with the same conditions to confirm which records will be deleted before executing the DELETE
statement.