DELETE SQL command

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_idnameage
11Alexa21
12Nick 
13Ada Chan 

 

Delete all Records from a Table

DELETE FROM student;

The command above will delete all the records from the table ‘student’.

 

Delete a particular Record from a Table

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_idnameage
11Alexa21
12Nick 

 

Isn't DELETE same as TRUNCATE

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.