我们可以在 MySQL 中创建名称中带有空格的表吗?
mysqlmysqli database
要在 MySQL 中创建名称中带有空格的表,您必须使用反引号,否则会出现错误。
让我们首先看看创建名称中带有空格的表(即下面的"Demo Table"表名)会出现什么错误:
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 中创建带有空格的表的查询如下:
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)
上面,我们将表名设置为带有反引号空格,因此没有错误:
`Demo Table37`