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 TABLEto 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 TABLEto review changes against the baseline, thenMERGE TABLEto 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 TABLEto 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 TABLEoutput. The result set is empty when there are no differences.
DIFF TABLEis read-only: It returns a diff result and does not modify either table.MERGE TABLEupdates the target: It merges changes fromincoming_tableintocurrent_table.MERGE TABLEdoes not delete rows that exist only in the target: If a row exists only incurrent_table, it is kept; it is not removed because it is missing fromincoming_table.- Conflicts follow the strategy:
FAIL: On any conflict, error and roll back.THEIRS: On conflict, take values fromincoming_table.OURS: On conflict, keep values incurrent_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 TABLEonly inserts new rows or resolves conflicts; it does not delete rows incurrent_tablethat have no matching primary key inincoming_table.MERGE TABLEruns in a single transaction; if it fails, the whole statement rolls back.
Result semantics
- In
DIFF TABLEoutput, for a given primary key the row fromcurrent_tableappears first, then the row fromincoming_table. - When a column value is
NULL, comparison uses null-safe equality:NULLequalsNULL.
Performance
- The current implementation fully scans both tables. On large data sets,
DIFF TABLEandMERGE TABLEcan be expensive in time and resources. Validate on small data first and use caution in production.
Compatibility
DIFF TABLEandMERGE TABLEare 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 = 1exists in both tables with differentscorevalues—a conflict row.id = 2exists only indemo_current; even thoughdemo_incomingno longer has that row, afterMERGE TABLEthat row remains indemo_current.id = 4exists only indemo_incoming; after the merge it is inserted intodemo_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.