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:
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;
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;
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!