I've got a production(prod.products) and a development(dev.products) database. I now need to insert the table ('prod.batch-numbers') from the production database into the ('dev.batch-numbers') table within the development database as this one is not up to date. Both tables have the same layout. Any suggestions on how to do this? Quite new to MySQL so any help is appreciated!
Try this:
INSERT INTO `dev`.`batch-numbersz` SELECT * FROM `prod`.`batch-numbers`;
Note: for this to work, you'll need to run the query from a user with proper rights on both databases.
Or simpler if you only need it done once: do an export of the production table and import it in the development database.
Most database tools support both export of tables and import of files.
Related
I'm working on joomla plugin that could help me with backups of my site.
I have problem with copying database of one site to another. Both databases have different users. I tried to use query like that:
INSERT INTO new_table SELECT * FROM another_database.old_table
Unfortunately I got an error:
SELECT command denied to user 'user_old'#'ip' for table 'old_table'
I understand an error, but I cant create another user that could have priviliges to both databases.
Can I somehow work this out in php? I create connection to both databases, but is there a way of doing this other than
SELECT * FROM old_table
then inserting all data fetched to new_table?
I would like to not use mysqldump because I want to have control which tables I will copy.
You can use mysqldump to get all the tables, load that sql file into the new database and then run:
DROP TABLE <table_name>;
on all the tables you do not want in the new database.
I want to copy everything from a table on my local server and insert it into a table on a remote server.
Something like
INSERT INTO table2
SELECT * FROM table1;
How can I adapt this for 2 tables on different servers and databases?
Well, if you want to use PHP, then you could do something like querying everything from one table, simply like SELECT * FROM table, and then iterating over your table with a while loop, inserting one record at a time to the new table. I assume you know some PHP, for this answer to help you. You can't do it with one SQL statement atleast, that's for sure.
Please look at this StackOverflow thread: SQL Insert into … values ( SELECT … FROM … ). There are discussed some compatibility issues across various database engines, too.
It should answer your question quite good as long as it is within a single database.
For copies between different database instances have look at backup & restore, export & import mechanisms or at seperate copy scripts in php, python, etc. using either native or ODBC database drivers.
At our company we have a business solution which includes CMS, CRM and several other systems.
These are installed in several domains, for each of our clients.
The systems are still in development, so new tables and fields are added to the database.
Each time we want to release a new version to our clients, i have to go through their database and insert the new fields and tables manually.
Is there a way that this could be done automatically(a script maybe that detects the new fields and tables and inserts them?)
We are using php and mysql.
We would like to avoid backing up the clients data, dropping the database tables, running the sql query to insert all the database tables(including the new ones) and then re-inserting the customers data. Is this possible?
Toad for MySQL
DB Extract, Compare-and-Search Utility — Lets you compare two MySQL databases, view the differences, and create the script to update the target.
What you are looking for is
ALTER TABLE 'xyz' ADD 'new_colum' INT(10) DEFAULT '0' NOT NULL;
or if you want to get rid of a colum
ALTER TABLE 'xyz' DROP 'new_colum';
Put all table edits into an update.php file and the either call and delete it once manually or try to select "new_colum" once and update the database when it's not present.
OR what I do: "I have a settingsfield "software version" and use this as a trigger to update my tables.
But since you have to install the new scripts anyways you can just call it manually.
In phpMyAdmin under operations I can "Copy database to:" and select
Structure and data
CREATE DATABASE before copying
Add AUTO_INCREMENT value
I need to be able to do that without using phpMyAdmin.
I know how to create the database and user.
I have a source database that's a shell that I can work from so all I really need is the how to copy all the table structure and data part. (I know, the harder part)
system() & exec() are not options for me which rules out mysqldump. (I think)
How can I loop through each table and recreate it's structure and data?
Is it just looping through the results of
SHOW TABLES
then for each table looping through
DESCRIBE tablename
Then, is there an easy way for getting the data copied?
UPDATE:
So, I ended up going with:
SHOW TABLES;
for each table then I did
SHOW CREATE TABLE `tablename`;
and then
INSERT INTO `newBDname`.`tablename` SELECT * FROM `existingDBname`.`tablename`;
You can use mysql_fetch_assoc on SHOW TABLES and then for each result, store the output of mysql_fetch_result SHOW CREATE TABLE $table_name and then issue mysql_query on $show_create_table
Hope that helps ... more into helping than doing it for you. :)
Use SELECT INTO to accomplish this.
SELECT *
INTO destinationDB..newTable
FROM sourceDB..existingTable
Is there already any software that will allow me to select a table or row from existing DB, edit that table, add new rows, or clone existing ones, then insert the new rows back into the DB?
Read: i want to ADD this data, do not want to update/replace existing data.
PHP5, and MySQL 5
There's PHPMyAdmin, which will let you to pretty much anything to a database.
You can clone rows by editing and selecting "insert as new row" (may have to blank out the primary key if you have one).
MySQL Administrator is a decent GUI put out by MySQL that should handle all of that. I'm quite happy with it.
There's also HeidiSQL which is a windows client for managing MySQL databases:
http://www.heidisql.com/
And there's jHeidi from the same site that's java based and so should run on other operating systems.