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
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.
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.
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.
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
Use with backslahes before backticks:
mysql -uuser -ppw -e 'CREATE DATABASE IF NOT EXISTS \`db-name\` CHARACTER SET utf8 COLLATE utf8_general_ci'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With