Question

I am new to PostgreSQL (version 9.3) and I have tried to run several UPDATE queries which have returned successfully but which have affected 0 rows.

Ex 1. I have two tables cropvalues and crops. crops contains various cropnames in one column (name) and an associated primary key integer in another column (id). I want to update an empty column in the cropvalues table (crops_id) - which is a foreign key to crops.id - so that it contains the respective primary key integer for each crop - I have a temporary column in the cropvalues table with the various cropnames (cropname) which I will delete after updating.

UPDATE cropvalues
SET crops_id = crops.id
FROM crops
WHERE cropvalues.cropname = crops.name;

Ex 2. I need to update cells containing null values in various columns. The following query affects 0 rows when the WHERE statement is included although there are clearly empty cells; when the WHERE statement is removed then the query affects all cells.

UPDATE cropvalues
SET time_id = '1'
WHERE time_id IS NULL;

I must be missing something simple, but it's so simple that I do not know where to look.

Thank you for your help

Was it helpful?

Solution

In this case it was the case sensitivity of PostgreSQL that led to the queries completing successfully but not affecting any rows as one column had the entries capitalized and the other column did not have the entries capitalized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top