You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SET<col_name>=<value> [ , <col_name>=<value> , ... ] -- Set new values
19
+
[ FROM<additional_tables> ] -- Use values from other tables
20
+
[ WHERE<condition> ] -- Filter rows
17
21
```
18
22
19
23
## Examples
20
24
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
+
21
33
```sql
22
-
-- create a table
23
34
CREATETABLEbookstore (
24
35
book_id INT,
25
36
book_name VARCHAR
26
37
);
27
38
28
-
-- insert values
29
39
INSERT INTO bookstore VALUES (101, 'After the death of Don Juan');
30
40
INSERT INTO bookstore VALUES (102, 'Grown ups');
31
41
INSERT INTO bookstore VALUES (103, 'The long answer');
32
42
INSERT INTO bookstore VALUES (104, 'Wartime friends');
33
43
INSERT INTO bookstore VALUES (105, 'Deconstructed');
44
+
```
45
+
46
+
#### Step 2: View the bookstore table before the update
34
47
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
36
51
SELECT*FROM bookstore;
37
52
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
+
CREATETABLEbook_updates (
94
+
book_id INT,
95
+
new_book_name VARCHAR
96
+
);
43
97
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.
46
105
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
+
WHEREbookstore.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**.
0 commit comments