Now, we meet the most dangerous SQL statement, DELETE. DELETE removes existing records from tables. Many nightmares have occurred because of the DELETE statement. I strongly recommend testing your DELETE statements in another test database before you run the command in the real database. SQL does not provide some magical way to recover deleted data because that is your job as a web developer. Programming and database languages are super smart, but they can’t save you from everything. Enough of the warnings, you will learn on your own… Example please:
Deleting Records in SQL
Our table before:
id | username | password | birthday |
---|---|---|---|
1 | bobdole32 | secretP | 1984-06-01 |
2 | rustyMeerkat | digholes | 1995-09-15 |
DELETE FROM table_name WHERE username = ‘rustyMeerkat’
It’s pretty easy to understand; however, I need to stress Don’t leave off the WHERE. If you leave off WHERE, you will remove all of the records. The DELETE FROM is the standard introduction into the statement that SQL understands. As used before,WHERE is the conditional. Deleting isn’t very difficult, but please be careful. It is good practice to slowly build up your delete queries and quickly changing your DELETE statement to a SELECT statement to see what kind of data you will be deleting. After you see that the SELECT statement returned what you want to delete, you can finally switch it back to a DELETE statement.
I have been involved a little bit in queries DBs. But here I read a very good advise (never thought about it) – first to build select query and after it returned expected result – replace “select” with “delete”. It is not that I never did it – I just never put my thought into it from this angle.