Database Reference
In-Depth Information
Sets and uniqueness
What happens if we try to add a value to a set that it already contains? Let's try adding
carol to the list again:
UPDATE "user_status_updates"
SET "starred_by_users" = "starred_by_users" + {'carol'}
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
Now, let's read the row from the database to see our update's effects:
As the definition of the SET data structure suggests, we see only one instance of carol
in the set; the column itself guarantees the uniqueness of its values.
Collections and upserts
In our previous example, we directly set the initial value of the starred_by_users
column in the status update row, and only then performed discrete additions and removals.
What happens if we try to add a value to a set that doesn't exist? Let's try it on a different
status update, one that has no value in the starred_by_users column:
UPDATE "user_status_updates"
SET "starred_by_users" = "starred_by_users" + {'alice'}
WHERE username = 'bob'
AND id = 97719c50-e797-11e3-90ce-5f98e903bf02;
On running the query, we don't get any error message, which is a promising sign. Let's
have a look at the contents of that row now:
Search WWH ::




Custom Search