The DELETE statement in MySQL is used to delete existing records in a table. It is a powerful command that can be used to remove large amounts of data from a table quickly and efficiently.
Points To Remember:
1. MySQL workbench won't allow you to delete rows because it will run in safe mode. To use the delete command, run this command SET SQL_SAFE_UPDATES = 0, and you are good to go.
2. One more thing I would suggest is to set auto-commit as off, the reason being, by any chance, if you delete something unwanted, you can just roll back the changes. If you have auto-commit as on, the changes will be committed to the database, and you won't be able to roll back. Run this command: SET autocommit = 0
3. Always use the WHERE
clause; if you don't, then it will delete all the values in the table
4. If you delete all the values in the table due to a mistake, you will still have the table structure intact. You can either roll back like mentioned above or add values.
DELETE STATEMENT:
The statement starts with the keyword DELETE FROM, followed by the name of the table that you want to delete records from. After the table name, you can specify a WHERE clause to specify which records should be deleted. The WHERE clause is used to filter the records that are deleted and is optional. If you leave out the WHERE clause, all records in the table will be deleted.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
We will be deleting the actor with actor_id = 1239.
To do that, let's check whether it exists and what it looks like.
SELECT * FROM actor
WHERE actor_id = 1239;
Output:
Now we use the delete statement to delete this entry
DELETE FROM actor
WHERE actor_id = 1239;
Output: We get the confirmation in the output below that the entry is deleted.
Now when looking for the entry, you get the below result.
SELECT * FROM actor
WHERE actor_id = 1239;
Output:
In conclusion, the DELETE statement in MySQL is a powerful command that can quickly and efficiently delete large amounts of data from a table. However, it is essential to use and test it thoroughly to avoid unwanted data loss.
'll meet again tomorrow!
To see more examples, you can always refer w3 schools; it has always been my go-to website to understand things.
References: w3schools.com/mysql/default.asp
*****All the outputs provided are a snippet of the actual result.