Truncate a specific table from a MySQL database using Command Line

12,662

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\`"
Share:
12,662
code-8
Author by

code-8

I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)

Updated on June 24, 2022

Comments

  • code-8
    code-8 almost 2 years

    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?