Insert only selected columns from MySQL dump file into database - mysql

I am having a simple (I think) problem.
I am having a dump of MySQL database before disaster.
I need to import and replace from this dump only three columns from single table (in over 5000 rows, so that's why I am aware of doing it manually).
What should I do to do it and do not destroy anything else in working database?
I am just thinking that there is an option to skip columns during import and replace (UPDATE command I think) only these I need.
I will be thankful for help :(
------------ UPDATE ---------------
Okay, I used PHPMyAdmin and first I used SELECT query to get only three columns from whole table. Then I dumped it and I have SQL file with a dump containing only three columns.
Now, having this dump, can I (I do not know how to name it) edit or change something inside this typical MySQL dump file to make it possible to import these three columns with replace all the existing values?
I mean - to make existing column empty, then use maybe "INSERT INTO" but to whole table?
It is just over 2600 rows and I can not change it manually, so it would be better do use automation.

As far as I know, this is not possible. You can try to use sed in order to extract only the table you want - but specifically 3 columns would be complicated if not impossible.
Can I restore a single table from a full mysql mysqldump file?
The best way would be as #Ali said and just import it to a temp DB and then export the required data/columns to a new dump.
Restore DB to temp db then:
mysql> CREATE TABLE `tempTable` AS SELECT `columnYouWant` from `table`;
$> mysqldump yourDB tempTable > temp.sql
// Since you updated the question:
You want to probably use REPLACE INTO with your dump with the --replace option - though this will delete the row and replace it, not just the individual columns. If you want just the individual columns, the only way I can think of is with UDPATE. To use UPDATE, your options are:
Multi-table update
UPDATE mydb.mytable AS dest JOIN tempdb.mytable AS origin USING (prim_key)
SET dest.col1 = origin.col1,
dest.col2 = origin.col2,
...
Then drop the temp database.
Search/Replace Dump
Take your dump and use the INSERT ... ON DUPLICATE KEY UPDATE option to add it to the end of each insert line (assuming you exported/dumped individual insert commands).

Related

Skipping or Ignoring Temp tables when using mysqldump

Wondering if there is a way to skip / ignore all temp tables using mysqldump. In our instance, these tables are prefixed as tmp{guid}.
These temp tables have a very short lifespan, they are used for building some sort of reports in its parent application. Lifetime may be up to 1 minute.
EDIT:
It has been suggested that I use the ignore-tables parameter, unfortunately this doesn't provide a way for me to specify a wildcard as the table name (tmp*).
You are not talking about tables from CREATE TEMPORARY TABLE ..., correct? Instead, you are talking about a set of tables with a particular naming convention?
Instead of trying to do it with table names, do it with a DATABASE:
CREATE TABLE TempTables;
CREATE TABLE TempTables.abcd (...);
And reference them via the db name:
INSERT INTO TempTables.abcd ...
SELECT ... FROM TempTables.abcd JOIN ...
Then use the suitable parameters on mysqldump to avoid that oneDATABASE` (or pick all the other databases to dump).

Insert Consecutive Id's between other Id's

I have the following table :
id x y z
1 z
3
6 x
7 zy
....
10000
I need to add id's in between the other id's that are already without deleting the data inside. Can't seem to find any solution, tryed all sorts of things but ended up making blank rows.
Kinda new to sql all together.
Using the information you provided in the comment...
Got a backup that i need to import to current db which is made with update only querys and need to create the rows first so that I can import them.
... and the fact that you are using MySQL, I think there is a simple solution for your problem.
Create a copy of your backup file (to have the original in case it doesn't work as expected), open it in a text editor and replace UPDATE <table_name> with INSERT INTO <table_name> (put the actual name of your table instead of <table_name>).
If some of the rows you want to import already exists in the table you have the following options to solve the conflicts:
use INSERT IGNORE INTO <table_name> as the replacement string to ignore the rows from the backup (the rows already existing in the table remain unmodified); technically, IGNORE doesn't ignore the rows you want to insert; it attempts to insert them and fails because they already exists but treats the failures as warnings (they are normally errors);
use REPLACE INTO <table_name> as the replacement string to replace the existing rows with the data from the backup; technically, REPLACE does DELETE followed by INSERT; it is not the best solution if the rows you want to insert are not complete.

What is a good way to erase the entire data and load the new data in a service table?

I am new in MySQL.
I receive full metadata every day.
However, I do not know which data is added, deleted and updated.
I use MySQL and the size of the data is about one million.
What is a good way to erase the entire data and load the new data in a service table?
I think about the following methods.
first option,
Run Delete and insert in a transaction.
second option,
RENAME TABLE foo TO foo_old, foo_new To foo
Please advise me how to deal with it.
you can truncate the table and import your new data afterwards. You dont need to rename anything.
query truncate:
TRUNCATE TABLE {tableName};
cli import:
mysql -u {username} -p {database} < {importFile.sql}
The answer by #Ben is perfect if you have data outside MySQL in a file. If your data is in another table then try this:
# Truncate table
truncate table table_name;
# Inserting data to new table, Provided, same column names in succession
insert into target_table_name select * from old_table_name;
(or)
insert into target_table_name (column names) select column_names from old_table_name;

INSERT even though column does not exist in MySQL

Let's say I have an old .SQL dump and since it was created, I have changed the table schema.
I could be running:
INSERT INTO `ec_product_campaign_relations` (`campaign_id`, `product_id`, `product_qty`) VALUES (30,28,1),(30,27,0),(30,31,0),(30,30,0);
But if column product_qty does no longer exist, the line will not get inserted.
How can I force the line to get inserted anyways and ignore that the column does not exist?
EDIT: It should mention I'm working in PHP and it is script used to sync table shema... So no "manual" control over this.
Since editing all your SQL dump won't be trivial, I suggest you to add the column to your table, make the import, then delete the column.
You might want to create a new database for this import and restore the dump as-is. Then, once you've got a handle on what changes have been made by comparing the schema in one to the new one, create a series of ALTER TABLE statements that bring it in sync.
I tend to record these in a text file in case I need to replay them later, and also keep them as a list of what's changed. You may have to do this more than once, so notes help.
Then, once you've cleaned them up to be column-compatible, dump this database table-by-table, and restore into the other as required.

How do I add rows from a table of a MySQL database A to an existing table in database B

I'm using MySQL.
I have a "customers" table in database A (located on server).
I want to add them to the customer table in database B (located on localhost) without deleting anything.
I'm very new to SQL commands or MySQL in general so try to be the most explanatory as you can.
If you need more information about something I will edit the post and add it.
(I have MySQL workbench)
Thanks.
On server (DB A):
# Sets our database as default, so we wont have to write `database`.`table` in the consecutive queries.
# Replace *database* with your database name.
USE *database*;
# Makes a copy of the customers table - named customers_export.
# This copy contains all the fields and rows from the original table (without indexes),
# and if you want, you can add a WHERE clause to filter out some data
CREATE TABLE `customers_export` SELECT * FROM `customers`;
Since you are using mysql_workbench, Do a Data Export (in MANAGEMENT section) by choosing the relevant database and only the customers_export table.
On localhost (DB B):
Assuming the database name is the same (otherwise you will need to change the database name in the dump file), do a Data Import/Restore by selecting the dump file which we exported in the previous step.
This will create the customer_export table.
# Set as default
USE *database*;
# If the data you want to import contains *NO* collisions (same primary or unique values in both tables), the structure and table name is the same
INSERT INTO `customers` SELECT * FROM `customers_export`;
And we are done.
If it does have collisions, or you want to play change the column names, some values and etc - you will need to either modify the select statement or update the customers_export table to suit your needs.
Also, back up the customers table on the second server - in case something goes wrong with the insert.
Finally - drop the customers_export table on both servers.
Just use this on localhost:
mysqldump -u YOUR_DATABASE_USER -p YOUR_DATABASE_PASS -h YOUR_SERVER_IP databaseA customers > customer.sql
mysql -u YOUR_DATABASE_USER -p YOUR_DATABASE_PASS databaseB < customer.sql
PD: If want some explanation just tell me