8

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);
1
  • mysql's prepared statements might work for this, but i'm not sure if you'd call it a single line. Commented Sep 7, 2014 at 19:10

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 :(

Comments

0

you can create multiple databases using a scripting language like Bash:

#!/bin/bash

# List of database names to create
database_names=("db1" "db2" "db3")

# MySQL credentials
mysql_user="username"
mysql_password="password"

for db_name in "${database_names[@]}"; do
    mysql -u "$mysql_user" -p"$mysql_password" -e "CREATE DATABASE $db_name;"
done
  • Save this script to a file (e.g., create_databases.sh)
  • Make it executable using chmod +x create_databases.sh, and then run it with ./create_databases.sh.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.