In today’s article, I will explain How to Update a Table in PostgreSQL using the “UPDATE” clause.
The “Update” clause is the sql clause that allows us to easily make the changes that need to be made in our table.
We need to pay attention to some values when using Update.
First of all, are we going to replace all records in our table or just a specific record?
With the update clause, we delete the old data and enter new values and this is irreversible, so be very careful to use the where condition when updating the records in the table, because if you update the records in the whole table without adding the where condition, you will delete your old data.
Let’s explain in more detail with examples.
1 | UPDATE Table_Name SET column= new value WHERE condition |
Vize and final grades are entered in our table named ogrenci_u1 above, but although the sections are the same, they are written differently. Let’s change all of them to Bilgisayar Mühendisliği.
1 | UPDATE ogrenci_u1 SET bölüm='Bilgisayar Mühendisliği'; |
The table name to be changed is entered after the UPDATE clause.
After the SET section, the column to be changed and its value are written.
You can use the RETURNING command to see what the updated columns are.
You can specify the columns after the RETURNING command or you can see all the columns by typing *. Let’s see this by making all students’ vize grades 30.
1 | UPDATE ogrenci_u1 SET vize=30 RETURNING *; |
If you want to see only certain columns, you can write as follows.
1 | UPDATE ogrenci_u1 SET vize=30 RETURNING adi,vize; |