Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i add database name with hyphen character using script in ubuntu

I've tried using this code in a script but it just created what is inside the backtick. In this example, "echo "table-db"" was created in the database. It didn't create the name of the folder with the hyphen character in the query

hyph=`ls -l $DBDIR | egrep '^d' | grep '.-.' | awk '{print $9}'`

dbhype=($hyph)


for dbtry in ${!dbhype[*]}
   do            
        mysql -u$dbUser -p$dbPass -e 'CREATE DATABASE IF NOT EXISTS `echo "table-db"` CHARACTER SET utf8 COLLATE utf8_general_ci';

        echo "Database ${dbhype[$dbtry]} created."

done
like image 216
1312EN Avatar asked Feb 24 '16 02:02

1312EN


People also ask

Can we use hyphen in database name?

This problem is characterized by an ORA-2083 'database name has illegal character '-'' error when using a fully qualified database link that has a hyphen in the domain name. This error only occurs when the database link is used in PL/SQL.

Can SQL table name have hyphen?

It is possible, but you don't want to do this because you'll have to enclose that table name in the proper quoting character for your database every single time.

How do I change my MySQL workbench username and password?

Click on your MySQL server instance under the Server Administrator section of MySQL workbench to create a new database user and assign privileges to your new database. Click on Users and Privileges. Then click on Add Account. Enter a login name for the new user, type localhost and a new password as shown.


1 Answers

In mysql queries table names may contain alphanumeric characters, underscore and dollar sign. In order to be able to use other ANSI characters you must place names in single backquotes.

`table-db`

But in bash they have different meaning and used for command substitution. In bash there are also several types of strings -- in double quotes and in single quotes. The difference between them is that in double quotes variable expansion and command substitution are performed, while in single quotes they don't. So you can use backquotes with mysql semantics within single quotes as is

mysql -e 'CREATE DATABASE `table-db`;'

but must escape them when using within double quotes, so that they wouldn't be interpreted by bash as command sustitution.

mysql -e "CREATE DATABASE \`table-db\`;"

As you want to get the database name from variable you need to place it beyond single quote strings, like that:

mysql -e "CREATE DATABASE \`$dbname\`;"

or like that:

mysql -e 'CREATE DATABASE `'$dbname'`;'
like image 103
5 revs, 3 users 91% Avatar answered Oct 12 '22 18:10

5 revs, 3 users 91%