Dynamically ALTERing MySQL tables to add missing fields on INSERT - mysql

I'm attempting to merge data from one MySQL database into another. The problem is, some of the tables in Source_DB have fields that the matching table in Target_DB does not have.
Is there a way to automatically ALTER the table in Target_DB to add these missing fields as they are found?
Or should I go about it another way, like doing a first pass where I compare each table to first add any missing fields?

You could query the INFORMATION_SCHEMA.COLUMNS on each DB and figure out what's missing with a NOT IN query and then using the data in the INFORMATION_SCHEMA.COLUMNS dynamically generate the DDL.
Or you could use a tool like MySQL Compare to do it.

Related

How to alter same table in multiple databases in MySQL

I have a set of database schemas, with each one having the same set of tables.
I want to alter a table by adding a column in each one of the schemas with a single command, instead of doing it for every schema separateley.
Is that possible?
This isn't quite identical to Querying multiple databases at once but it's similar. As in the linked question, you could write a procedure:
Get the schema names from information_schema.tables
Create your query template
Loop over the schema names and for each schema sub the schema name in the query template

Insert only selected columns from MySQL dump file into database

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).

Merge two database into a third one

I have two MySQL database k_db1 and k_db2 on a single server.
In k_db1, I have k_db1.table1 and k_db1.table2.
In k_db2, I have k_db2.table3 and k_db2.table4.
I want to create a third database k_db3 where I copy/paste tables of others databases.
It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.
Do you know a way to do this just with SQL command?
Thanks in advance for your help.
You can try something like this:
DROP TABLE IF EXISTS k_db3.db1_table_1;
CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;
Then you can recreate the indexes on the new table via ALTER TABLE statements.
Also I would avoid using - in table names.

Add mysql table column dynamically

How to add table column in mysql Dynamically?
I have a vb6 code and mysql as database.
Suppose I want to enter new column in the textbox then the value of textbox will be a column in mysql.
Is this possible? How to achieve this one?
See ALTER TABLE command for that.
But normally it is not a great idea to alter DB structure from your client application.
If you have to, this means that your DB is not very well designed.
You can always have a dynamic data structure presented in your statically structured DB tables set by using relationships between tables.
Yes its possible
Query for adding a new column to database is
"ALTER TABLE mytable ADD COLUMN " + textbox.Text + " VARCHAR(40)"
From VB you can use ADO for it

Alter table in multiple databases

I have a set of databases and each of them contains a table named 'data'. I would like to add a new column to the table 'data' in all databases. Is it possible to do this with a sql statement? And would it work on MySQL 3.x server?
As far as I know you must do an "Alter Table" on every table of every database.