title: Merging multiple databases with relations in SQLite
body: <p>I have couple of sqlite files (databases) with rather simple structure like:</p>
  
  <pre><code>  | Id | Category |                                | Id | CatId | Name |
    -----------------                                ---------------------
    | 1  |     A    |                                | 1  |   2   |  A   |
    -----------------  -- relations one to many  --&gt; ---------------------
    | 2  |     B    |                                | 2  |   1   |  B   |
                                                     ---------------------
                                                     | 3  |   2   |  BC  |
  </code></pre>
  
  <p>So as you see there is a table with categories witch is related to name tabe. Problem is I have couple of sutrch file and i want to merge them into one and keep relations.</p>
  
  <p>So merging first table is simple like:</p>
  
  <pre><code>ATTACH DATABASE '{databaseFilePath}' AS Db;
  
  BEGIN;
  INSERT INTO Category (Category) SELECT Category FROM Db.Category;
  COMMIT;
  
  DETACH DATABASE Db;
  </code></pre>
  
  <p>But this will change my id (it is set to autoincrement because in many db files there can have same id). Now I can do the same for second table with names, problem is with keeping relation as primary has changed. Is there any rational way to do this?</p>
  
  <p>Here is create tables:</p>
  
  <pre><code>CREATE TABLE Category (Id INTEGER PRIMARY KEY NOT NULL UNIQUE,Category STRING);
  INSERT INTO Category (Category, Id) VALUES ('B', 2), ('A', 1);
  
  CREATE TABLE Name (Id INTEGER PRIMARY KEY UNIQUE NOT NULL, 
  CatId INTEGER 
  REFERENCES Category (Id) ON DELETE CASCADE ON UPDATE CASCADE MATCH SIMPLE, Name  STRING);
  INSERT INTO Name (Name,CatId,Id)VALUES ('A',1,1),('AB',1,3 ),('B',2,2);
  </code></pre>