Skip to content

Commit 97da871

Browse files
authored
docs: update from table (#1678)
* Update dml-update.md * Update dml-update.md
1 parent 17dea17 commit 97da871

File tree

1 file changed

+94
-20
lines changed

1 file changed

+94
-20
lines changed
Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
---
22
title: UPDATE
33
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
45

5-
Modifies rows in a table with new values.
6+
<FunctionDescription description="Introduced or updated: v1.2.699"/>
7+
8+
Updates rows in a table with new values, optionally using values from other tables.
69

710
:::tip atomic operations
811
Databend ensures data integrity with atomic operations. Inserts, updates, replaces, and deletes either succeed completely or fail entirely.
@@ -11,45 +14,116 @@ Databend ensures data integrity with atomic operations. Inserts, updates, replac
1114
## Syntax
1215

1316
```sql
14-
UPDATE <table_name>
15-
SET <col_name> = <value> [ , <col_name> = <value> , ... ]
16-
[ WHERE <condition> ]
17+
UPDATE <target_table>
18+
SET <col_name> = <value> [ , <col_name> = <value> , ... ] -- Set new values
19+
[ FROM <additional_tables> ] -- Use values from other tables
20+
[ WHERE <condition> ] -- Filter rows
1721
```
1822

1923
## Examples
2024

25+
The following example demonstrates how to update rows in a table, both directly and using values from another table.
26+
27+
We will first create a **bookstore** table and insert some sample data, then update a specific row directly. After that, we will use a second table, **book_updates**, to update rows in the **bookstore** table based on the values from **book_updates**.
28+
29+
#### Step 1: Create the bookstore table and insert initial data
30+
31+
In this step, we create a table called **bookstore** and populate it with some sample book data.
32+
2133
```sql
22-
-- create a table
2334
CREATE TABLE bookstore (
2435
book_id INT,
2536
book_name VARCHAR
2637
);
2738

28-
-- insert values
2939
INSERT INTO bookstore VALUES (101, 'After the death of Don Juan');
3040
INSERT INTO bookstore VALUES (102, 'Grown ups');
3141
INSERT INTO bookstore VALUES (103, 'The long answer');
3242
INSERT INTO bookstore VALUES (104, 'Wartime friends');
3343
INSERT INTO bookstore VALUES (105, 'Deconstructed');
44+
```
45+
46+
#### Step 2: View the bookstore table before the update
3447

35-
-- show the table before update
48+
We can now check the contents of the **bookstore** table to see the initial data.
49+
50+
```sql
3651
SELECT * FROM bookstore;
3752

38-
101|After the death of Don Juan
39-
102|Grown ups
40-
103|The long answer
41-
104|Wartime friends
42-
105|Deconstructed
53+
┌───────────────────────────────────────────────┐
54+
│ book_id │ book_name │
55+
├─────────────────┼─────────────────────────────┤
56+
102 │ Grown ups │
57+
103 │ The long answer │
58+
101 │ After the death of Don Juan │
59+
105 │ Deconstructed │
60+
104 │ Wartime friends │
61+
└───────────────────────────────────────────────┘
62+
```
63+
64+
#### Step 3: Update a single row directly
65+
66+
Next, let's update the book with book_id `103` to change its name.
67+
68+
```sql
69+
UPDATE bookstore
70+
SET book_name = 'The long answer (2nd)'
71+
WHERE book_id = 103;
72+
```
73+
74+
#### Step 4: View the bookstore table after the update
75+
76+
Now, let's check the table again to see the result of our direct update.
77+
78+
```sql
79+
SELECT book_name FROM bookstore WHERE book_id=103;
80+
81+
┌───────────────────────┐
82+
│ book_name │
83+
├───────────────────────┤
84+
│ The long answer (2nd) │
85+
└───────────────────────┘
86+
```
87+
88+
#### Step 5: Create a new table for updated values
89+
90+
In this step, we create a second table called **book_updates**, which holds updated book names that we will use to update the **bookstore** table.
91+
92+
```sql
93+
CREATE TABLE book_updates (
94+
book_id INT,
95+
new_book_name VARCHAR
96+
);
4397

44-
-- Update a book (Id: 103)
45-
UPDATE bookstore SET book_name = 'The long answer (2nd)' WHERE book_id = 103;
98+
INSERT INTO book_updates VALUES (103, 'The long answer (Revised)');
99+
INSERT INTO book_updates VALUES (104, 'Wartime friends (Expanded Edition)');
100+
```
101+
102+
#### Step 6: Update the bookstore table using values from book_updates
103+
104+
Now, we will update the **bookstore** table with values from the **book_updates** table.
46105

47-
-- show the table again after update
106+
```sql
107+
UPDATE bookstore
108+
SET book_name = book_updates.new_book_name
109+
FROM book_updates
110+
WHERE bookstore.book_id = book_updates.book_id;
111+
```
112+
113+
#### Step 7: View the bookstore table after the update
114+
115+
Finally, we check the **bookstore** table again to confirm that the names have been updated using the values from **book_updates**.
116+
117+
```sql
48118
SELECT * FROM bookstore;
49119

50-
101|After the death of Don Juan
51-
102|Grown ups
52-
103|The long answer (2nd)
53-
104|Wartime friends
54-
105|Deconstructed
120+
┌──────────────────────────────────────────────────────┐
121+
│ book_id │ book_name │
122+
├─────────────────┼────────────────────────────────────┤
123+
105 │ Deconstructed │
124+
101 │ After the death of Don Juan │
125+
102 │ Grown ups │
126+
104 │ Wartime friends (Expanded Edition) │
127+
103 │ The long answer (Revised) │
128+
└──────────────────────────────────────────────────────┘
55129
```

0 commit comments

Comments
 (0)