Starting from version 3.00.5.1, the INSERT INTO statement supports the ON
DUPLICATE KEY UPDATE clause for inserting data under the MySQL
dialect. If the primary key already exists, the corresponding
record will be updated; otherwise, a new record will be inserted.
Note: This feature is available only with the MySQL dialect,
and the target table must be a dimension table of PKEY engine. You must set
enableInsertStatementForDFSTable=true before using
it.Create a PKEY engine dimension table and insert initial
data.
t = keyedTable(`id, 1 2 3 as id, 10 20 30 as c, 100 200 300 as c2, 2024.01.01T09:30:00.000 2024.01.01T09:31:00.000 2024.01.01T09:32:00.000 as update_time)
dbName = "dfs://test_duplicate"
if (existsDatabase(dbName))
{
dropDatabase(dbName)
}
db = database(dbName, VALUE, [1], engine=`PKEY)
pt = db.createDimensionTable (t, `pt, primaryKey=`id).append!(t)
| id |
c |
c2 |
update_time |
| 1 |
10 |
100 |
2024.01.01 09:30:00.000 |
| 2 |
20 |
200 |
2024.01.01 09:31:00.000 |
| 3 |
30 |
300 |
2024.01.01 09:32:00.000 |
Insert data using the ON DUPLICATE KEY UPDATE
clause.
insert into pt (id, c) values (1 2 3 4 5, 0 0 0 40 50) on duplicate key update c = c + 1
| id |
c |
c2 |
update_time |
| 1 |
11 |
100 |
2024.01.01 09:30:00.000 |
| 2 |
21 |
200 |
2024.01.01 09:31:00.000 |
| 3 |
31 |
300 |
2024.01.01 09:32:00.000 |
| 4 |
40 |
|
|
| 5 |
50 |
|
|
As shown above, records with primary key id 1, 2, and 3 already exist,
so the c column in those rows is updated by
c = c + 1. Records
with primary key id 4 and 5 do not exist, so they are inserted as new
records.Functions can be used in the ON DUPLICATE KEY UPDATE clause. For
example, use the
now() function to refresh the update time to
the current
time:
insert into pt (id, c, update_time) values (1 2 3, 0 0 0, 0 0 0) on duplicate key update c = c + 1, update_time = now()
| id |
c |
c2 |
update_time |
| 1 |
12 |
100 |
2026.07.31 16:17:20.683 |
| 2 |
22 |
200 |
2026.07.31 16:17:20.683 |
| 3 |
32 |
300 |
2026.07.31 16:17:20.683 |
| 4 |
40 |
|
|
| 5 |
50 |
|
|
You can use
VALUES(col) to reference the new value from
the current INSERT. Note that
VALUES(col) can only be used as a
complete right-hand value; it cannot be part of an expression (e.g.,
VALUES(col) +
1).
insert into pt (id, c, c2, update_time) values (1 2 3, 4 5 6, 0 0 0, 0 0 0) on duplicate key update c2 = values(c), update_time = now()
| id |
c |
c2 |
update_time |
| 1 |
12 |
4 |
2026.07.31 16:19:05.325 |
| 2 |
22 |
5 |
2026.07.31 16:19:05.325 |
| 3 |
32 |
6 |
2026.07.31 16:19:05.325 |
| 4 |
40 |
|
|
| 5 |
50 |
|
|
You can update a field to
NULL:
insert into pt (id, c, c2, update_time) values (1 2 3, 4 5 6, 0 0 0, 0 0 0) on duplicate key update c2 = NULL, update_time = now()
| id |
c |
c2 |
update_time |
| 1 |
12 |
|
2026.07.31 16:21:26.608 |
| 2 |
22 |
|
2026.07.31 16:21:26.608 |
| 3 |
32 |
|
2026.07.31 16:21:26.608 |
| 4 |
40 |
|
|
| 5 |
50 |
|
|