Using DELETE
SQL command
When you ask any question in Studytonight's Forum it gets saved into a table. And using the Delete option, you can even delete a question asked by you. How do you think that works? Yes, using the Delete DML command.
Let's study about the syntax and the usage of the Delete command.
DELETE
command
DELETE
command is used to delete data from a table.
Following is its general syntax,
DELETE FROM table_name;
Let's take a sample table student:
s_id | name | age |
101 | Adam | 15 |
102 | Alex | 18 |
103 | Abhi | 17 |
Delete all Records from a Table
DELETE FROM student;
The above command will delete all the records from the table student.
Delete a particular Record from a Table
In our student table if we want to delete a single record, we can use the WHERE
clause to provide a condition in our DELETE
statement.
DELETE FROM student WHERE s_id=103;
The above command will delete the record where s_id
is 103 from the table student.
S_id | S_Name | age |
101 | Adam | 15 |
102 | Alex | 18 |
Isn't DELETE
same as TRUNCATE
TRUNCATE
command is different from DELETE
command. The delete command will delete all the rows from a table whereas truncate command not only deletes all the records stored in the table, but it also re-initializes the table(like a newly created table).
For eg: If you have a table with 10 rows and an auto_increment primary key, and if you use DELETE
command to delete all the rows, it will delete all the rows, but will not re-initialize the primary key, hence if you will insert any row after using the DELETE
command, the auto_increment primary key will start from 11. But in case of TRUNCATE
command, primary key is re-initialized, and it will again start from 1.