itsource

database-name에서 대시를 사용하여 bash를 통해 데이터베이스 생성

mycopycode 2023. 10. 9. 22:33
반응형

database-name에서 대시를 사용하여 bash를 통해 데이터베이스 생성

이름에 대시가 있는 bash를 통해 새로운 Database를 생성하려고 합니다.

그게 내가 시도한 것입니다.

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
'-name CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1

나는 그때 백칙을 추가했습니다.

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

제가 좀 놀다가 mysql이 이름에 대시가 없어도 백틱을 좋아하지 않는다는 것을 알게 되었습니다.

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

좀 헷갈리네요.여기 무슨 일 있어요?

추신: phpmyadmin에서 백틱을 추가할 때 예상대로 작동합니다.

뒷따옴표를 붙이거나 명령어를 중심으로 큰따옴표 대신 작은따옴표를 사용합니다.

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

그렇지 않으면 셸은 백틱을 명령 대체로 확장합니다.확인하기: http://tldp.org/LDP/abs/html/commandsub.html

echo 명령이 필요하지 않다는 점을 유의하십시오.사용할 수 있습니다.-emysql의 명령줄 옵션

백스틱을 사용하기 전에 백슬래시와 함께 사용합니다.

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

언급URL : https://stackoverflow.com/questions/28881624/creating-database-via-bash-with-dash-in-database-name

반응형