Bird
0
0

Which of the following is the correct syntax to remove a key named "age" from a JSONB column data in PostgreSQL?

easy📝 Syntax Q12 of 15
PostgreSQL - JSON and JSONB
Which of the following is the correct syntax to remove a key named "age" from a JSONB column data in PostgreSQL?
AUPDATE table SET data = data - 'age';
BUPDATE table SET data = jsonb_set(data, '{age}', NULL);
CUPDATE table SET data = data + 'age';
DUPDATE table SET data = jsonb_remove(data, 'age');
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct operator for key removal

    In PostgreSQL, the minus operator (-) removes a key from JSONB data, so data - 'age' removes the key "age".
  2. Step 2: Check other options for correctness

    UPDATE table SET data = jsonb_set(data, '{age}', NULL); tries to set the key to NULL but does not remove it. UPDATE table SET data = data + 'age'; adds a key, which is incorrect. UPDATE table SET data = jsonb_remove(data, 'age'); uses a non-existent function jsonb_remove.
  3. Final Answer:

    UPDATE table SET data = data - 'age'; -> Option A
  4. Quick Check:

    Minus operator removes JSONB key [OK]
Quick Trick: Use minus (-) operator to remove JSONB keys [OK]
Common Mistakes:
  • Using jsonb_set with NULL to remove keys
  • Trying to add keys to remove them
  • Using non-existent functions like jsonb_remove

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes