Update another table in MySQL when i use "source" command line - mysql

I have two database and i want to import/export form one to other with source command line.
The names of my databases is melka and demelka.
I want to copy form melka to demelka. for this i follow this steps:
Export from melka DB and save in D:/db/melka.sql and then run this command line
cd D:\wamp64\bin\mysql\mysql5.7.24\bin
mysql -u root
USE demelka;
source D:/db/melka.sql
But after executing the above commands, melka Table is updated And demelka Table is empty.
Environment:
I use WAMP 3.1.7
and mysql5.7.24

Thanks to #madhur-bhaiya
I am open my melka.sql file in editor and deleted the following commands from within and corrected them
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `melka` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `melka`;

Related

XAMPP Retore Bulk Databases with CLI

On practice the way to retore MySQL or DBMaria Database from file "dump.sql" is this:
mysql --user=root --password="" --database=myblog < bitnami_wordpress.sql
But what do I do if they are multiple databases in File, any comand Line to upload this without pointing to a database?, becouse database is in Dump File like this:
DROP DATABASE IF EXISTS `myblog `;
CREATE DATABASE IF NOT EXISTS `myblog ` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `myblog `;
i use PHPMyAdmin to crerate the dump.sql file; but i want to upload via CLI

Create multiple Databases in one query line

Is there any way to create multiple databases in a single line of a query?
Something like this:
$sql="CREATE DATABASE `db1` AND/,/./etc `db2` AND/,/./etc `db3`";
$mysql_query=($sql,$con);
Other options would be to create a SQL file such as:
/*myFile.sql*/
CREATE DATABASE db1;
CREATE DATABASE db2;
Then run:
mysql -u user -p < myFile.sql
If you absolutely have to have it on a single console line you could do:
mysql -u user -p -e "CREATE DATABASE db3; CREATE DATABASE db4; ..."
I am not sure, but it should work only separated by semicolon ;
You could try
mysqli_multi_query
but you have to been connected to database with mysqli_connect not mysql_connect :(
If PHPMyAdmin command or MySQL command -
CREATE DATABASE db01; CREATE DATABASE db02; CREATE DATABASE d03;CREATE DATABASE d04;
Create long script inside any text editor like notepad++, and copy, paste the Command in Console.
Then run command with
Ctrl + Enter

Restore database in mysql command line

I have taken a backup of a database from one server and trying to restore it in another server.
Mysql version is 5.5
When I try to restore the database using the following command, screen -r
mysql -u root -p password mydb < mydump.sql
ERROR 1005 (HY000) at line 356: Can't create table 'mydb.mytable' (errno: 150)
I understand this is foreignkey constraint problem. The dump file has the following statement inside.
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
But it is still failing. My dumo file is very large is size, so opening it and editing is not possible. So instead of adding SET FOREIGN_KEY_CHECKS=0; in the dump, can I directly set it inside the mysql commandline like the following?
mysql> SET FOREIGN_KEY_CHECKS=0;
...
mysql> source "mydump.sql";
...
mysql> SET FOREIGN_KEY_CHECKS=1;
Will it work? My database reload takes hours to complete. So I am asking the help here before spending hours on this.
Thanks for the help.
Usually, I'll just pipe in 2 sql files -- first, a one-liner with SET FOREIGN_KEY_CHECKS=0;, and then dump file. You needn't worry about setting it back; it will only last for the current session (which will terminate when the files have been loaded).
FYI, you can also use sed -i 1i"SET FOREIGN_KEY_CHECKS=0;" dump.sql if you want to permanently prepend this line without visually editing the file.

Importing using MySQL WorkBench... error ERROR 1046 (3D000)

Scenario: building a RoR enviroment locally for development. Production is on EngineYard / GitHub. All now working ok, except DB isn't importing.
I have a .sql file that i've taken from my prod EY site. Now i need to import it to my MySQL locally. I'm using Workbench (as i'm new to this), but getting error below.
Please help?
08:07:43 Restoring /home/james/Downloads/Futology.sql Running: mysql
--defaults-extra-file="/tmp/tmpAVeE58/extraparams.cnf" --host=localhost --user=root --port=3306 --default-character-set=utf8 --comments < "/home/james/Downloads/Futology.sql" ERROR 1046 (3D000) at line 22: No database selected
Operation failed with exitcode 1 08:07:43 Import of
/home/james/Downloads/Futology.sql has finished with 1 errors
Workbench doesn't know the database (schema) you want to use.
In workbench, when using Data Import/Restore, just below where you choose the file to import, it asks "Default Schema to be Imported To"
Just choose the database (schema) you want it to use from the dropdown titled Default Target Schema. If you don't have a database (schema) already, you can create it with the "New" button.
This is confusing because MySQL generally seems to use the term database but Workbench uses schema. They mean the same thing for most purposes. MySQL 'create schema' and 'create database' - Is there any difference
Not used Workbench too much however it's easy enough to do from command line have a look at this (below phpMyAdmin instructions)
The command you're after is:
mysql -u #username# -p #database# < #dump_file#
Simply by choosing your target schema
As I circled in above image
Similar to brynn's answer, simply modify your SQL file and insert the following line at the very top:
use yourdatabasename
Replacing yourdatabasename with the database into which you are trying to import. Also, this database should already be created (albeit empty) before you import into it.
Here's another option that worked for me. I'm using MySQL 5.5 on a VM I set up for importing a large MySQL .sql dump that contained: 1). a create table statement 2). insert statements for inserting a large amount of data into the table.
at the MySQL command line client prompt type:
use yourdatabasename
source d:\yourpath\yourfilename.sql
for more info on the 'source' and other commands, enter ? at the prompt.
The above command line is correct. I found I have to do this when importing .sql files from older versions of MySQL. I also found I had to edit the .sql file (top of the file) and set the db name to be the same as the blank db you create before doing the import.

How can we rename the database name in MySQL 5.0 [duplicate]

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: