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

How to Escape Reserved MySQL Keywords in Column Names?

Barbara Streisand
Release: 2024-12-16 15:59:11
Original
867 people have browsed it

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

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

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!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template