要在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 )
让我们使用表名的反引号概念消除错误。在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) );
上面,我们设置了表名,并用反引号将空格括起来,因此没有错误:
`Demo Table37`