Create a MySQL table with dashes

Problem

You want to create a MySQL table that contains spaces, but when you try you get the following error message:

ERROR 1064 (42000): 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 '-afterdash' at line 1

Solution

Use the backticks to create the table:

mysql> create database `table-afterdash`;
Query OK, 1 row affected (0.00 sec)

Using tables with dashes in MySQL queries

Problem

You have some databases in MySQL that contain dashes in their names, and when you try to use them (ie grant access to them), you get the following error:

ERROR 1064 (42000): 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 '-db.* to 'username'@'%'' at line 1

Solution

To be able to use them, enclose the database name in backticks (`) like:

GRANT ALL on `databasename-db`.* to 'username'@'%'