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!