Complete the code to add a new key-value pair to a JSONB column.
UPDATE users SET data = data || '{"[1]": "active"}' WHERE id = 1;
The || operator merges JSONB objects. Adding "status": "active" updates the JSONB column with a new key-value pair.
Complete the code to remove the key 'age' from the JSONB column.
UPDATE users SET data = data - '[1]' WHERE id = 2;
The - operator removes a key from a JSONB object. Here, it removes the key "age".
Fix the error in the code to update the 'email' key inside the JSONB column.
UPDATE users SET data = jsonb_set(data, '{"[1]"}', '"new@example.com"') WHERE id = 3;
The jsonb_set function updates a value at a specified path. The path must be an array of keys, so {"email"} is correct.
Fill both blanks to update the nested key 'city' inside the 'address' object in JSONB.
UPDATE users SET data = jsonb_set(data, '[1]', '"New York"') WHERE id = 4 AND data->'[2]' IS NOT NULL;
The path to update a nested key must be an array of keys: {"address", "city"}. The condition checks if the 'address' key exists.
Fill all three blanks to insert a new key 'phone' with value '123-456' into the JSONB column only if it does not exist.
UPDATE users SET data = CASE WHEN NOT data ? '[1]' THEN jsonb_set(data, '[2]', '"[3]"') ELSE data END WHERE id = 5;
The ? operator checks if a key exists. If 'phone' does not exist, jsonb_set adds it at path {"phone"} with value "123-456".