MERGE TABLE
Description
The MERGE TABLE statement merges changes from one table into another according to a specified strategy. Use it to publish edits from a copy, experiment table, or branch table into the current table and to resolve conflicts with a single, consistent rule.
Privileges
Executing MERGE TABLE requires read access to incoming_table and write access to current_table.
Syntax
MERGE TABLE incoming_db.incoming_table INTO current_db.current_table
STRATEGY {FAIL | THEIRS | OURS};
Parameters
| Parameter | Description |
|---|---|
incoming_table | The source table to merge from. Supports db.table or table. |
current_table | The target table that receives the merge. Supports db.table or table. |
STRATEGY | Conflict-handling strategy. Optional. Default is FAIL. |
FAIL | On any conflict, return an error and roll back the entire statement. |
THEIRS | On conflict, overwrite current_table with values from incoming_table. |
OURS | On conflict, leave current_table unchanged. |
Behavior
MERGE TABLEmodifies data incurrent_table.- The statement runs in a single transaction; on failure, the whole statement rolls back.
- If a row exists only in
incoming_table, it is inserted intocurrent_table. - If a row exists only in
current_table, it is left unchanged. - When the same primary key exists in both tables with different values, the system applies the chosen
STRATEGY:FAIL: Error and roll back.THEIRS: Use values fromincoming_table.OURS: Keep values incurrent_table.
MERGE TABLEdoes not delete rows incurrent_tablethat have no matching primary key inincoming_table.
Limitations and considerations
incoming_tableandcurrent_tablemust have identical column definitions, including names, types, and order.- Both tables must have a primary key.
- Tables that include vector, RoaringBitmap, or UDT columns are not supported.
MERGE TABLEonly performs inserts and conflict resolution; it does not delete rows.- We recommend running
DIFF TABLEfirst to preview differences. - The current implementation fully scans both tables. Use caution on large data sets.
MERGE TABLEis a seekdb extension and is not compatible with standard MySQL or Oracle SQL syntax.
Examples
The examples below create a baseline table, load data, fork a copy with FORK TABLE, modify the copy, then merge back into the baseline—similar to a Git branch/merge loop.
Example 1: Merge conflicting data with the default FAIL strategy
DROP TABLE IF EXISTS demo_current;
DROP TABLE IF EXISTS demo_incoming;
CREATE TABLE demo_current (
id INT PRIMARY KEY,
name VARCHAR(20),
score INT
);
INSERT INTO demo_current VALUES
(1, 'Alice', 80);
FORK TABLE demo_current TO demo_incoming;
UPDATE demo_incoming SET score = 85 WHERE id = 1;
MERGE TABLE demo_incoming INTO demo_current;
Here, id = 1 is a conflict. Because STRATEGY is not specified, the default FAIL applies, so the statement errors and rolls back.
Expected outcome:
-
MERGE TABLE demo_incoming INTO demo_current;fails and rolls back. -
Querying
demo_currentagain shows unchanged data.id name score
1 Alice 80
Example 2: Use THEIRS to overwrite conflicts and insert new rows
DROP TABLE IF EXISTS demo_current;
DROP TABLE IF EXISTS demo_incoming;
CREATE TABLE demo_current (
id INT PRIMARY KEY,
name VARCHAR(20),
score INT
);
INSERT INTO demo_current VALUES
(1, 'Alice', 80),
(2, 'Bob', 90);
FORK TABLE demo_current TO demo_incoming;
UPDATE demo_incoming SET score = 85 WHERE id = 1;
DELETE FROM demo_incoming WHERE id = 2;
INSERT INTO demo_incoming VALUES (3, 'David', 95);
MERGE TABLE demo_incoming INTO demo_current STRATEGY THEIRS;
SELECT * FROM demo_current ORDER BY id;
After execution:
id = 1hasscoreupdated to85.id = 3is inserted intodemo_current.id = 2remains unchanged.
Expected output for SELECT * FROM demo_current ORDER BY id;:
id name score
1 Alice 85
2 Bob 90
3 David 95
Example 3: Use OURS to keep conflict values in the target table
DROP TABLE IF EXISTS demo_current;
DROP TABLE IF EXISTS demo_incoming;
CREATE TABLE demo_current (
id INT PRIMARY KEY,
name VARCHAR(20),
score INT
);
INSERT INTO demo_current VALUES
(1, 'Alice', 80);
FORK TABLE demo_current TO demo_incoming;
UPDATE demo_incoming SET score = 85 WHERE id = 1;
INSERT INTO demo_incoming VALUES (2, 'Bob', 90);
MERGE TABLE demo_incoming INTO demo_current STRATEGY OURS;
SELECT * FROM demo_current ORDER BY id;
After execution:
id = 1keeps the original value80fromdemo_current.id = 2is inserted intodemo_current.
Expected output for SELECT * FROM demo_current ORDER BY id;:
id name score
1 Alice 80
2 Bob 90
FAQ
Q1: Does MERGE TABLE delete data in the target table?
No. MERGE TABLE does not delete rows in current_table that have no matching primary key in incoming_table.
Q2: If a conflict occurs during the merge, is only part of the data merged?
No. MERGE TABLE runs in a single transaction; on failure, the whole statement rolls back.
Q3: When should I use THEIRS versus OURS?
Use THEIRS when you want the source table to overwrite conflicting values in the target. Use OURS when you want to keep the target’s values on conflicts and only add new rows.