Can we create a table with a space in name in MySQL?


To create a table with a space in the table name in MySQL, you must use backticks otherwise you will get an error.

Let us first see what error will arise by creating a table with a space in the name i.e. “Demo Table” table name below:

mysql> create table Demo Table
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeFirstName varchar(20),
   EmployeeLastName varchar(20),
   EmployeeAge int,
   EmployeeSalary int,
   EmployeeAddress varchar(200)
);
ERROR 1064 (42000): You have an error in your syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'Table37
(
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeFirstName varchar(' at line 1 )

Let us use the concept of backticks for table name to remove the error. The query to create a table with a space in MySQL is as follows:

mysql> create table `Demo Table37`
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeFirstName varchar(20),
   EmployeeLastName varchar(20),
   EmployeeAge int,
   EmployeeSalary int,
   EmployeeAddress varchar(200)
);
Query OK, 0 rows affected (0.66 sec)

Above, we have set the table name with space surrounded by backtick symbol, therefore no error:

`Demo Table37`

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements