MySQL DELETE Statement
MySQL DELETE Statement
The DELETE statement in MySQL is used to remove existing records from a table. It is commonly used with the WHERE clause to delete specific rows.
β Syntax
-
table_name: The name of the table from which you want to delete records. -
WHERE: Specifies which record(s) should be deleted.
β οΈ Important Warning
If you omit the WHERE clause, all records in the table will be deleted, but the table structure remains intact.
Example:
β Deletes all rows in the customers table.
π§ Example Table: students
| id | name | age | city |
|---|---|---|---|
| 1 | John | 20 | New York |
| 2 | Emma | 21 | Chicago |
| 3 | Raj | 22 | Mumbai |
β Example 1: Delete a Single Record
β Deletes the record where student id = 1.
β Example 2: Delete Multiple Records Based on Condition
β Deletes all students whose age is greater than 21.
β Example 3: Delete Records with NULL Values
β Removes all rows where the city value is NULL.
β Reset Auto Increment (Optional)
After many deletes, you may want to reset the autoβincrement counter:
π DELETE vs TRUNCATE
| Feature | DELETE | TRUNCATE |
|---|---|---|
| Deletes specific rows | βοΈ Yes | β No |
| Can use WHERE | βοΈ Yes | β No |
| Deletes all rows | βοΈ Yes | βοΈ Yes |
| Faster performance | β No | βοΈ Yes |
| Resets auto-increment | β No | βοΈ Yes |
Example of TRUNCATE:
π― Summary
-
Use DELETE to remove data from a table.
-
Always use a WHERE clause unless you want to delete all records.
-
DELETE does not remove the table structure.
