Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate a specific table from a MySQL database using Command Line

Goal

I'm trying to truncate one of my table in my database.


Details

database name : site-local table name : cloud_securities


I've tried

mysql -u root -p'' -h localhost site-local -e "truncate table site-local.cloud_securities"

Result

After enter my password, I kept getting

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 '-local.cloud_securities' at line 1

What can I try to resolve this?

like image 259
code-8 Avatar asked Mar 11 '23 23:03

code-8


1 Answers

The commandline has trouble with the dashes in the db and tablenames.

You can however access the client and then send your statement, have to put the db and table in backticks.

mysql -u root -h localhost
truncate table `site-local`.`cloud_securities`;

edit: nvm, you can do it from the commandline by using backticks, but you have to escape them.

Solution:

mysql -u root -p'' -h localhost site-local -e "truncate table \`site-local\`.\`cloud_securities\`"
like image 53
Philipp Avatar answered Mar 16 '23 02:03

Philipp