Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating database via bash with dash in database-name

Tags:

bash

mysql

I try to create a new Database via bash which has a dash in the name.

Thats what I tried:

echo "CREATE DATABASE IF NOT EXISTS db-name CHARACTER SET utf8 COLLATE utf8_general_ci" | mysql -uuser -ppw

That fails with the following error:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the 
right syntax to use near
'-name CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1

I added backticks then:

echo "CREATE DATABASE IF NOT EXISTS `db-name` CHARACTER SET utf8 COLLATE utf8_general_ci" | mysql -uuser -ppw

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the 
right syntax to use near
'CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1

I played around a bit and found out that mysql doesnt like backticks even without a dash in the name:

echo "CREATE DATABASE IF NOT EXISTS `dbname` CHARACTER SET utf8 COLLATE utf8_general_ci" | mysql -uuser -ppw

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the 
right syntax to use near
'CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1

I am kinda confused. Whats wrong here?

PS: In phpmyadmin it works as expected when adding backticks

like image 715
Fuzzyma Avatar asked Mar 05 '15 15:03

Fuzzyma


People also ask

Can I use dash in MySQL database name?

MySQL Bugs: #461: dash both allowed and disallowed in database name. You are using the wrong quote-marks. You want the backquote (`), not the single quote ('). On a US keyboard, the backquote is generally to the immediate left of the '1' key.

How can I create database name?

On the File tab, click New, and then click Blank Database. Type a file name in the File Name box. To change the location of the file from the default, click Browse for a location to put your database (next to the File Name box), browse to the new location, and then click OK. Click Create.

Which one is the correct command for create DB?

Syntax: CREATE DATABASE database_name; database_name: name of the database. Example Query: This query will create a new database in SQL and name the database as my_database.


2 Answers

Either you quote the backticks or simply use single quotes instead double quotes around the command:

mysql -uuser -ppw -e 'CREATE DATABASE IF NOT EXISTS `db-name` CHARACTER SET utf8 COLLATE utf8_general_ci'

Otherwise the shell would expand the backticks to a command substitution. Check this: http://tldp.org/LDP/abs/html/commandsub.html

Further note that you don't need the echo command. You can use the -e commandline option of mysql

like image 55
hek2mgl Avatar answered Oct 14 '22 14:10

hek2mgl


Use with backslahes before backticks:

mysql -uuser -ppw -e 'CREATE DATABASE IF NOT EXISTS \`db-name\` CHARACTER SET utf8 COLLATE utf8_general_ci'
like image 38
Alex K Avatar answered Oct 14 '22 14:10

Alex K