SQL

SQL Update

SQL Update

This tutorial covers SQL Update statement.

In SQL, records can be modified. This is the function of the “update” statement.

The syntax is easy to understand and the keywords are “update set” (with the addition of the “where” statement).

The SQL “where” statement is explored in further tutorials. Simply, its functionality is to filter data (select specific data).

This page encompasses:

  • updating records
  • updating multiple records

Let’s use the previously created table “student_info“.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL

Update records

As mentioned above, the “where” statement selects the specified data.

In this instance, it chooses where to update the records. The “where” statement is extremely important when updating. In the event of not specifying which records to update, the “update” statement will update all records within the table.

You can select any information as a filter, but if different cells have the same data, all will update. For this reason, it is a good practice to use the unique id of the row when updating a single record row.

Update records without “where”

Let’s check what happens when we do not use the “where” statement.

UPDATE student_info
SET firstName='Peter', lastName='Bernard', age=20;
idfirstNamelastNameage
1PeterBernard20
2PeterBernard20
3PeterBernard20
4PeterBernard20

Update records with “where”

Let’s update the table with the “where” statement.

Below is an example. Please note that the “age” column does not change.

UPDATE student_info
SET firstName='Peter', lastName='Bernard'
WHERE id=1;

If no errors, the output produces “1 row affected“. Let’s display the result with the “SELECT * FROM student_info;“.

idfirstNamelastNameage
1PeterBernard21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL

The first student (previously John Smith) is now Peter Bernard. The age stays the same, as it was not specified in the “update” statement.

Update multiple records

In the above scenario, we select the “id“, which is a unique data.

Sometimes, we may need to update multiple rows simultaneously. For this scenario, we add a 5th student (Patrick Dumont, 21). This creates partial duplicates (two students with the same name, Patrick).

The table is below.

idfirstNamelastNameage
1PeterBernard21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL
5PatrickDumont21

Now let’s update the records, using the “where” statement with the name Patrick (two students have the same name).

UPDATE student_info
SET firstName='Jason', lastName='Brown', age=35
WHERE firstName='Patrick';

2 rows affected. Note that here we update the age as well.

idfirstNamelastNameage
1PeterBernard21
2JasonBrown35
3SamAdams19
4MikeGarciaNULL
5JasonBrown35

After the update, the second and fifth student has the same name and age, Jason Brown (35).


Next: SQL Delete

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.