The DELETE command in SQL is used to delete data from a table.
This can be done with the syntax shown below;
DELETE FROM table_name;
Take a look at this sample table ‘student’:
s_id | name | age |
---|---|---|
11 | Alexa | 21 |
12 | Nick | |
13 | Ada Chan |
DELETE FROM student;
The command above will delete all the records from the table ‘student’.
In the ‘student’ table, when you want to delete a single record, you can use the where clause to provide a condition in our delete statement.
DELETE FROM student WHERE s_id=13;
s_id | name | age |
---|---|---|
11 | Alexa | 21 |
12 | Nick |
You must know that the truncate command is quite different from the delete command. The delete command is used to delete all the rows from a table, while the truncate command not only deletes all the records stored in the table but will re-initialize the table(like a newly created table).
For instance, when you have a table with 10 rows and an ‘auto_increment’ primary key, and if you use the delete command to delete all the rows, it will delete all the rows, however, it will not re-initialize the primary key, therefore if you decide to insert any row after using the delete command, the ‘auto_increment’ primary key will start from 11. In contrast, in the truncate command, the primary key is re-initialized, and it will start from 1 again.