DELETE DATA SQL

Deleting all records is similar to the TRUNCATE funciton

Basic syntax for deleting records.

DELETE FROM table_name
WHERE condition;

Deleting All Records:
To delete all records from a table, you can use a simple DELETE statement without a WHERE clause:

Delete with JOIN:

You can also use the DELETE statement with JOIN to delete records from multiple tables simultaneously. Here’s a basic example:

DELETE customers, orders
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
WHERE customers.customer_id = 1001;

A good idea when deleting data from tables is outlined below:

1. START TRANSACTION;
2. SELECT COUNT(*) AS NUMBER_OF_RECORDS FROM TABLE
3. THE CONDITIONAL DELETE QUERY
4. SELECT COUNT(*) AS NUMBER_OF_RECORDS FROM TABLE. Do this again to see how many rows were actually deleted.
5. ROLLBACK;–COMMIT; Do the rooback first to check how many were actually deleted. If you are satisfied, you can then change it to say commit and remove the rollback to save the query.

Leave a Comment

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

Scroll to Top