SQLite CRUD Operation: Update Query
SQLite is a popular open-source relational database management system. It supports the basic CRUD operations, which stands for Create, Read, Update, and Delete. In this tutorial, we will learn how to perform an update operation in SQLite.
Syntax
The update query syntax in SQLite is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
table_name
: the name of the table that you want to update.column1
,column2
, ...: the name of the columns that you want to update.value1
,value2
, ...: the new values for the columns.condition
: the condition that specifies which records to update.
Example
Suppose we have a table called students
that contains information on students in a school. We want to update the email address of a student whose ID is 101.
UPDATE students
SET email = 'new_email@example.com'
WHERE id = 101;
Output
The output of the above query would be the number of rows affected by the update operation.
1
Explanation
In the example above, we use the UPDATE
statement to update the email address of a student in the students
table. We use the SET
keyword to specify the new value for the email column and the WHERE
keyword to specify the condition that specifies which record to update. In this case, we want to update the email address for the student whose ID is 101.
Use
The update query is used when you want to modify the data that is already present in the database. This is useful when you want to correct or modify erroneous/obsolete records in a table.
Important Points
- The
WHERE
clause is essential while using theUPDATE
statement; it helps to identify which records to update. - The columns that you want to update should be specified in the
SET
clause, separated by commas. - If you do not specify a condition in the
WHERE
clause, all the records in the table will be updated.
Summary
In this tutorial, we learned how to perform an update operation in SQLite using the UPDATE
statement. We saw the syntax for the update operation, an example of updating the email address for a student in the students
table, and the output of the update operation. We also learned about the importance of using the WHERE
clause while updating records in the table.