Skip to main content
Version: V1.3.0

Compare and merge table data

When you use seekdb for branched development, experiments, or data revisions, you usually need to see how two tables differ before you decide whether to merge changes back into the target table. DIFF TABLE compares data between two tables; MERGE TABLE merges changes from one table into another according to a chosen strategy. This topic covers the core concepts, use cases, mechanics, and limitations of these two features.

Overview​

DIFF TABLE and MERGE TABLE are table-level compare-and-merge capabilities in seekdb. They fit workflows where you "change in a copy first, review, then publish." The main benefits are:

  • Review differences before merging, instead of changing the target table directly with no visibility.
  • Resolve conflicts by policy rather than reconciling tables by hand.
  • Split the change flow into "edit copy → review diff → merge publish," which lowers risk to production.

If you already created a data copy with FORK TABLE or FORK DATABASE, DIFF TABLE and MERGE TABLE can help you finish change review and roll results back to the baseline.

Use cases​

  • Vibe Coding self-tests and regression checks: After AI-generated business logic, you can prepare two result tables with the same schema—one loaded with expected results, the other running the logic under test with actual results. When self-testing is done, use DIFF TABLE to compare them; no differences means the generated logic matches expectations; if there are differences, use them to debug and fix in a "generate → test → fix" loop.
  • Review and publish after AI-driven data edits: After an agent labels, cleans, or rule-corrects data in a forked copy table, use DIFF TABLE to review changes against the baseline, then MERGE TABLE to merge approved results into the target under a chosen strategy, reducing the risk of editing the primary table directly.
  • Parallel edits by multiple people: Members can each edit their own copy from the same baseline; before merging, use DIFF TABLE to spot conflicts, then pick an appropriate merge strategy.
  • Data quality checks: Compare an actual result table to an expected one to quickly find value mismatches, missing rows, or extra rows.

How it works​

DIFF TABLE and MERGE TABLE involve two tables: incoming_table and current_table. incoming_table is the source to compare or merge from; current_table is the current table, baseline, or merge target. The corresponding syntax is:

  • DIFF TABLE:

    DIFF TABLE incoming_db.incoming_table AGAINST current_db.current_table;
  • MERGE TABLE:

    MERGE TABLE incoming_db.incoming_table INTO current_db.current_table
    STRATEGY {FAIL | THEIRS | OURS};

Core semantics for DIFF TABLE and MERGE TABLE:

  • Compare by primary key, row by row: Both tables must have a primary key. The system aligns rows by primary key and compares them.
  • Schemas must match: Column definitions must be identical, including names, types, and order.
  • Difference categories:
    • Same primary key but different non–primary key values count as a conflict. The result set contains two rows for that key—one from each table—showing that the key exists in both but values differ.
    • Rows that exist in only one table count as exclusive to that table. The result set contains a single row for that primary key.
    • Rows with the same primary key and equal values do not appear in DIFF TABLE output. The result set is empty when there are no differences.
  • DIFF TABLE is read-only: It returns a diff result and does not modify either table.
  • MERGE TABLE updates the target: It merges changes from incoming_table into current_table.
  • MERGE TABLE does not delete rows that exist only in the target: If a row exists only in current_table, it is kept; it is not removed because it is missing from incoming_table.
  • Conflicts follow the strategy:
    • FAIL: On any conflict, error and roll back.
    • THEIRS: On conflict, take values from incoming_table.
    • OURS: On conflict, keep values in current_table.

Limitations and considerations​

Schema requirements​

  • Column definitions must match exactly, including names, types, and order.
  • Both tables must define a primary key.
  • Tables used for compare or merge cannot include columns of vector, RoaringBitmap, or UDT types.

Merge semantics​

  • MERGE TABLE only inserts new rows or resolves conflicts; it does not delete rows in current_table that have no matching primary key in incoming_table.
  • MERGE TABLE runs in a single transaction; if it fails, the whole statement rolls back.

Result semantics​

  • In DIFF TABLE output, for a given primary key the row from current_table appears first, then the row from incoming_table.
  • When a column value is NULL, comparison uses null-safe equality: NULL equals NULL.

Performance​

  • The current implementation fully scans both tables. On large data sets, DIFF TABLE and MERGE TABLE can be expensive in time and resources. Validate on small data first and use caution in production.

Compatibility​

  • DIFF TABLE and MERGE TABLE are seekdb extensions and are not compatible with standard MySQL or Oracle SQL syntax.

Example​

The following shows a typical flow: create a copy from a baseline, change the copy, compare diffs, then merge back into the baseline.

-- Clean up
DROP TABLE IF EXISTS demo_current;
DROP TABLE IF EXISTS demo_incoming;

-- Create baseline table
CREATE TABLE demo_current (
id INT PRIMARY KEY,
name VARCHAR(20),
score INT
);

INSERT INTO demo_current VALUES
(1, 'Alice', 80),
(2, 'Bob', 90),
(3, 'Charlie', 70);

-- Create copy table
FORK TABLE demo_current TO demo_incoming;

-- Change data in the copy
UPDATE demo_incoming SET score = 85 WHERE id = 1;
DELETE FROM demo_incoming WHERE id = 2;
INSERT INTO demo_incoming VALUES (4, 'David', 95);

-- Compare the two tables
DIFF TABLE demo_incoming AGAINST demo_current;

-- Resolve conflicts using incoming values and merge new rows into current
MERGE TABLE demo_incoming INTO demo_current STRATEGY THEIRS;

-- Inspect merged result
SELECT * FROM demo_current ORDER BY id;

When you run this example, note:

  • id = 1 exists in both tables with different score values—a conflict row.
  • id = 2 exists only in demo_current; even though demo_incoming no longer has that row, after MERGE TABLE that row remains in demo_current.
  • id = 4 exists only in demo_incoming; after the merge it is inserted into demo_current.

Expected output for DIFF TABLE demo_incoming AGAINST demo_current;:

__table       __flag  id  name   score
demo_current INSERT 1 Alice 80
demo_incoming INSERT 1 Alice 85
demo_current INSERT 2 Bob 90
demo_incoming INSERT 4 David 95

After MERGE TABLE demo_incoming INTO demo_current STRATEGY THEIRS;, expected output for SELECT * FROM demo_current ORDER BY id;:

id  name     score
1 Alice 85
2 Bob 90
3 Charlie 70
4 David 95

FAQ​

Q1: What does it mean when DIFF TABLE returns no rows?​

If schemas are valid and the comparison rules apply, an empty result means the two tables are fully identical at the primary-key and value level—there is nothing to report.

Q2: After MERGE TABLE, are rows that exist only in current_table deleted?​

No. MERGE TABLE does not delete rows that exist in current_table but not in incoming_table.

Q3: When should I choose THEIRS or OURS?​

Use THEIRS when you want the copy table's values to overwrite the target on conflicts. Use OURS when you only want to add new rows and keep the target's values where both sides conflict.

Q4: Can I compare or merge if the two tables have different structures?​

No. Column names, types, and order must match exactly; otherwise the statement returns an error.

References​