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.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *