Basic Syntax
Update Single Field
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET status = 'active' WHERE id = 1 |
| MySQL | UPDATE users SET status = 'active' WHERE id = 1 |
| MongoDB | db.users.updateOne({ _id: 1 }, { $set: { status: 'active' } }) |
| Redis | HSET users:1 status "active" |
Update Multiple Fields
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET name = 'John Updated', age = 31, updated_at = '...' WHERE id = 1 |
| MongoDB | db.users.updateOne({ _id: 1 }, { $set: { name: 'John Updated', age: 31, ... } }) |
Update with Conditions
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET verified = true WHERE email_confirmed = true AND created_at < '2024-01-01' |
| MongoDB | db.users.updateMany({ email_confirmed: true, created_at: { $lt: '...' } }, { $set: { verified: true } }) |
Bulk Update
Update multiple records matching condition.| Database | Output |
|---|---|
| PostgreSQL | UPDATE products SET on_sale = true WHERE category = 'Electronics' |
| MongoDB | db.products.updateMany({ category: 'Electronics' }, { $set: { on_sale: true } }) |
Update with IN
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET role = 'premium' WHERE id IN (1, 2, 3, 4, 5) |
| MongoDB | db.users.updateMany({ _id: { $in: [1, 2, 3, 4, 5] } }, { $set: { role: 'premium' } }) |
Set to NULL
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET deleted_at = NULL WHERE id = 1 |
| MongoDB | db.users.updateOne({ _id: 1 }, { $set: { deleted_at: null } }) |
Increment Values
| Database | Output |
|---|---|
| PostgreSQL | UPDATE products SET quantity = quantity + 10 WHERE id = 1 |
| MongoDB | db.products.updateOne({ _id: 1 }, { $inc: { quantity: 10 } }) |
Decrement Values
| Database | Output |
|---|---|
| PostgreSQL | UPDATE products SET stock = stock - 1 WHERE id = 1 |
| MongoDB | db.products.updateOne({ _id: 1 }, { $inc: { stock: -1 } }) |
Using Functions
| Database | Output |
|---|---|
| PostgreSQL | UPDATE users SET name = UPPER(name) WHERE id = 1 |

