Create multiple Databases in one query line - mysql

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

Related

Create MySQL database with sql file import in a single line

I have a sql file that I want to upload from the command line. To do this I first need to go into MySQL and create the database
mysql> create database myDB;
and then call the following from the command line:
mysql -u username -p myDB < myDB.sql
Is there a way to do this all in one line from the command line?
I tried adding the following lines (and various combinations) to the beginning of the myDB.sql file:
create database `myDB`;
use `myDB`;
and then running
mysql -u username -p < myDB.sql
but that did not work.
ERROR 1046 (3D000) at line 7: No database selected
.sql files are generally LITERALLY just a sequence of SQL DDL/DML queries. If it was created by mysqldump, then it should contain EVERYTHING needed to recreate the database from the ground up, including the necessary create db and create table queries.
if they're not there, you can manually add them to the top of the file yourself via simple file concatenation/modification, e.g.
echo 'create database foo;' > new.sql
cat otherstuff.sql >> new.sql
mysql < new.sql
If you generated the mysqldump file with the --databases or --all-databases options, then it puts the correct CREATE DATABASE statements into the dump file.

How to clone MySQL database under a different name with the same name and the same tables and rows/content using SQL query

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

Create MySQL Database with .SQL File

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):
How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code:
mysql -u username -p password database_name < filename.sql
using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?
1) Create a file "filename.sql"
2) Create a database in your DB in which you want to import this file.
3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".
4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sql will also work.
In your case if you have created a database with name ojs and also created a file with name ojs.sql in C: drive then run the following command:
Edit: Put the path inside quotes.
mysql -u username -p password ojs < "C:\ojs.sql"
There is another way of importing tables in mysql. You can do it this way as well:
1) Connect your database
2) Type command "use ojs;"
3) Type command "source C:/ojs.sql"
Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:
log-in
mysql -u [username] -p[password]
(Note: make sure you do NOT include a space (' ') character between the -p and the [password].
MySQL will think that [password] is the name of the database you want to connect to.
The 'general' log-in (above) does not assume you want to connect to any particular schema.)
source the file (do not use quotes around filename)
mysql> source [database_creation_file].sql
you can simply do it using mysql workbench
1> create a new query tab
2> CREATE DATABASE database_name;
3> USE database_name;
4> open the filename.sql file and execute it ctrl + shift + enter
5> all the tables in the filename.sql are created
To create a MySQL database using a SQL file, you can follow these steps:
Log in to your MySQL server using the mysql command-line tool and the appropriate credentials.
Use the CREATE DATABASE command to create a new database with the desired name:
CREATE DATABASE database_name;
Use the USE command to switch to the newly created database:
USE database_name;
Use the SOURCE command to import the SQL file into the database:
SOURCE path/to/sql/file;
The database will now be created and populated with the data from the SQL file. You can verify this by running some SQL queries against the database.
It's important to note that this process assumes that the SQL file contains valid SQL statements compatible with the version of MySQL you are using. If the SQL file contains any errors or unsupported statements, they will be displayed in the mysql command-line tool, and the import process will be interrupted.

MySQL: Create database like?

In MySQL I want to clone certain databases. Is there such a thing as a
CREATE DATABASE LIKE <template database here>
command?
I know, there is
CREATE TABLE LIKE
If you want to clone a database, my_database for example, you'll want to run a mysql query and then run a command in the linux terminal:
mysql> CREATE DATABASE my_database_copy;
linux terminal (on the machine with the database):
mysqldump -u username -p my_database | mysql -u username -p my_database_copy
From there you'll likely get 2 "Enter password:" prompts....just input your password, press enter, and wait :)
There is no such command.
But you can create a backup (SQL file) of your database, and then restore all objects in the new database.
Also, you can use GUI tools in dbForge Studio for MySQL (free express edition) - Backup or restore a database. It will help you quickly recreate database and its contents.
I will prefer taking backup and restoring
If it's just for cloning, I will go for MySQLDump
This is just one another way of doing:
mysql> create database new_dbname;
mysql> select concat('create table
new_dbname.',TABLE_NAME,' like wings2020.',TABLE_NAME,'; insert into
new_dbname.',TABLE_NAME,' select * from ol_dbname.',TABLE_NAME,';')
from information_schema.tables where table_schema = 'old_dbname' into
outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;

Backing up a MySQL database and restoring it under another name

I am trying to do some maintenance on MySQL database data and I created a dump file with the backed up current database.
I want to restore all that data into another database called something like original_db_name_test
Is there a command for that?
This depends on how you invoked mysqldump
If you used mysqldump dbname, then your dump contains neither CREATE DATABASE nor USE DATABASE.
Just create the database with the new name and feed the dump to mysql -D new_dbname.
If you used mysqldump --database dbname, then the dump contains CREATE DATABASE and USE DATABASE statements.
You need to comment them out or replace with new_dbname.
mysql -u usernamehere -p original_db_name_test < yourdumpfilehere.sql
If you used mysqldump to create the dump file, simply:
Create a new database (use the mysqladmin command line tool - "mysqladmin create [new database name]").
Edit the dump file to add a "USE [new database name];" at the top. (There might be an existing use statement that's commented out, so you can change this and un-comment it.)
Import the dump into the new table via "mysql -u <user name> -p < [dump file name]".
Incidentally, when creating a dump via mysqldump, I'd be tempted to use the "--add-drop-table" option, as this will cull any existing table with the same name prior to issuing the table creation statement.
you can use the 'MySQL Workbench' Application and do this with a nice gui