Home > Database > Mysql Tutorial > How Do I Escape Reserved Keywords When Creating MySQL Tables?

How Do I Escape Reserved Keywords When Creating MySQL Tables?

Linda Hamilton
Release: 2024-12-24 18:26:15
Original
258 people have browsed it

How Do I Escape Reserved Keywords When Creating MySQL Tables?

Escaping Reserved Column Names in MySQL Create Table Statements

When creating tables in MySQL, it's possible to encounter class field names that match reserved MySQL keywords. To prevent errors, it's necessary to escape these reserved words within the CREATE TABLE statement.

There are two ways to achieve this:

1. Double Quotes (ANSI SQL Mode)

If ANSI SQL mode is enabled, double quotes can be used to enclose the reserved word. For example:

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

2. Back Tick Escaping

If ANSI SQL mode is not enabled or if preferred, the proprietary back tick character (`) can be used to escape the reserved word. For example:

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

It's important to note that the back tick character is not available on all keyboards. Refer to this guide for information on accessing the symbol on different keyboard layouts: https://stackoverflow.com/questions/32470558/where-is-the-backtick-key-on-my-keyboard

The above is the detailed content of How Do I Escape Reserved Keywords When Creating MySQL Tables?. 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