createtablegroups−>(−>idint,−>namevarchar(40)−>);ERROR1064(42000): YouhaveanerrorinyourSQLsynt"/> createtablegroups−>(−>idint,−>namevarchar(40)−>);ERROR1064(42000): YouhaveanerrorinyourSQLsynt">

Home  >  Article  >  Database  >  What is the MySQL syntax error in this query - Create table using reserved keywords?

What is the MySQL syntax error in this query - Create table using reserved keywords?

王林
王林forward
2023-09-08 16:13:02744browse

此查询中的 MySQL 语法错误是什么 – 使用保留关键字创建表?

Suppose we try to create a table called "groups", which is a reserved keyword in MySQL. You cannot use "groups" because groups is a reserved keyword in MySQL.

The following error occurred while creating a table named "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

In order to create a table with reserved keywords, you need to use the concept of backticks (``).

Let us create a table-

mysql> create table `groups`
-> (
−> id int,
−> name varchar(40)
−> )
−> ;
Query OK, 0 rows affected (3.08 sec)

Use the insert command to insert some records into the table−

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)

Use the select statement to display the records in the table

mysql> select *from `groups`;

This will produce the following output−

+------+------+
| id   | name |
+------+------+
|   10 | John |
|   11 | Bob  |
|   12 | Mike |
+------+------+
3 rows in set (0.04 sec)

The above is the detailed content of What is the MySQL syntax error in this query - Create table using reserved keywords?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete