此查询中的 MySQL 语法错误是什么 – 创建带有保留关键字的表?
假设我们尝试创建一个名为“groups”的表,它在 MySQL 中是保留关键字,不能使用“groups”,因为 groups 是 MySQL 中的保留关键字。
使用命名为“groups”的表创建时发生以下错误 −
mysql> create table groups −> ( −> id int, −> name varchar(40) −> ); 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 'groups ( id int, name varchar(40) )' at line 1
为了创建带有保留关键字的表,你需要使用反引号(``)的概念。
让我们创建一个表 −
mysql> create table `groups` -> ( −> id int, −> name varchar(40) −> ) −> ; Query OK, 0 rows affected (3.08 sec)
在表中插入一些记录,使用插入命令 −
mysql> insert into `groups` values(10,'John'); Query OK, 1 row affected (0.30 sec) mysql> insert into `groups` values(11,'Bob'); Query OK, 1 row affected (0.32 sec) mysql> insert into `groups` values(12,'Mike'); Query OK, 1 row affected (0.40 sec)
使用 select 语句显示表中的记录
mysql> select *from `groups`;
这将产生以下输出 −
+------+------+ | id | name | +------+------+ | 10 | John | | 11 | Bob | | 12 | Mike | +------+------+ 3 rows in set (0.04 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP