Home >Database >Mysql Tutorial >How to Escape Reserved MySQL Keywords in Column Names?

How to Escape Reserved MySQL Keywords in Column Names?

Barbara Streisand
Barbara StreisandOriginal
2024-12-16 15:59:11863browse

How to Escape Reserved MySQL Keywords in Column Names?

Escaping Reserved MySQL Keywords in Column Names

In MySQL, a reserved word such as "key" cannot be used directly as a column name. To overcome this issue, we can use the following methods:

Using Double Quotes (ANSI SQL Mode)

If ANSI SQL mode is enabled, double quotes can be used to escape reserved words:

CREATE TABLE IF NOT EXISTS misc_info
  (
     id    INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
     "key" TEXT UNIQUE NOT NULL,
     value TEXT NOT NULL
  )
ENGINE=INNODB;

Using Back Ticks (Proprietary)

If ANSI SQL mode is not enabled, back ticks can be used to escape reserved words:

CREATE TABLE IF NOT EXISTS misc_info
  (
     id    INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
     `key` TEXT UNIQUE NOT NULL,
     value TEXT NOT NULL
  )
ENGINE=INNODB;

Note that back ticks are proprietary and not a standard ANSI SQL feature.

The above is the detailed content of How to Escape Reserved MySQL Keywords in Column Names?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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