Skip to main content
Version: V1.4.0

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​

ParameterDescription
incoming_tableThe source table to merge from. Supports db.table or table.
current_tableThe target table that receives the merge. Supports db.table or table.
STRATEGYConflict-handling strategy. Optional. Default is FAIL.
FAILOn any conflict, return an error and roll back the entire statement.
THEIRSOn conflict, overwrite current_table with values from incoming_table.
OURSOn conflict, leave current_table unchanged.

Behavior​

  • MERGE TABLE modifies data in current_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 into current_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 from incoming_table.
    • OURS: Keep values in current_table.
  • MERGE TABLE does not delete rows in current_table that have no matching primary key in incoming_table.

Limitations and considerations​

  • incoming_table and current_table must have identical column definitions, including names, types, and order.
  • Both tables must have a primary key.
  • Tables that include vector or UDT columns are not supported.
  • MERGE TABLE only performs inserts and conflict resolution; it does not delete rows.
  • We recommend running DIFF TABLE first to preview differences.
  • The current implementation fully scans both tables. Use caution on large data sets.
  • MERGE TABLE is 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_current again 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 = 1 has score updated to 85.
  • id = 3 is inserted into demo_current.
  • id = 2 remains 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 = 1 keeps the original value 80 from demo_current.
  • id = 2 is inserted into demo_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.

References​