76 lines
1.9 KiB
Plaintext
76 lines
1.9 KiB
Plaintext
---
|
|
title: UPDATE | Datasquirel docs
|
|
description: Modify existing rows using UPDATE statements in Datasquirel
|
|
page_title: UPDATE
|
|
page_description: Modify existing rows in your tables using SQL UPDATE statements.
|
|
---
|
|
|
|
## Overview
|
|
|
|
`UPDATE` modifies existing rows in a table. Always include a `WHERE` clause to target specific rows — an UPDATE without a WHERE clause updates every row in the table.
|
|
|
|
## Basic Syntax
|
|
|
|
```sql
|
|
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Update a Single Row by ID
|
|
|
|
```sql
|
|
UPDATE users SET name = 'Alice Smith', email = 'alice.smith@example.com' WHERE id = 42;
|
|
```
|
|
|
|
### Update Multiple Rows
|
|
|
|
```sql
|
|
UPDATE posts SET is_published = 1 WHERE category = 'announcements';
|
|
```
|
|
|
|
### Update with a Calculated Value
|
|
|
|
```sql
|
|
UPDATE products SET price = price * 1.10 WHERE category = 'electronics';
|
|
```
|
|
|
|
### Update with LIMIT
|
|
|
|
MariaDB supports `LIMIT` on UPDATE to cap how many rows can be modified in one statement:
|
|
|
|
```sql
|
|
UPDATE notifications SET is_read = 1 WHERE user_id = 5 ORDER BY created_at ASC LIMIT 50;
|
|
```
|
|
|
|
## Safety Note
|
|
|
|
An `UPDATE` without a `WHERE` clause will update **every row** in the table. Always double-check your `WHERE` clause before running an update, especially in production.
|
|
|
|
```sql
|
|
-- This updates ALL rows in the table:
|
|
UPDATE users SET is_active = 0;
|
|
|
|
-- This updates only the intended row:
|
|
UPDATE users SET is_active = 0 WHERE id = 42;
|
|
```
|
|
|
|
## Using UPDATE via the API
|
|
|
|
The [CRUD PUT endpoint](/docs/api-reference/crud/put) handles updates with a target ID:
|
|
|
|
```javascript
|
|
const result = await datasquirel.crud.update({
|
|
dbName: "my_database",
|
|
tableName: "users",
|
|
targetID: 42,
|
|
body: {
|
|
name: "Alice Smith",
|
|
email: "alice.smith@example.com",
|
|
},
|
|
apiKey: process.env.DATASQUIREL_API_KEY,
|
|
});
|
|
```
|
|
|
|
Only the fields you include in `body` are changed — other columns retain their current values.
|