sqlite
  1. sqlite-delete-query

SQLite Delete Query

CRUD (Create, Read, Update, Delete) operations are essential for managing data in any database. In SQLite, the delete query is used to remove data from a table based on certain conditions.

Syntax

The basic syntax for the SQLite delete query is as follows:

DELETE FROM table_name WHERE condition;

Here, table_name is the name of the table from which you want to delete data, and condition specifies the criteria for deleting data.

Example

Suppose we have a table called students with the following data:

id  | name   | gender  | age
----+--------+---------+-----
1   | Alice  | Female  | 20
2   | Bob    | Male    | 22
3   | Charlie| Male    | 19

We can use the delete query to remove data for a specific student.

DELETE FROM students WHERE id = 2;

Output

After executing the above query, the output would be:

id  | name   | gender  | age
----+--------+---------+-----
1   | Alice  | Female  | 20
3   | Charlie| Male    | 19

Explanation

In the example above, we use the DELETE FROM statement to delete data from the students table. We specify the condition for deletion using the WHERE clause, which specifies that we want to delete the row where the id is equal to 2. SQLite then deletes the row for Bob from the students table.

Use

The delete query is used to remove data from a table based on specific conditions. This can be useful when you need to remove obsolete or inaccurate data, or when you need to update your database with new information.

Important Points

  • Always specify the condition for deletion using the WHERE clause, otherwise all the records in the table will be deleted.
  • Be careful when using the delete query as it permanently deletes data. Make sure you have a backup of your data before executing the query.
  • You can use the TRUNCATE statement to remove all the data from a table, but this operation cannot be undone.

Summary

In this tutorial, we learned about the SQLite delete query and how to use it to remove data from a table based on specific conditions. We saw an example of deleting data from a students table and learned about important points to consider when using the delete query.

Published on: