To delete rows from a table, use the DELETE statement. You should use the WHERE condition to remove a specific row from a table.
However, if you don't include a WHERE condition, the table will be stripped of all rows.
There are also other terms that are similar to the DELETE statement, such as DROP and TRUNCATE, but they are not identical and have some variances.
There is a little distinction between the delete and truncate statements. The DELETE statement only deletes rows from the table that meet the WHERE clause's condition, or it deletes all rows from the table if no condition is supplied. But it does not free the space containing by the table.
The TRUNCATE statement: it is used to delete all the rows from the table and free the containing space.
When you use the drop statement, it deletes the table's row as well as the table's definition, making all of the table's associations with other tables invalid.
When we TRUNCATE a table, on the other hand, the table structure remains unchanged, so none of the above issues arise.
Let us take an example of student table.
Original table:
ID | STUDENT _NAME | ADDRESS |
---|---|---|
001 | John | Kigali |
002 | Claude | Nyagatare |
003 | Benoit | Kigali |
The SQL DELETE query for deleting a student with id 003 from the student_name table should look like this:
Resulting table after SQL DELETE query:
ID | STUDENT_NAME | ADDRESS |
---|---|---|
001 | John | Kigali |
002 | Claude | Nyagatare |