Merging two datababases - sql

UPD: Where from the requirement is coming.
My friend is using Mnemosine (http://mnemosyne-proj.org/) which is python program that uses sqlite as db. The issue that mobile version works only with one database file and my friend has already several. So he asked me if I can merge two databases.
So! I have two sqlite db files with same schema but different data.
Is there an automated way to include data from one file to another? I just need to insert additional values to dictionary tables and correctly insert values from other tables based on new ids.
Unfortunately there are no foreign keys defined so I need probably first specify columns/tables relationship. But in general, if I solve relationship issue, is it possible to merge dbs?

You can open the database you want to merge into, then attach the other database.
ATTACH DATABASE "foo.database" AS foo;
Then you can access the other database's tables by prefixing it with the database's name and a dot:
INSERT INTO bar (baz) SELECT baz FROM foo.bar;

You could try this:
sqlite3 bar.db ".dump t1" | grep -v "^CREATE" | sqlite3 foo.db
That will put the contents of table t1 from bar.db into table t1 in foo.db.

Related

Merging multiple .db files - Ubuntu

I have a number of .db files that I'd like to merge into one.
Each database has four tables.
'Associated'
'Candidate'
'Picks'
'Picks_modified'
The tables might be empty in some of the files. I would like to merge these files using python2.7.
Thanks in advance for your help,
Antoine
This is a general SQLite solution which should work for you in Python as well. Assuming you had three databases, and you wanted the contents of the second and third tables in the tables of the first database, then you could try this:
ATTACH 'database1.db' AS db1;
ATTACH 'database2.db' AS db2;
ATTACH 'database3.db' AS db3;
INSERT INTO db1.Associated
SELECT * FROM db2.Associated
UNION ALL
SELECT * FROM db3.Associated
UNION ALL
...
Repeat the above for the other three tables in your databases (Candidate, Picks, Picks_modified).
In other words, we can insert the records from the other databases into the tables of the first database. If you wanted to aggregate everything in a different, perhaps new, database, then you can easily modfiy the above code to handle this.

Export and Import tables (not data) using pgAdmin

With pgAdmin, on my created DB, I have seven tables (all of them have at least one primary key, and some of them include foreign keys). Let's suppose in a moment I need to do a mayor correction so I don't have no other choice that to move it to another DB, and I don't want to rewrite the characteristics of the seven tables over and over again.
So, with that said, how I can export my tables (the structure, not the data), so, when I create another DB with pgAdmin, I can import my tables previously created.
create table new (
like old
including defaults
including constraints
including indexes
);
REF Copy table structure into new table
With pgAdmin 1.14 and prior releases, there were only two ways to insert data in a table:
use the restore tool (which uses pg_restore) ;
use the query tool to execute INSERT queries (COPY queries are allowed if they use a file, but not stdin/stdout).
This link would help you

Copy (Import) Data from Oracle Database To Another

I want to Copy a data from One oracle database to another.
I have checked Import/Export Utility but the problem is import utility doesn't support conflicts resolution techniques between rows.
For Example if there's a table in the source database have the same row key in the destination database. if i use 'Ignore' parameter with value = y, the destination table will have a duplicate rows.
I want to ask if there's another way to import data from oracle database to another with some mechanism of detecting the conflicts and resolve them?
You might want to consider using a database link from database A to database B. You can query the data from database B to insert into your database A tables. You are free to query whatever you want using SQL or PL/SQL.
More on database links:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5005.htm

Copy table from one database to another

I've just created an empty database on my machine. I now wish to copy a table from our server database to this local database.
What sql commands do I need to run to do this? I wish to create the new table, copy data from the old table and insert it into the new table.
Create a linked server to the source server. The easiest way is to right click "Linked Servers" in Management Studio; it's under Management -> Server Objects.
Then you can copy the table using a 4-part name, server.database.schema.table:
select *
into DbName.dbo.NewTable
from LinkedServer.DbName.dbo.OldTable
This will both create the new table with the same structure as the original one and copy the data over.
Assuming that they are in the same server, try this:
SELECT *
INTO SecondDB.TableName
FROM FirstDatabase.TableName
This will create a new table and just copy the data from FirstDatabase.TableName to SecondDB.TableName and won't create foreign keys or indexes.
Another method that can be used to copy tables from the source database to the destination one is the SQL Server Export and Import wizard, which is available in SQL Server Management Studio.
You have the choice to export from the source database or import from the destination one in order to transfer the data.
This method is a quick way to copy tables from the source database to the destination one, if you arrange to copy tables having no concern with the tables’ relationships and orders.
When using this method, the tables’ indexes and keys will not be transferred. If you are interested in copying it, you need to generate scripts for these database objects.
If these are Foreign Keys, connecting these tables together, you need to export the data in the correct order, otherwise the export wizard will fail.
Feel free to read more about this method, as well as about some more methods (including generate scripts, SELECT INTO and third party tools) in this article:
https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/
SELECT ... INTO :
select * into <destination table> from <source table>
INSERT INTO ProductPurchaseOrderItems_bkp
(
[OrderId],
[ProductId],
[Quantity],
[Price]
)
SELECT
[OrderId],
[ProductId],
[Quantity],
[Price]
FROM ProductPurchaseOrderItems
WHERE OrderId=415
If migrating constantly between two databases, then insert into an already existing table structure is a possibility. If so, then:
Use the following syntax:
insert into DESTINATION_DB.dbo.destination_table
select *
from SOURCE_DB.dbo.source_table
[where x ...]
If migrating a LOT of tables (or tables with foreign keys constraints) I'd recommend:
Generating Scripts with the Advanced option / Types of data to script : Data only OR
Resort using a third party tool.
Hope it helps!
Assuming that you want different names for the tables.
If you are using PHPmyadmin you can use their SQL option in the menu. Then you simply copy the SQL-code from the first table and paste it into the new table.
That worked out for me when I was moving from localhost to a webhost.
Hope it works for you!
If you need the entire table structure (not just the basic data layout), use Task>Script Table As>Create To>New Query Window and run in your new database. Then you can copy the data at your leisure.
The quickest way is to use a tool, like RazorSQL or Toad for doing this.
To copy the table from one database to another database, first you have to create the exact table structure for the new table as old one, than copy the table entries from one table to another.
Solution for MySQL database
create table [new database.]<new table name> like [old database.]<old table name>;
insert [new database.]<new table name> select * from [old database.]<old table name>;

Exporting data from SQLite 3

I need a simple way to export data from an SQLite database of multiple tables, then import them into another database.
Here is my scenario. I have 5 tables: A, B, C, D, E.
Each table has a primary key as the first column called ID. I want a Unix command that will dump ONLY the data in the row from the primary key in a format that can be imported into another database.
I know I can do a
sqlite3 db .dump | grep INSERT
but that gives me ALL data in the table. I'm not a database expert, and I'm trying to do this with all unix commands in which I can write a shell script, rather than writing C++ code to do it (because that's what people are telling me that's the easiest way). I just refuse to write C++ code to accomplish a task that possible can be done in 4-5 statements from the command line.
Any suggestions?
This may look a little weird, however you may give it a try:
Create text file and place following statements there:
.mode insert
.output insert.sql
select * from TABLE where STATEMENT; -- place the needed select query here
.output stdout
Feed this file to sqlite3:
$ sqlite3 -init initf DATA.DB .schema > schema.sql
As the result you will get two files: one with simple "inserts" (insert.sql) and another with db schema (schema.sql).
Suggest finding a tool that can take your query and export to a CSV. It sounds like you wanted a script. Did you want to reuse and automate this?
For other scenarios, perhaps consider the sqlite-manager Firefox plugin. It supports running your adhoc queries, and exporting the results to a CSV.
Within that, give it this statement:
SELECT ID FROM TableA
Repeat for each table as you need.
You can use sqlite3 bash. For example if you want to get insert query for all records in one table, you can do the followings:
$ sqlite3 /path/to/db_name.db
>>.mode insert
>>.output insert.sql
>>select * from table_name;
It will creates a file name called insert.sql and puts insert query for every record in the given table.
A sample for what you get in insert.sql:
INSERT INTO "table" VALUES("data for record one");
INSERT INTO "table" VALUES("data for record two");
..
you also can use the quote function of SQLite.
echo "SELECT 'INSERT INTO my_new_table (my_new_key) VALUES (' || quote(my_old_key) || ');' FROM my_old_table;" | sqlite my_table > statements.sql