If I use command
mysql -u root -pdb_pass testdb < database.sql
I was wondering if a database which already exist and has data, does it override current database? Or must to DROP DATABASE (and create it) before to import SQL? For example: I have database_one (origin) and database_two (copy of database_one), I want to update my database_two, but on database_one I created and edited some tables and indices/foreign keys and columns. If on database_two the table already exist does it create the new column which I created on database_one (or FK...)?
this is based on the sql in database.sql.
if it has drop database sql in it,it will doing drop database.
I suppose the database.sql is created by mysqldump,typically it will not include drop database sql.
but in case it has drop database,you could open the sql with editor check on it.
for tables,has the same logic with database,but typically it would drop table.
actually database.sql from mysqldump is purely sql we known,there is no mystery in it.
Related
I need to export rows from one database (db1) and append them in another database (db2). Basically it is a kind of appending data from operational database db1 into archive database db2, once a month.
I'm trying to use mysqldump to create partial dump file from db1 and then import that dump by appending data into db2 but facing problems. I tried adding --skip-add-drop-table to mysqldump to avoid dropping tables at import with
mysql -u user -p db2 < db1_partial_dump.sql
Also consider important: there are new tables in db1 from time to time. What would need to be done is to append rows to existing tables, and create new table if does not exist. To achieve that I tried to replace CREATE TABLE in sql dump by
sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' db1_partial_dump.sql
however I have error "ERROR 1062 (23000) at line 136: Duplicate entry '809' for key 'PRIMARY'".
I don't know how to proceed from here nor if it is possible at all to do such appending. Please advise.
Let's say I have created a MySQL database (in an encapsulated local development environment):
$ mysql -uroot -proot -e "create database FOO"
And imported an SQL dump:
$ mysql -uroot -proot FOO < /var/tmp/BAR.sql
And now I simply re-import a newer dump from the same source:
$ mysql -uroot -proot FOO < /var/tmp/FUBAR.sql
Questions: Is this a reliable method to simply import the newest data? Will existing tables simply be overwritten? As far as I can inspect it seems to be no problem. Or should I always better drop the database first, recreate it and then import the newer dump?
Depends on the dump file. If it has DROP TABLE IF EXISTS and CREATE TABLE statements, it will overwrite tables. But it can also do any other thing SQL allows you to do - alter tables, only insert data, etc.
I see nothing unreliable in that.
I know how to clone tables e.g.:
CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new
SELECT * FROM production.recipes;
But I don't know how to clone e.g. a database_old to database_new database with all the tables and rows from database_old.
So, only the name of the database will change. Everything else stays the same.
Right now I am cloning it by exporting the database in phpmyadmin ad then creating a new database and importing it to the new database.
But I guess there must be a more efficient way of doing this task via SQL query like that one for cloning tables.
IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.
Thanks in advance for you suggestion how to do that.
have you tried using MySQL Dump?
$ mysqldump yourFirstDatabase -u user -ppassword > yourDatabase.sql
$ echo "create database yourSecondDatabase" | mysql -u user -ppassword
$ mysql yourSecondDatabase -u user -ppassword < yourDatabase.sql
IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.
First create a blank database:
CREATE DATABASE `destination` DEFAULT CHARACTER SET
latin1 COLLATE latin1_swedish_ci;
Then use the command show tables;
show source.tables;
and then run the command for each DB table (Optimized Create table and inserting rows) as:
create table destination.table select * from source.table;
and other way is using like command:
create table destination.table like source.table
and then inserting rows;
insert into destination.table select * from source.table
If phpMyAdmin is available for the database, you can clone it by following these steps:
Select required database
Click on the operation tab
In the operation tab, go to the "copy database"-option and type your desired clone-db-name into the input field
Select "Structure and data" (Depends on your requirement)
Check the boxes, "CREATE DATABASE before copying" and "Add AUTO_INCREMENT value"
Click "GO"
Tested with phpMyAdmin 4.2.13.3
export your chosen database using phpmyadmin (export.sql)
import it using terminal/cmd:
mysql -u username -p databasename < export.sql
and get a cup of coffee...
I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes)
How would I restore those two tables without restoring the entire database?
Well, you have three main options.
You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.
Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).
Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.
Which one you choose will depend upon the exact situation (including how much data you have)...
You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.
Open the .sql file and copy the insert statements for the tables you want.
create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.
What you want is a "Single Table Restore"
http://hashmysql.org/wiki/Single_table_restore
A few options are outlined above ... However the one which worked for me was:
Create a new DB
$ mysql -u root -p CREATE DATABASE temp_db
Insert the .sql file ( the one with the desired table ) into the new DB
$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql
dump the desired table
$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql
import desired table
$ mysql -u root -p original_database < ~/awesome_single_table.sql
Then delete the temp_db and you're all golden!
This question already has answers here:
How do I rename a MySQL database (change schema name)?
(46 answers)
Closed 11 days ago.
I am using MySQL 5.0.
I have created a database named accounts, but now I want to change the database name to FinanceAccounts.
How can I change the database name in MySQL 5.0?
I think there is only one way (besides renaming directory in the MySQL datadir which will fail for InnoDB tables):
create new database (with new name)
make dump of old database
import dumped data into new database
delete old database
To create the new DB:
mysql> CREATE DATABASE new_database;
To create the dump of the old DB:
mysqldump -u "your_username" -p --lock-tables old_database > old_database_dump.sql
To import dumped data into the new DB:
mysql -u "your username" -p new_database < old_database_dump.sql
To delete the old DB:
mysql> DROP DATABASE old_database;
Bear in mind that your permissions on the old DB will need to be deleted as well. See here for more info:
Revoke all privileges for all users on a MySQL DB
MySQL 5.1.7 to MySQL 5.1.22 had a RENAME {DATABASE | SCHEMA} db_name TO new_db_name; command but this one has been removed in MySQL 5.1.23 for being too dangerous.
The best way is probably to rename each of the tables inside the database to the new name. For example:
Update: There are two steps here
Create a new blank database as you want say "new accounts"
CREATE DATABASE newaccounts;
Migrate each table one-by-one
RENAME TABLE accounts.tablename TO newaccounts.tablename;
See
http://dev.mysql.com/doc/refman/5.0/en/rename-table.html
for more information.
MySQL kinda sucks for this. The only solid reliable solution is to use phpMyAdmin.
Login > click Scheme > click "Operations" > find "Rename database to:" > write NewName > click "Go."
As simple as that. All permissions are carried over.
here , I rename mydb database to ecommerce, you follow this steps, but usin phpmyadmin is to easy
CREATE DATABASE `ecommerce` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
RENAME TABLE `mydb`.`Articles` TO `ecommerce`.`Articles` ;
RENAME TABLE `mydb`.`Categories` TO `ecommerce`.`Categories` ;
RENAME TABLE `mydb`.`Utilisateurs` TO `ecommerce`.`Utilisateurs` ;
ALTER TABLE `Articles` ADD CONSTRAINT fk_Articles_Categories FOREIGN KEY ( Categorie_id ) REFERENCES Categories( id ) ON DELETE NO ACTION ON UPDATE NO ACTION ;
DROP DATABASE `mydb` ;
To Rename MySQL Database name follow the following steps:
1) Click the database name
2) Click at Operations from the top menu
3) Type new database name Under Rename database to: