How to copy values from one MySQL database to another - mysql

I have 2 databases with different structures.
I need to copy information from database A to database B.
Database A has 1 table while database B has 2 related ones.
It is a Q&A site so the old database (A) has a table that contains both the question and the answer.
In the new database these are separate and the answer must contain a field with the id of the question.
Please help me make a SQL request.
Something like
"INSERT INTO table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2"
One more thing .. some values in the new database are known (will be hardtyped .. not taken from the old database)

You can simply use the below and specify manually where needed or grab it from the old database/table you are copying from. Also helps for if the columns are named differently or not as many in the new database.
USE `old_database`;
INSERT INTO `new_database`.`new_table`(`column1`,`column2`,`column3`)
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5`
FROM `old_table`

you need to specify database in statement...
insert into database1.table1
select from database2.table2

Related

How to insert new values in MySql tables only those differents from rows that already exists?

I have a database1 in one computer and a database2 (mirrror) in another computer.
Db1 is updated. I should update db2 once a week. To do this task I coulde make a dump from db1 e delete and reload de dump in computer 2. But in fact only one table changes data.
Is there a way to insert into this table (in db2) only the new values from db1 ?
Note: I could do thins using php code. But I am trying to know if is there a way to do that directly in mysql.
To get rows that don't exist use an exception join.
insert into destinationtable
select a.* from mytable a exception join destinationtable b on a.id = b.id
yes..use database replication by SQLYog.. using this technique database values from one server to another or one db to another may be transferred or updated. install SQLYog community edition and follow synchronization tool.
You could use INSERT IGNORE instead of just INSERT. This will only add rows if they don't already exist.
Refer to https://dev.mysql.com/doc/refman/5.5/en/insert.html for more info

MySQL - Trigger or Replication is better?

I want to replicate certain table from one database into another database in the same server. This tables contain exactly the same fields.
I was considering to use MySQL Replication to replicate that table but some people said that it will increase IO so i find another way to create 3 Trigger (Insert, update and Delete) that will perform exactly the same thing like what i expect.
My Question is, which way is better? Is it using MySQL replication is better even though it's in the same server or using Trigger to replicate the data is better.
Thanks.
I don't know what is your goal, but I got mine getting use of the VIEW functionality.
I had two different applications with separate databases but in the same Mysql server. Application2 needed to get a few data from Application1. In general, this is a trivial situation that you can handle with USE DB1; or USE DB2; as your needing, but my programming framework does not work very well with multiple DBs.
So, lets see my solution...
Here is my select query to retrieve this data:
SELECT id, name FROM DB1.customers;
So, using DB2 as default schema, I've created a VIEW:
USE DB2;
CREATE VIEW app1_customers AS SELECT id, name FROM DB1.customers;
Now I can retrieve this data in DB2 as a regular table with a regular SELECT statement.
SELECT * FROM DB2.app1_customers;
Hope ts useful. BR
Assuming you have two databases on the same server i.e DB1 and DB2 and the table is called tbl1 and it is sitting in DB1 you can query the table like this:
USE DB1;
SELECT * FROM tbl1;
USE DB2;
SELECT * FROM DB1.tbl1;
This way you wont need to copy the data and worry about extra space and extra code. You can query a table in another database on the same server. Replication and triggers are not your answer here. You could also create a view to encapsulate the SQL statement.
Definitely triggers is the way to go. Having another server (slave) will need to spare several MB for installation, logs, cpu and memory usage.
I'd use triggers to keep both tables equal. If you want to create a table with the same columns definition and data use:
USE db2;
CREATE TABLE t1 AS SELECT * FROM db1.t1;
After that, go ahead and create the triggers for Update, Insert and Delete statemetns.
Also you could ALTER the new table to a different engine like MEMORY or add indexes to see if you can improve something.

Script to migrate data between two SQL Server databases

I have two SQL Server databases, and I need to write a script to migrate data from database A to database B. Both databases have the same schema.
I must loop through the tables and for each table I must follow those rules:
If the item I'm migrating does not exist in the target table (for example, the comparison is made on a column Name) then I insert it directly.
If the item I'm migrating exists in the target table then I need to only update certain columns (for example, only update Age and Address but do not touch other columns)
Can anyone help me with that script? Any example would suffice. Thanks a lot
EDIT:
I just need an example for one table. No need to loop, I can handle each table separately (because each table has its own comparison column and update columns)
The MERGE statement looks like it can help you here. An Example:
MERGE StudentTotalMarks AS stm
USING (SELECT StudentID,StudentName FROM StudentDetails) AS sd
ON stm.StudentID = sd.StudentID
WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
WHEN NOT MATCHED THEN
INSERT(StudentID,StudentMarks)
VALUES(sd.StudentID,25);
The merge statement is available as of SQL Server 2008 so you are in luck
Instead of creating a script why don't you copy the source table under a different name into the target server (update needs to take place).
Then just do a simple insert where name does not exist.
Here is the SQL for step 1 only.
INSERT INTO [TableA]
SELECT Name,
XX,
XXXX
FROM TableB
WHERE NOT NAME IN(SELECT NAME
FROM TableA)

Replicate a single table

Is it possible to replicate a single table?
Yes this is possible. Have a look at the slave options of the MySQL manual. This still requires to create a complete binlog of the whole database though.
To sync specific tables again to one or more slaves rather use
pt-table-checksum
and then
pt-table-sync
That should automatically identify the out-of-sync tables and only sync those.
CREATE TABLE new_table_name
SELECT *
FROM original_table_name;
Use "*" if you want to select all columns from the original table, otherwise give specific columns name.
This will replicate table within same database.
I know this is an old question but this is for anyone who comes here looking for an answer:
CREATE TABLE table2 LIKE table1;
This will create a table with the same format and columns but no data. To transfer the data use:
INSERT INTO table2 SELECT * FROM table1;
EDIT:
It is important to note that this is an information transfer only. Meaning if you had indexes on table1 they are not transferred to table2. You will have to manually index table2

How to retrieve MYSQL records as an INSERT statement

I'm trying come up with the best method of synchronizing particular rows of 2 different database tables. So, for example there's 2 product tables in different databases as such...
Origin Database
product{
merchant_id,
product_id,
... additional fields
}
Destination Database
product{
merchant_id
product_id
... additional fields
}
So, the database schema is the same for both. However I'm looking to select records with a particular merchant_id, remove all records from the destination table that have that merchant_id and replace those records with records from the origin database of the same merchant_id.
My first thought was using mysqldump, parsing out the create table statements, and only running the Insert Statements. Seems like a pain though. So I was wondering if there is a better technique to do this.
I would think mysql has some method of creating INSERT statements as output from a SELECT statement, so you can define how to insert specific record information into a new db.
Any help would be appreciated, thank you much.
phpMyAdmin has part of this capability: You can run a query and then export the results of that query into a file containing CREATE statements.
Update: And mysqldump has it too: Link
mysqldump -u username -p --where="id='merchant_id'" databasename
In regards to replacing merchant IDs, that part I don't entirely understand yet. You may be better off doing a manual search+replace on them. Can you make a real life example of two such records?