Home > Database > Mysql Tutorial > body text

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

王林
Release: 2023-09-08 16:13:02
forward
739 people have browsed it

此查询中的 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
Copy after login

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)
Copy after login

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)
Copy after login

Use the select statement to display the records in the table

mysql> select *from `groups`;
Copy after login

This will produce the following output−

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template